File: cbf.cl

package info (click to toggle)
python-fabio 2024.9.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,844 kB
  • sloc: python: 21,160; ansic: 1,126; lisp: 450; makefile: 253; sh: 244
file content (477 lines) | stat: -rw-r--r-- 11,054 bytes parent folder | download | duplicates (3)
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477


#define SWAP(a,b) {local int *tmp=a;a=b;b=tmp;}
static void my_group_scan_inclusive_add( 	local int *inp_buf,
											local int *out_buf,
											local int *tmp_buf)
{
    uint lid = get_local_id(0);
    uint ws = get_local_size(0);

    out_buf[lid] = tmp_buf[lid] = inp_buf[lid];
    barrier(CLK_LOCAL_MEM_FENCE);

    for(uint s = 1; s < ws; s <<= 1) {
        if(lid > (s-1)) {
        	tmp_buf[lid] = out_buf[lid]+out_buf[lid-s];
        } else {
        	tmp_buf[lid] = out_buf[lid];
        }
        barrier(CLK_LOCAL_MEM_FENCE);
        SWAP(out_buf, tmp_buf);
    }
    tmp_buf[lid] = out_buf[lid];
    barrier(CLK_LOCAL_MEM_FENCE);
}

static int my_group_scan_exclusive_add( 	int value,
											local int *inp_buf,
											local int *out_buf,
											local int *tmp_buf)
{
	my_group_scan_inclusive_add(inp_buf, out_buf, tmp_buf);
	uint lid = get_local_id(0);

	out_buf[lid] = (lid >0)? tmp_buf[lid-1] + value : value;
	int ret_val = tmp_buf[get_local_size(0)-1] + value;
	barrier(CLK_LOCAL_MEM_FENCE);
	tmp_buf[lid] = out_buf[lid];
	barrier(CLK_LOCAL_MEM_FENCE);
	return ret_val;
}

static void dummy_sort(local int *data, int left, int right)
{
   for (int i = left ; i < right ; i++)
       for (int j = i + 1; j <= right; j++)
       {
           if (data[i] > data[j])
           {
               int aux = data[i];
               data[i] = data[j];
               data[j] = aux;
           }
       }
}

/*
Simple CumSum
*/
kernel void cumsum(
	global char* input,
	global int* output,
	int input_size,
	int output_size,
	local int *a,
	local int *b,
	local int *c)
{
	uint gid = get_global_id(0);
	uint lid = get_local_id(0);
	//uint ws = get_local_size(0);
	if (gid<input_size)
	{
		a[lid] = input[gid];
	}
	else
	{
		a[lid] = 0;
	}
	my_group_scan_inclusive_add(a, b, c);
	if (gid<input_size)	output[gid] = b[lid];
}

/*
 * Function called to determine the data type length per char: 1, 3 or 7
 */
static int data_type(uint idx,
		             global int* input,
					 uint input_size)
{
	int current, previous, value, ret_val=0;
	if (idx < input_size)
	{
		current =  input[idx];
		previous = (idx > 0) ? input[idx-1] : 0;
		value = abs(current - previous);
		if (value > 32767)
		{
			ret_val = 7;
		}
		else if (value > 127)
		{
			ret_val = 3;
		}
		else
		{
			ret_val = 1;
		}
	}
	return ret_val;

}

/**
 * \brief byte_offset decompression for CBF
 *
 * Stage 0 initialize the size_array to -1
 * Stage 1 write 0 or 1 to size_array depending on in the pixel size
 * stage 2 perform a cum_sum on the index to know where to read
 * stage 3 perform a cum_sum on the values
 */
kernel void dec_byte_offset0(
	global int* indices,
	int input_size)
{
	uint gid = get_group_id(0);
	if (gid<input_size)
	{
		indices[gid] = -1;
	}
}

