-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherode_tidsp_cl.cc
More file actions
214 lines (176 loc) · 7.07 KB
/
erode_tidsp_cl.cc
File metadata and controls
214 lines (176 loc) · 7.07 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Using TI C66x optimizated OpenCL kernel
#define __CL_ENABLE_EXCEPTIONS
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <assert.h>
#include <string.h>
#include "cl.hpp"
// Only include source code if TI OpenCL extension is found
#ifdef CL_MEM_USE_MSMC_TI
#include "erode_tidsp_cl.h"
static const char *kernelSource =
#include "erode3x3_tidsp.cl"
;
static cl::Kernel kernel;
static cl::CommandQueue queue;
static cl::Buffer d_a;
static cl::Buffer d_b;
static cl::Context context;
static int NUMCOMPUNITS;
static int width, height;
static void
ocl_event_times(const cl::Event &ev, const char* name)
{
cl_ulong t_que, t_sub, t_strt, t_end;
ev.getProfilingInfo(CL_PROFILING_COMMAND_QUEUED, &t_que);
ev.getProfilingInfo(CL_PROFILING_COMMAND_SUBMIT, &t_sub);
ev.getProfilingInfo(CL_PROFILING_COMMAND_START, &t_strt);
ev.getProfilingInfo(CL_PROFILING_COMMAND_END, &t_end);
/*----------------------------------------------------------------------
* Normalize the time to microseconds
*--------------------------------------------------------------------*/
t_que /= 1000; t_sub /= 1000; t_strt /= 1000; t_end /= 1000;
if (!name) name = "";
std::cout<< name << " : Queue to Submit: " << t_sub-t_que << " us" << std::endl;
std::cout<< name << " : Submit to Start : " << t_strt-t_sub << " us" << std::endl;
std::cout<< name << " : Start to End : " << t_end-t_strt << " us" << std::endl;
std::cout<< std::endl;
}
int
erode3x3_tidsp_cl_init(int w, int h)
{
size_t bytes = w * h;
cl_int err = CL_SUCCESS;
width = w;
height = h;
assert(w % 8 == 0);
assert(w < 2046);
assert(h > 2);
#ifdef __CL_ENABLE_EXCEPTIONS
try {
#endif
// Query platforms
std::vector<cl::Platform> platforms;
cl::Platform::get(&platforms);
if (platforms.size() == 0) {
std::cout << "Platform size 0\n";
return -1;
}
std::cout << "Platform size " << platforms.size() << std::endl;
// Get list of devices on default platform and create context
cl_context_properties properties[] =
{ CL_CONTEXT_PLATFORM, (cl_context_properties)(platforms[0])(), 0};
context = cl::Context(CL_DEVICE_TYPE_ALL, properties);
std::vector<cl::Device> devices = context.getInfo<CL_CONTEXT_DEVICES>();
std::cout << "Device size " << devices.size() << std::endl;
cl::Device device = devices[0];
std::cout << "Device Name: " << device.getInfo<CL_DEVICE_NAME>() << std::endl;
std::cout << "Device Version: " << device.getInfo<CL_DEVICE_VERSION>() << std::endl;
std::cout << "Device Type: " << device.getInfo<CL_DEVICE_TYPE>();
std::cout << " (GPU: " << CL_DEVICE_TYPE_GPU << ", CPU: " << CL_DEVICE_TYPE_CPU << ", ACCEL: " << CL_DEVICE_TYPE_ACCELERATOR << ")" << std::endl;
std::cout << "Device Vendor: " << device.getInfo<CL_DEVICE_VENDOR>() << std::endl;
device.getInfo(CL_DEVICE_MAX_COMPUTE_UNITS, &NUMCOMPUNITS);
std::cout << "Device Max Compute Units: " << NUMCOMPUNITS << std::endl;
std::cout << "Device Global Memory: " << device.getInfo<CL_DEVICE_GLOBAL_MEM_SIZE>() << std::endl;
std::cout << "Device Max Clock Frequency: " << device.getInfo<CL_DEVICE_MAX_CLOCK_FREQUENCY>() << std::endl;
std::cout << "Device Max Allocateable Memory: " << device.getInfo<CL_DEVICE_MAX_MEM_ALLOC_SIZE>() << std::endl;
std::cout << "Device Local Memory: " << device.getInfo<CL_DEVICE_LOCAL_MEM_SIZE>() << std::endl;
cl_ulong msmc_mem = 0;
device.getInfo(CL_DEVICE_MSMC_MEM_SIZE_TI, &msmc_mem);
assert(w * h * 2 <= msmc_mem);
std::cout << "Device TI MSMC Memory: " << msmc_mem << std::endl;
std::cout << "Device max constant buffer size: " << device.getInfo<CL_DEVICE_MAX_CONSTANT_BUFFER_SIZE>() << std::endl;
std::cout << "Device max work item dims: " << device.getInfo< CL_DEVICE_MAX_WORK_ITEM_DIMENSIONS>() << std::endl;
std::cout << "Device max work group size: " << device.getInfo< CL_DEVICE_MAX_WORK_GROUP_SIZE>() << std::endl;
std::vector<size_t> device_max_work_item_sizes = device.getInfo<CL_DEVICE_MAX_WORK_ITEM_SIZES>();
std::cout << "Max work item sizes are: ";
for (std::vector<size_t>::const_iterator i = device_max_work_item_sizes.begin(); i != device_max_work_item_sizes.end(); ++i)
std::cout << *i << ' ';
std::cout << std::endl;
std::cout << "Device Available: " << device.getInfo< CL_DEVICE_AVAILABLE>() << std::endl;
std::cout << "Device Extensions: " << device.getInfo< CL_DEVICE_EXTENSIONS>() << std::endl;
// Create command queue for first device
queue = cl::CommandQueue(context, device, CL_QUEUE_PROFILING_ENABLE, &err);
// Create device memory buffers
d_a = cl::Buffer(context, CL_MEM_READ_ONLY|CL_MEM_USE_MSMC_TI, bytes);
d_b = cl::Buffer(context, CL_MEM_WRITE_ONLY|CL_MEM_USE_MSMC_TI, bytes);
//Build kernel from source string
cl::Program::Sources source(1,
std::make_pair(kernelSource,strlen(kernelSource)));
cl::Program program_ = cl::Program(context, source);
program_.build(devices);
// Create kernel object
kernel = cl::Kernel(program_, "tidsp_morph_erode", &err);
#ifdef __CL_ENABLE_EXCEPTIONS
} catch (cl::Error err) {
std::cerr
<< "ERROR: "<<err.what()<<"("<<err.err()<<")"<<std::endl;
return -1;
}
#endif
return 0;
}
uint8_t*
erode3x3_tidsp_cl_map_input_buf()
{
return (uint8_t*) queue.enqueueMapBuffer(d_a, CL_TRUE, CL_MAP_WRITE,
0, width*height, NULL, NULL);
}
void
erode3x3_tidsp_cl_unmap_input_buf(uint8_t *srcA)
{
queue.enqueueUnmapMemObject(d_a, srcA, NULL, NULL);
}
uint8_t*
erode3x3_tidsp_cl_map_output_buf()
{
return (uint8_t*) queue.enqueueMapBuffer(d_b, CL_TRUE, CL_MAP_READ,
0, width*height, NULL, NULL);
}
void
erode3x3_tidsp_cl_unmap_output_buf(uint8_t *srcB)
{
queue.enqueueUnmapMemObject(d_b, srcB, NULL, NULL);
}
void
erode3x3_tidsp_cl()
{
size_t bytes = width * height;
#ifdef __CL_ENABLE_EXCEPTIONS
try {
#endif
// Bind kernel arguments to kernel
kernel.setArg(0, d_a);
kernel.setArg(1, d_b);
kernel.setArg(2, width);
kernel.setArg(3, height);
// Number of work items in each local work group
cl::NDRange localSize(1);
// Number of total work items - localSize must be divisor
cl::NDRange globalSize(NUMCOMPUNITS);
// Enqueue kernel
cl::Event event;
queue.enqueueNDRangeKernel(
kernel,
cl::NullRange,
globalSize,
localSize,
NULL,
&event);
// Block until kernel completion
event.wait();
ocl_event_times(event, "Kernel Exec ");
#ifdef __CL_ENABLE_EXCEPTIONS
} catch (cl::Error err) {
std::cerr
<< "ERROR: "<<err.what()<<"("<<err.err()<<")"<<std::endl;
}
#endif
}
void
erode3x3_tidsp_cl_destroy()
{
// TODO
}
#endif // CL_MEM_USE_MSMC_TI