forked from hughperkins/EasyCL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestprofiling.cpp
98 lines (88 loc) · 2.67 KB
/
testprofiling.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
#include "EasyCL.h"
#include <iostream>
#include <cstdlib>
#include "gtest/gtest.h"
#include "test/asserts.h"
using namespace std;
using namespace easycl;
static const char *kernelSource =
"kernel void test(int N, global float *in){\n"
" if((int)get_global_id(0) >= N) {\n"
" return;\n"
" }\n"
" in[get_global_id(0)] += 1.0f;\n"
"}\n"
;
TEST(testprofiling, basic) {
if(!EasyCL::isOpenCLAvailable()) {
cout << "opencl library not found" << endl;
exit(-1);
}
cout << "found opencl library" << endl;
EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu();
cl->setProfiling(true);
CLKernel *kernel = cl->buildKernelFromString(kernelSource, "test", "", "source1");
CLKernel *kernel2 = cl->buildKernelFromString(kernelSource, "test", "", "source2");
CLKernel *kernel3 = cl->buildKernelFromString(kernelSource, "test", "", "source3");
CLKernel *kernel4 = cl->buildKernelFromString(kernelSource, "test", "", "source4");
const int N = 8 * 1024 * 1024;
float *in = new float[N];
for(int i = 0; i < N; i++) {
in[i] = i * 3.0f;
}
CLWrapper *inwrapper = cl->wrap(N, in);
inwrapper->copyToDevice();
const int its = 16;
for(int i = 0 ; i < its; i++) {
kernel->in(N);
kernel->inout(inwrapper);
kernel->run_1d(((N+64-1)/64) * 64, 64);
kernel2->in(N/8);
kernel2->inout(inwrapper);
kernel2->run_1d(((N/8+64-1)/64) * 64, 64);
kernel3->in(N/64);
kernel3->inout(inwrapper);
kernel3->run_1d(((N/64+64-1)/64) * 64, 64);
kernel4->in(N/64/8);
kernel4->inout(inwrapper);
kernel4->run_1d(((N/64/8+64-1)/64) * 64, 64);
}
cl->dumpProfiling();
inwrapper->copyToHost();
delete inwrapper;
delete[] in;
delete kernel;
delete kernel2;
delete kernel3;
delete kernel4;
delete cl;
}
TEST(testprofiling, noprofiling) {
if(!EasyCL::isOpenCLAvailable()) {
cout << "opencl library not found" << endl;
exit(-1);
}
cout << "found opencl library" << endl;
EasyCL *cl = EasyCL::createForFirstGpuOtherwiseCpu();
// cl->setProfiling(true);
CLKernel *kernel = cl->buildKernelFromString(kernelSource, "test", "", "source1");
const int N = 2048;
float *in = new float[N];
for(int i = 0; i < N; i++) {
in[i] = i * 3.0f;
}
CLWrapper *inwrapper = cl->wrap(N, in);
inwrapper->copyToDevice();
const int its = 16;
for(int i = 0 ; i < its; i++) {
kernel->in(N);
kernel->inout(inwrapper);
kernel->run_1d(((N+64-1)/64) * 64, 64);
}
cl->dumpProfiling();
inwrapper->copyToHost();
delete inwrapper;
delete[] in;
delete kernel;
delete cl;
}