kernel void dec_byte_offset1(
		global char* input,
		global int* indices,
		uint input_size,
		uint chunk,
		global int* start_position,
		global int* end_position,
		local int *local_a,
		local int *local_b
		)
{
	uint lid = get_local_id(0);
	uint ws = get_local_size(0);
	uint wid = get_group_id(0);
	uint nbwg = get_num_groups(0);

	local int exceptions[1];
	local int local_start[1];
	local int new_offset[1];
	local int value_offset[1];

	uint to_process = chunk * ws;
	uint start_process = wid *  to_process;
	uint actual_start = 0;
	uint actual_end;
	uint end_process = min(input_size, start_process + to_process);
	int last = 0;
	char first = (wid==0)? 0: 1;
	if (lid == 0)
	{
		exceptions[0] = 0;
		value_offset[0] = 0;
	}
	local_b[lid] = ws;
	barrier(CLK_LOCAL_MEM_FENCE);


	for (uint offset=start_process; offset< end_process; offset+=ws)
	{
		uint pos = offset + lid;
		if (pos<input_size)
		{
			local_a[lid] = input[pos];
		}
		else
		{
			local_a[lid] = 0;
		}
		if (local_a[lid] == -128)
		{
			int exc_pos;
			exc_pos = atomic_inc(exceptions);
			local_b[exc_pos] = lid;
		}
		barrier(CLK_LOCAL_MEM_FENCE);
		if (first)
		{
			if (exceptions[0])
			{
                if (lid==0)
                {
                	int last_ext = 0;
                	dummy_sort(local_b, 0, exceptions[0]);
                	for (int i=0; i<exceptions[0]; i++)
                	{
                		if ((local_b[i]-last_ext)<=4)
                		{
                			last_ext = local_b[i];
                		}
                		else
                		{
                			local_start[0] = last_ext + 5;
                			break;
                		}
                	}
                }
			}
			else
			{
				local_start[0] = 5;
			}
			barrier(CLK_LOCAL_MEM_FENCE);
			actual_start = local_start[0];
			start_position[wid] = start_process + local_start[0];
		}
		else
		{
			actual_start = 0;
		}
		if ((actual_start==0) && (exceptions[0] == 0))
		{
			local_b[lid] = lid + value_offset[0];

			if (lid==0)
				value_offset[0] = local_b[ws-1]
		}
		else
		{
			if (lid==0)
			{
				for (int i=actual_start, i<ws, i++)
				{

				}
			}
		}

		barrier(CLK_LOCAL_MEM_FENCE);
		offset += new_offset[0];
		value_offset=local_b[lid]

	}
}
/**
 * \brief byte_offset decompression for CBF: Second pass: store the value at the right place
 *
 * Nota: This enforces little-endian storage
 *
 * @param input: input data in 1D as int8
 * @param local_index: input data with output positions, reference
 * @param global_offset: absolute offset of the workgroup, reference
 * @param output: output as int32
 * @param input_size: length of the input
 * @param output_size: length of the output, also size of local_index
 * @param chunk: number of data-point every thread will process
 *
 */
kernel void dec_byte_offset2(
		global int* input,
		global int* local_index,
		global int* global_offset,
		global char* output,
		uint input_size,
		uint output_size,
		uint chunk)
{
	uint gid = get_global_id(0);
	uint wid = get_group_id(0);
	uint ws = get_local_size(0);
	uint lid = get_local_id(0);
	uint to_process = chunk * ws;
	uint start_process = wid *  to_process;
	uint end_process;
	end_process = min(input_size, start_process + to_process);
	int pos_offset = (wid > 0) ? global_offset[wid-1] : 0;
	for (uint offset=start_process; offset< end_process; offset+=ws)
	{
		int current, previous, value, absvalue;
		uint pos = offset + lid;
		if (pos<end_process)
		{
			previous = (pos>0)? input[pos -1]: 0;
			current = input[pos];
			value = current - previous;
			absvalue = abs(value);
			uint dest = pos_offset + local_index[pos];
			if (dest < output_size)
			{
				if (absvalue > 32767)
				{
					output[dest] = -128;
					output[dest+1] = 0;
					output[dest+2] = -128;
					output[dest+3] = (char) (value & 255);
					output[dest+4] = (char) ((value >> 8) & 255);
					output[dest+5] = (char) ((value >> 16) & 255);
					output[dest+6] = (char) (value >> 24);
				}
				else if (absvalue > 127)
				{
					output[dest] = -128;
					output[dest+1] = (char) (value & 255);
					output[dest+2] = (char) (value >> 8);
				}
				else
				{
					output[dest] =  (char) value;
				}
			}
		}
	}
}



/**
 * \brief byte_offset compression for CBF: first pass: measure the size of the elt. UNUSED
 *
 *
 * @param input: input data in 1D as int32
 * @param output: temporary output as int8 with the size of every single
 * @param input_size: length of the input
 *
 */
