-
Notifications
You must be signed in to change notification settings - Fork 4
/
StreamMR.cpp
5213 lines (4602 loc) · 164 KB
/
StreamMR.cpp
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************************************
* (c) Virginia Polytechnic Insitute and State University, 2011.
* This is the source code for StreamMR, a MapReduce framework on graphics processing units.
* Developer: Marwa K. Elteir (City of Scientific Researches and Technology Applications, Egypt)
*******************************************************************************************************/
/* ============================================================
Copyright (c) 2009 Advanced Micro Devices, Inc. All rights reserved.
Redistribution and use of this material is permitted under the following
conditions:
Redistributions must retain the above copyright notice and all terms of this
license.
In no event shall anyone redistributing or accessing or using this material
commence or participate in any arbitration or legal action relating to this
material against Advanced Micro Devices, Inc. or any copyright holders or
contributors. The foregoing shall survive any expiration or termination of
this license or any agreement or access or use related to this material.
ANY BREACH OF ANY TERM OF THIS LICENSE SHALL RESULT IN THE IMMEDIATE REVOCATION
OF ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE THIS MATERIAL.
THIS MATERIAL IS PROVIDED BY ADVANCED MICRO DEVICES, INC. AND ANY COPYRIGHT
HOLDERS AND CONTRIBUTORS "AS IS" IN ITS CURRENT CONDITION AND WITHOUT ANY
REPRESENTATIONS, GUARANTEE, OR WARRANTY OF ANY KIND OR IN ANY WAY RELATED TO
SUPPORT, INDEMNITY, ERROR FREE OR UNINTERRUPTED OPERA TION, OR THAT IT IS FREE
FROM DEFECTS OR VIRUSES. ALL OBLIGATIONS ARE HEREBY DISCLAIMED - WHETHER
EXPRESS, IMPLIED, OR STATUTORY - INCLUDING, BUT NOT LIMITED TO, ANY IMPLIED
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
ACCURACY, COMPLETENESS, OPERABILITY, QUALITY OF SERVICE, OR NON-INFRINGEMENT.
IN NO EVENT SHALL ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, PUNITIVE,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, REVENUE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED OR BASED ON ANY THEORY OF LIABILITY
ARISING IN ANY WAY RELATED TO THIS MATERIAL, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE. THE ENTIRE AND AGGREGATE LIABILITY OF ADVANCED MICRO DEVICES,
INC. AND ANY COPYRIGHT HOLDERS AND CONTRIBUTORS SHALL NOT EXCEED TEN DOLLARS
(US $10.00). ANYONE REDISTRIBUTING OR ACCESSING OR USING THIS MATERIAL ACCEPTS
THIS ALLOCATION OF RISK AND AGREES TO RELEASE ADVANCED MICRO DEVICES, INC. AND
ANY COPYRIGHT HOLDERS AND CONTRIBUTORS FROM ANY AND ALL LIABILITIES,
OBLIGATIONS, CLAIMS, OR DEMANDS IN EXCESS OF TEN DOLLARS (US $10.00). THE
FOREGOING ARE ESSENTIAL TERMS OF THIS LICENSE AND, IF ANY OF THESE TERMS ARE
CONSTRUED AS UNENFORCEABLE, FAIL IN ESSENTIAL PURPOSE, OR BECOME VOID OR
DETRIMENTAL TO ADVANCED MICRO DEVICES, INC. OR ANY COPYRIGHT HOLDERS OR
CONTRIBUTORS FOR ANY REASON, THEN ALL RIGHTS TO REDISTRIBUTE, ACCESS OR USE
THIS MATERIAL SHALL TERMINATE IMMEDIATELY. MOREOVER, THE FOREGOING SHALL
SURVIVE ANY EXPIRATION OR TERMINATION OF THIS LICENSE OR ANY AGREEMENT OR
ACCESS OR USE RELATED TO THIS MATERIAL.
NOTICE IS HEREBY PROVIDED, AND BY REDISTRIBUTING OR ACCESSING OR USING THIS
MATERIAL SUCH NOTICE IS ACKNOWLEDGED, THAT THIS MATERIAL MAY BE SUBJECT TO
RESTRICTIONS UNDER THE LAWS AND REGULATIONS OF THE UNITED STATES OR OTHER
COUNTRIES, WHICH INCLUDE BUT ARE NOT LIMITED TO, U.S. EXPORT CONTROL LAWS SUCH
AS THE EXPORT ADMINISTRATION REGULATIONS AND NATIONAL SECURITY CONTROLS AS
DEFINED THEREUNDER, AS WELL AS STATE DEPARTMENT CONTROLS UNDER THE U.S.
MUNITIONS LIST. THIS MATERIAL MAY NOT BE USED, RELEASED, TRANSFERRED, IMPORTED,
EXPORTED AND/OR RE-EXPORTED IN ANY MANNER PROHIBITED UNDER ANY APPLICABLE LAWS,
INCLUDING U.S. EXPORT CONTROL LAWS REGARDING SPECIFICALLY DESIGNATED PERSONS,
COUNTRIES AND NATIONALS OF COUNTRIES SUBJECT TO NATIONAL SECURITY CONTROLS.
MOREOVER, THE FOREGOING SHALL SURVIVE ANY EXPIRATION OR TERMINATION OF ANY
LICENSE OR AGREEMENT OR ACCESS OR USE RELATED TO THIS MATERIAL.
NOTICE REGARDING THE U.S. GOVERNMENT AND DOD AGENCIES: This material is
provided with "RESTRICTED RIGHTS" and/or "LIMITED RIGHTS" as applicable to
computer software and technical data, respectively. Use, duplication,
distribution or disclosure by the U.S. Government and/or DOD agencies is
subject to the full extent of restrictions in all applicable regulations,
including those found at FAR52.227 and DFARS252.227 et seq. and any successor
regulations thereof. Use of this material by the U.S. Government and/or DOD
agencies is acknowledgment of the proprietary rights of any copyright holders
and contributors, including those of Advanced Micro Devices, Inc., as well as
the provisions of FAR52.227-14 through 23 regarding privately developed and/or
commercial computer software.
This license forms the entire agreement regarding the subject matter hereof and
supersedes all proposals and prior discussions and writings between the parties
with respect thereto. This license does not affect any ownership, rights, title,
or interest in, or relating to, this material. No terms of this license can be
modified or waived, and no breach of this license can be excused, unless done
so in a writing signed by all affected parties. Each term of this license is
separately enforceable. If any term of this license is determined to be or
becomes unenforceable or illegal, such term shall be reformed to the minimum
extent necessary in order for this license to remain in effect in accordance
with its terms as modified by such reformation. This license shall be governed
by and construed in accordance with the laws of the State of Texas without
regard to rules on conflicts of law of any state or jurisdiction or the United
Nations Convention on the International Sale of Goods. All disputes arising out
of this license shall be subject to the jurisdiction of the federal and state
courts in Austin, Tetas, and all defenses are hereby waived concerning personal
jurisdiction and venue of these courts.
============================================================ */
#include "StreamMR.hpp"
#include <malloc.h>
#include <ctime>
#include <sys/time.h>
#include <errno.h>
#include "timeRec.h"
#include "rdtsc.h"
#include "scan.h"
int quiet = 1;
#define CEIL(n,m) (n/m + (int)(n%m !=0))
int MapReduce::setupCL()
{
cl_int status = CL_SUCCESS;
cl_uint deviceListSize = 1;
/* Now allocate memory for device list based on the size we got earlier */
devices = (cl_device_id*)malloc(deviceListSize*sizeof(cl_device_id));
if(devices == NULL)
{
error("Failed to allocate memory (devices).");
return SDK_FAILURE;
}
devices[0] = GetDevice(0, 0);
context = clCreateContext(0,
1,
devices,
NULL,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateContextFromType failed."))
{
return SDK_FAILURE;
}
/* Create command queue */
commandQueue = clCreateCommandQueue(context,
devices[0],
CL_QUEUE_PROFILING_ENABLE,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateCommandQueue failed."))
{
return SDK_FAILURE;
}
/* Get Device specific Information */
status = clGetDeviceInfo(devices[0],
CL_DEVICE_MAX_WORK_GROUP_SIZE,
sizeof(size_t),
(void*)&maxWorkGroupSize,
NULL);
if(!checkVal(status,
CL_SUCCESS,
"clGetDeviceInfo"
"CL_DEVICE_MAX_WORK_GROUP_SIZE failed."))
{
return SDK_FAILURE;
}
status = clGetDeviceInfo(devices[0],
CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS,
sizeof(cl_uint),
(void*)&maxDimensions,
NULL);
if(!checkVal(status,
CL_SUCCESS,
"clGetDeviceInfo"
"CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS failed."))
{
return SDK_FAILURE;
}
maxWorkItemSizes = (size_t*)malloc(maxDimensions * sizeof(size_t));
status = clGetDeviceInfo(devices[0],
CL_DEVICE_MAX_WORK_ITEM_SIZES,
sizeof(size_t) * maxDimensions,
(void*)maxWorkItemSizes,
NULL);
if(!checkVal(status,
CL_SUCCESS,
"clGetDeviceInfo"
"CL_DEVICE_MAX_WORK_ITEM_SIZES failed."))
{
return SDK_FAILURE;
}
status = clGetDeviceInfo(devices[0],
CL_DEVICE_LOCAL_MEM_SIZE,
sizeof(cl_ulong),
(void*)&totalLocalMemory,
NULL);
if(!checkVal(status,
CL_SUCCESS,
"clGetDeviceInfo"
"CL_DEVICE_LOCAL_MEM_SIZE failed."))
{
return SDK_FAILURE;
}
/* create a CL program using the kernel source */
FILE *kernelFile;
char *kernelSource;
size_t kernelLength;
std::string kernelPath = getPath();
if (jobSpec->workflow == MAP_ONLY)
{
kernelPath.append(kernelfilename.c_str());
}
else //MAP_REDUCE
{
kernelPath.append(kernelfilename.c_str());
}
kernelFile = fopen(kernelPath.c_str(), "r");
fseek(kernelFile, 0, SEEK_END);
if (!kernelFile)
{
std::cerr << "ERROR: " << __FILE__ << ":" << __LINE__ << " Failed to open kernel file " << kernelPath << std::endl;
std::cerr << "\tError reason: " << strerror(errno) << std::endl;
exit(-1);
}
kernelLength = (size_t) ftell(kernelFile);
kernelSource = (char *) malloc(sizeof(char)*kernelLength);
rewind(kernelFile);
if (!fread((void *) kernelSource, kernelLength, 1, kernelFile))
{
std::cerr << "ERROR: " << __FILE__ << ":" << __LINE__ << " Failed to read from kernel file: " << kernelPath << std::endl;
std::cerr << "\tError reason: " << strerror(errno) << std::endl;
exit(-1);
}
fclose(kernelFile);
// if(!kernelFile.open(kernelPath.c_str()))
// {
// std::cout << "Failed to load kernel file : " << kernelPath << std::endl;
// return SDK_FAILURE;
// }
// printf("Successfully Load the Kernel File \n");
//
program = clCreateProgramWithSource(context,
1,
((const char **)(&kernelSource)),
(const size_t*) &kernelLength,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateProgramWithSource failed."))
{
return SDK_FAILURE;
}
/* create a cl program executable for all the devices specified */
char *options = new char[100];
strcpy( options, " ");
if (jobSpec->perfectHashing)
{
strcat( options, "-D PERFECT ");
}
if (jobSpec->outputIntermediate)
{
strcat( options, "-D OUTPUTINTER ");
}
if (jobSpec->overflow == true)
{
strcat( options, "-D OVERFLOW ");
}
status = clBuildProgram(program,
1,
devices,
options,
NULL,
NULL);
if(status != CL_SUCCESS)
{
if(status == CL_BUILD_PROGRAM_FAILURE)
{
cl_int logStatus;
char * buildLog = NULL;
size_t buildLogSize = 0;
logStatus = clGetProgramBuildInfo(program,
devices[0],
CL_PROGRAM_BUILD_LOG,
buildLogSize,
buildLog,
&buildLogSize);
if(!checkVal(logStatus,
CL_SUCCESS,
"clGetProgramBuildInfo failed."))
{
return SDK_FAILURE;
}
buildLog = (char*)malloc(buildLogSize);
if(buildLog == NULL)
{
error("Failed to allocate host memory.(buildLog)");
return SDK_FAILURE;
}
memset(buildLog, 0, buildLogSize);
logStatus = clGetProgramBuildInfo(program,
devices[0],
CL_PROGRAM_BUILD_LOG,
buildLogSize,
buildLog,
NULL);
if(!checkVal(logStatus,
CL_SUCCESS,
"clGetProgramBuildInfo failed."))
{
free(buildLog);
return SDK_FAILURE;
}
std::cout << " \n\t\t\tBUILD LOG\n";
std::cout << " ************************************************\n";
std::cout << buildLog << std::endl;
std::cout << " ************************************************\n";
free(buildLog);
}
if(!checkVal(status,
CL_SUCCESS,
"clBuildProgram failed."))
{
return SDK_FAILURE;
}
}
printf("Successully build the kernel \n");
/* get a kernel object handle for a kernel with the given name */
mapperExtendedKernel = clCreateKernel(program,
"mapperExtended",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed1."))
{
return SDK_FAILURE;
}
mapperKernel = clCreateKernel(program,
"mapper",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed2."))
{
return SDK_FAILURE;
}
reducerKernel = clCreateKernel(program,
"reducer",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed3."))
{
return SDK_FAILURE;
}
reducerInOverflowKernel = clCreateKernel(program,
"reducerInOverflow",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed4."))
{
return SDK_FAILURE;
}
copyerKernel = clCreateKernel(program,
"copyerHashToArray",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed7."))
{
return SDK_FAILURE;
}
copyerInOverflowKernel = clCreateKernel(program,
"copyerHashToArrayInOverflow",
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateKernel failed8."))
{
return SDK_FAILURE;
}
/* Check whether specified groupSize is plausible on current kernel */
status = clGetKernelWorkGroupInfo(mapperExtendedKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
&mapperExtendedWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
status = clGetKernelWorkGroupInfo(mapperKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
&mapperWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
status = clGetKernelWorkGroupInfo(reducerKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
&reducerWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
status = clGetKernelWorkGroupInfo(reducerInOverflowKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
&reducerInOverflowWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
status = clGetKernelWorkGroupInfo(copyerKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
©erWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
status = clGetKernelWorkGroupInfo(copyerInOverflowKernel,
devices[0],
CL_KERNEL_WORK_GROUP_SIZE,
sizeof(size_t),
©erInOverflowWorkGroupSize,
0);
if(!checkVal(status,
CL_SUCCESS,
"clGetKernelWorkGroupInfo failed."))
{
return SDK_FAILURE;
}
/* If groupSize exceeds the maximum supported on kernel
* fall back */
printf("Successfully query the workgroup size of each kernel\n");
size_t temp;
if (kernelWorkGroupSize > mapperWorkGroupSize)
temp = mapperWorkGroupSize;
else
temp = kernelWorkGroupSize;
if (temp > reducerWorkGroupSize) temp = reducerWorkGroupSize;
if (temp > mapperExtendedWorkGroupSize) temp = mapperExtendedWorkGroupSize;
if (temp > reducerInOverflowWorkGroupSize) temp = reducerInOverflowWorkGroupSize;
if (temp > copyerWorkGroupSize) temp = copyerWorkGroupSize;
if (temp > copyerInOverflowWorkGroupSize) temp = copyerInOverflowWorkGroupSize;
if(groupSize > temp)
{
if(!quiet)
{
std::cout << "Out of Resources!" << std::endl;
std::cout << "Group Size specified : " << groupSize << std::endl;
std::cout << "Max Group Size supported on the kernel : "
<< temp << std::endl;
std::cout << "Falling back to " << temp << std::endl;
}
groupSize = temp;
}
return SDK_SUCCESS;
}
int MapReduce::runCLKernels()
{
return SDK_SUCCESS;
}
int MapReduce::initialize(JobSpecification* jobSpecification)
{
//initialize the job Specification
jobSpec = jobSpecification;
// Call base class Initialize to get default configuration
if(initialize())
return SDK_FAILURE;
return SDK_SUCCESS;
}
int MapReduce::setup()
{
//int timer = createTimer();
//resetTimer(timer);
//startTimer(timer);
if(setupCL() != SDK_SUCCESS)
{
return SDK_FAILURE;
}
//stopTimer(timer);
/* Compute setup time */
//setupTime = (double)(readTimer(timer));
return SDK_SUCCESS;
}
int MapReduce::cleanup()
{
/* Releases OpenCL resources (Context, Memory etc.) */
cl_int status;
status = clReleaseKernel(mapperKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseKernel(mapperExtendedKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseKernel(reducerKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseKernel(copyerInOverflowKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseKernel(copyerKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseKernel(reducerInOverflowKernel);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseKernel failed."))
{
return SDK_FAILURE;
}
status = clReleaseProgram(program);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseProgram failed."))
{
return SDK_FAILURE;
}
status = clReleaseCommandQueue(commandQueue);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseCommandQueue failed."))
{
return SDK_FAILURE;
}
status = clReleaseContext(context);
if(!checkVal(status,
CL_SUCCESS,
"clReleaseContext failed."))
{
return SDK_FAILURE;
}
return SDK_SUCCESS;
}
MapReduce::~MapReduce()
{
if (devices)
{
free(devices);
devices = NULL;
}
if(maxWorkItemSizes)
{
free(maxWorkItemSizes);
maxWorkItemSizes = NULL;
}
}
//--------------------------------------------------------------------------------------
//Start Map phase for applications with Map and Reduce phase like KMeans and Wordcount
//--------------------------------------------------------------------------------------
int MapReduce::startMapType2()
{
timerStart();
cl_int status;
if (!jobSpec->Validate()) return -1;
//1- Get map input data on host
//------------------------------------------------------------------
printf("\n(1)- Get map input data on host:\n");
printf("----------------------------------\n");
int h_inputRecordCount = jobSpec->inputRecordCount;
int h_inputKeysBufSize = jobSpec->inputKeysBufSize;
int h_inputValsBufSize = jobSpec->inputValsBufSize;
cl_char* h_inputKeys = jobSpec->inputKeys;
cl_char* h_inputVals = jobSpec->inputVals;
cl_uint4* h_inputOffsetSizes = jobSpec->inputOffsetSizes;
printf(" Map Input: keys size: %i bytes, values size: %i bytes, records: %i \n",h_inputKeysBufSize,h_inputValsBufSize,h_inputRecordCount);
//2- Upload map input data to device memory
//------------------------------------------------------------------
printf("\n(2)- Upload map input data to device memory:\n");
printf("----------------------------------------------\n");
cl_mem d_inputRecordsMeta = NULL;
cl_mem d_inputKeys = NULL;
cl_mem d_inputVals = NULL;
cl_mem d_inputOffsetSizes = NULL;
d_inputKeys = clCreateBuffer(context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
h_inputKeysBufSize,
h_inputKeys,
&status);
if(!checkVal(
status,
CL_SUCCESS,
"clCreateBuffer failed. (d_inputKeys)"))
return SDK_FAILURE;
d_inputVals = clCreateBuffer(context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
h_inputValsBufSize,
h_inputVals,
&status);
if(!checkVal(
status,
CL_SUCCESS,
"clCreateBuffer failed. (d_inputVals)"))
return SDK_FAILURE;
d_inputOffsetSizes = clCreateBuffer(context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof(cl_int4)*h_inputRecordCount,
h_inputOffsetSizes,
&status);
if(!checkVal(
status,
CL_SUCCESS,
"clCreateBuffer failed. (d_inputOffsetSizes)"))
return SDK_FAILURE;
//3- Determine the block size
//------------------------------------------------------------------
printf("\n(3)- Determine the block size:\n");
printf("--------------------------------\n");
int h_recordsPerTask = jobSpec->numRecTaskMap;
int h_actualNumThreads=CEIL(h_inputRecordCount, h_recordsPerTask);
size_t globalThreads[1]= {h_actualNumThreads};
groupSize=jobSpec->userSize;
size_t localThreads[1] = {groupSize};
numGroups = h_actualNumThreads/groupSize;
int wavefrontSize= isAMD? 64 : 32;
printf("workgroup Size: %zu wavefrontSize: %d\n", groupSize, wavefrontSize);
numWavefrontsPerGroup = groupSize/wavefrontSize;
int localValuesSize = jobSpec->estimatedValSize * groupSize;
int localKeysSize = jobSpec->estimatedKeySize * groupSize;
printf("localKeysSize: %d localValuesSize%d\n",localKeysSize,localValuesSize);
numHashTables=numWavefrontsPerGroup*numGroups;
// 6- Allocate intermediate memory on device memory
//-----------------------------------------------
printf("\n(6)- Allocate intermediate memory on device memory:\n");
printf("---------------------------------------------------------------------\n");
d_gOutputKeySize = NULL;
d_gOutputValSize = NULL;
d_gHashBucketSize = NULL;
cl_int *h_interAllKeys,*h_interAllVals, * h_outputAllVals, * h_outputAllKeys,
* h_hashTableAllVals , * h_hashBucketAllVals;
h_estimatedInterValSize = (jobSpec->overflow)? jobSpec->estimatedInterRecords * jobSpec->estimatedInterValSize * 30: jobSpec->estimatedInterRecords * jobSpec->estimatedInterValSize ;
h_estimatedInterKeySize = (jobSpec->overflow)? jobSpec->estimatedInterRecords * jobSpec->estimatedInterKeySize * 4: jobSpec->estimatedInterRecords * jobSpec->estimatedInterKeySize;
printf("All Allocated Inter Keys Buffers: %i",h_estimatedInterKeySize);
printf("All Allocated Inter vals Buffers: %i",h_estimatedInterValSize);
d_interKeys = clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_estimatedInterKeySize,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_interKeys1)"))
return SDK_FAILURE;
d_interVals = clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_estimatedInterValSize,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_interVals)"))
return SDK_FAILURE;
d_interOffsets = clCreateBuffer(context,
CL_MEM_READ_WRITE,
sizeof(cl_int4)* jobSpec->estimatedInterRecords,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_interOffsets)"))
return SDK_FAILURE;
h_estimatedOutputValSize= (jobSpec->overflow)? jobSpec->estimatedRecords * jobSpec->estimatedValSize * numGroups* numWavefrontsPerGroup* 30 : jobSpec->estimatedRecords * jobSpec->estimatedValSize* numGroups;
h_estimatedOutputKeySize= (jobSpec->overflow)? jobSpec->estimatedRecords * jobSpec->estimatedKeySize * numGroups* numWavefrontsPerGroup * 4 : jobSpec->estimatedRecords * jobSpec->estimatedKeySize * numGroups;
printf("All Allocated Keys Buffers: %i\n", h_estimatedOutputKeySize);
printf("All Allocated vals Buffers: %i\n",h_estimatedOutputValSize);
printf("records: %i, valSize: %i, keySize: %i, numGroups: %i\n",jobSpec->estimatedRecords, jobSpec->estimatedValSize, jobSpec->estimatedKeySize, numGroups);
d_outputKeys = clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_estimatedOutputKeySize,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_outputKeys1)"))
return SDK_FAILURE;
d_outputVals = clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_estimatedOutputValSize,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_outputVals1)"))
return SDK_FAILURE;
uint hashEntriesNum = jobSpec->estimatedRecords ;
totalHashSize = hashEntriesNum * numWavefrontsPerGroup * numGroups;
cl_uint4* h_initial= (cl_uint4 *)malloc(sizeof(cl_uint4) * totalHashSize);
printf("Hash Entries Num: %i numWavefrontsPerGroup: %d numGroups:%d Hash Table Size: %i\n",hashEntriesNum, numWavefrontsPerGroup, numGroups, totalHashSize);
for (int i=0; i<totalHashSize ; i++)
{
h_initial[i]=(cl_uint4){0,0,0,0};
}
d_hashTable = clCreateBuffer(context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof(cl_uint4) * totalHashSize ,
h_initial,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_hashTable)"))
return SDK_FAILURE;
//Create another data structure to hold the linked list of each hash bucket
totalHashBucketSize = (jobSpec->overflow)? hashEntriesNum* numWavefrontsPerGroup * numGroups * 20 :hashEntriesNum* numGroups ; //assuming 20 collisions per hash entry
d_hashBucket = clCreateBuffer(context,
CL_MEM_READ_WRITE,
sizeof(cl_int4) * totalHashBucketSize ,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_hashBucket)"))
return SDK_FAILURE;
//Create number of offsets equal to the number of workgroups
//for each one to work independently from the other
h_workgroupOutputValsizes = h_estimatedOutputValSize/numGroups;
h_workgroupOutputKeysizes = h_estimatedOutputKeySize/numGroups;
h_workgrouphashSizes = totalHashSize / (numWavefrontsPerGroup * numGroups) ;
h_workgrouphashBucketSizes = totalHashBucketSize / ( numGroups) ;
printf("Number of WorkGroups: %i\n",numGroups);
printf("OutputVals Sizes per workgroup: %i\n",h_workgroupOutputValsizes);
d_gOutputKeySize = clCreateBuffer(context,
CL_MEM_READ_WRITE,
numGroups * sizeof(cl_uint) * 3,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_gOutputKeySize) "))
return SDK_FAILURE;
d_gOutputValSize = clCreateBuffer(context,
CL_MEM_READ_WRITE,
numGroups * sizeof(cl_uint) * 3,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_gOutputValSize) "))
return SDK_FAILURE;
d_gHashBucketSize = clCreateBuffer(context,
CL_MEM_READ_WRITE,
numGroups * sizeof(cl_uint) * 3,
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_ghashBucketSize) "))
return SDK_FAILURE;
cl_uint * overflowWGId; // Ids of overflowWG Used to force only overflowed workgroups to continue their work
overflowWGId = (cl_uint *) malloc(sizeof(cl_uint) * numGroups);
cl_mem d_metaEmitted = clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_inputRecordCount*sizeof(cl_int),
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_metaEmitted)"))
return SDK_FAILURE;
cl_mem d_metaOverflow= clCreateBuffer(context,
CL_MEM_READ_WRITE,
h_inputRecordCount*sizeof(cl_int),
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_metaOverflow)"))
return SDK_FAILURE;
cl_mem d_metaEmitted2= clCreateBuffer(context, //From second Map kernel
CL_MEM_READ_WRITE,
h_inputRecordCount*sizeof(cl_int),
NULL,
&status);
if(!checkVal(status,
CL_SUCCESS,
"clCreateBuffer failed. (d_metaOverflow)"))
return SDK_FAILURE;
//Start Map
//---------------------------------------------------------------------
printf("\n(7)- Start Map:\n");
printf("-----------------\n");
cl_uint *h_gKeySizes,*h_gValSizes,*h_gCounts;
cl_event events[3];
//Set Kernel Arguments
status = clSetKernelArg(
mapperKernel,
0,
sizeof(cl_mem),
(void*)&(d_inputDataSet));
if(!checkVal(
status,
CL_SUCCESS,
"clSetKernelArg failed. (inputDataSet)"))
return SDK_FAILURE;
status = clSetKernelArg(
mapperKernel,
1,
sizeof(cl_mem),
(void*)&(d_constantData));
if(!checkVal(
status,
CL_SUCCESS,
"clSetKernelArg failed. (constantDataSet)"))
return SDK_FAILURE;
status = clSetKernelArg(
mapperKernel,
2,
sizeof(cl_mem),
&d_inputKeys);
if(!checkVal(
status,
CL_SUCCESS,
"clSetKernelArg failed. (d_inputKeys)"))
return SDK_FAILURE;
status = clSetKernelArg(
mapperKernel,
3,
sizeof(cl_mem),
&d_inputVals);
if(!checkVal(
status,
CL_SUCCESS,
"clSetKernelArg failed. (d_inputVals)"))
return SDK_FAILURE;
status = clSetKernelArg(
mapperKernel,
4,
sizeof(cl_mem),
&d_inputOffsetSizes);
if(!checkVal(
status,
CL_SUCCESS,
"clSetKernelArg failed. (d_inputOffsetSizes)"))
return SDK_FAILURE;
status = clSetKernelArg(
mapperKernel,
5,
sizeof(cl_mem),