-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.cpp
294 lines (238 loc) · 9.07 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
/*******************************************************************************
* Copyright (c) 2015 StreamComputing BV - All Rights Reserved *
* www.streamcomputing.eu *
* *
* This software contains source code provided by NVIDIA Corporation *
******************************************************************************/
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
#include<math.h>
#include "cl_util.h"
// CUDA SDK header that we use for time measurements
///////////////////////////////////////////////////////////////////////////////
#include "helper_timer.h"
///////////////////////////////////////////////////////////////////////////////
// Allocates memory for an array.
void AllocArray(float** pInput, int count)
{
CHECK_NULL(pInput)
*pInput = (float*)malloc(count * sizeof(float));
CHECK_NULL(*pInput)
}
///////////////////////////////////////////////////////////////////////////////
// Initializes an array.
void InitArray(float* pInput, int count)
{
CHECK_NULL(pInput)
srand((unsigned)time(NULL));
for (int i = 0; i < count; i++)
{
pInput[i] = rand();
}
}
///////////////////////////////////////////////////////////////////////////////
// Frees an image buffer.
void FreeArray(float** pInput)
{
CHECK_NULL(pInput)
if (*pInput)
{
free(*pInput);
*pInput = NULL;
}
}
///////////////////////////////////////////////////////////////////////////////
// Compares two images pixel by pixel.
int CompareArrays(float* input1, float* input2, int count)
{
CHECK_NULL(input1)
CHECK_NULL(input2)
float epsilon = 1.e-4f;
for (int i = 0; i < count; i++)
if (fabs(input1[i] - input2[i]) > epsilon)
{
printf("\nDifference found at offset [%d]: %.4f != %.4f",
i, input1[i], input2[i]);
return 0;
}
return 1;
}
///////////////////////////////////////////////////////////////////////////////
// Launches the OpenCL kernel for doing color balance.
void SimpleFunction(float* input1, float* input2, float* output, int count)
{
CHECK_NULL(input1);
CHECK_NULL(input2);
CHECK_NULL(output);
for (int i = 0; i < count; i++)
{
output[i] = input1[i] + input2[i];
}
}
///////////////////////////////////////////////////////////////////////////////
// Launches the OpenCL kernel for doing color balance.
void SimpleFunctionOpenCL(cl_mem input1, cl_mem input2, cl_mem output, int count, cl_command_queue queue, cl_kernel simpleFunctionKernel)
{
cl_int clError;
cl_uint workDim;
size_t globalWorkSize[3];
size_t localWorkSize[3];
// Set the kernel arguments
clError = clSetKernelArg(simpleFunctionKernel, 0, sizeof(cl_mem), (void*)(&input1));
CHECK_OCL_ERR("clSetKernelArg", clError);
clError = clSetKernelArg(simpleFunctionKernel, 1, sizeof(cl_mem), (void*)(&input2));
CHECK_OCL_ERR("clSetKernelArg", clError);
clError = clSetKernelArg(simpleFunctionKernel, 2, sizeof(cl_mem), (void*)(&output));
CHECK_OCL_ERR("clSetKernelArg", clError);
clError = clSetKernelArg(simpleFunctionKernel, 3, sizeof(int), (void*)(&count));
CHECK_OCL_ERR("clSetKernelArg", clError);
// Specify work-item configuration
workDim = 1;
localWorkSize[0] = 256;
localWorkSize[1] = 0;
localWorkSize[2] = 0;
globalWorkSize[0] = ((count + localWorkSize[0] - 1) / localWorkSize[0]) * localWorkSize[0];
globalWorkSize[1] = 0;
globalWorkSize[2] = 0;
// Add the kernel to the queue
clError = clEnqueueNDRangeKernel(queue, simpleFunctionKernel, workDim, NULL, globalWorkSize, localWorkSize, 0, NULL, NULL);
CHECK_OCL_ERR("clEnqueueNDRangeKernel", clError);
}
///////////////////////////////////////////////////////////////////////////////
//
int main(void)
{
int count = 4096;
float* input1 = NULL;
float* input2 = NULL;
float* output = NULL;
float* gpuOutput = NULL;
StopWatchInterface *timer = NULL;
StopWatchInterface *cpuProcTimer = NULL;
StopWatchInterface *gpuProcTimer = NULL;
float elapsedTimeInMs = 0;
cl_platform_id platform = 0;
cl_device_id device = 0;
cl_context context = 0;
cl_command_queue queue = 0;
char* sourceCode = NULL;
size_t sourceCodeLength = 0;
cl_program program = 0;
cl_kernel simpleFunctionKernel = 0;
cl_mem dInput1 = 0;
cl_mem dInput2 = 0;
cl_mem dOutput = 0;
cl_int clError = 0;
// Initializations
sdkCreateTimer(&timer);
sdkCreateTimer(&gpuProcTimer);
sdkCreateTimer(&cpuProcTimer);
AllocArray(&input1, count);
AllocArray(&input2, count);
InitArray(input1, count);
InitArray(input2, count);
AllocArray(&output, count);
AllocArray(&gpuOutput, count);
// Check if OpenCL is supported and print info
if (!PrintOpenCLInfo())
{
printf("\nNo OpenCL platform or device detected.");
exit(EXIT_FAILURE);
}
// OpenCL initializations
// Select an OpenCL platform and device
SelectOpenCLPlatformAndDevice(&platform, &device);
// Print the names of the selected platform and device
printf("\nUsing platform "); PrintPlatformName(platform);
printf(" and device "); PrintDeviceName(device);
printf("\n");
// Create an OpenCL context and command queue
context = CreateOpenCLContext(platform, device);
queue = CreateOpenCLQueue(device, context);
// Load the OpenCL source code from file and create an OpenCL program.
sourceCode = LoadOpenCLSourceFromFile("OpenCLKernels.cl", &sourceCodeLength);
program = CreateAndBuildProgram(context, sourceCode, sourceCodeLength);
// Create the OpenCL kernels
simpleFunctionKernel = CreateKernel(program, "SimpleFunction");
// Create OpenCL buffers on device
dInput1 = CreateDeviceBuffer(context, count * sizeof(float));
dInput2 = CreateDeviceBuffer(context, count * sizeof(float));
dOutput = CreateDeviceBuffer(context, count * sizeof(float));
// ________________________________________________________________________
// Do processing on CPU
// ________________________________________________________________________
sdkStartTimer(&cpuProcTimer);
// [CPU] Color Balance
sdkStartTimer(&timer);
{
SimpleFunction(input1, input2, output, count);
}
sdkStopTimer(&timer);
sdkStopTimer(&cpuProcTimer);
elapsedTimeInMs = sdkGetTimerValue(&timer);
printf("\n%-55s %10.4f ms", "SimpleFunction (CPU)", elapsedTimeInMs);
elapsedTimeInMs = sdkGetTimerValue(&cpuProcTimer);
printf("\n%-55s %10.4f ms", "TOTAL CPU PROC TIME", elapsedTimeInMs);
printf("\n");
// ________________________________________________________________________
// Do processing on GPU
// ________________________________________________________________________
sdkStartTimer(&gpuProcTimer);
// [GPU] Copy data from host to device
// ________________________________________________________________________
sdkStartTimer(&timer);
{
CopyHostToDevice(input1, dInput1, count * sizeof(float), queue, CL_TRUE);
CopyHostToDevice(input2, dInput2, count * sizeof(float), queue, CL_TRUE);
}
sdkStopTimer(&timer);
elapsedTimeInMs = sdkGetTimerValue(&timer);
printf("\n%-55s %10.4f ms", "Transfer data to GPU ", elapsedTimeInMs);
// [GPU] Color Balance
// ________________________________________________________________________
sdkStartTimer(&timer);
{
SimpleFunctionOpenCL(dInput1, dInput2, dOutput, count, queue, simpleFunctionKernel);
clFinish(queue);
}
sdkStopTimer(&timer);
elapsedTimeInMs = sdkGetTimerValue(&timer);
printf("\n%-55s %10.4f ms", "SimpleFunction (GPU)", elapsedTimeInMs);
// [GPU] Copy data from device to host
// ________________________________________________________________________
sdkStartTimer(&timer);
{
CopyDeviceToHost(dOutput, gpuOutput, count * sizeof(float), queue, CL_TRUE);
}
sdkStopTimer(&timer);
sdkStopTimer(&gpuProcTimer);
elapsedTimeInMs = sdkGetTimerValue(&timer);
printf("\n%-55s %10.4f ms", "Transfer data from GPU ", elapsedTimeInMs);
printf("\nChecking the last OpenCL output...");
if (!CompareArrays(output, gpuOutput, count))
printf("\nIncorrect OpenCL output.");
else
printf("passed.");
elapsedTimeInMs = sdkGetTimerValue(&gpuProcTimer);
printf("\n%-55s %10.4f ms", "TOTAL GPU PROC TIME", elapsedTimeInMs);
printf("\n");
// Free allocated resources
FreeArray(&input1);
FreeArray(&input2);
FreeArray(&output);
FreeArray(&gpuOutput);
if (timer)
sdkDeleteTimer(&timer);
if (gpuProcTimer)
sdkDeleteTimer(&gpuProcTimer);
if (sourceCode)
free(sourceCode);
ReleaseDeviceBuffer(&dInput1);
ReleaseDeviceBuffer(&dInput2);
ReleaseDeviceBuffer(&dOutput);
ReleaseKernel(&simpleFunctionKernel);
ReleaseProgram(&program);
ReleaseOpenCLQueue(&queue);
ReleaseOpenCLContext(&context);
}