-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsat_GPU.c
435 lines (376 loc) · 14.1 KB
/
sat_GPU.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
// -----------------------------------------------------------------------
//
// This program solves the Propositional (Boolean) Satisfiability problem
// using Depth-First Search algorithm and validates each vector using OpenCl.
// Problems are read from an input file, while solution is written to screen
// and an output file.
//
// Author: Aggelos Stamatiou, April 2017
//
// -----------------------------------------------------------------------
#define CL_TARGET_OPENCL_VERSION 300
#include <CL/cl.h>
// Common code file
#include "core.c"
// OpenCl global variables
int WI; // Work items.
cl_int status;
cl_context context;
cl_command_queue cmdQueue;
cl_kernel kernel;
size_t globalWorkSize[1];
cl_mem d_problem;
cl_mem d_finish;
int d_step; // Used to define starting index in Problem vector for each work item.
// Extra timer.
float communication_time;
// GPU timers.
cl_event myEvent;
cl_ulong startTimeNs, endTimeNs;
float GPU_run_time_sum;
// Auxiliary function that displays a message in case of wrong input parameters.
void syntax_error(char **argv)
{
printf("Wrong syntax. Use the following:\n\n");
printf("%s <work items> <inputfile>\n\n", argv[0]);
printf("where:\n");
printf("<work items> = number of computing units of the graphics card\n");
printf("<inputfile> = name of the file with the problem description\n");
printf("Program terminates.\n");
}
// Reading the kernel file.
char *readSource(const char *sourceFilename)
{
FILE *fp;
int err;
char *source;
int size;
fp = fopen(sourceFilename, "rb");
if (fp == NULL) {
printf("Could not open kernel file: %s\n", sourceFilename);
exit(-1);
}
err = fseek(fp, 0, SEEK_END);
if (err != 0) {
printf("Error seeking to end of file.\n");
exit(-1);
}
size = ftell(fp);
if (size < 0) {
printf("Error getting file position.\n");
exit(-1);
}
err = fseek(fp, 0, SEEK_SET);
if (err != 0) {
printf("Error seeking to start of file.\n");
exit(-1);
}
source = (char*)malloc(size + 1);
if (source == NULL) {
printf("Error allocating %d bytes for the program source.\n", size + 1);
exit(-1);
}
err = fread(source, 1, size, fp);
if (err != size) {
printf("only read %d bytes.\n", err);
exit(0);
}
source[size] = '\0';
return source;
}
// This function checks whether a current partial assignment is already invalid using the GPU.
// In order for a partial assignment to be invalid, there should exist a clause such that
// all propositions in the clause have already value and their values are such that
// the clause is false. We validate the vector by counting how many clauses are valid.
// In order for the vector to be invalid, count is less than K (number of clauses).
int valid(struct frontier_node *node)
{
// Pass the vector to GPU.
cl_mem d_vector;
d_vector = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, N * sizeof(int), node->vector, &status);
if (status != CL_SUCCESS || d_vector == NULL) {
printf("clCreateBuffer failed\n");
exit(-1);
}
// Create a partial sums table for the GPU.
int *partial_sums = (int*)malloc(WI * sizeof(int));
if (partial_sums == NULL) {
printf("Error: malloc for partial_sums failed.\n");
exit(-1);
}
for (int i = 0; i < WI; i++) {
partial_sums[i] = 0;
}
cl_mem d_partial_sums;
d_partial_sums = clCreateBuffer(context, CL_MEM_WRITE_ONLY, WI * sizeof(int), NULL, &status);
if (status != CL_SUCCESS || d_partial_sums == NULL) {
printf("clCreateBuffer failed\n");
exit(-1);
}
// Set kernel arguments.
status = clSetKernelArg(kernel, 1, sizeof(cl_mem), &d_vector);
status |= clSetKernelArg(kernel, 3, sizeof(cl_mem), &d_partial_sums);
if (status != CL_SUCCESS) {
printf("clSetKernelArg failed\n");
exit(-1);
}
clock_t S_idle_timer = clock();
// Run kernel.
status = clEnqueueNDRangeKernel(cmdQueue, kernel, 1, NULL, globalWorkSize, NULL, 0, NULL, &myEvent);
if (status != CL_SUCCESS) {
printf("clEnqueueNDRangeKernel failed\n");
exit(-1);
}
// Copy back the memory to the host.
status = clEnqueueReadBuffer(cmdQueue, d_partial_sums, CL_TRUE, 0, WI * sizeof(int), partial_sums, 0, NULL, NULL);
if (status != CL_SUCCESS) {
printf("clEnqueueReadBuffer failed\n");
exit(-1);
}
clock_t E_idle_timer = clock();
float idle_time = ((float)(E_idle_timer - S_idle_timer) / CLOCKS_PER_SEC);
// Release OpenCL objects.
clReleaseMemObject(d_partial_sums);
clReleaseMemObject(d_vector);
// Get timers values.
clWaitForEvents(1, &myEvent);
clGetEventProfilingInfo(myEvent, CL_PROFILING_COMMAND_START, sizeof(cl_ulong), &startTimeNs, NULL);
clGetEventProfilingInfo(myEvent, CL_PROFILING_COMMAND_END, sizeof(cl_ulong), &endTimeNs, NULL);
clReleaseEvent(myEvent);
float GPU_run_time = ((endTimeNs - startTimeNs) / 1000000000.0);
GPU_run_time_sum += GPU_run_time;
communication_time += idle_time - GPU_run_time;
// Reduce the partial sums.
int sum = 0;
for (int i = 0; i < WI; i++) {
sum += partial_sums[i];
}
free(partial_sums);
// Check validation.
if (sum < K) {
return 0;
}
return 1;
}
// Depth-First Search functions
#include "dfs.c"
int main(int argc, char **argv)
{
int err;
srand((unsigned)time(NULL));
if (argc != 3) {
syntax_error(argv);
exit(-1);
}
WI = atoi(argv[1]);
err = readfile(argv[2]);
if (err < 0) {
exit(-1);
}
printf("\nThis OpenCL programm solves the Propositional (Boolean) Satisfiability Problem \n");
printf("written in file %s, using Depth First Search Algorithm.\n", argv[2]);
printf("Number of work items: %s\n\n", argv[1]);
printf("Device info:\n\n");
cl_uint numPlatforms = 0;
cl_platform_id* platforms;
// Query for the number of recongnized platforms.
status = clGetPlatformIDs(0, NULL, &numPlatforms);
if (status != CL_SUCCESS) {
printf("clGetPlatformIDs failed. Program terminates.\n");
exit(-1);
}
// Make sure some platforms were found.
if (numPlatforms == 0) {
printf("No platforms detected. Program terminates.\n");
exit(-1);
}
// Allocate enough space for each platform.
platforms = (cl_platform_id*)malloc(numPlatforms * sizeof(cl_platform_id));
if (platforms == NULL) {
perror("malloc");
exit(-1);
}
// Fill in platforms.
clGetPlatformIDs(numPlatforms, platforms, NULL);
if (status != CL_SUCCESS) {
printf("clGetPlatformIDs failed. Program terminates.\n");
exit(-1);
}
// Print out some basic information about each platform.
printf("%u platforms detected\n", numPlatforms);
for (int i = 0; i < numPlatforms; i++) {
char buf[100];
printf("Platform %u: \n", i);
status = clGetPlatformInfo(platforms[i], CL_PLATFORM_VENDOR, sizeof(buf), buf, NULL);
printf("\tVendor: %s\n", buf);
status |= clGetPlatformInfo(platforms[i], CL_PLATFORM_NAME, sizeof(buf), buf, NULL);
printf("\tName: %s\n", buf);
if (status != CL_SUCCESS) {
printf("clGetPlatformInfo failed. Program terminates.\n");
exit(-1);
}
}
printf("\n");
// Retrive the number of devices present.
cl_uint numDevices = 0;
cl_device_id* devices;
status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
if (status != CL_SUCCESS) {
printf("clGetDeviceIDs failed. Program terminates.\n");
exit(-1);
}
// Make sure some devices were found.
if (numDevices == 0) {
printf("No devices detected. Program terminates.\n");
exit(-1);
}
// Allocate enough space for each device.
devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
if (devices == NULL) {
perror("malloc");
exit(-1);
}
// Fill in devices.
status = clGetDeviceIDs(platforms[0], CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
if (status != CL_SUCCESS) {
printf("clGetDeviceIDs failed. Program terminates.\n");
exit(-1);
}
// Print out some basic information about each device.
printf("%u devices detected\n", numDevices);
for (int i = 0; i < numDevices; i++) {
char buf[100];
printf("Device %u: \n", i);
status = clGetDeviceInfo(devices[i], CL_DEVICE_VENDOR, sizeof(buf), buf, NULL);
printf("\tDevice: %s\n", buf);
status |= clGetDeviceInfo(devices[i], CL_DEVICE_NAME, sizeof(buf), buf, NULL);
printf("\tName: %s\n", buf);
if (status != CL_SUCCESS) {
printf("clGetDeviceInfo failed. Program terminates.\n");
exit(-1);
}
}
printf("\n");
// Create a context and associate it with the devices.
context = clCreateContext(NULL, numDevices, devices, NULL, NULL, &status);
if (status != CL_SUCCESS || context == NULL) {
printf("clCreateContext failed. Program terminates.\n");
exit(-1);
}
// Create a command queue and associate it with the device you want to execute on.
cmdQueue = clCreateCommandQueueWithProperties(context, devices[0], NULL, &status);
if (status != CL_SUCCESS || cmdQueue == NULL) {
printf("clCreateCommandQueue failed. Program terminates.\n");
exit(-1);
}
cl_program program;
char *source;
const char *sourceFile = "cl_valid.cl";
// This function reads in the source code of the program.
source = readSource(sourceFile);
// Create a program. The 'source' string is the code from the cl_valid.cl file.
program = clCreateProgramWithSource(context, 1, (const char**)&source, NULL, &status);
if (status != CL_SUCCESS) {
printf("clCreateProgramWithSource failed. Program terminates.\n");
exit(-1);
}
cl_int buildErr;
// Build (compile & link) the program for the devices.
// Save the return value in 'buildErr' (the following
// code will print any compilation errors to the screen).
buildErr = clBuildProgram(program, numDevices, devices, NULL, NULL, NULL);
// If there are build errors, print them to the screen.
if (buildErr != CL_SUCCESS) {
printf("Program failed to build.\n");
cl_build_status buildStatus;
for (int i = 0; i < numDevices; i++) {
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_STATUS, sizeof(cl_build_status), &buildStatus, NULL);
if (buildStatus == CL_SUCCESS) {
continue;
}
char *buildLog;
size_t buildLogSize;
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_LOG, 0, NULL, &buildLogSize);
buildLog = (char*)malloc(buildLogSize);
if (buildLog == NULL) {
perror("malloc");
exit(-1);
}
clGetProgramBuildInfo(program, devices[i], CL_PROGRAM_BUILD_LOG, buildLogSize, buildLog, NULL);
buildLog[buildLogSize - 1] = '\0';
printf("Device %u Build Log:\n%s\n", i, buildLog);
free(buildLog);
}
exit(0);
}
// Create a kernel from the vector validation function (named "clvalid").
kernel = clCreateKernel(program, "clvalid", &status);
if (status != CL_SUCCESS) {
printf("clCreateKernel failed. Program terminates.\n");
exit(-1);
}
// Define an index space (global work size) of threads for execution.
// A workgroup size (local work size) is not required, but can be used.
// There are WI threads.
if (K <= WI) {
WI = K; // If K is less than threads, we use K threads.
}
globalWorkSize[0] = WI;
// Define the step and finishing index for each thread.
d_step = K / WI;
int *finish = (int*)malloc(WI * sizeof(int)); // Finishing index of each work item.
if (finish == NULL) {
printf("Memory exhausted. Program terminates.\n");
exit(-1);
}
for (int i = 0; i < WI - 1; i++) {
finish[i] = d_step*(i + 1);
}
// Last thread gets the extra work if K mod WI != 0.
finish[WI - 1] = K;
// Pass data to GPU.
d_finish = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, WI * sizeof(int), finish, &status);
if (status != CL_SUCCESS || d_finish == NULL) {
printf("clCreateBuffer failed. Program terminates.\n");
exit(-1);
}
d_problem = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, K*M * sizeof(int), Problem, &status);
if (status != CL_SUCCESS || d_problem == NULL) {
printf("clCreateBuffer failed. Program terminates.\n");
exit(-1);
}
// Set kernel arguments.
status = clSetKernelArg(kernel, 0, sizeof(cl_mem), &d_problem);
status |= clSetKernelArg(kernel, 2, sizeof(cl_mem), &d_finish);
status |= clSetKernelArg(kernel, 4, sizeof(int), &d_step);
status |= clSetKernelArg(kernel, 5, sizeof(int), &M);
if (status != CL_SUCCESS) {
printf("clSetKernelArg failed. Program terminates.\n");
exit(-1);
}
printf("No build errors, starting solving the problem...\n");
//display_problem();
struct frontier_node* solution_node = search(); // The main call.
if (solution_node != NULL) {
printf("\nSolution found with depth-first!\n");
printf("\nSolution vector propositions values:\n");
display(solution_node->vector);
} else {
if (mem_error == -1) {
printf("Memory exhausted. Program terminates.\n");
} else {
printf("\nNO SOLUTION EXISTS. Proved by depth-first!");
}
}
printf("\n\nTime spent = %0.3f\n", ((float)t2 - t1) / CLOCKS_PER_SEC);
printf("GPU execution time = %0.3f\n", GPU_run_time_sum);
printf("Communication time = %0.3f\n", communication_time);
// Cleanup OpenCL structures.
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(cmdQueue);
clReleaseMemObject(d_problem);
clReleaseMemObject(d_finish);
clReleaseContext(context);
return 0;
}