kernel void comp_byte_offset0(
		global int* input,
		global int* output,
		uint input_size)
{
	uint gid = get_global_id(0);
	if (gid < input_size)
	{
		output[gid] = data_type(gid, input, input_size);
	}
}

/**
 * \brief byte_offset compression for CBF: First pass: cumsum for position calc,
 * merged with stage0
 *
 *
 * @param input: input data in 1D as int32.
 * @param output: output data in 1D as int32.
 * @param input_size: length of the input
 * @param nbwg: number of workgroup finished
 * @param a,b,c: 3 local buffers of the size of the workgroup
 */

kernel void comp_byte_offset1(
		global int* input,
		global int* output,
		uint input_size,
		uint chunk,
		global int* last_wg,
		global uint* workgroup_counter,
		local int *a,
		local int *b,
		local int *c,
		//volatile local int *d,
		global int* ddebug1,
		global int* ddebug2,
		global int* ddebug3)
{
	uint lid = get_local_id(0);
	uint ws = get_local_size(0);
	uint wid = get_group_id(0);
	uint nbwg = get_num_groups(0);
	local int d[1];
	uint to_process = chunk * ws;
	uint start_process = wid *  to_process;
	uint end_process = min(input_size, start_process + to_process);
	ddebug2[wid] = end_process;
	ddebug3[wid] = start_process;
	int last = 0;
	for (uint offset=start_process; offset< end_process; offset+=ws)
	{
		uint pos = offset + lid;
		if (pos<input_size)
		{
			a[lid] = data_type(pos, input, input_size);
		}
		else
		{
			a[lid] = 0;
		}
		barrier(CLK_LOCAL_MEM_FENCE);
		last = my_group_scan_exclusive_add(last, a, b, c);
		if (pos<input_size)
			output[pos] = b[lid];
	}
	if (lid == 0)
	{
		last_wg[wid] = last;
		ddebug1[wid] = d[0] = atomic_inc(workgroup_counter);
	}
	barrier(CLK_LOCAL_MEM_FENCE);
	if ((d[0]+1) == nbwg) // we are the last work group
	{
//		Do a cum_sum of all groups results
		a[lid] = last_wg[lid];
		my_group_scan_inclusive_add(a, b, c);
		last_wg[lid] = b[lid];
	}
}


/**
 * \brief byte_offset compression for CBF: Second pass: store the value at the right place
 *
 * Nota: This enforces little-endian storage
 *
 * @param input: input data in 1D as int8
 * @param local_index: input data with output positions, reference
 * @param global_offset: absolute offset of the workgroup, reference
 * @param output: output as int32
 * @param input_size: length of the input
 * @param output_size: length of the output, also size of local_index
 * @param chunk: number of data-point every thread will process
 *
 */
kernel void comp_byte_offset2(
		global int* input,
		global int* local_index,
		global int* global_offset,
		global char* output,
		uint input_size,
		uint output_size,
		uint chunk)
{
	uint gid = get_global_id(0);
	uint wid = get_group_id(0);
	uint ws = get_local_size(0);
	uint lid = get_local_id(0);
	uint to_process = chunk * ws;
	uint start_process = wid *  to_process;
	uint end_process;
	end_process = min(input_size, start_process + to_process);
	int pos_offset = (wid > 0) ? global_offset[wid-1] : 0;
	for (uint offset=start_process; offset< end_process; offset+=ws)
	{
		int current, previous, value, absvalue;
		uint pos = offset + lid;
		if (pos<end_process)
		{
			previous = (pos>0)? input[pos -1]: 0;
			current = input[pos];
			value = current - previous;
			absvalue = abs(value);
			uint dest = pos_offset + local_index[pos];
			if (dest < output_size)
			{
				if (absvalue > 32767)
				{
					output[dest] = -128;
					output[dest+1] = 0;
					output[dest+2] = -128;
					output[dest+3] = (char) (value & 255);
					output[dest+4] = (char) ((value >> 8) & 255);
					output[dest+5] = (char) ((value >> 16) & 255);
					output[dest+6] = (char) (value >> 24);
				}
				else if (absvalue > 127)
				{
					output[dest] = -128;
					output[dest+1] = (char) (value & 255);
					output[dest+2] = (char) (value >> 8);
				}
				else
				{
					output[dest] =  (char) value;
				}
			}
		}
	}
}