-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOCL_Device.cpp
More file actions
106 lines (103 loc) · 3.11 KB
/
Copy pathOCL_Device.cpp
File metadata and controls
106 lines (103 loc) · 3.11 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
#include "OCL_Device.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <memory>
cl_context ocl::creteContext()
{
cl_int errNum;
cl_uint numPlatforms;
cl_context context = NULL;
errNum = clGetPlatformIDs(0, NULL, &numPlatforms);
std::unique_ptr<cl_platform_id[]> Platform_Id(new cl_platform_id[numPlatforms]);
errNum = clGetPlatformIDs(numPlatforms, Platform_Id.get(), NULL);
if (CL_SUCCESS != errNum || numPlatforms <= 0) {
std::cerr << "Failed to find any OpenCL platforms." << std::endl;
return NULL;
}
cl_context_properties contextProperties[] = {
CL_CONTEXT_PLATFORM,
//select first opencl platform in default,
//modify this in manual to select other platform.
(cl_context_properties)Platform_Id[0],
0
};
context = clCreateContextFromType(contextProperties,
CL_DEVICE_TYPE_GPU,
NULL, NULL, &errNum);
if (CL_SUCCESS != errNum) {
std::cout << "Could not create GPU context,try CPU..." << std::endl;
context = clCreateContextFromType(contextProperties,
CL_DEVICE_TYPE_CPU,
NULL, NULL, &errNum);
if (CL_SUCCESS != errNum) {
std::cerr << "Failed to create an OpenCL GPU or CPU context.";
return NULL;
}
}
return context;
}
cl_command_queue ocl::createCommandQueue(cl_context context, cl_device_id* device)
{
cl_int errNum;
cl_device_id * devices;
cl_command_queue commandQueue = NULL;
std::size_t deviceBufferSize = -1;
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES, 0, NULL,
&deviceBufferSize);
if (CL_SUCCESS != errNum) {
std::cerr << "Failed call to clGetContextInfo(...,GL_CONTEXT_DEVICES,...)";
return NULL;
}
if (deviceBufferSize <= 0) {
std::cerr << "No devices avaliable.";
return NULL;
}
devices = new cl_device_id[deviceBufferSize / sizeof(cl_device_id)];
errNum = clGetContextInfo(context, CL_CONTEXT_DEVICES,
deviceBufferSize, devices, NULL);
if (CL_SUCCESS != errNum) {
std::cerr << "Failed to get device IDs";
return NULL;
}
commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);
if (NULL == commandQueue) {
std::cerr << "Failed to create commandQueue for device 0";
return NULL;
}
*device = devices[0];
delete[]devices;
return commandQueue;
}
cl_program ocl::CreateProgram(cl_context context, cl_device_id device, const char *fileName)
{
cl_int errNum;
cl_program program;
std::ifstream kernelFile(fileName, std::ios::in);
if (!kernelFile.is_open()) {
std::cerr << "Failed to open file for reading: " << fileName
<< std::endl;
return NULL;
}
std::ostringstream oss;
oss << kernelFile.rdbuf();
std::string srcStdStr = oss.str();
const char *srcStr = srcStdStr.c_str();
program = clCreateProgramWithSource(context, 1, (const char**)&srcStr,
NULL, NULL);
if (NULL == program) {
std::cerr << "Failed to create CL program from source." << std::endl;
return NULL;
}
errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
if (CL_SUCCESS != errNum) {
char buildLog[16384];
clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG,
sizeof(buildLog), buildLog, NULL);
std::cerr << "Error in kernel: " << std::endl;
std::cerr << buildLog;
clReleaseProgram(program);
return NULL;
}
return program;
}