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
|
/* printf-opencl.cc - Test cases for SPIR-V printf (the host program).
Copyright (c) 2022 Pekka Jääskeläinen / Parmance
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 <CL/cl.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
#include <string.h>
#define CHECK_ERR(X) do { \
if ((X) != CL_SUCCESS) { \
std::cerr << #X "ERROR at " << __LINE__ << std::endl; \
abort(); \
} } while (0)
std::vector<std::string> getKernelNames(cl_program Program) {
size_t KernelNamesSize;
CHECK_ERR(clGetProgramInfo(Program, CL_PROGRAM_KERNEL_NAMES,
0, NULL,
&KernelNamesSize));
char Names[KernelNamesSize + 1];
CHECK_ERR(clGetProgramInfo(Program, CL_PROGRAM_KERNEL_NAMES,
KernelNamesSize, &Names[0],
&KernelNamesSize));
// The specs doesn't ensure a null terminated string.
Names[KernelNamesSize] = 0;
std::vector<std::string> KernelNames;
//Construct a stream from the string
std::stringstream StreamData(Names);
std::string Kern;
while (std::getline(StreamData, Kern, ';')) {
KernelNames.push_back(Kern);
}
return KernelNames;
}
int main(int Argc, char *Argv[]) {
cl_uint Count;
cl_platform_id Platform;
clGetPlatformIDs (1, &Platform, &Count);
if (Count == 0)
abort();
cl_context_properties Properties[] =
{CL_CONTEXT_PLATFORM, (cl_context_properties)Platform, 0};
cl_context Context =
clCreateContextFromType (Properties, CL_DEVICE_TYPE_ALL,
NULL, NULL, NULL);
cl_uint NumDevices;
cl_int Err = clGetDeviceIDs(Platform, CL_DEVICE_TYPE_ALL, 0, NULL,
&NumDevices);
if (Err != CL_SUCCESS || NumDevices == 0)
return EXIT_FAILURE;
cl_device_id Device;
Err = clGetDeviceIDs (Platform, CL_DEVICE_TYPE_ALL, 1, &Device, NULL);
if (Err != CL_SUCCESS)
return EXIT_FAILURE;
std::ifstream File(SRCDIR "/printf-kernels.spv", std::ios::binary | std::ios::ate);
std::streamsize Binsize = File.tellg();
File.seekg(0, std::ios::beg);
std::vector<char> Binary(Binsize);
if (!File.read(Binary.data(), Binsize))
return EXIT_FAILURE;
cl_program Program =
clCreateProgramWithIL (Context, (const void *)Binary.data(), Binsize,
&Err);
CHECK_ERR(Err);
Err = clBuildProgram (Program, 0, NULL, "", NULL, NULL);
CHECK_ERR(Err);
cl_command_queue Queue =
clCreateCommandQueueWithProperties(Context, Device, 0, &Err);
CHECK_ERR(Err);
auto KernelNames = getKernelNames(Program);
for (auto KernelName : KernelNames) {
std::cout << "Testing '" << KernelName << "'" << std::endl;
cl_kernel Kernel = clCreateKernel (Program, KernelName.c_str(), NULL);
int IO = 1;
char HostStr[] = "String from the HOST\n\0";
cl_mem Buffer =
clCreateBuffer (Context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
sizeof(int), &IO, &Err);
CHECK_ERR(Err);
Err = clSetKernelArg (Kernel, 0, sizeof (cl_mem), (void *)&Buffer);
CHECK_ERR(Err);
cl_mem Buffer2;
if (KernelName == "spirv_host_defined_strings") {
Buffer2 =
clCreateBuffer (Context,
CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR,
strlen(HostStr) + 1, &HostStr, &Err);
Err = clSetKernelArg (Kernel, 1, sizeof (cl_mem), (void *)&Buffer2);
CHECK_ERR(Err);
}
const size_t LocalSize[] = {1, 1, 1};
const size_t GlobalSize[] = {1, 1, 1};
Err = clEnqueueNDRangeKernel (Queue, Kernel, 1, NULL, LocalSize,
GlobalSize, 0, NULL, NULL);
CHECK_ERR(Err);
Err = clEnqueueReadBuffer (Queue, Buffer, CL_TRUE, 0,
sizeof(int), &IO, 0, NULL, NULL);
CHECK_ERR(Err);
clFinish(Queue);
clReleaseMemObject(Buffer);
std::cout << "IO == " << IO << std::endl << std::endl;
}
return EXIT_SUCCESS;
}
|