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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
/* Tests a kernel create with binary.
Copyright (c) 2018 Julius Ikkala / TUT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "pocl_opencl.h"
#define CL_HPP_ENABLE_EXCEPTIONS
#include <CL/opencl.hpp>
#include <iostream>
#include <numeric>
#ifdef _WIN32
# include "vccompat.hpp"
#endif
std::string read_text_file(const std::string& path)
{
FILE* f = fopen(path.c_str(), "rb");
if(!f)
{
throw std::runtime_error("Unable to open " + path);
}
fseek(f, 0, SEEK_END);
size_t sz = ftell(f);
fseek(f, 0, SEEK_SET);
char* data = new char[sz];
if(fread(data, 1, sz, f) != sz)
{
delete [] data;
throw std::runtime_error("Unable to read " + path);
}
fclose(f);
std::string ret(data, sz);
delete [] data;
return ret;
}
cl::Platform get_platform(unsigned force_platform = 0)
{
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if(platforms.empty())
throw std::runtime_error("No platforms found!");
for(unsigned i = 0; i < platforms.size(); ++i)
{
cl::Platform& p = platforms[i];
std::string name = p.getInfo<CL_PLATFORM_NAME>();
std::cout << i << ": " << name << std::endl;
}
if(force_platform >= platforms.size()) force_platform = 0;
return platforms[force_platform];
}
cl::Device get_device(cl::Platform pl,
[[maybe_unused]] unsigned force_device = 0) {
std::vector<cl::Device> devices;
pl.getDevices(CL_DEVICE_TYPE_ALL, &devices);
if (devices.empty())
throw std::runtime_error("No devices found!");
return devices[0];
}
void exclusive_scan_cpu(const std::vector<int>& input, std::vector<int>& output)
{
output.resize(input.size());
int a = 0;
for(unsigned i = 0; i < input.size(); ++i)
{
output[i] = a;
a += input[i];
}
}
void exclusive_scan_cl(const std::vector<int>& input, std::vector<int>& output)
{
// Fails on POCL, works with AMDGPU-PRO
cl::Platform p = get_platform(0);
cl::Device d = get_device(p);
try {
cl::Context ctx(d);
std::string src =
read_text_file(SRCDIR "/test_flatten_barrier_subs.cl");
cl::Program::Sources sources;
sources.push_back({src.c_str(), src.length()});
cl::Program program(ctx, sources);
program.build({d});
cl::Buffer input_buf(ctx, CL_MEM_READ_WRITE,
sizeof(int) * input.size());
cl::Buffer output_buf(ctx, CL_MEM_READ_WRITE,
sizeof(int) * input.size());
cl::CommandQueue q(ctx, d);
q.enqueueWriteBuffer(input_buf, CL_TRUE, 0, sizeof(int) * input.size(),
input.data());
int numElems = input.size();
#define WG_SIZE 64
int GROUP_BLOCK_SIZE_SCAN = (WG_SIZE << 3);
int GROUP_BLOCK_SIZE_DISTRIBUTE = (WG_SIZE << 2);
int NUM_GROUPS_BOTTOM_LEVEL_SCAN = (numElems + GROUP_BLOCK_SIZE_SCAN - 1) / GROUP_BLOCK_SIZE_SCAN;
int NUM_GROUPS_MID_LEVEL_SCAN = (NUM_GROUPS_BOTTOM_LEVEL_SCAN + GROUP_BLOCK_SIZE_SCAN - 1) / GROUP_BLOCK_SIZE_SCAN;
int NUM_GROUPS_TOP_LEVEL_SCAN = (NUM_GROUPS_MID_LEVEL_SCAN + GROUP_BLOCK_SIZE_SCAN - 1) / GROUP_BLOCK_SIZE_SCAN;
int NUM_GROUPS_BOTTOM_LEVEL_DISTRIBUTE = (numElems + GROUP_BLOCK_SIZE_DISTRIBUTE - 1) / GROUP_BLOCK_SIZE_DISTRIBUTE;
int NUM_GROUPS_MID_LEVEL_DISTRIBUTE = (NUM_GROUPS_BOTTOM_LEVEL_DISTRIBUTE + GROUP_BLOCK_SIZE_DISTRIBUTE - 1) / GROUP_BLOCK_SIZE_DISTRIBUTE;
cl::Buffer devicePartSumsBottomLevel(
ctx, CL_MEM_READ_WRITE, sizeof(int)*NUM_GROUPS_BOTTOM_LEVEL_SCAN
);
cl::Buffer devicePartSumsMidLevel(
ctx, CL_MEM_READ_WRITE, sizeof(int)*NUM_GROUPS_MID_LEVEL_SCAN
);
cl::Kernel bottomLevelScan(program, "scan_exclusive_part_int4");
cl::Kernel topLevelScan(program, "scan_exclusive_int4");
cl::Kernel distributeSums(program, "distribute_part_sum_int4");
bottomLevelScan.setArg(0, input_buf);
bottomLevelScan.setArg(1, output_buf);
bottomLevelScan.setArg(2, numElems);
bottomLevelScan.setArg(3, devicePartSumsBottomLevel);
bottomLevelScan.setArg(4, WG_SIZE * sizeof(cl_int), nullptr);
q.enqueueNDRangeKernel(
bottomLevelScan,
cl::NullRange,
cl::NDRange(NUM_GROUPS_BOTTOM_LEVEL_SCAN * WG_SIZE),
cl::NDRange(WG_SIZE)
);
bottomLevelScan.setArg(0, devicePartSumsBottomLevel);
bottomLevelScan.setArg(1, devicePartSumsBottomLevel);
bottomLevelScan.setArg(2, (cl_uint)NUM_GROUPS_BOTTOM_LEVEL_SCAN);
bottomLevelScan.setArg(3, devicePartSumsMidLevel);
bottomLevelScan.setArg(4, WG_SIZE * sizeof(cl_int), nullptr);
q.enqueueNDRangeKernel(
bottomLevelScan,
cl::NullRange,
cl::NDRange(NUM_GROUPS_MID_LEVEL_SCAN * WG_SIZE),
cl::NDRange(WG_SIZE)
);
topLevelScan.setArg(0, devicePartSumsMidLevel);
topLevelScan.setArg(1, devicePartSumsMidLevel);
topLevelScan.setArg(2, (cl_uint)NUM_GROUPS_MID_LEVEL_SCAN);
topLevelScan.setArg(3, WG_SIZE * sizeof(cl_int), nullptr);
q.enqueueNDRangeKernel(
topLevelScan,
cl::NullRange,
cl::NDRange(NUM_GROUPS_TOP_LEVEL_SCAN * WG_SIZE),
cl::NDRange(WG_SIZE)
);
distributeSums.setArg(0, devicePartSumsMidLevel);
distributeSums.setArg(1, devicePartSumsBottomLevel);
distributeSums.setArg(2, (cl_uint)NUM_GROUPS_BOTTOM_LEVEL_SCAN);
q.enqueueNDRangeKernel(
distributeSums,
cl::NullRange,
cl::NDRange(NUM_GROUPS_MID_LEVEL_DISTRIBUTE * WG_SIZE),
cl::NDRange(WG_SIZE)
);
distributeSums.setArg(0, devicePartSumsBottomLevel);
distributeSums.setArg(1, output_buf);
distributeSums.setArg(2, (cl_uint)numElems);
q.enqueueNDRangeKernel(
distributeSums,
cl::NullRange,
cl::NDRange(NUM_GROUPS_BOTTOM_LEVEL_DISTRIBUTE * WG_SIZE),
cl::NDRange(WG_SIZE)
);
output.resize(input.size());
q.enqueueReadBuffer(
output_buf, CL_TRUE, 0, sizeof(int)*input.size(), output.data()
);
q.finish();
} catch (cl::Error &err) {
std::cerr << "ERROR: " << err.what() << "(" << err.err() << ")"
<< std::endl;
return;
}
p.unloadCompiler();
}
std::vector<int> generate_hits(unsigned n)
{
std::vector<int> res;
res.reserve(n);
for (unsigned i = 0; i < n; ++i)
res.push_back(rand() & 1);
return res;
}
template<typename T>
void print_vec(const std::vector<T>& t)
{
for(unsigned i = 0; i < t.size(); ++i)
{
if(i != 0) std::cout << ", ";
std::cout << t[i];
}
std::cout << std::endl;
}
int main()
{
std::vector<int> hits = generate_hits(100);
std::vector<int> indices;
exclusive_scan_cpu(hits, indices);
std::vector<int> cl_indices;
exclusive_scan_cl(hits, cl_indices);
if (indices == cl_indices) {
std::cout << "OK: CL gave correct results" << std::endl;
return EXIT_SUCCESS;
} else {
std::cout << "ERROR: CL gave wrong results:" << std::endl;
print_vec(cl_indices);
std::cout << "expected:" << std::endl;
print_vec(indices);
return EXIT_FAILURE;
}
}
|