-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathmain.cpp
More file actions
127 lines (103 loc) · 3.92 KB
/
Copy pathmain.cpp
File metadata and controls
127 lines (103 loc) · 3.92 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
// Copyright (c) 2019-2026 Ben Ashbaugh
//
// SPDX-License-Identifier: MIT
*/
#include <popl/popl.hpp>
#include <CL/opencl.hpp>
#include <fstream>
#include <string>
#include "util.hpp"
int main(
int argc,
char** argv )
{
int platformIndex = 0;
int deviceIndex = 0;
std::string fileName("sample_kernel.cl");
std::string kernelName("Test");
std::string buildOptions;
size_t gwx = 512;
{
popl::OptionParser op("Supported Options");
op.add<popl::Value<int>>("p", "platform", "Platform Index", platformIndex, &platformIndex);
op.add<popl::Value<int>>("d", "device", "Device Index", deviceIndex, &deviceIndex);
op.add<popl::Value<std::string>>("", "file", "Kernel File Name", fileName, &fileName);
op.add<popl::Value<std::string>>("", "name", "Kernel Name", kernelName, &kernelName);
op.add<popl::Value<std::string>>("", "options", "Program Build Options", buildOptions, &buildOptions);
op.add<popl::Value<size_t>>("", "gwx", "Global Work Size", gwx, &gwx);
bool printUsage = false;
try {
op.parse(argc, argv);
} catch (std::exception& e) {
fprintf(stderr, "Error: %s\n\n", e.what());
printUsage = true;
}
if (printUsage || !op.unknown_options().empty() || !op.non_option_args().empty()) {
fprintf(stderr,
"Usage: kernelfromfile [options]\n"
"%s", op.help().c_str());
return -1;
}
}
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (!checkPlatformIndex(platforms, platformIndex)) {
return -1;
}
printf("Running on platform: %s\n",
platforms[platformIndex].getInfo<CL_PLATFORM_NAME>().c_str() );
std::vector<cl::Device> devices;
platforms[platformIndex].getDevices(CL_DEVICE_TYPE_ALL, &devices);
printf("Running on device: %s\n",
devices[deviceIndex].getInfo<CL_DEVICE_NAME>().c_str() );
cl::Context context{devices[deviceIndex]};
cl::CommandQueue commandQueue{context, devices[deviceIndex]};
printf("Reading program source from file: %s\n", fileName.c_str() );
std::string kernelString = readStringFromFile(fileName.c_str());
printf("Building program with build options: %s\n",
buildOptions.empty() ? "(none)" : buildOptions.c_str() );
cl::Program program{ context, kernelString };
program.build(buildOptions.c_str());
for( auto& device : program.getInfo<CL_PROGRAM_DEVICES>() )
{
printf("Program build log for device %s:\n",
device.getInfo<CL_DEVICE_NAME>().c_str() );
printf("%s\n",
program.getBuildInfo<CL_PROGRAM_BUILD_LOG>(device).c_str() );
}
printf("Creating kernel: %s\n", kernelName.c_str() );
cl::Kernel kernel = cl::Kernel{ program, kernelName.c_str() };
cl::Buffer deviceMemDst = cl::Buffer{
context,
CL_MEM_ALLOC_HOST_PTR,
gwx * sizeof( cl_uint ) };
// execution
kernel.setArg(0, deviceMemDst);
commandQueue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
cl::NDRange{gwx});
// verify results by printing the first few values
if (gwx > 3) {
auto ptr = (const cl_uint*)commandQueue.enqueueMapBuffer(
deviceMemDst,
CL_TRUE,
CL_MAP_READ,
0,
gwx * sizeof( cl_uint ) );
printf("First few values:\n"
" [0] = 0x%08X (as hex) = %u (as int) = %.2f (as float)\n"
" [1] = 0x%08X (as hex) = %u (as int) = %.2f (as float)\n"
" [2] = 0x%08X (as hex) = %u (as int) = %.2f (as float)\n",
ptr[0], ptr[0], *((float*)&ptr[0]),
ptr[1], ptr[1], *((float*)&ptr[1]),
ptr[2], ptr[2], *((float*)&ptr[2]));
commandQueue.enqueueUnmapMemObject(
deviceMemDst,
(void*)ptr );
}
commandQueue.finish();
printf("Done.\n");
return 0;
}