-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOpenCLFramework.cpp
70 lines (50 loc) · 1.76 KB
/
OpenCLFramework.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
#define __CL_ENABLE_EXCEPTIONS
#include "OpenCLFramework.h"
#include <iostream>
#include <sstream>
#include <fstream>
/**
OpenCL Basic Functions by Evil-Knievel
**/
std::string helloStr = "__kernel void hello(void) { }\n";
using namespace std;
OpenCLFramework::OpenCLFramework()
{
}
OpenCLFramework::~OpenCLFramework()
{
}
std::string OpenCLFramework::intToHex(unsigned int p){
char str[64];
sprintf(str, "%x", p);
return std::string(str);
}
cl::Program OpenCLFramework::buildProgramFromSource(cl::Context* context, std::string filename, std::string buildOptions) {
// Read source file
std::ifstream sourceFile(filename.c_str());
if (sourceFile.fail())
throw cl::Error(1, "Failed to open OpenCL source file");
std::string sourceCode(
std::istreambuf_iterator<char>(sourceFile),
(std::istreambuf_iterator<char>()));
//sourceCode = helloStr;
std::cout << "(Kernel) source code size in bytes: " << sourceCode.length() << std::endl;
std::cout << "(Kernel) starting to build for GPU device." << std::endl;
cl::Program::Sources source(1, std::make_pair(sourceCode.c_str(), strlen(sourceCode.c_str())));
std::cout << "(Kernel) cl source object has size: " << source.size() << std::endl;
// Make program of the source code in the context
cl::Program program = cl::Program(*context, source);
VECTOR_CLASS<cl::Device> devices = context->getInfo<CL_CONTEXT_DEVICES>();
// Build program for these specific devices
try{
program.build(devices, buildOptions.c_str());
}
catch (cl::Error error) {
//if(error.err() == CL_BUILD_PROGRAM_FAILURE) {
std::cout << "Build log:" << std::endl << program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(devices[0]) << std::endl;
//}
throw error;
}
std::cout << "(Kernel) kernel has compiled fine." << std::endl;
return program;
}