-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
301 lines (240 loc) · 7.79 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <curses.h>
#include <string.h>
#include <time.h>
#include <CL/cl.h>
#define PROGRAM_FILE "kernel.cl"
#define GRID_SIZE 24
/* Find a GPU or CPU associated with the first available platform */
cl_device_id create_device() {
cl_platform_id platform;
cl_device_id dev;
int err;
/* Identify a platform */
err = clGetPlatformIDs(1, &platform, NULL);
if(err < 0) {
perror("Couldn't identify a platform");
exit(1);
}
/* Access a device */
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &dev, NULL);
if(err == CL_DEVICE_NOT_FOUND) {
err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &dev, NULL);
}
if(err < 0) {
perror("Couldn't access any devices");
exit(1);
}
return dev;
}
/* Create program from a file and compile it */
cl_program build_program(cl_context ctx, cl_device_id dev, const char* filename) {
cl_program program;
FILE *program_handle;
char *program_buffer, *program_log;
size_t program_size, log_size;
int err;
/* Read program file and place content into buffer */
program_handle = fopen(filename, "r");
if(program_handle == NULL) {
perror("Couldn't find the program file");
exit(1);
}
fseek(program_handle, 0, SEEK_END);
program_size = ftell(program_handle);
rewind(program_handle);
program_buffer = (char*)malloc(program_size + 1);
program_buffer[program_size] = '\0';
fread(program_buffer, sizeof(char), program_size, program_handle);
fclose(program_handle);
/* Create program from file */
program = clCreateProgramWithSource(ctx, 1, (const char**)&program_buffer, &program_size, &err);
if(err < 0) {
perror("Couldn't create the program");
exit(1);
}
free(program_buffer);
/* Build program */
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if(err < 0) {
/* Find size of log and print to std output */
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size);
program_log = (char*) malloc(log_size + 1);
program_log[log_size] = '\0';
clGetProgramBuildInfo(program, dev, CL_PROGRAM_BUILD_LOG, log_size + 1, program_log, NULL);
printf("%s\n", program_log);
free(program_log);
exit(1);
}
return program;
}
void setVal(int *arr, int size, int x, int y, int val) {
arr[size*y+x] = val;
}
int main(int argc, char* argv[])
{
srand(time(NULL));
// OpenCL init
cl_device_id device_id;
cl_context context;
cl_command_queue queue;
cl_program program;
cl_kernel kernel;
cl_int err;
cl_mem grid;
cl_mem gridSwp;
// Valid factors of 24 to be used for concurrency
int kernelCount = 2;
int multiples[] = {24, 12, 8, 4, 2, 1};
// Tick counter
int tick = 0;
int tickFinal = 1000;
// Grid store
// Store in 1d and expand to 2d
int *initGrid;
int output = true;
int argPtr;
if (argc > 1) {
argPtr = 1;
while (argPtr < argc) {
if (strcmp(argv[argPtr], "-o") == 0) {
output = false;
argPtr += 1;
} else if (strcmp(argv[argPtr], "-n") == 0) {
sscanf(argv[argPtr + 1], "%d", &kernelCount);
kernelCount -= 1;
argPtr += 2;
}
}
}
if (kernelCount < 0 || kernelCount >= 6)
printf("Kernel count must be on [1,6]");
// Ncurses init
WINDOW* ncursesWin;
int ch;
if (output) {
if ((ncursesWin = initscr()) == NULL) {
fprintf(stderr, "Error initialising ncurses.\n");
exit(EXIT_FAILURE);
}
nodelay(ncursesWin, TRUE);
noecho();
}
// Size in bytes of grid
size_t gridSizeBytes = sizeof(int) * GRID_SIZE * GRID_SIZE;
// Allocate memory for initial grid
initGrid = (int*)malloc(gridSizeBytes);
// Randomly set grid memory
for (int j = 0; j < GRID_SIZE; ++j)
for (int i = 0; i < GRID_SIZE; ++i)
initGrid[i*GRID_SIZE+j] = rand()%5 == 0;
setVal(initGrid, GRID_SIZE, 4,4, 1);
setVal(initGrid, GRID_SIZE, 4,5, 1);
setVal(initGrid, GRID_SIZE, 4,6, 1);
setVal(initGrid, GRID_SIZE, 5,6, 1);
setVal(initGrid, GRID_SIZE, 5,7, 1);
device_id = create_device();
// Create a context
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context) {
printf("Error: Failed to create a compute context\n");
return EXIT_FAILURE;
}
// Create a command queue
queue = clCreateCommandQueueWithProperties(context, device_id, 0, &err);
if (!queue) {
printf("Error: Failed to create a command commands\n");
return EXIT_FAILURE;
}
program = build_program(context, device_id, PROGRAM_FILE);
// Create the GOL kernel
kernel = clCreateKernel(program, "GOL", &err);
if (!kernel || err != CL_SUCCESS) {
printf("Error: Failed to create GOL kernel \n");
return EXIT_FAILURE;
}
// Create the input and output arrays in device memory for our calculation
grid = clCreateBuffer(context, CL_MEM_READ_WRITE, gridSizeBytes, NULL, NULL);
gridSwp = clCreateBuffer(context, CL_MEM_READ_WRITE, gridSizeBytes, NULL, NULL);
if (!grid || !gridSwp) {
printf("Error: Failed to allocate device memory\n");
return EXIT_FAILURE;
}
// Write our data set into the input array in device memory
err = clEnqueueWriteBuffer(queue, grid, CL_TRUE, 0, gridSizeBytes, initGrid, 0, NULL, NULL);
if (err != CL_SUCCESS) {
printf("Error: Failed to write to source array\n");
return EXIT_FAILURE;
}
// Set the arguments to GOL kernel
int gridSize = GRID_SIZE;
err = clSetKernelArg(kernel, 0, sizeof(int), &gridSize);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &grid);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &gridSwp);
if (err != CL_SUCCESS) {
printf("Error: Failed to set kernel arguments\n");
return EXIT_FAILURE;
}
// Here we are setting up the worker groups. Worker groups will be run concurrently
// Work groups will be assigned to free cores. This is like running multiple kernels
size_t workerSizes[2] = {multiples[kernelCount], multiples[kernelCount]};
size_t workGroupSizes[2] = {24, 24};
// Main game loop
while (tick <= tickFinal || output) {
// Create two dimensional work item / work groups and use this for the xy coordinate to update
// Queue up kernel to run with workers and worker group
err = clEnqueueNDRangeKernel(queue, kernel, 2, NULL, workGroupSizes, workerSizes, 0, NULL, NULL);
// On every tick switch boards
if(tick%2 == 1) {
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &grid);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &gridSwp);
} else {
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &gridSwp);
err |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &grid);
}
if (output) {
// Wait for queue to finish before reading it
clFinish(queue);
// Read the results from kernel
if (tick%2 == 1)
err = clEnqueueReadBuffer(queue, gridSwp, CL_TRUE, 0, gridSizeBytes, initGrid, 0, NULL, NULL );
else
err = clEnqueueReadBuffer(queue, grid, CL_TRUE, 0, gridSizeBytes, initGrid, 0, NULL, NULL );
if (err != CL_SUCCESS) {
printf("Error: Failed to read output array\n");
return EXIT_FAILURE;;
}
// Check if q was pressed
ch = getch();
if (ch == 'q')
break;
// Draw with ncurses
for (int j = 0; j < GRID_SIZE; j++) {
for (int i = 0; i < GRID_SIZE; i++) {
if (initGrid[i * GRID_SIZE+j])
mvaddstr(i, j, "+");
else
mvaddstr(i, j, ".");
}
}
refresh();
sleep(1);
}
tick++;
}
if (err != CL_SUCCESS) {
printf("Error: Failed to launch kernels%d\n",err);
return EXIT_FAILURE;
}
// Free grid memory
free(initGrid);
if (output) {
// Free ncurses memory
delwin(ncursesWin);
endwin();
refresh();
}
return 0;
}