forked from jlgreathouse/StreamMR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scan.cpp
244 lines (213 loc) · 7.69 KB
/
scan.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
//#include <libc.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
//#include <mach/mach_time.h>
#include <math.h>
#include "scan.h"
static char *
LoadProgramSourceFromFile(const char *filename)
{
struct stat statbuf;
FILE *fh;
char *source;
fh = fopen(filename, "r");
if (!fh)
{
fprintf(stderr,"ERROR: %s:%d Failed to open kernel file %s\n", __FILE__, __LINE__, filename);
fprintf(stderr,"\tError reason: %s\n", strerror(errno));
exit(-1);
}
if (fh == 0)
return 0;
stat(filename, &statbuf);
source = (char *) malloc(statbuf.st_size + 1);
if (!fread(source, statbuf.st_size, 1, fh))
{
fprintf(stderr,"ERROR: %s:%d Failed to read from kernel file %s\n", __FILE__, __LINE__, filename);
fprintf(stderr,"\tError reason: %s\n", strerror(errno));
exit(-1);
}
source[statbuf.st_size] = '\0';
return source;
}
cl_device_id ComputeDeviceId;
cl_command_queue ComputeCommands;
cl_context ComputeContext;
cl_program ComputeProgram;
cl_kernel* ComputeKernels;
cl_mem* ScanPartialSums = 0;
unsigned int ElementsAllocated = 0;
unsigned int LevelsAllocated = 0;
int GROUP_SIZE = 256;
#define min(A,B) ((A) < (B) ? (A) : (B))
int Scan(cl_context ctx, cl_mem *input_buffer, cl_mem *output_buffer, cl_uint count)
{
int i;
int err = 0;
const size_t local_wsize = min(GROUP_SIZE, count);
const size_t global_wsize = count; // i.e. 64 work groups
const size_t num_work_groups = global_wsize / local_wsize;
printf("scan: %u %zu %zu %zu\n", count, local_wsize, global_wsize, num_work_groups);
cl_platform_id platforms;
err = clGetPlatformIDs(1, &platforms, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to locate a compute platform!\n");
return EXIT_FAILURE;
}
// Connect to a GPU compute device
//
err = clGetDeviceIDs(platforms, CL_DEVICE_TYPE_GPU, 1, &ComputeDeviceId, NULL);
if (err != CL_SUCCESS)
{
printf("Error: Failed to locate a compute device!\n");
return EXIT_FAILURE;
}
const char* filename = "scan.cl";
char *source = LoadProgramSourceFromFile(filename);
if(!source)
{
printf("Error: Failed to load compute program from file!\n");
return EXIT_FAILURE;
}
// We should *not* create a new context here. These kernels will be using
// buffers that were allocated from other contexts.
//
ComputeContext = ctx;
if (!ComputeContext)
{
printf("Error: Failed to create a compute ComputeContext!\n");
return EXIT_FAILURE;
}
// Create a command queue
//
ComputeCommands = clCreateCommandQueue(ComputeContext, ComputeDeviceId, 0, &err);
if (!ComputeCommands)
{
printf("Error: Failed to create a command ComputeCommands!\n");
return EXIT_FAILURE;
}
// Create the compute program from the source buffer
//
ComputeProgram = clCreateProgramWithSource(ComputeContext, 1, (const char **) & source, NULL, &err);
if (!ComputeProgram || err != CL_SUCCESS)
{
printf("%s\n", source);
printf("Error: Failed to create compute program!\n");
return EXIT_FAILURE;
}
// Build the program executable
//
err = clBuildProgram(ComputeProgram, 1, &ComputeDeviceId, NULL, NULL, NULL);
if (err != CL_SUCCESS)
{
size_t length;
char *build_log = NULL;
size_t build_log_size = 0;
printf("source: %s\n", source);
printf("Error: Failed to build program executable!\n");
clGetProgramBuildInfo(ComputeProgram, ComputeDeviceId, CL_PROGRAM_BUILD_LOG, build_log_size, build_log, &build_log_size);
build_log = (char *)malloc(build_log_size);
clGetProgramBuildInfo(ComputeProgram, ComputeDeviceId, CL_PROGRAM_BUILD_LOG, build_log_size, build_log, NULL);
printf("log: %s\n", build_log);
fflush(stdout);
free(build_log);
return EXIT_FAILURE;
}
free(source);
cl_kernel reduce = clCreateKernel(ComputeProgram, "reduce", &err);
if (!reduce || err != CL_SUCCESS)
{
printf("Error: Failed to create compute kernel!\n");
return EXIT_FAILURE;
}
cl_kernel top_scan = clCreateKernel(ComputeProgram, "top_scan", &err);
if (!top_scan || err != CL_SUCCESS)
{
printf("Error: Failed to create compute kernel!\n");
return EXIT_FAILURE;
}
cl_kernel bottom_scan = clCreateKernel(ComputeProgram, "bottom_scan", &err);
if (!bottom_scan || err != CL_SUCCESS)
{
printf("Error: Failed to create compute kernel!\n");
return EXIT_FAILURE;
}
cl_mem d_isums = clCreateBuffer(ComputeContext, CL_MEM_READ_WRITE,
num_work_groups * sizeof(float), NULL, &err);
// Set the kernel arguments for the reduction kernel
err = clSetKernelArg(reduce, 0, sizeof(cl_mem), (void*)input_buffer);
err |= clSetKernelArg(reduce, 1, sizeof(cl_mem), (void*)&d_isums);
err |= clSetKernelArg(reduce, 2, sizeof(cl_uint), (void*)&count);
err |= clSetKernelArg(reduce, 3, local_wsize * sizeof(float), NULL);
if(err)
{
printf("Error: Failed clSetKernelArg for Reduce");
return EXIT_FAILURE;
}
// Set the kernel arguments for the top-level scan
err = clSetKernelArg(top_scan, 0, sizeof(cl_mem), (void*)&d_isums);
err |= clSetKernelArg(top_scan, 1, sizeof(cl_int), (void*)&num_work_groups);
err |= clSetKernelArg(top_scan, 2, local_wsize * 2 * sizeof(float), NULL);
if(err)
{
printf("Error: Failed clSetKernelArg for top_scan");
return EXIT_FAILURE;
}
// Set the kernel arguments for the bottom-level scan
err = clSetKernelArg(bottom_scan, 0, sizeof(cl_mem), (void*)input_buffer);
err |= clSetKernelArg(bottom_scan, 1, sizeof(cl_mem), (void*)&d_isums);
err |= clSetKernelArg(bottom_scan, 2, sizeof(cl_mem), (void*)output_buffer);
err |= clSetKernelArg(bottom_scan, 3, sizeof(cl_uint), (void*)&count);
err |= clSetKernelArg(bottom_scan, 4, local_wsize * 2 * sizeof(float), NULL);
if(err)
{
printf("Error: Failed clSetKernelArg for bottom_scan");
return EXIT_FAILURE;
}
// Create the input buffer on the device
//
//size_t buffer_size = sizeof(float) * count;
err = clEnqueueNDRangeKernel(ComputeCommands, reduce, 1, NULL,
&global_wsize, &local_wsize, 0, NULL, NULL);
if(err)
{
printf("Error: Failed clEnqueueNDRangeKernel for reduce");
return EXIT_FAILURE;
}
// Next, a top-level exclusive scan is performed on the array
// of block sums
err = clEnqueueNDRangeKernel(ComputeCommands, top_scan, 1, NULL,
&local_wsize, &local_wsize, 0, NULL, NULL);
if(err)
{
printf("Error: Failed clEnqueueNDRangeKernel for top_scan");
return EXIT_FAILURE;
}
// Finally, a bottom-level scan is performed by each block
// that is seeded with the scanned value in block sums
err = clEnqueueNDRangeKernel(ComputeCommands, bottom_scan, 1, NULL,
&global_wsize, &local_wsize, 0, NULL, NULL);
if(err)
{
printf("Error: Failed clEnqueueNDRangeKernel for bottom_scan");
return EXIT_FAILURE;
}
clReleaseKernel(reduce);
clReleaseKernel(top_scan);
clReleaseKernel(bottom_scan);
clReleaseProgram(ComputeProgram);
clReleaseCommandQueue(ComputeCommands);
clReleaseContext(ComputeContext);
free(ComputeKernels);
//free(float_data);
//free(reference);
//free(result);
return 0;
}