-
Notifications
You must be signed in to change notification settings - Fork 3
/
mpa_16bits.c
746 lines (618 loc) · 28.3 KB
/
mpa_16bits.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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
// add Unit test using cmocka.org
/*
https://community.amd.com/thread/145960
Okay, I see what the problem is now.
I had 'uint64_t' defined as 'unsigned long long' (which is standard in 32-bit gcc and also works in 64-bit gcc).
But in the OpenCL spec, the 64-bit type is 'unsigned long', and 'unsigned long long' is the 128-bit type.
If I define uint64_t as 'ulong' as per the OpenCL spec, mul_hi works correctly.
The reason why didn't blow up earlier is that, apparently, Stream does NOT really treat 'unsigned long long' as a 128-bit type. (So it does not get off scot-free, there's still a bug there). In particular, sizeof(unsigned long long) is 8,
and all my code except for the mul_hi instruction works as if it were uint64_t.
64-bit Atomics
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmp.h>
#include <openssl/rand.h>
#include <time.h>
#include <limits.h>
#include <errno.h>
#ifdef __APPLE__
#include <OpenCL/opencl.h>
#else
#include <CL/cl.h>
#endif
#define COMPARE 0
#define ADD 1
#define SUBTRACT 2
#define ADDMOD 3
#define SUBTRACTMOD 4
#define MULTIPLYOPRANDSCANNING 5
#define MULTIPLYPRODUCTSCANNING 6
#define MONTGOMERYMULTIPLICATION 7
#define ARITHMETICS 8
#define MAX_SOURCE_SIZE (0x100000)
// Terminal Colors
#define END_COLOR "\x1b[0m"
#define BLUE_TERMINAL "\x1b[34m"
#define RED_TERMINAL "\x1b[31m"
#define GREEN_TERMINAL "\x1b[32m"
int compareArray(unsigned short* a,unsigned short* b,const int SIZE,const int WORDLINGTH) {
int diff=WORDLINGTH-SIZE;
for (int i = 0; i < SIZE; i++){
if(a[i+diff]>b[i]) {
//printf("> %d %d %d\n",i,a[i],b[i]);
return 1;}
if(a[i+diff]<b[i]) {
//printf("< %d %d %d\n",i,a[i],b[i]);
return -1;
}
}
return 0;
}
char ansi[]= "\x1b[32m";
void printArray(unsigned short* bytes,const int SIZE,const size_t ID) {
char charAti[8];
sprintf(charAti,"[%5ld", bytes[ID*SIZE]);
printf("%s",charAti );
for (int i = 1; i < SIZE; i++){
sprintf(charAti,",%5ld", bytes[i+ID*SIZE]);
printf("%s",charAti );
}
printf("]\n" );
}
const char *getErrorString(cl_int error)
{
switch(error){
case 0: return "CL_SUCCESS";
case -1: return "CL_DEVICE_NOT_FOUND";
case -2: return "CL_DEVICE_NOT_AVAILABLE";
case -3: return "CL_COMPILER_NOT_AVAILABLE";
case -4: return "CL_MEM_OBJECT_ALLOCATION_FAILURE";
case -5: return "CL_OUT_OF_RESOURCES";
case -6: return "CL_OUT_OF_HOST_MEMORY";
case -7: return "CL_PROFILING_INFO_NOT_AVAILABLE";
case -8: return "CL_MEM_COPY_OVERLAP";
case -9: return "CL_IMAGE_FORMAT_MISMATCH";
case -10: return "CL_IMAGE_FORMAT_NOT_SUPPORTED";
case -11: return "CL_BUILD_PROGRAM_FAILURE";
case -12: return "CL_MAP_FAILURE";
case -13: return "CL_MISALIGNED_SUB_BUFFER_OFFSET";
case -14: return "CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST";
case -15: return "CL_COMPILE_PROGRAM_FAILURE";
case -16: return "CL_LINKER_NOT_AVAILABLE";
case -17: return "CL_LINK_PROGRAM_FAILURE";
case -18: return "CL_DEVICE_PARTITION_FAILED";
case -19: return "CL_KERNEL_ARG_INFO_NOT_AVAILABLE";
case -30: return "CL_INVALID_VALUE";
case -31: return "CL_INVALID_DEVICE_TYPE";
case -32: return "CL_INVALID_PLATFORM";
case -33: return "CL_INVALID_DEVICE";
case -34: return "CL_INVALID_CONTEXT";
case -35: return "CL_INVALID_QUEUE_PROPERTIES";
case -36: return "CL_INVALID_COMMAND_QUEUE";
case -37: return "CL_INVALID_HOST_PTR";
case -38: return "CL_INVALID_MEM_OBJECT";
case -39: return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR";
case -40: return "CL_INVALID_IMAGE_SIZE";
case -41: return "CL_INVALID_SAMPLER";
case -42: return "CL_INVALID_BINARY";
case -43: return "CL_INVALID_BUILD_OPTIONS";
case -44: return "CL_INVALID_PROGRAM";
case -45: return "CL_INVALID_PROGRAM_EXECUTABLE";
case -46: return "CL_INVALID_KERNEL_NAME";
case -47: return "CL_INVALID_KERNEL_DEFINITION";
case -48: return "CL_INVALID_KERNEL";
case -49: return "CL_INVALID_ARG_INDEX";
case -50: return "CL_INVALID_ARG_VALUE";
case -51: return "CL_INVALID_ARG_SIZE";
case -52: return "CL_INVALID_KERNEL_ARGS";
case -53: return "CL_INVALID_WORK_DIMENSION";
case -54: return "CL_INVALID_WORK_GROUP_SIZE";
case -55: return "CL_INVALID_WORK_ITEM_SIZE";
case -56: return "CL_INVALID_GLOBAL_OFFSET";
case -57: return "CL_INVALID_EVENT_WAIT_LIST";
case -58: return "CL_INVALID_EVENT";
case -59: return "CL_INVALID_OPERATION";
case -60: return "CL_INVALID_GL_OBJECT";
case -61: return "CL_INVALID_BUFFER_SIZE";
case -62: return "CL_INVALID_MIP_LEVEL";
case -63: return "CL_INVALID_GLOBAL_WORK_SIZE";
case -64: return "CL_INVALID_PROPERTY";
case -65: return "CL_INVALID_IMAGE_DESCRIPTOR";
case -66: return "CL_INVALID_COMPILER_OPTIONS";
case -67: return "CL_INVALID_LINKER_OPTIONS";
case -68: return "CL_INVALID_DEVICE_PARTITION_COUNT";
case -1000: return "CL_INVALID_GL_SHAREGROUP_REFERENCE_KHR";
case -1001: return "CL_PLATFORM_NOT_FOUND_KHR";
case -1002: return "CL_INVALID_D3D10_DEVICE_KHR";
case -1003: return "CL_INVALID_D3D10_RESOURCE_KHR";
case -1004: return "CL_D3D10_RESOURCE_ALREADY_ACQUIRED_KHR";
case -1005: return "CL_D3D10_RESOURCE_NOT_ACQUIRED_KHR";
default: return "Unknown OpenCL error";
}
}
void printDeviceInfo(cl_device_id device)
{
char queryBuffer[1024];
int queryInt;
size_t querySize_t[3];
unsigned int queryUint;
cl_int clError;
clError = clGetDeviceInfo(device, CL_DEVICE_NAME,
sizeof(queryBuffer),
&queryBuffer, NULL);
printf("CL_DEVICE_NAME: %s\n", queryBuffer);
queryBuffer[0] = '\0';
clError = clGetDeviceInfo(device, CL_DEVICE_VENDOR,
sizeof(queryBuffer), &queryBuffer,
NULL);
printf("CL_DEVICE_VENDOR: %s\n", queryBuffer);
queryBuffer[0] = '\0';
clError = clGetDeviceInfo(device, CL_DRIVER_VERSION,
sizeof(queryBuffer), &queryBuffer,
NULL);
printf("CL_DRIVER_VERSION: %s\n", queryBuffer);
queryBuffer[0] = '\0';
clError = clGetDeviceInfo(device, CL_DEVICE_VERSION,
sizeof(queryBuffer), &queryBuffer,
NULL);
printf("CL_DEVICE_VERSION: %s\n", queryBuffer);
queryBuffer[0] = '\0';
clError = clGetDeviceInfo(device, CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(int), &queryInt, NULL);
printf("CL_DEVICE_MAX_COMPUTE_UNITS: %d\n", queryInt);
clError = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
sizeof(int), &queryInt, NULL);
printf("CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS: %d\n", queryInt);
clError = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t), querySize_t, NULL);
printf("CL_DEVICE_MAX_WORK_GROUP_SIZE: %d\n", querySize_t[0]);
clError = clGetDeviceInfo(device, CL_DEVICE_MAX_WORK_ITEM_SIZES,
3*sizeof(size_t), querySize_t, NULL);
printf("CL_DEVICE_MAX_WORK_GROUP_SIZE: {%zu, %zu, %zu}\n", querySize_t[0], querySize_t[1], querySize_t[2]);
}
void printKernelWorkGroupInfo(cl_kernel kernel,cl_device_id device)
{
size_t SIZE[3];
int queryInt;
cl_int clError;
clError = clGetKernelWorkGroupInfo(kernel,device, CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE,
sizeof(size_t),
SIZE, NULL);
printf("CL_KERNEL_PREFERRED_WORK_GROUP_SIZE_MULTIPLE : %zu\n", SIZE[0]);
clError = clGetKernelWorkGroupInfo(kernel,device, CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
SIZE, NULL);
printf("CL_KERNEL_WORK_GROUP_SIZE : %zu\n", SIZE[0]);
}
const char *decode(int OPERATOR){
switch(OPERATOR){
case ADD: return "ADD";
case ADDMOD: return "ADDMOD";
case SUBTRACTMOD: return "SUBTRACTMOD";
case SUBTRACT: return "SUBTRACT";
case MULTIPLYPRODUCTSCANNING: return "MULTIPLYPRODUCTSCANNING";
case MULTIPLYOPRANDSCANNING: return "MULTIPLYOPRANDSCANNING";
case MONTGOMERYMULTIPLICATION: return "MONTGOMERYMULTIPLICATION";
}
return "INDEFINED OPERATOR";
}
int main(int argc, char **argv)
{
int base=10;
int DEBUG_MODE=0;
char *endptr, *str;
unsigned long long int iterations;
if (argc < 5) {
fprintf(stderr, "Usage: %s NumberOfIteration OPERATOR BITLENGTH WORDSIZE [DEBUG_MODE] \n", argv[0]);
exit(EXIT_FAILURE);
}
str = argv[1];
errno = 0; /* To distinguish success/failure after call */
iterations = strtoull(str, &endptr, base);
/* Check for various possible errors */
if ((errno == ERANGE && (iterations == LONG_MAX || iterations == LONG_MIN))
|| (errno != 0 && iterations == 0)) {
perror("strtoull");
exit(EXIT_FAILURE);
}
if (endptr == str) {
fprintf(stderr, "Please specify these arguments NumberOfIteration OPERATOR BITLENGTH WORDSIZE !\n");
exit(EXIT_FAILURE);
}
DEBUG_MODE = (argc > 5) ? atoi(argv[5]) : 0;
int OPERATOR = atoi(argv[2]);
int WORDSIZE = atoi(argv[4]);
int BITSLENGTH = atoi(argv[3]);
int WORDLENGTH = BITSLENGTH/WORDSIZE;
// printf("WORDLENGTH = %d\n",WORDLENGTH);
// printf("DEBUG_MODE = %d\n",DEBUG_MODE);
int MPRIME = 49;
unsigned short* PRIME;
PRIME= (unsigned short*)malloc(WORDLENGTH*sizeof(unsigned short));
mpz_t bigPrime;
switch(BITSLENGTH){
case 256 : {
const char* primeStr= "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F";
mpz_init(bigPrime);
size_t* count;
count = (size_t*) malloc(sizeof(size_t));
mpz_set_ui(bigPrime,0);
mpz_set_str(bigPrime,primeStr, 16);
PRIME=mpz_export(NULL, count, 1, sizeof(unsigned short), 1, 0, bigPrime);
//precalculated m_prime
MPRIME = 49;
}
break;
case 512 : {
// brainpoolP512r1 https://www.teletrust.de/fileadmin/files/oid/ecgdsa_final.pdf
const char* primeStr=
"AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3";
mpz_init(bigPrime);
size_t* count;
count = (size_t*) malloc(sizeof(size_t));
mpz_set_ui(bigPrime,0);
mpz_set_str(bigPrime,primeStr, 16);
PRIME=mpz_export(NULL, count, 1, sizeof(unsigned short), 1, 0, bigPrime);
//precalculated m_prime
MPRIME = 49;
}
break;
case 1024 : {
const char* primeStr=
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"\
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF97";
mpz_init(bigPrime);
size_t* count;
count = (size_t*) malloc(sizeof(size_t));
mpz_set_ui(bigPrime,0);
mpz_set_str(bigPrime,primeStr, 16);
PRIME=mpz_export(NULL, count, 1, sizeof(unsigned short), 1, 0, bigPrime);
//precalculated m_prime
MPRIME = 49;
}
break;
case 2048 :{
const char* primeStr=
"E53DF5FC3F650D066875837012A4E7BEA863C65CB592D9C36942CF69CBC6DD4F"\
"D804E19CCF2696C9BEBCF18742FA5FB091CBDE1782E8291009464913ECE19745"\
"7800EA6E43B0E2A64615D182B6DE150479C58D1C7C702D47EA3031B379CA13A2"\
"048C964E1D1E8D4CD3815D0895BF31E53271D4607E16461B77FB26100915D679"\
"9060203EDEBFEA9495A5A8E7CED68FC9DB2D47CE7992461BA78174608AD0BBE3"\
"F5E63EC6C960564430CBD2E6E587D08EE12F94B5B99DFFB12C6727A25E800DAC"\
"6CD8DE77A5BBC93B36E444B070888CB5ADD991870466968A6E9A23C2EE0A1D67"\
"1C9B601081A44AA6A58D4DC76686EF15FCE1C9AEB4033395A9B24BE1AA1929BB";
mpz_init(bigPrime);
size_t* count;
count = (size_t*) malloc(sizeof(size_t));
mpz_set_ui(bigPrime,0);
mpz_set_str(bigPrime,primeStr, 16);
PRIME=mpz_export(NULL, count, 1, sizeof(unsigned short), 1, 0, bigPrime);
//precalculated m_prime
MPRIME = 49;
}
break;
}
// printArray(PRIME,WORDLENGTH,0);
struct timespec tstart={0,0}, tend_init={0,0} , tend_createContext={0,0},
tend_loadTomemory={0,0},tend_BuildProgram={0,0}, tend_createKernel={0,0}, tend_exec={0,0}, tend_redResults={0,0}, tend_test={0,0};
clock_gettime(CLOCK_MONOTONIC, &tstart);
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_context context = NULL;
cl_command_queue command_queue = NULL;
cl_mem Amobj = NULL;
cl_mem Bmobj = NULL;
cl_mem Cmobj = NULL;
cl_mem Omobj = NULL;
cl_mem Pmobj = NULL;
cl_program program = NULL;
cl_kernel kernel = NULL;
cl_uint ret_num_devices;
cl_uint ret_num_platforms;
cl_int ret;
size_t k1=1024;
size_t K = k1*iterations;
// printf("K = %zu\n", K);
const size_t global[]={K/WORDLENGTH}; // global domain size
const size_t local[]={1};
int i, j;
unsigned short* A;
unsigned short* B;
unsigned short* C;
int* OPERATOR_WORDSIZE_BITSLENGHT_MPRIME;
A = (unsigned short*)malloc(K*sizeof(unsigned short));
B = (unsigned short*)malloc(K*sizeof(unsigned short));
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING) C = (unsigned short*)malloc(2*K*sizeof(unsigned short));
else C = (unsigned short*)malloc(K*sizeof(unsigned short));
OPERATOR_WORDSIZE_BITSLENGHT_MPRIME = (int*)malloc(4*sizeof(int));
FILE *fp;
const char fileName[] = "mpaKernel_16bits.cl";
size_t source_size;
char *source_str;
/* Load kernel source file */
fp = fopen(fileName, "rb");
if (!fp) {
fprintf(stderr, "Failed to load kernel.\n");
exit(1);
}
source_str = ( char *)malloc(MAX_SOURCE_SIZE);
source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
fclose(fp);
/* Initialize input data randomly using Openssl RAND_bytes */
unsigned short* AR;
unsigned short* BR;
AR = (unsigned short*)malloc(WORDLENGTH*sizeof(unsigned short));
BR = (unsigned short*)malloc(WORDLENGTH*sizeof(unsigned short));
for (size_t i=0; i < K/ (WORDLENGTH); i++) {
RAND_pseudo_bytes(AR, 2*WORDLENGTH);
RAND_pseudo_bytes(BR, 2*WORDLENGTH);
if(compareArray(AR,BR,WORDLENGTH,WORDLENGTH)==-1){
unsigned short* tempArr;
tempArr=AR;
AR=BR;
BR=tempArr;
}
for(int j=0;j<WORDLENGTH;j++){
A[i*WORDLENGTH+j]=AR[j];
B[i*WORDLENGTH+j]=BR[j];
}
}
// for montgomery , ADDMOD and SUBTRACTMOD test ensure A<PRIMEtest
if(OPERATOR==MONTGOMERYMULTIPLICATION||OPERATOR==ADDMOD||OPERATOR==SUBTRACTMOD){
for (size_t i=0; i < K; i+=WORDLENGTH){
if (A[i]>=PRIME[0]) A[i]=PRIME[0]-1;
if(B[i]>=PRIME[0]) B[i]=PRIME[0]-1;
}
}
free(AR);
free(BR);
clock_gettime(CLOCK_MONOTONIC, &tend_init);
/* Get platform/device information */
ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
//ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices);
ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_CPU, 1, &device_id, &ret_num_devices);
printDeviceInfo(device_id);
/* Create OpenCL Context */
context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
/* Create command queue */
command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
clock_gettime(CLOCK_MONOTONIC, &tend_createContext);
/* Create buffer object */
Amobj = clCreateBuffer(context, CL_MEM_READ_ONLY, K*sizeof(unsigned short), NULL, &ret);
Bmobj = clCreateBuffer(context, CL_MEM_READ_ONLY, K*sizeof(unsigned short), NULL, &ret);
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING) Cmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, 2*K*sizeof(unsigned short), NULL, &ret);
else Cmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, K*sizeof(unsigned short), NULL, &ret);
Omobj = clCreateBuffer(context, CL_MEM_READ_WRITE, 4*sizeof(int), NULL, &ret);
Pmobj = clCreateBuffer(context, CL_MEM_READ_WRITE, WORDLENGTH*sizeof(unsigned short), NULL, &ret);
/* Copy input data to memory buffer */
ret = clEnqueueWriteBuffer(command_queue, Amobj, CL_TRUE, 0, K*sizeof(unsigned short), A, 0, NULL, NULL);
ret = clEnqueueWriteBuffer(command_queue, Bmobj, CL_TRUE, 0, K*sizeof(unsigned short), B, 0, NULL, NULL);
OPERATOR_WORDSIZE_BITSLENGHT_MPRIME[0]=OPERATOR;
OPERATOR_WORDSIZE_BITSLENGHT_MPRIME[1]=WORDSIZE;
OPERATOR_WORDSIZE_BITSLENGHT_MPRIME[2]=BITSLENGTH;
OPERATOR_WORDSIZE_BITSLENGHT_MPRIME[3]=MPRIME;
ret = clEnqueueWriteBuffer(command_queue, Omobj, CL_TRUE, 0, 4*sizeof(int), OPERATOR_WORDSIZE_BITSLENGHT_MPRIME , 0, NULL, NULL);
ret = clEnqueueWriteBuffer(command_queue, Pmobj, CL_TRUE, 0, WORDLENGTH*sizeof(unsigned short), PRIME, 0, NULL, NULL);
clock_gettime(CLOCK_MONOTONIC, &tend_loadTomemory);
/* Create kernel from source */
program = clCreateProgramWithSource(context, 1, (const char **)&source_str, (const size_t *)&source_size, &ret);
ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
if (ret != CL_SUCCESS) {
char buffer[10240];
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, NULL);
fprintf(stderr, "CL Compilation failed:\n%s", buffer);
abort();
}
clock_gettime(CLOCK_MONOTONIC, &tend_BuildProgram);
/* Create task parallel OpenCL kernel */
kernel = clCreateKernel(program, "mpaKernel", &ret);
if (ret != CL_SUCCESS)
{
printf("Error: Failed to create kernel ! %s\n", getErrorString(ret));
exit(1);
}
printKernelWorkGroupInfo(kernel,device_id);
/* Set OpenCL kernel arguments */
ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&Amobj);
ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&Bmobj);
ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&Cmobj);
ret = clSetKernelArg(kernel, 3, sizeof(cl_mem), (void *)&Omobj);
ret = clSetKernelArg(kernel, 4, sizeof(cl_mem), (void *)&Pmobj);
if (ret != CL_SUCCESS)
{
printf("Error: Failed to set kernel arguments! %s\n", getErrorString(ret));
exit(1);
}
clock_gettime(CLOCK_MONOTONIC, &tend_createKernel);
/* Execute OpenCL kernel as task parallel */
ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, global, local, 0, NULL, NULL);
if (ret)
{
printf("Error: Failed to execute kernel %s!\n",getErrorString(ret));
return EXIT_FAILURE;
}
clFinish(command_queue);
clock_gettime(CLOCK_MONOTONIC, &tend_exec);
/* Copy result to host */
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING) ret = clEnqueueReadBuffer(command_queue, Cmobj, CL_TRUE, 0, 2*K*sizeof(unsigned short), C, 0, NULL, NULL);
else ret = clEnqueueReadBuffer(command_queue, Cmobj, CL_TRUE, 0, K*sizeof(unsigned short), C, 0, NULL, NULL);
printf("clEnqueueReadBuffer for Cmobj %s \n",getErrorString(ret));
clFinish(command_queue);
clock_gettime(CLOCK_MONOTONIC, &tend_redResults);
//Display result
/* for (i=0; i < 256; i++) {
printf("%u \n", C[K-i]);
}*/
printf("Entring Test for %s OPERATOR using K=%zu and WORDLENGTH=%d and BITSLENGTH=%d \n",decode(OPERATOR),K,WORDLENGTH ,BITSLENGTH);
if(testGpuResults(A,B,C,K,OPERATOR,DEBUG_MODE,PRIME,WORDLENGTH,bigPrime)==1)printf("%s executed %zu times Successefully \n",decode(OPERATOR),K, WORDLENGTH, bigPrime);
clock_gettime(CLOCK_MONOTONIC, &tend_test);
double Initialization=0,CREATECONTEXT=0, LOADToMEMORY=0,BuildProgram=0,CREATEKERNEL=0,EXECUTION=0,READRESULTS=0, CPUTIME=0,OPENCLOVRALLTime=0, SPEEDUP;
Initialization = ((double)tend_init.tv_sec + 1.0e-9*tend_init.tv_nsec) -
((double)tstart.tv_sec + 1.0e-9*tstart.tv_nsec);
CREATECONTEXT = ((double)tend_createContext.tv_sec + 1.0e-9*tend_createContext.tv_nsec) -
((double)tend_init.tv_sec + 1.0e-9*tend_init.tv_nsec);
LOADToMEMORY =((double)tend_loadTomemory.tv_sec + 1.0e-9*tend_loadTomemory.tv_nsec) -
((double)tend_createContext.tv_sec + 1.0e-9*tend_createContext.tv_nsec);
BuildProgram = ((double)tend_BuildProgram.tv_sec + 1.0e-9*tend_BuildProgram.tv_nsec) -
((double)tend_loadTomemory.tv_sec + 1.0e-9*tend_loadTomemory.tv_nsec);
CREATEKERNEL = ((double)tend_createKernel.tv_sec + 1.0e-9*tend_createKernel.tv_nsec) -
((double)tend_BuildProgram.tv_sec + 1.0e-9*tend_BuildProgram.tv_nsec),
EXECUTION = ((double)tend_exec.tv_sec + 1.0e-9*tend_exec.tv_nsec) -
((double)tend_createKernel.tv_sec + 1.0e-9*tend_createKernel.tv_nsec),
READRESULTS = ((double)tend_redResults.tv_sec + 1.0e-9*tend_redResults.tv_nsec) -
((double)tend_exec.tv_sec + 1.0e-9*tend_exec.tv_nsec),
CPUTIME =((double)tend_test.tv_sec + 1.0e-9*tend_test.tv_nsec) -
((double)tend_redResults.tv_sec + 1.0e-9*tend_redResults.tv_nsec);
OPENCLOVRALLTime= LOADToMEMORY+ EXECUTION + READRESULTS;
// dispaly timing in table
printf("----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
if(OPENCLOVRALLTime<CPUTIME){
SPEEDUP = (CPUTIME/OPENCLOVRALLTime)*100;
printf( "Initialization | CREATE CONTEXT | LOAD To MEMORY |Build Program src| CREATE KERNEL | EXECUTION | READ RESULTS " RED_TERMINAL "| CPUTIME " END_COLOR GREEN_TERMINAL "| OPENCLOVRALLTime | SPEEDUP |\n" END_COLOR);
}
else { printf( "Initialization | CREATE CONTEXT | LOAD To MEMORY |Build Program src| CREATE KERNEL | EXECUTION | READ RESULTS " GREEN_TERMINAL "| CPUTIME " END_COLOR RED_TERMINAL "| OPENCLOVRALLTime | CPU SPEEDUP |\n" END_COLOR);
SPEEDUP = (OPENCLOVRALLTime/CPUTIME)*100;
}
printf( " %.6f | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f | %.6f | %.2f %% |\n",
Initialization , CREATECONTEXT , LOADToMEMORY , BuildProgram , CREATEKERNEL , EXECUTION, READRESULTS , CPUTIME, OPENCLOVRALLTime, SPEEDUP );
printf("----------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
free(source_str);
free(A);
free(B);
free(C);
/* Finalization */
ret = clFlush(command_queue);
ret = clFinish(command_queue);
ret = clReleaseKernel(kernel);
ret = clReleaseProgram(program);
ret = clReleaseMemObject(Amobj);
ret = clReleaseMemObject(Bmobj);
ret = clReleaseMemObject(Cmobj);
ret = clReleaseMemObject(Omobj);
ret = clReleaseCommandQueue(command_queue);
printf("clReleaseContext %s \n",getErrorString(ret));
ret = clReleaseContext(context);
return 0;
}
int testGpuResults(unsigned short* input1, unsigned short* input2, unsigned short* outputBytes, size_t K, int OPERATOR, int DEBUG_MODE, unsigned short* PRIME, int WORDLENGTH , mpz_t bigPrime){
int result=1;
// declare bigA and bigB outside loop
mpz_t bigA;
mpz_t bigB;
//Define TWOPOW_WL
mpz_t TWOPOW_WL;
mpz_t RmoinsUn;
mpz_init(TWOPOW_WL);
mpz_init(RmoinsUn);
const unsigned short* TWOPOW_WLStr="10000000000000000000000000000000000000000000000000000000000000000";
mpz_set_str(TWOPOW_WL,TWOPOW_WLStr, 16);
//mpz_out_str(stdout,16,TWOPOW_WL);
switch(WORDLENGTH){
case 512 : mpz_mul(TWOPOW_WL,TWOPOW_WL,TWOPOW_WL);
case 256 : mpz_mul(TWOPOW_WL,TWOPOW_WL,TWOPOW_WL);
case 128 : mpz_mul(TWOPOW_WL,TWOPOW_WL,TWOPOW_WL);
case 64 : mpz_mul(TWOPOW_WL,TWOPOW_WL,TWOPOW_WL);
case 32 : mpz_mul(TWOPOW_WL,TWOPOW_WL,TWOPOW_WL);
}
unsigned short* GMPBytes;
// for tsets to be removed
/* mpz_sub_ui(TWOPOW_WL,TWOPOW_WL,1);
mpz_out_str(stdout,16,TWOPOW_WL);
printf("\n");
*/
// mpz_out_str(stdout,16,bigPrime);
// printf("\n");
size_t* count;
count = (size_t*) malloc(sizeof(size_t));
unsigned short aBytes[WORDLENGTH];
unsigned short bBytes[WORDLENGTH];
unsigned short resultBytes[WORDLENGTH*2];
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING)
GMPBytes=(unsigned short*)malloc(WORDLENGTH*2*sizeof(unsigned short));
else
GMPBytes=(unsigned short*)malloc(WORDLENGTH*sizeof(unsigned short));
for(size_t i=0;i<K/WORDLENGTH;i++){
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING) memcpy(resultBytes,&outputBytes[i*WORDLENGTH*2],sizeof(resultBytes) );
else memcpy(resultBytes,&outputBytes[i*WORDLENGTH],WORDLENGTH*sizeof(unsigned short) );
// copy the i th element
memcpy(aBytes,&input1[i*WORDLENGTH],sizeof(aBytes) );
mpz_init(bigA);
mpz_import(bigA, WORDLENGTH, 1, sizeof(aBytes[0]), 0, 0, aBytes);
// copy the i th element
memcpy(bBytes,&input2[i*WORDLENGTH],sizeof(bBytes) );
mpz_init(bigB);
mpz_import(bigB, WORDLENGTH, 1, sizeof(bBytes[0]), 0, 0, bBytes);
if(OPERATOR==ADD){
// bigA=bigB+bigA
mpz_add(bigA,bigB,bigA);
if(mpz_cmp(bigA,TWOPOW_WL)==1) mpz_sub(bigA,bigA,TWOPOW_WL);
}
if(OPERATOR==SUBTRACT){
// bigA=bigA-bigB%PRIME
mpz_sub(bigA,bigA,bigB);
}
if(OPERATOR==MONTGOMERYMULTIPLICATION){
// R.modInverse(bigPrime).multiply(bigA).multiply(bigB).mod(bigPrime);
// int mpz_invert (mpz_t rop, const mpz_t op1, const mpz_t op2)
mpz_invert(RmoinsUn,TWOPOW_WL,bigPrime);
mpz_mul(bigA,RmoinsUn,bigA);
mpz_mul(bigA,bigA,bigB);
mpz_mod(bigA,bigA,bigPrime);
}
if(OPERATOR==SUBTRACTMOD){
// bigA=bigA-bigB%PRIME
mpz_sub(bigA,bigA,bigB);
mpz_mod(bigA,bigA,bigPrime);
}
if(OPERATOR==ADDMOD){
// bigA=(bigA+bigB)%PRIME
mpz_add(bigA,bigA,bigB);
mpz_mod(bigA,bigA,bigPrime);
}
if(OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING){
// bigA=bigB+bigA
mpz_mul(bigA,bigB,bigA);
}
if((OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING))
mpz_export(GMPBytes, count, -1, 2*WORDLENGTH*sizeof(unsigned short), 0, 0, bigA);
else
mpz_export(GMPBytes, count, -1, WORDLENGTH*sizeof(unsigned short), 0, 0, bigA);
//mpz_export((void*)GMPBytes, count, 1, sizeof(unsigned short), 1, 0, bigB);
// the bellow block should be uncommented in debug mod
if((OPERATOR==MULTIPLYOPRANDSCANNING||OPERATOR==MULTIPLYPRODUCTSCANNING))
{ if(DEBUG_MODE!=0&&compareArray(resultBytes,GMPBytes,count[0],WORDLENGTH*2) != 0) {
printf("64 mult Error at index %zu\n", i);
result=0;
printf("aBytes = "); printArray(aBytes,WORDLENGTH,0);
printf("bBytes = "); printArray(bBytes,WORDLENGTH,0);
printf("resultBytes = "); printArray(resultBytes,WORDLENGTH*2,0);
printf("GMPBytes = "); printArray(GMPBytes,WORDLENGTH*2,0);
mpz_out_str(stdout,16,bigA);
printf ("\n");
break;
}
}
else if(DEBUG_MODE!=0&&compareArray(resultBytes,GMPBytes,count[0],WORDLENGTH) != 0) {
printf("Error at index %zu\n", i);
result=0;
printf("PRIME = "); printArray(PRIME,WORDLENGTH,0);
printf("aBytes = "); printArray(aBytes,WORDLENGTH,0);
printf("bBytes = "); printArray(bBytes,WORDLENGTH,0);
printf("resultBytes = "); printArray(resultBytes,WORDLENGTH,0);
printf("GMPBytes = "); printArray(GMPBytes,WORDLENGTH,0);
mpz_out_str(stdout,16,bigA);
printf ("\n");
break;
}
}
mpz_clear(bigA);
mpz_clear(bigB);
mpz_clear(TWOPOW_WL);
free(count);
free(GMPBytes);
return result;
}