/
reduce.c
206 lines (165 loc) · 6.21 KB
/
reduce.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
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <OpenCL/opencl.h>
#define SHARED
#define NUM_GROUPS (2)
#define DATA_SIZE (16)
#define STRINGFY(x) #x
const char* source_reduce_global = STRINGFY(
__kernel void kernel_reduce(__global float* input, __global float* output) {
int global_idx = get_global_id(0);
int local_idx = get_local_id(0);
int block_size = get_local_size(0);
int group_id = get_group_id(0);
for(int i = block_size/2; i > 0; i >>= 1) {
if(local_idx < i)
input[global_idx] += input[global_idx + i];
barrier(CLK_GLOBAL_MEM_FENCE);
}
if(local_idx == 0)
output[group_id] = input[global_idx];
}
);
const char* source_reduce_shared = STRINGFY(
__kernel void kernel_reduce(__global float* input, __global float* output, __local float* sinput) {
int global_idx = get_global_id(0);
int local_idx = get_local_id(0);
int block_size = get_local_size(0);
int group_id = get_group_id(0);
sinput[local_idx] = input[global_idx];
barrier(CLK_GLOBAL_MEM_FENCE);
//write_mem_fence(CLK_GLOBAL_MEM_FENCE);
for(int i = block_size/2; i > 0; i >>= 1) {
if(local_idx < i)
sinput[local_idx] += sinput[local_idx + i];
barrier(CLK_LOCAL_MEM_FENCE);
//mem_fence(CLK_LOCAL_MEM_FENCE);
}
if(local_idx == 0)
output[group_id] = sinput[0];
}
);
////////////////////////////////////////////////////////////////////////////////
int execute_kernel(cl_command_queue commands, cl_kernel kernel, cl_mem input, cl_mem output) {
int err;
err = 0;
err |= clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
err |= clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
#ifdef SHARED
err |= clSetKernelArg(kernel, 2, sizeof(cl_float)*DATA_SIZE/NUM_GROUPS, NULL);
#endif
if (err != CL_SUCCESS) {
printf("Error: Failed to set kernel arguments! %d\n", err);
return err;
}
size_t global = DATA_SIZE;
size_t local = DATA_SIZE/NUM_GROUPS;
err = clEnqueueNDRangeKernel(commands, kernel, 1, NULL, &global, &local, 0, NULL, NULL);
if (err != CL_SUCCESS) {
printf("Error: Failed to execute kernel!\n");
return err;
}
return err;
}
int main(int argc, char** argv) {
int err; // error code returned from api calls
float data[DATA_SIZE]; // original data set given to device
float results[DATA_SIZE]; // results returned from device
size_t global; // global domain size for our calculation
size_t local; // local domain size for our calculation
cl_device_id device_id; // compute device id
cl_context context; // compute context
cl_command_queue commands; // compute command queue
cl_program program; // compute program
cl_kernel kernel; // compute kernel
cl_mem input; // device memory used for the input array
cl_mem output; // device memory used for the output array
cl_mem temporary; //
int i = 0;
for(i = 0; i < DATA_SIZE; i++)
data[i] = i+1;
err = clGetDeviceIDs(NULL, CL_DEVICE_TYPE_GPU, 1, &device_id, NULL);
if (err != CL_SUCCESS) {
printf("Error: Failed to create a device group!\n");
return EXIT_FAILURE;
}
context = clCreateContext(0, 1, &device_id, NULL, NULL, &err);
if (!context) {
printf("Error: Failed to create a compute context!\n");
return EXIT_FAILURE;
}
commands = clCreateCommandQueue(context, device_id, 0, &err);
if (!commands) {
printf("Error: Failed to create a command commands!\n");
return EXIT_FAILURE;
}
#ifdef SHARED
const char* source_reduce = source_reduce_shared;
#else
const char* source_reduce = source_reduce_global;
#endif
program = clCreateProgramWithSource(context, 1, &source_reduce, NULL, &err);
if (!program) {
printf("Error: Failed to create compute program!\n");
return EXIT_FAILURE;
}
err = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (err != CL_SUCCESS) {
size_t len;
char buffer[2048];
printf("Error: Failed to build program executable!\n");
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, sizeof(buffer), buffer, &len);
printf("%s\n", buffer);
exit(1);
}
kernel = clCreateKernel(program, "kernel_reduce", &err);
if (!kernel || err != CL_SUCCESS) {
printf("Error: Failed to create compute kernel!\n");
exit(1);
}
input = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);
output = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * DATA_SIZE, NULL, NULL);
temporary = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(float) * DATA_SIZE/NUM_GROUPS, NULL, NULL);
if (!input || !output || !temporary) {
printf("Error: Failed to allocate device memory!\n");
exit(1);
}
err = clEnqueueWriteBuffer(commands, input, CL_TRUE, 0, sizeof(float) * DATA_SIZE, data, 0, NULL, NULL);
if (err != CL_SUCCESS) {
printf("Error: Failed to write to source array!\n");
exit(1);
}
err = execute_kernel(commands, kernel, input, temporary);
if(err != CL_SUCCESS) {
exit(1);
}
err = execute_kernel(commands, kernel, temporary, output);
if(err != CL_SUCCESS) {
exit(1);
}
clFinish(commands);
err = clEnqueueReadBuffer( commands, output, CL_TRUE, 0, sizeof(float) * DATA_SIZE, results, 0, NULL, NULL );
if (err != CL_SUCCESS) {
printf("Error: Failed to read output array! %d\n", err);
exit(1);
}
printf("Result:\n");
for(int i = 0; i < DATA_SIZE; i++) {
printf("%f", results[i]);
printf( (i+1) % 4 == 0 ? "\n" : "\t" );
}
printf("\n");
clReleaseMemObject(input);
clReleaseMemObject(output);
clReleaseProgram(program);
clReleaseKernel(kernel);
clReleaseCommandQueue(commands);
clReleaseContext(context);
return 0;
}