-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathOpenCL_Test1.cpp
More file actions
366 lines (304 loc) · 10.5 KB
/
OpenCL_Test1.cpp
File metadata and controls
366 lines (304 loc) · 10.5 KB
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
//OpenCL Demo application made by Dmytro Konobrytskyi 2012
#pragma comment( lib, "opencl.lib" )
#define __CL_ENABLE_EXCEPTIONS
#include "cl.hpp"
#include <time.h>
#include <vector>
#include <iostream>
#include <string>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <algorithm>
#include <conio.h>
#include <thread>
#include <ppl.h>
#include <array>
#include <sstream>
#include <iostream>
#include <vector>
using namespace Concurrency;
using namespace std;
float MathCalculations(float a, float b);
const int DATA_SIZE = 20*1024*1024;
const int TESTS_NUMBER = 200;
float *pInputVector1;
float *pInputVector2;
float *pOutputVector;
float *pOutputVectorHost;
double hostPerformanceTimeMS = 0;
std::vector<double> timeValues;
void PrintTimeStatistic()
{
std::sort(timeValues.begin(), timeValues.end());
double totalTime = std::accumulate(timeValues.begin(), timeValues.end(), 0.0);
double averageTime = totalTime/timeValues.size();
double minTime = timeValues[0];
double maxTime = timeValues[timeValues.size()-1];
double medianTime = timeValues[timeValues.size()/2];
cout << "Calculation time statistic: (" << timeValues.size() << " runs)" << endl;
cout << "Med: " << medianTime << " ms (" << hostPerformanceTimeMS/medianTime << "X faster then host)" << endl;
cout << "Avg: " << averageTime << " ms" << endl;
cout << "Min: " << minTime << " ms" << endl;
cout << "Max: " << maxTime << " ms" << endl << endl;
}
void GenerateTestData()
{
pInputVector1 = new float[DATA_SIZE];
pInputVector2 = new float[DATA_SIZE];
pOutputVector = new float[DATA_SIZE];
pOutputVectorHost = new float[DATA_SIZE];
srand ((unsigned int)time(NULL));
for (int i=0; i<DATA_SIZE; i++)
{
pInputVector1[i] = rand() * 1000.0f / RAND_MAX;
pInputVector2[i] = rand() * 1000.0f / RAND_MAX;
}
}
void STDThreadCalculationFunction(int start, int end)
{
for(int iJob=start; iJob<end; iJob++)
{
//Perform calculations
pOutputVector[iJob] = MathCalculations(pInputVector1[iJob], pInputVector2[iJob]);
}
}
void PerformCalculationsOnHostSeparateFunction()
{
cout << endl << "-------------------------------------------------" << endl;
cout << "Device: Host separate function" << endl << endl;
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
for(int iTest = 0; iTest < (TESTS_NUMBER / 5); iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
STDThreadCalculationFunction(0, DATA_SIZE);
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
PrintTimeStatistic();
}
void PerformCalculationsOnHost()
{
cout << "Device: Host" << endl << endl;
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
for(int iTest=0; iTest<(TESTS_NUMBER/10); iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
for(int iJob=0; iJob<DATA_SIZE; iJob++)
{
//Perform calculations
pOutputVectorHost[iJob] = MathCalculations(pInputVector1[iJob], pInputVector2[iJob]);
}
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
hostPerformanceTimeMS = std::accumulate(timeValues.begin(), timeValues.end(), 0.0)/timeValues.size();
PrintTimeStatistic();
}
void PerformCalculationsOnHostParallelFor()
{
cout << endl << "-------------------------------------------------" << endl;
cout << "Device: Host parallel_for" << endl << endl;
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
for(int iTest=0; iTest<(TESTS_NUMBER/5); iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
parallel_for(size_t(0), size_t(DATA_SIZE), [&](size_t iJob)
{
//Perform calculations
pOutputVector[iJob] = MathCalculations(pInputVector1[iJob], pInputVector2[iJob]);
});
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
PrintTimeStatistic();
}
void PerformCalculationsOnHostSTDThread()
{
cout << endl << "-------------------------------------------------" << endl;
cout << "Device: Host std::thread" << endl << endl;
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
int threadsNumber = max(1, std::thread::hardware_concurrency());
cout << "Threads number: " << threadsNumber << endl << endl;
int jobsPerThread = DATA_SIZE/threadsNumber;
for(int iTest=0; iTest<(TESTS_NUMBER/5); iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
int curStartJob = 0;
std::vector<std::thread> threadVector;
for(int iThread=0; iThread<threadsNumber; iThread++)
{
threadVector.push_back(std::thread(STDThreadCalculationFunction, curStartJob, min(curStartJob+jobsPerThread, DATA_SIZE)));
curStartJob += jobsPerThread;
}
for(auto thread=threadVector.begin(); thread!=threadVector.end(); thread++)
thread->join();
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
PrintTimeStatistic();
}
void PerformCalculationsOnHostSTDThread1()
{
cout << endl << "-------------------------------------------------" << endl;
cout << "Device: Host std::thread 1 " << endl << endl;
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
int threadsNumber = 1;
cout << "Threads number: " << threadsNumber << endl << endl;
int jobsPerThread = DATA_SIZE/threadsNumber;
for(int iTest=0; iTest<(TESTS_NUMBER/5); iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
int curStartJob = 0;
std::vector<std::thread> threadVector;
for(int iThread=0; iThread<threadsNumber; iThread++)
{
threadVector.push_back(std::thread(STDThreadCalculationFunction, curStartJob, min(curStartJob+jobsPerThread, DATA_SIZE)));
curStartJob += jobsPerThread;
}
for(auto thread=threadVector.begin(); thread!=threadVector.end(); thread++)
thread->join();
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
PrintTimeStatistic();
}
void PerformTestOnDevice(cl::Device device)
{
cout << endl << "-------------------------------------------------" << endl;
cout << "Device: " << device.getInfo<CL_DEVICE_NAME>() << endl << endl;
//For the selected device create a context
vector<cl::Device> contextDevices;
contextDevices.push_back(device);
cl::Context context(contextDevices);
//For the selected device create a context and command queue
cl::CommandQueue queue(context, device);
//Clean output buffers
fill_n(pOutputVector, DATA_SIZE, 0);
//Create memory buffers
cl::Buffer clmInputVector1 = cl::Buffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, DATA_SIZE * sizeof(float), pInputVector1);
cl::Buffer clmInputVector2 = cl::Buffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, DATA_SIZE * sizeof(float), pInputVector2);
cl::Buffer clmOutputVector = cl::Buffer(context, CL_MEM_READ_WRITE|CL_MEM_COPY_HOST_PTR, DATA_SIZE * sizeof(float), pOutputVector);
//Load OpenCL source code
std::ifstream sourceFile("OpenCLFile1.cl");
std::string sourceCode(std::istreambuf_iterator<char>(sourceFile),(std::istreambuf_iterator<char>()));
//Build OpenCL program and make the kernel
cl::Program::Sources source(1, std::make_pair(sourceCode.c_str(), sourceCode.length()+1));
cl::Program program = cl::Program(context, source);
program.build(contextDevices);
cl::Kernel kernel(program, "TestKernel");
//Set arguments to kernel
int iArg = 0;
kernel.setArg(iArg++, clmInputVector1);
kernel.setArg(iArg++, clmInputVector2);
kernel.setArg(iArg++, clmOutputVector);
kernel.setArg(iArg++, DATA_SIZE);
//Some performance measurement
timeValues.clear();
__int64 start_count;
__int64 end_count;
__int64 freq;
QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
//Run the kernel on specific ND range
for(int iTest=0; iTest<TESTS_NUMBER; iTest++)
{
QueryPerformanceCounter((LARGE_INTEGER*)&start_count);
queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(DATA_SIZE), cl::NDRange(128));
queue.finish();
QueryPerformanceCounter((LARGE_INTEGER*)&end_count);
double time = 1000 * (double)(end_count - start_count) / (double)freq;
timeValues.push_back(time);
}
PrintTimeStatistic();
// Read buffer C into a local list
queue.enqueueReadBuffer(clmOutputVector, CL_TRUE, 0, DATA_SIZE * sizeof(float), pOutputVector);
}
void CheckResults()
{
double avgRelAbsDiff = 0;
double maxRelAbsDiff = 0;
for(int iJob=0; iJob<DATA_SIZE; iJob++)
{
double absDif = abs(pOutputVectorHost[iJob] - pOutputVector[iJob]);
double relAbsDif = abs(absDif/pOutputVectorHost[iJob]);
avgRelAbsDiff += relAbsDif;
maxRelAbsDiff = max(maxRelAbsDiff, relAbsDif);
pOutputVector[iJob] = 0;
}
avgRelAbsDiff /= DATA_SIZE;
cout << "Errors:" << endl;
cout << "avgRelAbsDiff = " << avgRelAbsDiff << endl;
cout << "maxRelAbsDiff = " << maxRelAbsDiff << endl;
}
int main(int argc, char* argv[])
{
GenerateTestData();
PerformCalculationsOnHost();
PerformCalculationsOnHostSeparateFunction();
CheckResults();
PerformCalculationsOnHostParallelFor();
CheckResults();
PerformCalculationsOnHostSTDThread();
CheckResults();
PerformCalculationsOnHostSTDThread1();
CheckResults();
//Get all available platforms
vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
for (unsigned int iPlatform=0; iPlatform<platforms.size(); iPlatform++)
{
//Get all available devices on selected platform
std::vector<cl::Device> devices;
platforms[iPlatform].getDevices(CL_DEVICE_TYPE_ALL, &devices);
//Perform test on each device
for (unsigned int iDevice=0; iDevice<devices.size(); iDevice++)
{
try
{
PerformTestOnDevice(devices[iDevice]);
}
catch(cl::Error error)
{
std::cout << error.what() << "(" << error.err() << ")" << std::endl;
}
CheckResults();
}
}
//Clean buffers
delete[](pInputVector1);
delete[](pInputVector2);
delete[](pOutputVector);
delete[](pOutputVectorHost);
_getch();
return 0;
}