|
| 1 | +// OpenCL SDK includes |
| 2 | +#include <CL/Utils/File.hpp> |
| 3 | + |
| 4 | +// STL includes |
| 5 | +#include <fstream> |
| 6 | +#include <iterator> |
| 7 | +#include <algorithm> |
| 8 | +#include <iostream> |
| 9 | + |
| 10 | +std::string cl::util::read_text_file(const char* const filename, |
| 11 | + cl_int* const error) |
| 12 | +{ |
| 13 | + std::ifstream in(filename); |
| 14 | + if (in.good()) |
| 15 | + { |
| 16 | + try |
| 17 | + { |
| 18 | + std::string red((std::istreambuf_iterator<char>(in)), |
| 19 | + std::istreambuf_iterator<char>()); |
| 20 | + |
| 21 | + if (error != nullptr) *error = CL_SUCCESS; |
| 22 | + return red; |
| 23 | + |
| 24 | + } catch (std::bad_alloc&) |
| 25 | + { |
| 26 | + detail::errHandler(CL_OUT_OF_RESOURCES, error, "Bad allocation!"); |
| 27 | + return std::string(); |
| 28 | + } |
| 29 | + } |
| 30 | + else |
| 31 | + { |
| 32 | + detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error, "No file!"); |
| 33 | + return std::string(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +std::vector<unsigned char> |
| 38 | +cl::util::read_binary_file(const char* const filename, cl_int* const error) |
| 39 | +{ |
| 40 | + std::ifstream in(filename, std::ios::binary); |
| 41 | + if (in.good()) |
| 42 | + { |
| 43 | + try |
| 44 | + { |
| 45 | + std::vector<unsigned char> buffer( |
| 46 | + std::istreambuf_iterator<char>(in), {}); |
| 47 | + if (error != nullptr) *error = CL_SUCCESS; |
| 48 | + return buffer; |
| 49 | + } catch (std::bad_alloc&) |
| 50 | + { |
| 51 | + detail::errHandler(CL_OUT_OF_RESOURCES, error, "Bad allocation!"); |
| 52 | + return std::vector<unsigned char>(); |
| 53 | + } |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error, "No file!"); |
| 58 | + return std::vector<unsigned char>(); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | + |
| 63 | +cl::Program::Binaries |
| 64 | +cl::util::read_binary_files(const std::vector<cl::Device>& devices, |
| 65 | + const char* const program_base_name, |
| 66 | + cl_int* const error) |
| 67 | +{ |
| 68 | + cl::Program::Binaries binaries(0); |
| 69 | + |
| 70 | + for (const auto& device : devices) |
| 71 | + { |
| 72 | + string device_name = device.getInfo<CL_DEVICE_NAME>(); |
| 73 | + string binary_name = |
| 74 | + string(program_base_name) + "-" + device_name + ".bin"; |
| 75 | + |
| 76 | + binaries.push_back( |
| 77 | + cl::util::read_binary_file(binary_name.c_str(), error)); |
| 78 | + if (error != nullptr) |
| 79 | + { |
| 80 | + if (*error != CL_SUCCESS) |
| 81 | + { |
| 82 | + detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error, |
| 83 | + "Not all binaries found!"); |
| 84 | + return cl::Program::Binaries(); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return binaries; |
| 89 | +} |
| 90 | + |
| 91 | +cl_int cl::util::write_binaries(const cl::Program::Binaries& binaries, |
| 92 | + const std::vector<cl::Device>& devices, |
| 93 | + const char* const program_file_name) |
| 94 | +{ |
| 95 | + cl_int error = CL_SUCCESS; |
| 96 | + if (binaries.size() == devices.size()) |
| 97 | + { |
| 98 | + try |
| 99 | + { |
| 100 | + for (auto i = 0; i < binaries.size(); ++i) |
| 101 | + { |
| 102 | + string binary_name = string(program_file_name) + "-" |
| 103 | + + devices.at(i).getInfo<CL_DEVICE_NAME>() + ".bin"; |
| 104 | + |
| 105 | + std::ofstream out(binary_name, std::ios::binary); |
| 106 | + out.write((const char*)binaries[i].data(), |
| 107 | + binaries[i].size() * sizeof(char)); |
| 108 | + } |
| 109 | + return error; |
| 110 | + } catch (std::bad_alloc&) |
| 111 | + { |
| 112 | + detail::errHandler(CL_OUT_OF_RESOURCES, &error, "Bad allocation!"); |
| 113 | + return error; |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + detail::errHandler(CL_INVALID_VALUE, &error, |
| 119 | + "Binaries and devices don't match!"); |
| 120 | + return error; |
| 121 | + } |
| 122 | +} |
0 commit comments