-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cpp
204 lines (191 loc) · 8.44 KB
/
Util.cpp
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
197
198
199
200
201
202
203
204
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <string>
#include <map>
#include "Util.h"
void PrintPlatformInfo(cl_platform_id platformId)
{
cl_int err = 0;
// Get Required Size
size_t length;
err = clGetPlatformInfo(platformId, CL_PLATFORM_NAME, NULL, NULL, &length);
CHECK_OPENCL_ERROR(err);
char* sInfo = new char[length];
err = clGetPlatformInfo(platformId, CL_PLATFORM_NAME, length, sInfo, NULL);
CHECK_OPENCL_ERROR(err);
printf("%s\n", sInfo);
delete[] sInfo;
}
void PrintDeviceInfo(cl_device_id deviceId)
{
cl_int err = 0;
// Get Required Size
size_t length;
err = clGetDeviceInfo(deviceId, CL_DEVICE_NAME, NULL, NULL, &length);
// Get actual device name
char* sInfo = new char[length];
err = clGetDeviceInfo(deviceId, CL_DEVICE_NAME, length, sInfo, NULL);
printf("%s\n", sInfo);
cl_ulong size;
err = clGetDeviceInfo(deviceId, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(size),
&size, NULL);
CHECK_OPENCL_ERROR(err);
printf("Total device memory: %llu MB\n", size >> 20);
err = clGetDeviceInfo(deviceId, CL_DEVICE_MAX_MEM_ALLOC_SIZE, sizeof(size),
&size, NULL);
CHECK_OPENCL_ERROR(err);
printf("Maximum buffer size: %llu MB\n", size >> 20);
delete[] sInfo;
}
std::string GetFileContents(std::string filename)
{
std::ifstream in(filename, std::ios::in | std::ios::binary);
if (in)
{
std::string contents;
in.seekg(0, std::ios::end);
contents.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&contents[0], contents.size());
in.close();
return(contents);
}
return "";
}
bool FileExists(std::string filename)
{
FILE* file;
if ((file = fopen(filename.c_str(), "r")))
{
fclose(file);
return true;
}
return false;
}
void CHECK_OPENCL_ERROR(cl_int err)
{
if (err != CL_SUCCESS)
{
switch (err)
{
case CL_DEVICE_NOT_FOUND:
printf("CL_DEVICE_NOT_FOUND\n"); throw std::runtime_error("OpenCL error");
case CL_DEVICE_NOT_AVAILABLE:
printf("CL_DEVICE_NOT_AVAILABLE\n"); throw std::runtime_error("OpenCL error");
case CL_COMPILER_NOT_AVAILABLE:
printf("CL_COMPILER_NOT_AVAILABLE\n"); throw std::runtime_error("OpenCL error");
case CL_MEM_OBJECT_ALLOCATION_FAILURE:
printf("CL_MEM_OBJECT_ALLOCATION_FAILURE\n"); throw std::runtime_error("OpenCL error");
case CL_OUT_OF_RESOURCES:
printf("CL_OUT_OF_RESOURCES\n"); throw std::runtime_error("OpenCL error");
case CL_OUT_OF_HOST_MEMORY:
printf("CL_OUT_OF_HOST_MEMORY\n"); throw std::runtime_error("OpenCL error");
case CL_PROFILING_INFO_NOT_AVAILABLE:
printf("CL_PROFILING_INFO_NOT_AVAILABLE\n"); throw std::runtime_error("OpenCL error");
case CL_MEM_COPY_OVERLAP:
printf("CL_MEM_COPY_OVERLAP\n"); throw std::runtime_error("OpenCL error");
case CL_IMAGE_FORMAT_MISMATCH:
printf("CL_IMAGE_FORMAT_MISMATCH\n"); throw std::runtime_error("OpenCL error");
case CL_IMAGE_FORMAT_NOT_SUPPORTED:
printf("CL_IMAGE_FORMAT_NOT_SUPPORTED\n"); throw std::runtime_error("OpenCL error");
case CL_BUILD_PROGRAM_FAILURE:
printf("CL_BUILD_PROGRAM_FAILURE\n"); throw std::runtime_error("OpenCL error");
case CL_MAP_FAILURE:
printf("CL_MAP_FAILURE\n"); throw std::runtime_error("OpenCL error");
case CL_MISALIGNED_SUB_BUFFER_OFFSET:
printf("CL_MISALIGNED_SUB_BUFFER_OFFSET\n"); throw std::runtime_error("OpenCL error");
case CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST:
printf("CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST\n"); throw std::runtime_error("OpenCL error");
case CL_COMPILE_PROGRAM_FAILURE:
printf("CL_COMPILE_PROGRAM_FAILURE\n"); throw std::runtime_error("OpenCL error");
case CL_LINKER_NOT_AVAILABLE:
printf("CL_LINKER_NOT_AVAILABLE\n"); throw std::runtime_error("OpenCL error");
case CL_LINK_PROGRAM_FAILURE:
printf("CL_LINK_PROGRAM_FAILURE\n"); throw std::runtime_error("OpenCL error");
case CL_DEVICE_PARTITION_FAILED:
printf("CL_DEVICE_PARTITION_FAILED\n"); throw std::runtime_error("OpenCL error");
case CL_KERNEL_ARG_INFO_NOT_AVAILABLE:
printf("CL_KERNEL_ARG_INFO_NOT_AVAILABLE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_VALUE:
printf("CL_INVALID_VALUE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_DEVICE_TYPE:
printf("CL_INVALID_DEVICE_TYPE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_PLATFORM:
printf("CL_INVALID_PLATFORM\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_DEVICE:
printf("CL_INVALID_DEVICE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_CONTEXT:
printf("CL_INVALID_CONTEXT\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_QUEUE_PROPERTIES:
printf("CL_INVALID_QUEUE_PROPERTIES\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_COMMAND_QUEUE:
printf("CL_INVALID_COMMAND_QUEUE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_HOST_PTR:
printf("CL_INVALID_HOST_PTR\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_MEM_OBJECT:
printf("CL_INVALID_MEM_OBJECT\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR:
printf("CL_INVALID_IMAGE_FORMAT_DESCRIPTOR\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_IMAGE_SIZE:
printf("CL_INVALID_IMAGE_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_SAMPLER:
printf("CL_INVALID_SAMPLER\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_BINARY:
printf("CL_INVALID_BINARY\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_BUILD_OPTIONS:
printf("CL_INVALID_BUILD_OPTIONS\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_PROGRAM:
printf("CL_INVALID_PROGRAM\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_PROGRAM_EXECUTABLE:
printf("CL_INVALID_PROGRAM_EXECUTABLE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_KERNEL_NAME:
printf("CL_INVALID_KERNEL_NAME\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_KERNEL_DEFINITION:
printf("CL_INVALID_KERNEL_DEFINITION\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_KERNEL:
printf("CL_INVALID_KERNEL\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_ARG_INDEX:
printf("CL_INVALID_ARG_INDEX\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_ARG_VALUE:
printf("CL_INVALID_ARG_VALUE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_ARG_SIZE:
printf("CL_INVALID_ARG_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_KERNEL_ARGS:
printf("CL_INVALID_KERNEL_ARGS\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_WORK_DIMENSION:
printf("CL_INVALID_WORK_DIMENSION\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_WORK_GROUP_SIZE:
printf("CL_INVALID_WORK_GROUP_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_WORK_ITEM_SIZE:
printf("CL_INVALID_WORK_ITEM_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_GLOBAL_OFFSET:
printf("CL_INVALID_GLOBAL_OFFSET\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_EVENT_WAIT_LIST:
printf("CL_INVALID_EVENT_WAIT_LIST\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_EVENT:
printf("CL_INVALID_EVENT\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_OPERATION:
printf("CL_INVALID_OPERATION\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_GL_OBJECT:
printf("CL_INVALID_GL_OBJECT\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_BUFFER_SIZE:
printf("CL_INVALID_BUFFER_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_MIP_LEVEL:
printf("CL_INVALID_MIP_LEVEL\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_GLOBAL_WORK_SIZE:
printf("CL_INVALID_GLOBAL_WORK_SIZE\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_PROPERTY:
printf("CL_INVALID_PROPERTY\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_IMAGE_DESCRIPTOR:
printf("CL_INVALID_IMAGE_DESCRIPTOR\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_COMPILER_OPTIONS:
printf("CL_INVALID_COMPILER_OPTIONS\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_LINKER_OPTIONS:
printf("CL_INVALID_LINKER_OPTIONS\n"); throw std::runtime_error("OpenCL error");
case CL_INVALID_DEVICE_PARTITION_COUNT:
printf("CL_INVALID_DEVICE_PARTITION_COUNT\n"); throw std::runtime_error("OpenCL error");
}
}
}