-
Notifications
You must be signed in to change notification settings - Fork 0
/
source_lm.cl
265 lines (243 loc) · 5.42 KB
/
source_lm.cl
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//--------------------------------------
//IMFILTER X
__kernel void imfilterX_cl(
__global float * im,
__global float * im_out,
__constant float * filt,
__local float * tile,
const uint CONV_SIZE,
const uint width,
const uint height,
int z)
{
int ii, jj;
int j = get_global_id(0);
int i = get_global_id(1);
int tx = get_local_id(0);
int ty = get_local_id(1);
int work_size = get_local_size(0);//Returns the number of local work-items.
int CONV_SIZE2 = (CONV_SIZE-1)/2;
int tile_dim = work_size+CONV_SIZE2*2;
/* filter copy */
//if (ty==0 && tx<CONV_SIZE)
// filt_loc[tx] = filt[tx];
tile[ty*tile_dim+tx+CONV_SIZE2] = im[i*width+j];//copio la parte central
if(tx>=0 && tx<CONV_SIZE2){//margen izquierdo
if (j>CONV_SIZE2)
tile[ty*tile_dim+tx] = im[i*width+j-CONV_SIZE2];
else
tile[ty*tile_dim+tx] = 0.0;
}
if (tx>=work_size-CONV_SIZE2 && tx<work_size){//margen derecho
if (j<width-CONV_SIZE2)
tile[ty*tile_dim+CONV_SIZE2+tx] = im[i*width+j+CONV_SIZE2];
else
tile[ty*tile_dim+CONV_SIZE2+tx] = 0.0;
}
barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
//esperamos a que se copien todos
if(i<height && j>=CONV_SIZE2 && j<width-CONV_SIZE2)
{
im_out[i*width+j] = 0.0;// iniciamos a cero la salida
for (jj=-CONV_SIZE2;jj<=CONV_SIZE2; jj++)
im_out[i*width+j] += filt/*_loc*/[jj+CONV_SIZE2]*tile[ty*tile_dim + (jj+tx)];//+ (jj+tx+CONV_SIZE2)
}
}
//--------------------------------------
//IMFILTER Y
__kernel void imfilterY_cl(
__global float * im,
__global float * im_out,
__constant float * filt,
__local float * tile,
const uint CONV_SIZE,
const uint width,
const uint height,
int z)
{
int ii, jj;
int j = get_global_id(0);
int i = get_global_id(1);
int tx = get_local_id(0);
int ty = get_local_id(1);
int work_size = get_local_size(0);
int CONV_SIZE2 = (CONV_SIZE-1)/2;
int tile_dim = work_size+CONV_SIZE2*2;
/* filter copy */
//if (ty==0 && tx<CONV_SIZE)
// filt_loc[tx] = filt[tx];
tile[(ty+CONV_SIZE2)*tile_dim+tx] = im[i*width+j];//copio la parte central
if(ty>=0 && ty<CONV_SIZE2){//margen superior
if (i>CONV_SIZE2)
tile[ty*tile_dim+tx] = im[(i-CONV_SIZE2)*width+j];
else
tile[ty*tile_dim+tx] = 0.0;
}
if (ty>=work_size-CONV_SIZE2 && ty<work_size){//margen inferior
if (i < height-CONV_SIZE2)
tile[ty*tile_dim+CONV_SIZE2+tx] = im[(i+CONV_SIZE2)*width+j];
else
tile[ty*tile_dim+CONV_SIZE2+tx] = 0.0;
}
barrier(CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE);
//esperamos a que se copien todos
if(j<width && i>=CONV_SIZE2 && i<height-CONV_SIZE2)
{
im_out[i*width+j] = 0.0;// iniciamos a cero la salida
for (jj=-CONV_SIZE2;jj<=CONV_SIZE2; jj++)
im_out[i*width+j] += filt/*_loc*/[jj+CONV_SIZE2]*tile[(ty+jj)*tile_dim + tx];
}
}
//--------------------------------------
//SUBST
__kernel void subst_cl(
__global float *M1,
__global float *It,
int NR,
int NC,
int z)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC)
It[i*NC+j] = M1[NR*NC*z+i*NC+j] - M1[NR*NC*(z+1)+i*NC+j];
}
//--------------------------------------
//SUBST2
__kernel void sub2_cl(
__global float *M1,
__global float *M2,
__global float *R,
int NR,
int NC)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC)
R[i*NC+j] = M1[i*NC+j] - M2[i*NC+j];
}
//--------------------------------------
//SUM
__kernel void sum_cl(
__global float *M1,
__global float *M2,
__global float *S,
int NR,
int NC)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC)
S[i*NC+j] = M1[i*NC+j] + M1[i*NC+j];
}
//--------------------------------------
//MULT
__kernel void mult_cl(
__global float *M1,
__global float *M2,
__global float *R,
int NR,
int NC)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC){
R[i*NC+j] = M1[i*NC+j] * M2[i*NC+j];
}
}
//--------------------------------------
//MULTESCALAR
__kernel void multEsc_cl(
__global float *M1,
float E,
__global float *R,
int NR,
int NC)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC){
R[i*NC+j] = M1[i*NC+j] * E;
}
}
//--------------------------------------
//DIVESCALAR con control de division por cero
__kernel void divEsc_cl(
__global float *M1,
float E,
__global float *R,
int NR,
int NC)
{
int i, j;
i = get_global_id(0);
j = get_global_id(1);
if (i<NR && j<NC){
if(M1[i*NC+j]!=0){
R[i*NC+j] = E / M1[i*NC+j];
}
else{
R[i*NC+j]=0;
}
}
}
//--------------------------------------
//IMFILTERS
/*__kernel void imfilterS_cl(
__global float *A, //frame input
__global float *R,//frame output
int NR,
int NC,
int BX)
{
int i, j, k, l, difx, dify;
difx = BX/2;
dify = BX/2;
for (i=0; i<NR; i++)
{
for (j=0; j<NC; j++)
{
float tmp = 0.0;
for (k=-dify; k<=dify; k++)
{
for (l=-difx; l<=difx; l++)
{
if (i+k>=0 && j+l>=0 && i+k<NR && j+l<NC)
tmp += A[(i+k)*NC+(j+l)];
}
}
R[i*NC+j] = tmp;
}
}
}
*/
//--------------------------------------
//IMFILTERS
__kernel void imfilterS_cl(
__global float *A, //frame input
__global float *R,//frame output
int NR,
int NC,
int BX)//must be odd:impar
{
int i; int j; int k, l, difx, dify;
i = get_global_id(0);
j = get_global_id(1);
difx = (BX-1)/2;
dify = (BX-1)/2;
float tmp = 0.0;
for (k=-dify; k<=dify; k++)
{
for (l=-difx; l<=difx; l++)
{
if (i+l>=0 && j+k>=0 && i+l<NR && j+k<NC)
tmp += A[(j+k)*NC+(i+l)];
}
}
R[j*NC+i] = tmp;
}