Skip to content

Commit 313d9a9

Browse files
Add C++ variant for binaries sample (#65)
* Add C++ variant for binaries sample * file utils were expanded to support reading and writing binaries * Update samples/core/binaries/main.cpp Include climits to enable Linux build. Co-authored-by: Ben Ashbaugh <ben.ashbaugh@intel.com> * Improved error handling for file utils. * Improved error reporting for binary file reading. --------- Co-authored-by: Ben Ashbaugh <ben.ashbaugh@intel.com>
1 parent f7d5ba4 commit 313d9a9

6 files changed

Lines changed: 372 additions & 40 deletions

File tree

lib/include/CL/Utils/File.hpp

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,30 @@
11
#pragma once
22

33
// OpenCL SDK includes
4-
#include <CL/Utils/Utils.hpp>
4+
#include "OpenCLUtilsCpp_Export.h"
5+
6+
#include <CL/Utils/Error.hpp>
7+
8+
// OpenCL includes
9+
#include <CL/opencl.hpp>
510

6-
// STL includes
7-
#include <fstream>
8-
#include <string>
911

1012
namespace cl {
1113
namespace util {
12-
// Scott Meyers, Effective STL, Addison-Wesley Professional, 2001, Item 29
13-
// with error handling
14-
UTILSCPP_EXPORT
15-
std::string read_text_file(const char* const filename, cl_int* const error)
16-
{
17-
std::ifstream in(filename);
18-
if (in.good())
19-
{
20-
try
21-
{
22-
std::string red((std::istreambuf_iterator<char>(in)),
23-
std::istreambuf_iterator<char>());
24-
if (in.good() && in.eof())
25-
{
26-
if (error != nullptr) *error = CL_SUCCESS;
27-
return red;
28-
}
29-
else
30-
{
31-
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
32-
"File read error!");
33-
return std::string();
34-
}
35-
} catch (std::bad_alloc& ex)
36-
{
37-
detail::errHandler(CL_OUT_OF_RESOURCES, error,
38-
"Bad allocation!");
39-
return std::string();
40-
}
41-
}
42-
else
43-
{
44-
detail::errHandler(CL_INVALID_VALUE, error, "No file!");
45-
return std::string();
46-
}
47-
}
14+
15+
std::string UTILSCPP_EXPORT read_text_file(const char* const filename,
16+
cl_int* const error);
17+
18+
std::vector<unsigned char> UTILSCPP_EXPORT
19+
read_binary_file(const char* const filename, cl_int* const error);
20+
21+
Program::Binaries UTILSCPP_EXPORT
22+
read_binary_files(const std::vector<cl::Device>& devices,
23+
const char* const program_file_name, cl_int* const error);
24+
25+
cl_int UTILSCPP_EXPORT
26+
write_binaries(const cl::Program::Binaries& binaries,
27+
const std::vector<cl::Device>& devices,
28+
const char* const program_file_name);
4829
}
4930
}

lib/include/CL/Utils/Utils.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <CL/Utils/Device.hpp>
1010
#include <CL/Utils/Context.hpp>
1111
#include <CL/Utils/Event.hpp>
12+
#include <CL/Utils/File.hpp>
1213

1314
// OpenCL includes
1415
#include <CL/opencl.hpp>

lib/src/Utils/File.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}

lib/src/Utils/Utils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
#include "Platform.cpp"
66
#include "Device.cpp"
77
#include "Context.cpp"
8+
#include "File.cpp"

samples/core/binaries/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ add_sample(
1818
VERSION 300
1919
SOURCES main.c
2020
KERNELS Collatz.cl)
21+
22+
add_sample(
23+
TEST
24+
TARGET binariescpp
25+
VERSION 300
26+
SOURCES main.cpp
27+
KERNELS Collatz.cl)
28+

0 commit comments

Comments
 (0)