/
main.cpp
363 lines (317 loc) · 15.2 KB
/
main.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
/*
* main.cpp
*
* Created on: 12 de November de 2013
* Author: Fernando Alexandre
*
* This program is an example of a multi-GPU computation (within a single system),
* that applies a Saxpy matrix operation decomposed evenly among the GPUs.
* Additionally it is possible to further decompose the data-sets so each GPU
* receives multiple, parallel executions (overlapping partitions).
*
*
*
* The MIT License (MIT)
*
* Copyright (c) 2014 Fernando Alexandre
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#if defined (__APPLE__) || defined(MACOSX)
#include <OpenCL/cl.h>
#else
#include <CL/cl.h>
#endif
// Default number of overlapping partitions within each GPU
#define DEFAULT_OVERLAP 2
// The alpha value used by Saxpy
#define ALPHA_SCALAR 5.0
// Location of the OpenCL computation
const std::string kernelFile = "saxpy.cl";
// Input/Output data-sets
float *inValues1, *inValues2, *outValues;
// Input/Output memory locations on the GPUs
// GPU Index -> Overlap Index -> cl_mem pointer
cl_mem ** input1, ** input2, ** output;
cl_context ** contexts;
// OpenCL Queues used to trigger data-transfers and computations
// GPU Index -> Overlap Index -> queue pointer
cl_command_queue ** commandQueues;
cl_program ** saxpyProgram;
cl_kernel ** saxpyKernel;
// Number of overlapping partitions within each GPU
unsigned int numOverlap;
// Number of elements in the matrix
unsigned int numberElems;
// Number of devices to be used
unsigned int numDevices;
// Work size used by OpenCL for each OpenCL execution
// (currently all have the same size because of even partitioning)
size_t globalWorkSize[1];
const char* errorString(int error);
// This auxiliary function has been adapted from work done by Ricardo Marques
// in the context of his thesis.
cl_program programFromSource(const std::string &fileName, const cl_context context){
std::ifstream kernelFile(fileName.data(), std::ios::in);
if (!kernelFile.is_open()){
kernelFile.close();
throw;
}
int errcode;
cl_program program;
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1, (const char**)&srcStr, NULL, &errcode);
if(errcode != CL_SUCCESS){
std::cerr << errorString(errcode) << std::endl;
return NULL;
}
return program;
}
// Initialization function for the OpenCL platform and all OpenCL resources
// required by this program.
void initOpenCL() {
int errcode = 0;
unsigned int i, j;
cl_platform_id platformId;
numDevices = 0;
cl_device_id deviceIds[8];
// Initialize the OpenCL platform itself
clGetPlatformIDs(1, &platformId, NULL);
clGetDeviceIDs(platformId, CL_DEVICE_TYPE_GPU, 8, &deviceIds[0], &numDevices);
contexts = (cl_context **) calloc(sizeof(cl_context *), numDevices);
commandQueues = (cl_command_queue **) calloc(sizeof(cl_command_queue*), numDevices);
saxpyProgram = (cl_program **) calloc(sizeof(cl_program *), numDevices);
globalWorkSize[0] = numberElems / (numDevices * numOverlap);
input1 = (cl_mem **) calloc(sizeof(cl_mem *), numDevices);
input2 = (cl_mem **) calloc(sizeof(cl_mem *), numDevices);
output = (cl_mem **) calloc(sizeof(cl_mem *), numDevices);
saxpyKernel = (cl_kernel **) calloc(sizeof(cl_kernel *), numDevices);
// Create the resources for each GPU
for(i = 0; i < numDevices; i++) {
contexts[i] = (cl_context *) calloc(sizeof(cl_context), numOverlap);
commandQueues[i] = (cl_command_queue *) calloc(sizeof(cl_command_queue), numOverlap);
saxpyProgram[i] = (cl_program *) calloc(sizeof(cl_program), numOverlap);
input1[i] = (cl_mem *) calloc(sizeof(cl_mem), numOverlap);
input2[i] = (cl_mem *) calloc(sizeof(cl_mem), numOverlap);
output[i] = (cl_mem *) calloc(sizeof(cl_mem), numOverlap);
saxpyKernel[i] = (cl_kernel *) calloc(sizeof(cl_kernel), numDevices);
// Create resources within each GPU
for(j = 0; j < numOverlap; j++) {
contexts[i][j] = clCreateContext(0, 1, &deviceIds[i], NULL, NULL, &errcode);
// Create a command queue so that the host can issue orders to the device
commandQueues[i][j] = clCreateCommandQueue(contexts[i][j], deviceIds[i], 0, &errcode);
// Create buffer objects for X matrix
input1[i][j] = clCreateBuffer(contexts[i][j], CL_MEM_READ_WRITE, sizeof(cl_float) * globalWorkSize[0], NULL, &errcode);
if(errcode != 0) {
std::cerr << "CreateBuffer (X): " << errorString(errcode) << std::endl;
}
// Create buffer objects for Y Matrix
input2[i][j] = clCreateBuffer(contexts[i][j], CL_MEM_READ_WRITE, sizeof(cl_float) * globalWorkSize[0], NULL, &errcode);
if(errcode != 0) {
std::cerr << "CreateBuffer (Y): " << errorString(errcode) << std::endl;
}
// Create buffer objects for the output
output[i][j] = clCreateBuffer(contexts[i][j], CL_MEM_READ_WRITE, sizeof(cl_float) * globalWorkSize[0], NULL, &errcode);
if(errcode != 0) {
std::cerr << "CreateBuffer (output): " << errorString(errcode) << std::endl;
}
// Load and build OpenCL kernels
saxpyProgram[i][j] = programFromSource(kernelFile, contexts[i][j]);
errcode = clBuildProgram(saxpyProgram[i][j], 0, NULL, NULL, NULL, NULL);
if(errcode != 0) {
char buildlog[16000];
clGetProgramBuildInfo(saxpyProgram[i][j], deviceIds[i], CL_PROGRAM_BUILD_LOG, sizeof(buildlog), buildlog, NULL);
std::cerr << "Error in clBuildProgram " << buildlog << std::endl;
exit(1);
}
// Create the kernel object
saxpyKernel[i][j] = clCreateKernel(saxpyProgram[i][j], "saxpy", &errcode);
if(errcode != 0) {
std::cerr << "Error creating kernel: " << errorString(errcode) << std::endl;
}
}
}
}
// Cleans up all the OpenCL resources.
void finishOpenCL() {
for(unsigned int i = 0; i < numDevices; i++) {
for(unsigned int j = 0; j < numOverlap; j++) {
clReleaseMemObject(input1[i][j]);
clReleaseMemObject(input2[i][j]);
clReleaseMemObject(output[i][j]);
clReleaseKernel(saxpyKernel[i][j]);
clReleaseCommandQueue(commandQueues[i][j]);
clReleaseContext(contexts[i][j]);
clReleaseProgram(saxpyProgram[i][j]);
}
}
}
// Launch a computation in a devices, overlap partition index and number of this partition
void run(unsigned int device, unsigned int overlap, unsigned int numPartition) {
int errcode = 0;
float alpha = ALPHA_SCALAR;
unsigned int start = numPartition * (sizeof(cl_float) * globalWorkSize[0]);
// Write input buffers to device memory
errcode = clEnqueueWriteBuffer(commandQueues[device][overlap], input1[device][overlap], CL_FALSE, 0, sizeof(cl_float) * globalWorkSize[0], inValues1 + start, 0, NULL, NULL);
if(errcode != 0) {
std::cerr << "WriteBuffer (X): " << errorString(errcode) << std::endl;
}
errcode = clEnqueueWriteBuffer(commandQueues[device][overlap], input2[device][overlap], CL_FALSE, 0, sizeof(cl_float) * globalWorkSize[0], inValues2 + start, 0, NULL, NULL);
if(errcode != 0) {
std::cerr << "WriteBuffer (Y): " << errorString(errcode) << std::endl;
}
// Set the Saxpy kernel arguments
errcode = clSetKernelArg(saxpyKernel[device][overlap], 0, sizeof(cl_mem), (void*) &input1[device][overlap]);
if(errcode != 0) {
std::cerr << "SetArg0 (X): " << errorString(errcode) << std::endl;
}
errcode = clSetKernelArg(saxpyKernel[device][overlap], 1, sizeof(cl_mem), (void*) &input2[device][overlap]);
if(errcode != 0) {
std::cerr << "SetArg1 (Y): " << errorString(errcode) << std::endl;
}
errcode = clSetKernelArg(saxpyKernel[device][overlap], 2, sizeof(float), (void*) &alpha);
if(errcode != 0) {
std::cerr << "SetArg2 (alpha): " << errorString(errcode) << std::endl;
}
errcode = clSetKernelArg(saxpyKernel[device][overlap], 3, sizeof(cl_mem), (void*) &output[device][overlap]);
if(errcode != 0) {
std::cerr << "SetArg3 (output): " << errorString(errcode) << std::endl;
}
// Queue the Saxpy kernel for execution
errcode = clEnqueueNDRangeKernel(commandQueues[device][overlap], saxpyKernel[device][overlap], 1, NULL, globalWorkSize, NULL, 0, NULL, NULL);
if(errcode != 0) {
std::cerr << "NDRange: " << errorString(errcode) << std::endl;
}
errcode = clEnqueueReadBuffer(commandQueues[device][overlap], output[device][overlap], CL_FALSE, 0, sizeof(cl_float) * globalWorkSize[0], outValues + start, 0, NULL, NULL);
if(errcode != 0) {
std::cerr << "ReadBuffer: " << errorString(errcode) << std::endl;
}
}
void startExecution() {
unsigned int i, j, acc = 0;
// Launch all computations (they are non-blocking)
// Ideally these should be threaded.
for(i = 0; i < numDevices; i++) {
for(j = 0; j < numOverlap; j++) {
run(i, j, acc);
acc++;
}
}
// Wait for all computations to finish
for(i = 0; i < numDevices; i++) {
for(j = 0; j < numOverlap; j++) {
clFinish(commandQueues[i][j]);
}
}
}
int main(int argc, char const *argv[])
{
unsigned int i;
if(argc < 2) {
std::cout << "Usage: " << argv[0] << "<numberElements> <Optional_numberOverlapPerGPU>" << std::endl;
return -1;
} else {
// Set the values in regards to the supplied arguments
if(argc >= 2) {
numberElems = atoi(argv[1]);
if(argc == 3) {
numOverlap = atoi(argv[2]);
} else {
numOverlap = DEFAULT_OVERLAP;
}
}
}
inValues1 = new float[numberElems];
inValues2 = new float[numberElems];
outValues = new float[numberElems];
// Initialize the example matrices
for(i = 0; i < numberElems; i++) {
inValues1[i] = 10;
inValues2[i] = 15;
}
initOpenCL();
startExecution();
finishOpenCL();
free(inValues1);
free(inValues2);
free(outValues);
return 0;
}
// Function adapted from the work of Ricardo Marques in the context
// of his thesis.
// Translates an OpenCL error code to a string.
const char* errorString(int error) {
switch (error) {
case CL_SUCCESS: return "Success!";
case CL_DEVICE_NOT_FOUND: return "Device not found";
case CL_DEVICE_NOT_AVAILABLE: return "Device not available";
case CL_COMPILER_NOT_AVAILABLE: return "Compiler not available";
case CL_MEM_OBJECT_ALLOCATION_FAILURE: return "Memory object allocation failure";
case CL_OUT_OF_HOST_MEMORY: return "Out of host memory";
case CL_OUT_OF_RESOURCES: return "Out of resources";
case CL_PROFILING_INFO_NOT_AVAILABLE: return "Profiling information not available";
case CL_MEM_COPY_OVERLAP: return "Memory copy overlap";
case CL_IMAGE_FORMAT_MISMATCH: return "Image format mismatch";
case CL_IMAGE_FORMAT_NOT_SUPPORTED: return "Image format not supported";
case CL_BUILD_PROGRAM_FAILURE: return "Program build failure";
case CL_MAP_FAILURE: return "Map failure";
case CL_INVALID_VALUE: return "Invalid value";
case CL_INVALID_DEVICE_TYPE: return "Invalid device type";
case CL_INVALID_PLATFORM: return "Invalid platform";
case CL_INVALID_DEVICE: return "Invalid device";
case CL_INVALID_CONTEXT: return "Invalid context";
case CL_INVALID_QUEUE_PROPERTIES: return "Invalid queue properties";
case CL_INVALID_COMMAND_QUEUE: return "Invalid command queue";
case CL_INVALID_HOST_PTR: return "Invalid host pointer";
case CL_INVALID_MEM_OBJECT: return "Invalid memory object";
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: return "Invalid image format descriptor";
case CL_INVALID_IMAGE_SIZE: return "Invalid image size";
case CL_INVALID_SAMPLER: return "Invalid sampler";
case CL_INVALID_BINARY: return "Invalid binary";
case CL_INVALID_BUILD_OPTIONS: return "Invalid build options";
case CL_INVALID_PROGRAM: return "Invalid program";
case CL_INVALID_PROGRAM_EXECUTABLE: return "Invalid program executable";
case CL_INVALID_KERNEL_NAME: return "Invalid kernel name";
case CL_INVALID_KERNEL_DEFINITION: return "Invalid kernel definition";
case CL_INVALID_KERNEL: return "Invalid kernel";
case CL_INVALID_ARG_INDEX: return "Invalid argument index";
case CL_INVALID_ARG_VALUE: return "Invalid argument value";
case CL_INVALID_ARG_SIZE: return "Invalid argument size";
case CL_INVALID_KERNEL_ARGS: return "Invalid kernel arguments";
case CL_INVALID_WORK_DIMENSION: return "Invalid work dimension";
case CL_INVALID_WORK_GROUP_SIZE: return "Invalid work group size";
case CL_INVALID_WORK_ITEM_SIZE: return "Invalid work item size";
case CL_INVALID_GLOBAL_OFFSET: return "Invalid global offset";
case CL_INVALID_EVENT_WAIT_LIST: return "Invalid event wait list";
case CL_INVALID_EVENT: return "Invalid event";
case CL_INVALID_OPERATION: return "Invalid operation";
case CL_INVALID_GL_OBJECT: return "Invalid OpenGL object";
case CL_INVALID_BUFFER_SIZE: return "Invalid buffer size";
case CL_INVALID_MIP_LEVEL: return "Invalid mip-map level";
case CL_INVALID_GLOBAL_WORK_SIZE: return "Invalid global work size";
default: return "Unknown error";
}
}