forked from GerhardR/kfusion
-
Notifications
You must be signed in to change notification settings - Fork 4
/
common_opencl.cpp
181 lines (149 loc) · 5.26 KB
/
common_opencl.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
/*
Copyright (c) 2014 University of Edinburgh, Imperial College, University of Manchester.
Developed in the PAMELA project, EPSRC Programme Grant EP/K008730/1
This code is licensed under the MIT License.
*/
#define EXTERNS
#include "common_opencl.h"
#include <sstream>
// OPEN CL STUFF
cl_int clError = CL_SUCCESS;
cl_platform_id platform_id = 0;
cl_device_id device_id; // compute device id
cl_context context;
cl_program program;
cl_command_queue commandQueue;
void opencl_clean(void) {
clReleaseContext(context);
clReleaseCommandQueue(commandQueue);
clReleaseProgram(program);
return;
}
void opencl_init(void) {
// get the platform
cl_uint num_platforms;
clError = clGetPlatformIDs(0, NULL, &num_platforms);
checkErr(clError, "clGetPlatformIDs( 0, NULL, &num_platforms );");
if (num_platforms <= 0) {
std::cout << "No platform..." << std::endl;
exit(1);
}
cl_platform_id* platforms = new cl_platform_id[num_platforms];
clError = clGetPlatformIDs(num_platforms, platforms, NULL);
checkErr(clError, "clGetPlatformIDs( num_platforms, &platforms, NULL );");
if (num_platforms > 1) {
char platformName[256];
clError = clGetPlatformInfo(platforms[0], CL_PLATFORM_VENDOR,
sizeof(platformName), platformName, NULL);
std::cerr << "Multiple platforms found defaulting to: " << platformName
<< std::endl;
}
platform_id = platforms[0];
if (getenv("OPENCL_PLATFORM")) {
char platformName[256];
unsigned int number = atoi(getenv("OPENCL_PLATFORM"));
if (number >= num_platforms) {
std::cerr << "Invalid extra parameter OPENCL_PLATFORM found, " << number << " doesn't exist"
<< std::endl;
} else {
platform_id = platforms[number];
clError = clGetPlatformInfo(platforms[number], CL_PLATFORM_VENDOR,
sizeof(platformName), platformName, NULL);
std::cerr << "Extra parameter OPENCL_PLATFORM found change to: " << platformName
<< std::endl;
}
}
delete[] platforms;
// Connect to a compute device
//
cl_uint device_count = 0;
clError = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, 0, NULL, &device_count);
checkErr(clError, "Failed to create a device group");
cl_device_id* deviceIds = (cl_device_id*) malloc(
sizeof(cl_device_id) * device_count);
clError = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL, device_count,
deviceIds, NULL);
if (device_count > 1) {
char device_name[256];
int compute_units;
clError = clGetDeviceInfo(deviceIds[0], CL_DEVICE_NAME,
sizeof(device_name), device_name, NULL);
checkErr(clError, "clGetDeviceInfo failed");
clError = clGetDeviceInfo(deviceIds[0], CL_DEVICE_MAX_COMPUTE_UNITS,
sizeof(cl_uint), &compute_units, NULL);
checkErr(clError, "clGetDeviceInfo failed");
std::cerr << "Multiple devices found defaulting to: " << device_name;
std::cerr << " with " << compute_units << " compute units" << std::endl;
}
device_id = deviceIds[0];
free(deviceIds);
// Create a compute context
//
context = clCreateContext(0, 1, &device_id, NULL, NULL, &clError);
checkErr(clError, "Failed to create a compute context!");
// Create a command commands
//
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
commandQueue = clCreateCommandQueue(context, device_id, 0, &clError);
checkErr(clError, "Failed to create a command commands!");
// READ KERNEL FILENAME
std::string filename = "NOTDEFINED.cl";
char const* tmp_name = getenv("OPENCL_KERNEL");
if (tmp_name) {
filename = std::string(tmp_name);
} else {
filename = std::string(__FILE__);
filename = filename.substr(0, filename.length() - 17);
filename += "/kernels.cl";
}
// READ OPENCL_PARAMETERS
std::string compile_parameters = "";
char const* tmp_params = getenv("OPENCL_PARAMETERS");
if (tmp_params) {
compile_parameters = std::string(tmp_params);
}
std::ifstream kernelFile(filename.c_str(), std::ios::in);
if (!kernelFile.is_open()) {
std::cout << "Unable to open " << filename << ". " << __FILE__ << ":"
<< __LINE__ << "Please set OPENCL_KERNEL" << std::endl;
exit(1);
}
/*
* Read the kernel file into an output stream.
* Convert this into a char array for passing to OpenCL.
*/
std::ostringstream outputStringStream;
outputStringStream << kernelFile.rdbuf();
std::string srcStdStr = outputStringStream.str();
const char* charSource = srcStdStr.c_str();
kernelFile.close();
// Create the compute program from the source buffer
//
program = clCreateProgramWithSource(context, 1, (const char **) &charSource,
NULL, &clError);
if (!program) {
printf("Error: Failed to create compute program!\n");
exit(1);
}
// Build the program executable
//
clError = clBuildProgram(program, 0, NULL, compile_parameters.c_str(), NULL,
NULL);
/* Get the size of the build log. */
size_t logSize = 0;
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG, 0, NULL,
&logSize);
if (clError != CL_SUCCESS) {
if (logSize > 1) {
char* log = new char[logSize];
clGetProgramBuildInfo(program, device_id, CL_PROGRAM_BUILD_LOG,
logSize, log, NULL);
std::string stringChars(log, logSize);
std::cerr << "Build log:\n " << stringChars << std::endl;
delete[] log;
}
printf("Error: Failed to build program executable!\n");
exit(1);
}
return;
}