-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevices.cpp
82 lines (58 loc) · 1.85 KB
/
devices.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
//#define CL_USE_DEPRECATED_OPENCL_2_0_APIS
#include <utility>
#include <assert.h>
#include <iostream>
#include <fstream>
#include <CL/cl.hpp>
int main(int argc, const char * argv[])
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
assert(platforms.size() > 0);
auto platform = platforms.front();
std::vector<cl::Device> devices;
platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
for(int i = 0; i < devices.size(); ++i) {
std::cout << "dev" << i << "\nvendor: " << devices.at(i).getInfo<CL_DEVICE_VENDOR>()
<< "\nType: " << devices.at(i).getInfo<CL_DEVICE_TYPE>();
}
assert(devices.size() > 0);
auto device = devices.at(0);
auto type = device.getInfo<CL_DEVICE_TYPE>();
auto vendor = device.getInfo<CL_DEVICE_VENDOR>();
auto version = device.getInfo<CL_DEVICE_VERSION>();
std::cout << "\nvendor: " << vendor << "\ntype: " << type << "\n\n";
std::ifstream kecl(argv[1]);
std::string src(std::istreambuf_iterator<char>(kecl), (std::istreambuf_iterator<char>()));
cl::Program::Sources sources(1, std::make_pair(src.c_str(), src.length() + 1));
cl::Context context(device);
cl::Program program(context, sources);
auto err = program.build("-cl-std=CL1.2");
std::cout << "err " << err << "\n";
char buf[16];
buf[0] = 'H';
buf[1] = 'e';
buf[2] = 'l';
buf[3] = 'l';
buf[4] = 'o';
buf[5] = ',';
buf[6] = ' ';
buf[7] = 'W';
buf[8] = 'o';
buf[9] = 'r';
buf[10] = 'l';
buf[11] = 'd';
buf[12] = '\n';
std::cout << buf;
std::cout << "\n";
cl::Buffer mem_buf(context, CL_MEM_WRITE_ONLY | CL_MEM_HOST_READ_ONLY, sizeof(buf));
cl::Kernel kernel(program, "kernel_example", &err);
kernel.setArg(0, mem_buf);
cl::CommandQueue queue(context, device);
queue.enqueueTask(kernel);
queue.enqueueReadBuffer(mem_buf, CL_TRUE, 0, sizeof(buf), buf);
std::cout << err;
std::cout << "\n";
std::cout << buf;
std::cout << "\n";
}