-
Notifications
You must be signed in to change notification settings - Fork 0
/
impurity.c
122 lines (103 loc) · 3.82 KB
/
impurity.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// $c: number of categories
// $d: number of features
// $n: number of samples
// $s: number of splits
// Compute if each point is above or below the given hyperplans
__global__ void impurity1(double samples[$n][$d],
double hyperplan[$s][$d+2],
unsigned int position[$s][$n]) {
int t_x, t_y; // Thread id
int i; // Index variable
double point = 0; // Value of the point (above or below hyperplan)
t_x = blockIdx.x * blockDim.x + threadIdx.x; // Indexes the samples
t_y = blockIdx.y * blockDim.y + threadIdx.y; // Indexes the splits
// Check boundaries
if(t_x < $n && t_y < $s) {
// Check if the hyperplan is valid
if(hyperplan[t_y][$d+1] > 0) {
// Compute if the point is above or below the hyperplan
for(i = 0 ; i < $d ; i++)
point += samples[t_x][i] * hyperplan[t_y][i];
point += hyperplan[t_y][$d];
if(point > 0)
position[t_y][t_x] = 0;
else
position[t_y][t_x] = 1;
}
}
}
// Counting per category per side
// Each thread is responsible of one category per split
__global__ void impurity2(unsigned int categories[$n],
unsigned int count[$s][$c][2],
unsigned int Tl[$s],
unsigned int position[$s][$n]) {
int t_x, t_y; // Thread id
int i; // Index variable
t_x = blockIdx.x * blockDim.x + threadIdx.x; // Indexes the categories
t_y = blockIdx.y * blockDim.y + threadIdx.y; // Indexes the splits
// Check boundaries
if(t_x < $c && t_y < $s) {
// Reset counts
count[t_y][t_x][0] = 0;
count[t_y][t_x][1] = 0;
for(i = 0 ; i < $n ; i++)
if(categories[i] == t_x) {
if(position[t_y][i] == 0)
count[t_y][t_x][0] += 1;
else {
if(position[t_y][i] == 1)
count[t_y][t_x][1] += 1;
}
}
}
// Thread counting samples above the hyperplan
if(t_x == $c && t_y < $s) {
Tl[t_y] = 0;
for(i = 0 ; i < $n ; i++)
if(position[t_y][i] == 0)
Tl[t_y] += 1;
}
}
// TODO: implement it as a reduction
__global__ void impurity3(unsigned int count[$s][$c][2],
unsigned int Tl[$s],
double impurity[$s]) {
int tid; // Thread id
int i; // Index variable
double GiniL = 1; // Gini value Left
double GiniR = 1; // Gini value Right
tid = blockIdx.x * blockDim.x + threadIdx.x; // Indexes the splits
if(tid < $s) {
for(i = 1 ; i < $c ; i++) {
GiniL -= (count[tid][i][0] * count[tid][i][0])/(double)((Tl[tid])*(Tl[tid]));
GiniR -= (count[tid][i][1] * count[tid][i][1])/(double)(($n-Tl[tid])*($n-Tl[tid]));
}
if(Tl[tid] == 0)
GiniL = 1;
if(Tl[tid] == $n)
GiniR = 1;
impurity[tid] = (Tl[tid] * GiniL + ($n-Tl[tid]) * GiniR)/((double)$n);
}
}
// First step of impurity computation in one dimension
__global__ void impurity4(double U[$n],
double splits[$n][2],
unsigned int position[$n][$n]) {
int t_x, t_y; // Thread id
t_x = blockIdx.x * blockDim.x + threadIdx.x; // Indexes the samples
t_y = blockIdx.y * blockDim.y + threadIdx.y; // Indexes the splits
// Check boundaries
if(t_x < $n && t_y < $s) {
// Check if the hyperplan is valid
if(splits[t_y][1] > 0) {
if(U[t_x] < splits[t_y][0])
position[t_y][t_x] = 0;
else
position[t_y][t_x] = 1;
}
else {
position[t_y][t_x] = 2;
}
}
}