-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathFile.cpp
More file actions
196 lines (180 loc) · 5.88 KB
/
File.cpp
File metadata and controls
196 lines (180 loc) · 5.88 KB
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
// OpenCL SDK includes
#include <CL/Utils/File.hpp>
// STL includes
#include <fstream>
#include <iterator>
#include <algorithm>
#include <iostream>
// whereami includes
#include <whereami.h>
std::string cl::util::read_text_file(const char* const filename,
cl_int* const error)
{
std::ifstream in(filename);
if (in.good())
{
try
{
std::string red((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
if (error != nullptr) *error = CL_SUCCESS;
return red;
} catch (std::bad_alloc&)
{
detail::errHandler(CL_OUT_OF_RESOURCES, error, "Bad allocation!");
return std::string();
}
}
else
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
(std::string("Unable to read ") + filename).c_str());
return std::string();
}
}
std::vector<unsigned char>
cl::util::read_binary_file(const char* const filename, cl_int* const error)
{
std::ifstream in(filename, std::ios::binary);
if (in.good())
{
try
{
std::vector<unsigned char> buffer(
std::istreambuf_iterator<char>(in), {});
if (error != nullptr) *error = CL_SUCCESS;
return buffer;
} catch (std::bad_alloc&)
{
detail::errHandler(CL_OUT_OF_RESOURCES, error, "Bad allocation!");
return std::vector<unsigned char>();
}
}
else
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
(std::string("Unable to read ") + filename).c_str());
return std::vector<unsigned char>();
}
}
cl::Program::Binaries
cl::util::read_binary_files(const std::vector<cl::Device>& devices,
const char* const program_base_name,
cl_int* const error)
{
cl::Program::Binaries binaries(0);
for (const auto& device : devices)
{
string device_name = device.getInfo<CL_DEVICE_NAME>();
string binary_name =
string(program_base_name) + "-" + device_name + ".bin";
binaries.push_back(
cl::util::read_binary_file(binary_name.c_str(), error));
if (error != nullptr)
{
if (*error != CL_SUCCESS)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Not all binaries found!");
return cl::Program::Binaries();
}
}
}
return binaries;
}
cl_int cl::util::write_binaries(const cl::Program::Binaries& binaries,
const std::vector<cl::Device>& devices,
const char* const program_file_name)
{
cl_int error = CL_SUCCESS;
if (binaries.size() == devices.size())
{
try
{
for (auto i = 0U; i < binaries.size(); ++i)
{
string binary_name = string(program_file_name) + "-"
+ devices.at(i).getInfo<CL_DEVICE_NAME>() + ".bin";
std::ofstream out(binary_name, std::ios::binary);
out.write((const char*)binaries[i].data(),
binaries[i].size() * sizeof(char));
}
return error;
} catch (std::bad_alloc&)
{
detail::errHandler(CL_OUT_OF_RESOURCES, &error, "Bad allocation!");
return error;
}
}
else
{
detail::errHandler(CL_INVALID_VALUE, &error,
"Binaries and devices don't match!");
return error;
}
}
std::string cl::util::executable_folder(cl_int* const error)
{
int wai_length = wai_getExecutablePath(NULL, 0, NULL);
if (wai_length == -1)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Unable to query executable path length!");
return "";
}
std::string wai_filename(wai_length, '\0');
int wai_dirname_length = -1;
wai_length = wai_getExecutablePath(&wai_filename.front(), wai_length,
&wai_dirname_length);
if (wai_length == -1 || wai_dirname_length == -1)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Unable to query executable path or folder length!");
return "";
}
return wai_filename.substr(0, wai_dirname_length);
}
std::string cl::util::read_exe_relative_text_file(const char* const filename,
cl_int* const error)
{
cl_int err = CL_SUCCESS;
std::string exe_folder = executable_folder(&err);
if (err != CL_SUCCESS)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Failed to query exe folder!");
return "";
}
std::string result =
read_text_file((exe_folder + "/" + filename).c_str(), &err);
if (err != CL_SUCCESS)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Unable to read file!");
return "";
}
return result;
}
std::vector<unsigned char>
cl::util::read_exe_relative_binary_file(const char* const filename,
cl_int* const error)
{
std::vector<unsigned char> result;
cl_int err = CL_SUCCESS;
std::string exe_folder = executable_folder(&err);
if (err != CL_SUCCESS)
{
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Failed to query exe folder!");
return result;
}
result = read_binary_file((exe_folder + "/" + filename).c_str(), &err);
if (err != CL_SUCCESS)
{
result.clear();
detail::errHandler(CL_UTIL_FILE_OPERATION_ERROR, error,
"Unable to read file!");
return result;
}
return result;
}