-
Notifications
You must be signed in to change notification settings - Fork 6
/
remap.c
executable file
·660 lines (511 loc) · 23.8 KB
/
remap.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
* Copyright (c) 2012-2019, Triad National Security, LLC.
* All rights Reserved.
*
* Copyright 2012-2019. Triad National Security, LLC. This material was produced
* under U.S. Government contract 89233218CNA000001 for Los Alamos National
* Laboratory (LANL), which is operated by Triad National Security, LLC
* for the U.S. Department of Energy. The U.S. Government has rights to use,
* reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR
* TRIAD NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
* ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is modified
* to produce derivative works, such modified software should be clearly marked,
* so as not to confuse it with the version available from LANL.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.”
*
* This is LANL Copyright Disclosure C13002/LA-CC-12-022
*
*/
/*
* Authors: Bob Robey XCP-2 brobey@lanl.gov
* David Nicholaeff dnic@lanl.gov, mtrxknight@aol.com
* Rachel Robey rnrobey@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include <time.h>
#include "kdtree/KDTree1d.h"
#include "gpu.h"
#include "timer.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __APPLE_CC__
#include <OpenCL/OpenCL.h>
#else
#include <CL/cl.h>
#endif
#ifdef HAVE_CL_DOUBLE
typedef double real;
typedef cl_double cl_real;
#else
typedef float real;
typedef cl_float cl_real;
#endif
typedef unsigned int uint;
#define CHECK 1
#define TILE_SIZE 256
#ifndef MIN
#define MIN(a,b) ((a)>(b)?(b):(a))
#define MAX(a,b) ((a)<(b)?(b):(a))
#endif
#define SWAP_PTR(p1,p2,p3) ((p3=p1), (p1=p2), (p2=p3))
uint seed = 0;
/* Structs to hold higher and lower cell boundaries */
struct cell {
double low;
double high;
};
struct rcell {
real low;
real high;
};
/* CPU Timing Variables */
struct timespec tstart;
double time_sum;
/* OpenCL variables */
cl_context context;
cl_command_queue queue;
cl_program program;
int is_nvidia=0;
cl_kernel cHash_kernel, remap1_kernel;
/* Declare Functions */
int* hashsort( uint length, real *arr, real min_diff, real min_val, real max_val );
real* hashsort2( uint length, real *arr, real *values, real *sorted_values, real min_diff, real min_val, real max_val );
void remaps( int asize, int bsize, double min_diff, double max_diff, double min_val );
real* remap1( struct rcell *arr_a, real *original_values, struct rcell *arr_b, int asize, int bsize, real max_a, real min_val, real min_diff );
real* remap2( real *arr_old_in, real *original_values, real *arr_new_in, int asize, int bsize, real max_a, real max_b, real min_val, real min_diff );
real* remap_kdTree( struct rcell *x, real* original_values, struct rcell *x_new, int ncells, int new_cells, double min_diff, double max_diff, double min_val, double max_a );
real* remap_bruteforce( struct rcell *x, real* original_values, struct rcell *x_new, int ncells, int new_cells, double min_diff, double max_diff, double min_val, double max_a );
void generateRealCells( int size, struct rcell *ptr, int mindx, int maxdx, real min, real *max );
cl_mem parallelRemap1( cl_mem a_buffer, cl_mem v_buffer, cl_mem b_buffer, uint asize, uint bsize, real max_a, real min_val, real min_diff, double *time );
int compare (const void * a, const void * b) { return ( *(double*)a - *(double*)b ); }
/* Begin Funtion Definitions */
int main (int argc, const char * argv[]) {
cl_int error;
#ifdef HAVE_OPENCL
GPUInit(&context, &queue, &is_nvidia, &program, "remap_kern.cl");
cHash_kernel = clCreateKernel(program, "cellHash_kern", &error);
remap1_kernel = clCreateKernel(program, "remap1_kern", &error);
#endif
printf(" REMAP\n\n");
if (is_nvidia)
printf("size, Brute Force, CPU kD Tree, CPU Hash1, CPU Hash2, NVIDIA Hash1\n");
else
printf("size, Brute Force, CPU kD Tree, CPU Hash1, CPU Hash2, ATI Hash1\n");
for( int levmx = 1; levmx < 10; levmx++) {
printf("\nlevmx is %d\n\n",levmx);
for( int i = 1024; i < 50000000; i *= 2) {
printf("%d, ", i);
remaps(i, i, 1.0, (double)levmx, 0.0);
printf("\n");
}
}
}
int* hashsort( uint length, real *arr, real min_diff, real min_val, real max_val ) {
uint temp_size;
int *index = (int*)malloc(length*sizeof(int));
temp_size = (uint)((max_val - min_val)/min_diff + 2.5); //create hash table with buckets of size min_diff -- +2.5 rounds up and adds one space to either side
int *temp = (int*)malloc(temp_size*sizeof(int));
memset(temp, -1, temp_size*sizeof(int)); //set all elements of temp hash array to -1
for(uint i = 0; i < length; i++) {
temp[(int)((arr[i]-min_val)/min_diff)] = i; //place index of current arr element into temp according to where the arr value
}
int count=0;
for(uint i = 0; i < temp_size; i++) {
if(temp[i] >= 0) {
index[count] = temp[i]; //sweep through temp and put set values in a sorted array
count++;
}
}
free(temp);
return index;
}
real* hashsort2( uint length, real *arr, real *values, real *sorted_values, real min_diff, real min_val, real max_val ) {
uint temp_size;
real *sorted = (real*)malloc(length*sizeof(real));
temp_size = (uint)((max_val - min_val)/min_diff + 2.5); //create hash table with buckets of size min_diff -- +2.5 rounds up and adds one space to either side
int *temp = (int*)malloc(temp_size*sizeof(int));
memset(temp, -1, temp_size*sizeof(int)); //set all elements of temp hash array to -1
for(uint i = 0; i < length; i++) {
temp[(int)((arr[i]-min_val)/min_diff)] = i; //place index of current arr element into temp according to where the arr value
}
int count=0;
for(uint i = 0; i < temp_size; i++) {
if(temp[i] >= 0) {
sorted[count] = arr[temp[i]]; //sweep through temp and put set values in a sorted array
sorted_values[count] = values[temp[i]]; //sweep through temp and put set values in a sorted array
count++;
}
}
free(temp);
return sorted;
}
void remaps( int asize, int bsize, double min_diff, double max_diff, double min_val ) {
struct rcell *arr_a, *arr_b;
real max_a, max_b;
int i;
real *remap_gold, *remap_test;
arr_a = (struct rcell*)malloc(asize*sizeof(struct rcell));
arr_b = (struct rcell*)malloc(bsize*sizeof(struct rcell));
generateRealCells( asize, arr_a, min_diff, max_diff, min_val, &max_a );
generateRealCells( bsize, arr_b, min_diff, max_diff, min_val, &max_b );
real *original_values = (real *)malloc((asize+1)*sizeof(real));
for (i = 0; i<asize; i++) { original_values[i] = arr_a[i].high - arr_a[i].low; }
real *arr_old_in = (real *)malloc((asize+1)*sizeof(real));
real *arr_new_in = (real *)malloc((bsize+1)*sizeof(real));
for(int a = 0; a < asize; a++) {
arr_old_in[a] = arr_a[a].low;
}
for(int b = 0; b < bsize; b++) {
arr_new_in[b] = arr_b[b].low;
}
arr_old_in[asize] = max_a;
arr_new_in[bsize] = max_b;
original_values[asize] = 0.0;
/* Brute Force remap */
if (asize < 600000) {
cpu_timer_start(&tstart);
remap_gold = remap_bruteforce(arr_a, original_values, arr_b, asize, bsize, min_diff, max_diff, min_val, max_a);
time_sum += cpu_timer_stop(tstart);
printf("%.6lf, ", time_sum);
} else {
printf("not_run, ");
}
/* CPU kD Tree */
cpu_timer_start(&tstart);
if (asize < 600000) {
remap_test = remap_kdTree(arr_a, original_values, arr_b, asize, bsize, min_diff, max_diff, min_val, max_a);
} else {
remap_gold = remap_kdTree(arr_a, original_values, arr_b, asize, bsize, min_diff, max_diff, min_val, max_a);
}
time_sum += cpu_timer_stop(tstart);
printf("%.6lf, ", time_sum);
if (asize < 600000){
for(i = 0; i < bsize; i++) {
if(fabs(remap_gold[i] - remap_test[i]) > 1.0e-6)
printf("Check failed for remap_kdTree CPU index %d remap value %lf gold standard %lf\n", i, remap_test[i], remap_gold[i]);
}
free(remap_test);
}
/* CPU Hash Remap1 */
cpu_timer_start(&tstart);
//for (int ic = 0; ic < asize; ic++){
// printf("Array In %d %lf %lf value %lf\n",ic,arr_a[ic].low,arr_a[ic].high,original_values[ic]);
//}
//for (int ic = 0; ic < bsize; ic++){
// printf("Array In %d %lf %lf\n",ic,arr_b[ic].low,arr_b[ic].high);
//}
remap_test = remap1( arr_a, original_values, arr_b, asize, bsize, max_a, min_val, min_diff );
time_sum += cpu_timer_stop(tstart);
printf("%.6lf, ", time_sum);
int icount = 0;
for(i = 0; i < bsize; i++) {
if(fabs(remap_gold[i] - remap_test[i]) > 1.0e-6){
printf("Check failed for remap1 CPU index %d remap value %lf gold standard %lf\n", i, remap_test[i], remap_gold[i]);
icount++;
}
if (icount > 20) exit(0);
}
free(remap_test);
/* CPU Hash Remap2 */
cpu_timer_start(&tstart);
remap_test = remap2( arr_old_in, original_values, arr_new_in, asize, bsize, max_a, max_b, min_val, min_diff );
time_sum += cpu_timer_stop(tstart);
printf("%.6lf, ", time_sum);
icount = 0;
for(i = 0; i < bsize; i++) {
if(fabs(remap_gold[i] - remap_test[i]) > 1.0e-6){
printf("Check failed for remap2 CPU index %d remap value %lf gold standard %lf\n", i, remap_test[i], remap_gold[i]);
icount++;
}
if (icount > 20) exit(0);
}
free(remap_test);
if (is_nvidia || (max_diff < 8.0 && asize < 20000000) || asize < 10000000) {
#ifdef HAVE_OPENCL
/* GPU Hash Remap1 */
cl_int error;
cl_mem a_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, asize*sizeof(struct rcell), NULL, &error);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clEnqueueWriteBuffer(queue, a_buffer, CL_TRUE, 0, asize*sizeof(struct rcell), arr_a, 0, NULL, NULL);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
cl_mem b_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, bsize*sizeof(struct rcell), NULL, &error);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clEnqueueWriteBuffer(queue, b_buffer, CL_TRUE, 0, bsize*sizeof(struct rcell), arr_b, 0, NULL, NULL);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
cl_mem v_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, asize*sizeof(real), NULL, &error);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clEnqueueWriteBuffer(queue, v_buffer, CL_TRUE, 0, asize*sizeof(real), original_values, 0, NULL, NULL);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
cl_mem remap_buffer = parallelRemap1( a_buffer, v_buffer, b_buffer, (uint)asize, (uint)bsize, max_a, min_val, min_diff, &time_sum );
printf("%.6lf, ", time_sum);
remap_test = (real *)malloc(bsize*sizeof(real));
error = clEnqueueReadBuffer(queue, remap_buffer, CL_TRUE, 0, bsize*sizeof(real), remap_test, 0, NULL, NULL);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
clReleaseMemObject(remap_buffer);
icount = 0;
for(i = 0; i < bsize; i++) {
if(fabs(remap_gold[i] - remap_test[i]) > 1.0e-6){
printf("Check failed for remap1 GPU index %d remap value %lf gold standard %lf\n", i, remap_test[i], remap_gold[i]);
icount++;
}
if (icount > 20) exit(0);
}
free(remap_test);
clReleaseMemObject(a_buffer);
clReleaseMemObject(b_buffer);
clReleaseMemObject(v_buffer);
#endif
} else {
printf("not_run ");
}
free(remap_gold);
free(arr_a);
free(arr_b);
free(original_values);
free(arr_old_in);
free(arr_new_in);
}
real* remap_bruteforce( struct rcell *x, real *original_values, struct rcell *x_new, int ncells, int new_cells, double min_diff, double max_diff, double min_val, double max_a ) {
int ic,jc;
int num;
real* remap = (real*)malloc((new_cells)*sizeof(real));
for (ic = 0; ic < new_cells; ic++){
remap[ic]=0.0;
//printf("x new %d low %lf high %lf\n",ic,x_new[ic].low,x_new[ic].high);
for (jc = 0; jc < ncells; jc++){
//printf("x original %d low %lf high %lf\n",jc,x[jc].low,x[jc].high);
real overlap_area = MIN(x[jc].high,x_new[ic].high)-MAX(x[jc].low,x_new[ic].low);
//printf("overlap is %lf\n",overlap_area);
if (overlap_area > 0) {
real original_area = x[jc].high - x[jc].low;
real mapped_value = overlap_area/original_area * original_values[jc];
remap[ic] += mapped_value;
}
}
}
//for (ic = 0; ic < new_cells; ic++){
// printf("%d: remap %lf\n",ic,remap[ic]);
//}
return remap;
}
real* remap_kdTree( struct rcell *x, real *original_values, struct rcell *x_new, int ncells, int new_cells, double min_diff, double max_diff, double min_val, double max_a ) {
int ic,jc;
int num;
int index_list[20];
TKDTree1d tree;
real* remap = (real*)malloc((new_cells)*sizeof(real));
KDTree_Initialize1d(&tree);
TBounds1d box;
//printf("\n");
for (ic = 0; ic < ncells; ic++) {
box.min.x = x[ic].low;
box.max.x = x[ic].high;
//printf("Add box %x min %lf max %lf\n",ic,box.min.x,box.max.x);
KDTree_AddElement1d(&tree, &box);
}
for (ic = 0; ic < new_cells; ic++){
remap[ic]=0.0;
box.min.x = x_new[ic].low;
box.max.x = x_new[ic].high;
//printf("box for %d min %lf max %lf\n",ic,box.min.x,box.max.x);
KDTree_QueryBoxIntersect1d(&tree, &num, &(index_list[0]), &box);
for (jc = 0; jc<num; jc++){
real overlap_area = MIN(x[index_list[jc]].high,x_new[ic].high)-MAX(x[index_list[jc]].low,x_new[ic].low);
real original_area = x[index_list[jc]].high - x[index_list[jc]].low;
real mapped_value = overlap_area/original_area * original_values[index_list[jc]];
remap[ic]+=mapped_value;
}
}
KDTree_Destroy1d(&tree);
//for (ic = 0; ic < new_cells; ic++){
// printf("%d: remap %lf\n",ic,remap[ic]);
//}
return remap;
}
real* remap1( struct rcell *arr_a, real* original_values, struct rcell *arr_b, int asize, int bsize, real max_a, real min_val, real min_diff ) {
int a, b, i;
int start, end;
int temp_size = (uint)((max_a - min_val)/min_diff);
real *remap = (real*)malloc(bsize*sizeof(real));
int* temp = malloc(temp_size*sizeof(int));
/* Create a Hash Table for the first (old) array */
memset(temp, -1, temp_size*sizeof(int));
for(a = 0; a < asize; a++) {
start = (int)((arr_a[a].low+min_val)/min_diff);
end = (int)((arr_a[a].high+min_val)/min_diff);
while( start < end ) {
temp[start] = a;
start++;
}
}
for(b = 0; b < bsize; b++) {
remap[b] = 0;
if( (start = (arr_b[b].low - min_val)/min_diff) < temp_size) {
end = MIN(temp_size, (arr_b[b].high - min_val)/min_diff);
for(i = start; i < end; i++) {
if(temp[i] >= 0) {
remap[b] += original_values[temp[i]] * 1.0/(arr_a[temp[i]].high-arr_a[temp[i]].low); //assume state variable is in original_value
}
}
}
}
free(temp);
//for (int ic = 0; ic < bsize; ic++){
// printf("%d: remap %lf\n",ic,remap[ic]);
//}
return remap;
}
real* remap2( real *arr_old_in, real *original_values, real *arr_new_in, int asize, int bsize, real max_a, real max_b, real min_val, real min_diff ) {
double range, fraction;
int a, b;
real* remap = (real*)malloc(bsize*sizeof(real));
for (int ic = 0; ic < bsize; ic++){
remap[ic] = 0.0;
}
/* convert from cell format to array of reals */
asize += 1; bsize += 1;
real *sorted_values = (real*)malloc(asize*sizeof(real));
real *arr_old = hashsort2((uint)asize, arr_old_in, original_values, sorted_values, min_diff, min_val, max_a);
int *index = hashsort((uint)bsize, arr_new_in, min_diff, min_val, max_b);
real *arr_new = (real*)malloc(bsize*sizeof(real));
for (b = 0; b < bsize; b++){
arr_new[b] = arr_new_in[index[b]];
}
//for(b = 0; b < bsize; b++) { //for bsize = asize
// printf("%d index %d\n",b,index[b]);
//}
//printf("\n\n");
//for(a = 0; a < asize; a++) { //for bsize = asize
// printf("%lf %lf\n", arr_old[a], arr_new[a]);
//}
//printf("\n\n");
b = 1;
for(a = 1; a < asize; a++) {
while(b < bsize && arr_new[b] <= arr_old[a] ) {
range = arr_new[b] - MAX(arr_old[a-1], arr_new[b-1]);
fraction = range/(arr_old[a] - arr_old[a-1]);
remap[index[b-1]] += fraction * sorted_values[a-1];
b++;
}
range = arr_old[a] - MAX(arr_old[a-1], arr_new[b-1]);
fraction = range/(arr_old[a]-arr_old[a-1]);
remap[index[b-1]] += fraction * sorted_values[a-1];
}
free(arr_old);
free(index);
free(sorted_values);
free(arr_new);
//for (int ic = 0; ic < bsize-1; ic++){
// printf("%d: remap %lf\n",ic,remap[ic]);
//}
return remap;
}
void generateRealCells( int size, struct rcell *ptr, int mindx, int maxdx, real min, real *max ) {
int i, index, front = 0;
struct rcell swap;
struct timespec tim; //random seeding
clock_gettime(CLOCK_MONOTONIC, &tim);
//srand(tim.tv_sec*tim.tv_nsec);
srand(seed);
seed++;
ptr[0].low = min; //start the array using the minimum value
/* for each element, add a random value between mindx and maxdx to the previous element's value */
for(i = 0; i < size-1; i++) {
ptr[i].high = ptr[i].low + mindx + rand() % (maxdx - mindx + 1);
ptr[i+1].low = ptr[i].high;
}
ptr[size-1].high = ptr[size-1].low + mindx + rand() % (maxdx - mindx + 1);
*max = ptr[size-1].high; //set the max value to the last element's value
/* Mix up the array by selecting elements from shrinking front portion of array and placing them on back end of array */
for(i = 0; (i < size) && (size - i != 0) ; i++) {
index = rand() % (size - i - front) + front;
swap = ptr[size-i-1];
ptr[size-i-1] = ptr[index];
ptr[index] = swap;
}
}
#ifdef HAVE_OPENCL
cl_mem parallelRemap1( cl_mem a_buffer, cl_mem v_buffer, cl_mem b_buffer, uint asize, uint bsize, real max_a, real min_val, real min_diff, double *time ) {
cl_int error = 0;
uint temp_size = (uint)((max_a - min_val)/min_diff);
cl_mem temp_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, temp_size*sizeof(int), NULL, &error);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
size_t global_work_size[1];
size_t local_work_size[1];
local_work_size[0] = TILE_SIZE;
global_work_size[0] = ((asize+local_work_size[0]-1)/local_work_size[0])*local_work_size[0];
/******************
* Hash Kernel
******************/
error = clSetKernelArg(cHash_kernel, 0, sizeof(real), &min_val);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(cHash_kernel, 1, sizeof(real), &min_diff);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(cHash_kernel, 2, sizeof(cl_uint), &asize);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(cHash_kernel, 3, sizeof(cl_mem), (void*)&a_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(cHash_kernel, 4, sizeof(cl_mem), (void*)&temp_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
global_work_size[0] = ((asize+local_work_size[0]-1)/local_work_size[0])*local_work_size[0];
cl_event hash_kernel_event;
error = clEnqueueNDRangeKernel(queue, cHash_kernel, 1, 0, global_work_size, local_work_size, 0, NULL, &hash_kernel_event);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
/*****************
* Remap Kernel
*****************/
cl_mem remap_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, bsize*sizeof(real), NULL, &error);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 0, sizeof(real), &min_val);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 1, sizeof(real), &min_diff);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 2, sizeof(cl_uint), &temp_size);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 3, sizeof(cl_uint), &bsize);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 4, sizeof(cl_mem), (void*)&a_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 5, sizeof(cl_mem), (void*)&v_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 6, sizeof(cl_mem), (void*)&b_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 7, sizeof(cl_mem), (void*)&temp_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
error = clSetKernelArg(remap1_kernel, 8, sizeof(cl_mem), (void*)&remap_buffer);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
global_work_size[0] = ((bsize+local_work_size[0]-1)/local_work_size[0])*local_work_size[0];
cl_event remap_event;
error = clEnqueueNDRangeKernel(queue, remap1_kernel, 1, 0, global_work_size, local_work_size, 0, NULL, &remap_event);
if (error != CL_SUCCESS) printf("Error is %d at line %d\n",error,__LINE__);
long gpu_time_start, gpu_time_end, gpu_time=0;
clWaitForEvents(1, &remap_event);
clGetEventProfilingInfo(hash_kernel_event, CL_PROFILING_COMMAND_START, sizeof(gpu_time_start), &gpu_time_start, NULL);
clGetEventProfilingInfo(hash_kernel_event, CL_PROFILING_COMMAND_END, sizeof(gpu_time_end), &gpu_time_end, NULL);
gpu_time += gpu_time_end - gpu_time_start;
clReleaseEvent(hash_kernel_event);
clGetEventProfilingInfo(remap_event, CL_PROFILING_COMMAND_START, sizeof(gpu_time_start), &gpu_time_start, NULL);
clGetEventProfilingInfo(remap_event, CL_PROFILING_COMMAND_END, sizeof(gpu_time_end), &gpu_time_end, NULL);
gpu_time += gpu_time_end - gpu_time_start;
clReleaseEvent(remap_event);
clReleaseMemObject(temp_buffer);
*time = gpu_time*1.0e-9;
return remap_buffer;
}
#endif