-
Notifications
You must be signed in to change notification settings - Fork 0
/
clprogram.cpp
134 lines (110 loc) · 3.48 KB
/
clprogram.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
/*
* File: clprogram.cpp
* Author: vbmacher
*
* Created on Sunday, 2010, october 17, 8:09
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include "main.hpp"
//#define __CL_ENABLE_EXCEPTIONS
#include "clprogram.hpp"
using namespace std;
CLprogram::CLprogram() {
error = CL_SUCCESS;
}
CLprogram::~CLprogram() {
}
bool CLprogram::initCL(int cmd_options) {
// Platform info
cl::vector<cl::Platform> platforms;
error = cl::Platform::get(&platforms);
if (error != CL_SUCCESS) {
cerr << "Platform::get() failed (" << error << ")" << endl;
return false;
}
if (platforms.size() == 0) {
cerr << "No compatible platforms found." << endl;
return false;
}
/*
* If we could find our platform, use it. Otherwise pass a NULL and get whatever the
* implementation thinks we should be using.
*/
cl_context_properties cps[3] = {CL_CONTEXT_PLATFORM,
(cl_context_properties)(platforms[0])(), 0};
if (cmd_options & CMD_GPU)
this->context = cl::Context(CL_DEVICE_TYPE_GPU, cps, NULL, NULL, &error);
else
this->context = cl::Context(CL_DEVICE_TYPE_CPU, cps, NULL, NULL, &error);
if (error != CL_SUCCESS) {
cerr << "Context::Context() failed (" << error << ")\n";
return false;
}
devices = context.getInfo<CL_CONTEXT_DEVICES > ();
if (error != CL_SUCCESS) {
cerr << "Context::getInfo() failed (" << error << ")\n";
return false;
}
if (devices.size() == 0) {
cerr << "No device available\n";
return false;
}
queue = cl::CommandQueue(context, devices[0], CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE, &error);
if (error != CL_SUCCESS) {
cerr << "CommandQueue::CommandQueue() failed (" << error << ")\n";
}
return true;
}
bool CLprogram::loadProgram(const char* source) {
if (error != CL_SUCCESS)
return false;
cl::Program::Sources sources(1, make_pair(source, strlen(source)));
program = cl::Program(context, sources, &error);
if (error != CL_SUCCESS) {
cerr << "Program::Program() failed (" << error << ")\n";
return false;
}
error = program.build(devices);
if (error != CL_SUCCESS) {
if (error == CL_BUILD_PROGRAM_FAILURE) {
cl::string str = program.getBuildInfo<CL_PROGRAM_BUILD_LOG > (devices[0]);
cout << " \n\t\t\tBUILD LOG\n";
cout << " ************************************************\n";
cout << str.c_str() << endl;
cout << " ************************************************\n";
}
cerr << "Program::build() failed (" << error << ")\n";
return false;
}
return true;
}
cl::Kernel CLprogram::getKernel(const char* name) {
cl::Kernel kernel(program, name, &error);
if (error != CL_SUCCESS) {
cerr << "Kernel::Kernel() failed (" << error << ")\n";
throw new exception;
}
return kernel;
}
bool CLprogram::runKernel(cl::Kernel kernel, cl::Event *event) {
if (error != CL_SUCCESS)
return false;
error = queue.enqueueTask(kernel, NULL, event);
if (error != CL_SUCCESS) {
cerr << "CommandQueue::enqueueTask()" \
" failed (" << error << ")\n";
return false;
}
return true;
}
bool CLprogram::finishAll() {
error = queue.finish();
if (error != CL_SUCCESS) {
cerr << "Event::wait() failed (" << error << ")\n";
return false;
}
return true;
}