6#ifndef _CPG_OPENCL_SYCL_HPP
7#define _CPG_OPENCL_SYCL_HPP
15#ifndef __SYCL_INTERNAL_API
16 #define __SYCL_INTERNAL_API
32 template<
typename =
int>
35 std::ifstream kernelFile(fileName, std::ios::in);
37 if (!kernelFile.is_open())
39 std::cout <<
"Failed to open file for reading: "
45 std::ostringstream oss;
46 oss << kernelFile.rdbuf();
47 std::string srcStdStr = oss.str();
49 if(srcStdStr.back() !=
'\n') srcStdStr.push_back(
'\n');
51 if(srcStdStr[0] == (
char)0xEF && srcStdStr[1] == (
char)0xBB && srcStdStr[2] == (
char)0xBF)
52 return std::string( &srcStdStr[3] );
57 template<
typename StringType>
60 std::size_t lines = 0;
62 for(
auto c: src)
if(c ==
'\n') ++lines;
67 template<
typename FileNameType,
typename CountType, std::size_t N,
70 file_names, std::array<CountType, N>
const& line_counts, StringType error_line_str)
72 std::size_t error_line = std::stoll(error_line_str);
73 std::size_t error_file_index = 0;
75 for(std::size_t i = 1; i < N; ++i)
77 if(line_counts[i-1] < error_line &&
78 error_line <= line_counts[i])
80 error_line -= line_counts[i-1];
81 error_file_index = i;
break;
85 std::ostringstream oss;
88 return std::tuple{ oss.str(), file_names[error_file_index] };
91 template<
typename CharType>
92 std::basic_string<CharType>
replace_string(std::basic_string<CharType> target,
93 std::basic_string<CharType>
const& str_find, std::basic_string<CharType>
const& str_replace)
95 auto pos = target.find(str_find);
97 if(pos != std::basic_string<CharType>::npos)
98 return target.replace(pos, str_find.size(), str_replace);
103 template<
typename FileNameType,
typename CountType, std::size_t N,
106 file_names, std::array<CountType, N>
const& line_counts, StringType error_msg)
109 std::regex pattern{ R
"((\d+):(\d+):(\d+):)" };
111 auto result = error_msg;
113 for(std::sregex_iterator p{error_msg.begin(), error_msg.end(), pattern};
114 p != std::sregex_iterator{}; ++p)
120 std::ostringstream oss;
121 oss<<
"[" << filename <<
"] "
131 template<
typename... OpenCLFiles>
132 cl_program
CreateProgram(cl_context context, cl_device_id device, OpenCLFiles... opencl_files)
137 constexpr std::size_t FileCount =
sizeof...(opencl_files);
139 std::array src_files{ opencl_files... };
141 std::array std_file_contents {
file_contents(opencl_files) ... };
143 auto srcStr = cgt::for_stallion<FileCount>(
146 return std::array{ std_file_contents[i].c_str()... };
150 clCreateProgramWithSource(context, FileCount, srcStr.data(), NULL, NULL);
158 errNum = clBuildProgram(program, 0, NULL, NULL, NULL, NULL);
160 if (errNum != CL_SUCCESS)
167 return std::array{
count_lines(std_file_contents[i])... };
170 for(std::size_t i = 1; i < FileCount; ++i)
171 line_counts[i] += line_counts[i-1];
174 clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0,
nullptr, &N);
176 std::string buildLog(N,
' ');
178 clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, N, buildLog.data(), NULL);
184 clReleaseProgram(program);
195 template<
typename SycleQueue,
typename... OpenCLFiles>
198 auto context = queue.get_context();
199 auto device = queue.get_device();
203 opencl_files...), clReleaseProgram);
206 clBuildProgram(program.get(), 0,
nullptr,
nullptr,
nullptr,
nullptr);
211 template<
typename ProgramType>
215 clCreateKernel(program.get(), kernel_name,
nullptr), clReleaseKernel);
220 return sycl::kernel{ opencl_kernel.get(), queue.get_context() };
auto count_lines(StringType &src)
cl_program CreateProgram(cl_context context, cl_device_id device, OpenCLFiles... opencl_files)
auto parse_error_message(std::array< FileNameType, N > const &file_names, std::array< CountType, N > const &line_counts, StringType error_msg)
std::basic_string< CharType > replace_string(std::basic_string< CharType > target, std::basic_string< CharType > const &str_find, std::basic_string< CharType > const &str_replace)
std::string file_contents(const char *fileName)
auto error_line_file_name(std::array< FileNameType, N > const &file_names, std::array< CountType, N > const &line_counts, StringType error_line_str)
auto sycl_kernel(auto &opencl_kernel, auto &queue)
auto create_program(SycleQueue &queue, OpenCLFiles... opencl_files)
sycl::access::mode access
auto create_kernel(ProgramType &program, const char *kernel_name)
sycl::info::device device_info
auto raii_create_object(PointerType object_ptr, FuncType func)
std::integer_sequence< std::common_type_t< std::remove_cvref_t< decltype(Indices)>... >, Indices... > sequence