-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernel.cu
143 lines (121 loc) · 3.46 KB
/
kernel.cu
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
__global__ void GMEMD_gradient(float *data, float *diff, float *direct,
int *list_x, int *list_y, float *list_deg,
int list_len, int width, int height) {
//kernel index
int x = threadIdx.x + blockDim.x * blockIdx.x;
int y = threadIdx.y + blockDim.y * blockIdx.y;
if(x<width && y<height){
//init variable
float pos_avg=0,neg_avg=0;
int pos_count=0,neg_count=0,weight=0,start,end,mid,max_weight=0,max_token,dist_token;
int tx,ty,otx,oty,stx,sty,etx,ety;
float current,target,otarget,starget,etarget;
//mid point
current=data[y*width+x];
//init direction (sliding windows, init weight)
start=0;
end=list_len/2+1;
mid=end/2+1;
weight=0;
max_token=mid;
max_weight=weight; //weight can be negative, just searching for largest weight
//calc direct
for(int i=0;i<list_len;++i){
stx=x+list_x[start];
sty=y+list_y[start];
etx=x+list_x[end];
ety=y+list_y[end];
//check start target of sliding window
if( (stx>=0 && stx<width) && (sty>=0 && sty<height) ){
starget=data[sty*width+stx];
}
else starget=-1;
//check end target of sliding window (assume it is opposite of start target)
if( (etx>=0 && etx<width) && (ety>=0 && ety<height) ){
etarget=data[ety*width+etx];
}
else etarget=-1;
//compare start and end to the middle point
if(starget>current && etarget>current){
//both is larger than middle point
if(starget>etarget) --weight;
else ++weight;
}
else{
if(starget>current) --weight;
if(etarget>current) ++weight;
}
//update max_weight
if(weight>max_weight){
max_token=mid;
max_weight=weight;
}
else if(weight==max_weight){
tx=x+list_x[max_token];
ty=y+list_y[max_token];
if( (tx>=0 && tx<width) && (ty>=0 && ty<height) ){
starget=data[ty*width+tx];
}
else starget=0;
tx=x+list_x[mid];
ty=y+list_y[mid];
if( (tx>=0 && tx<width) && (ty>=0 && ty<height) ){
etarget=data[ty*width+tx];
}
else etarget=0;
if(starget < etarget){
max_token=mid;
}
}
//move sliding window
start=(start+1)%list_len;
end=(end+1)%list_len;
mid=(mid+1)%list_len;
}
//calculate diff (magnitude)
for(int i=0;i<list_len;++i){
tx=x+list_x[i];
ty=y+list_y[i];
if( (tx>=0 && tx<width) && (ty>=0 && ty<height) ){
target=data[ty*width+tx];
if(i>max_token) dist_token=i-max_token;
else dist_token=max_token-i;
if(dist_token>list_len/2) dist_token=list_len-dist_token;
if(dist_token>list_len/4){
pos_avg+=target;
++pos_count;
}
else{
neg_avg+=target;
++neg_count;
}
}
}
//finish diff (magnitude)
if(pos_count){ pos_avg/=(float)pos_count; }
else{ pos_avg=current; }
if(neg_count){ neg_avg/=(float)neg_count; }
else{ neg_avg=current; }
diff[y*width+x]=pos_avg-neg_avg;
//direct finish
direct[y*width+x]=list_deg[max_token];
}
}
__global__ void GMEMD_integral(float *result, float *diff, float *direct,
int *list_x, int *list_y, float *list_deg,
int list_len, int width, int height) {
//kernel index
int x = threadIdx.x + blockDim.x * blockIdx.x;
int y = threadIdx.y + blockDim.y * blockIdx.y;
if(x<width && y<height){
int tx,ty;
result[y*width+x]=0;
for(int i=0;i<list_len;++i){
tx=x+list_x[i];
ty=y+list_y[i];
if( (tx>=0 && tx<width) && (ty>=0 && ty<height) ){
result[y*width+x]+=cos(direct[ty*width+tx]-list_deg[i])*diff[ty*width+tx];
}
}
}
}