File: OpenCLcontext.cpp

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (377 lines) | stat: -rw-r--r-- 11,808 bytes parent folder | download | duplicates (2)
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* OpenCLcontext.cpp - Dual-device Carla example

   Copyright (c) 2022 Topi Leppänen / Tampere University

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to
   deal in the Software without restriction, including without limitation the
   rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
   sell copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
   IN THE SOFTWARE.
*/


#include <CL/cl2.hpp>

#include <string>
#include <vector>
#include <deque>
#include <chrono>
#include <mutex>
#include <cmath>
#include <condition_variable>
#include <map>
#include <set>
#include <thread>
#include <unordered_map>

#include "OpenCLcontext.h"

#ifdef LIBCARLA_INCLUDED_FROM_UE4
#include "carla/Debug.h"
#include "carla/Logging.h"
#endif

static const char *PX_COUNT_SOURCE = R"(

#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_int64_extended_atomics : enable

__kernel void count_red_pixels(global const uchar4 *input, global ulong* output) {
    size_t x = get_global_id(0);
    size_t y = get_global_id(1);
    size_t width = get_global_size(0);
    size_t height = get_global_size(1);

    uchar4 px = input[y * width + x];
    if (px.x > 100)
        atom_add(output, 1);
}
)";

static const char *DOWNSAMPLE_SOURCE = R"(

#pragma OPENCL EXTENSION cl_khr_int64_base_atomics : enable
#pragma OPENCL EXTENSION cl_khr_int64_extended_atomics : enable

__kernel void downsample_image(global const uchar4 *input, global uchar4* output) {
    size_t x = get_global_id(0);
    size_t y = get_global_id(1);
    size_t width = get_global_size(0);
    size_t height = get_global_size(1);

/*
    uint4 sum = input[y * width + x]
              + input[y * width + x + 1]
              + input[(y+1) * width + x]
              + input[(y+1) * width + x + 1];
    output[y * (width/2) + x] = convert_uchar4(sum / 4);
*/
   unsigned out_idx = y + x*width;

   const unsigned factor = 2;
   width *= factor;
   height *= factor;
   unsigned y1, y2, x1, x2;
   y1=y*factor;
   y2=y1+1;
   x1=x*factor;
   x2=x1+1;

   uchar4 q11, q12, q21, q22;
   q11 = input[y1 * width + x1];
   q12 = input[y1 * width + x2];
   q21 = input[y2 * width + x1];
   q22 = input[y2 * width + x2];
   uint4 sum = convert_uint4(q11) + convert_uint4(q12) + convert_uint4(q21) + convert_uint4(q22);
   output[out_idx] = convert_uchar4(sum/4);
}
)";


#define OUTPUT_SIZE (cl::size_type)(sizeof(unsigned long)*64)

#ifdef LIBCARLA_INCLUDED_FROM_UE4
#define LOG_I(...) carla::log_info(__VA_ARGS__)
#define LOG_D(...) carla::log_debug(__VA_ARGS__)
#define LOG_E(...) carla::log_error(__VA_ARGS__)
#else
#define LOG_I(...)
#define LOG_D(...)
#define LOG_E(...)
#endif


#define CHECK_CL_ERROR(EXPR, ...) \
    if (EXPR != CL_SUCCESS) { LOG_E(__VA_ARGS__); return false; }

class OpenCL_Context {
    cl::Platform platform;
    std::vector<cl::Device> devices;
    cl::Context ClContext;

    cl::Device GpuDev;
    cl::CommandQueue GpuQueue;
    cl::Program GpuProgram;
    cl::Kernel GpuKernel;

    cl::Device FpgaDev;
    cl::CommandQueue FpgaQueue;
    cl::Program FpgaProgram;
    cl::Kernel FpgaKernel;

    cl::Buffer GpuInputBuffer, InterBuffer, FpgaOutputBuffer;
    unsigned long InputBufferSize;

    std::mutex OclMutex;
    cl::NDRange Offset, Global, Local;
    bool initialized = false;
    bool UsingFpgaBuiltinKernel = false;
    unsigned imgID = 0;

public:
    OpenCL_Context() {};

    bool isAvailable() {
      std::unique_lock<std::mutex> lock(OclMutex);
      if (!initialized)
          return false;
      if (GpuDev() == nullptr || FpgaDev() == nullptr)
          return false;
//      bool avail = GpuDev.getInfo<CL_DEVICE_AVAILABLE>() != CL_FALSE;
//      return avail;
      return true;
    }

    ~OpenCL_Context() {
        if (isAvailable())
            shutdown();
    }

    bool initialize(unsigned width, unsigned height, unsigned bpp);
    bool processCameraFrame(unsigned char* input, unsigned long *output);

private:
    void shutdown();
};

bool OpenCL_Manager::initialize(unsigned width, unsigned height, unsigned bpp) {
    if (Context->initialize(width, height, bpp))
        isValid = true;
    else {
        LOG_E("Error in OpenCL->initialize\n");
        isValid = false;
    }
    return isValid;
}

bool OpenCL_Manager::processCameraFrame(unsigned char* input, unsigned long *output) {

    if (isValid) {
        if (!Context->processCameraFrame(input, output)) {
            LOG_E("Error in OpenCL->processCameraFrame\n");
            isValid = false;
        }
    }
    if (!isValid)
        *output = 11223344;
    return isValid;
}

OpenCL_Manager::OpenCL_Manager()
    : Context{std::unique_ptr<OpenCL_Context>(new OpenCL_Context())} {}
OpenCL_Manager::~OpenCL_Manager() {}



bool OpenCL_Context::initialize(unsigned width, unsigned height, unsigned bpp)
{
    cl_int err;
    InputBufferSize = width * height * bpp / 8;
    // Take first platform and create a context for it.
    std::vector<cl::Platform> all_platforms;
    cl::Platform::get(&all_platforms);
    if(!all_platforms.size()) {
        LOG_E("No OpenCL platforms available!\n");
        return false;
    }

    platform = all_platforms[0];
    Offset = cl::NullRange;

    unsigned local_h = (height % 2) ? 1 : 2;
    local_h = (height % 4) ? local_h : 4;
    local_h = (height % 8) ? local_h : 8;
    local_h = (height % 16) ? local_h : 16;
    unsigned local_w = (width % 2) ? 1 : 2;
    local_w = (width % 4) ? local_w : 4;
    local_w = (width % 8) ? local_w : 8;
    local_w = (width % 16) ? local_w : 16;

    Local = cl::NDRange(local_w/2, local_h/2);
    Global = cl::NDRange(width/2, height/2);

    // Find all devices.
    platform.getDevices(CL_DEVICE_TYPE_ALL, &devices);
    if(devices.size() == 0) {
        LOG_E("No OpenCL devices available!\n");
        return false;
    }

    LOG_I("OpenCL platform name: %s\n",  platform.getInfo<CL_PLATFORM_NAME>().c_str());
    LOG_I("OpenCL platform version: %s\n", platform.getInfo<CL_PLATFORM_VERSION>().c_str());
    LOG_I("OpenCL platform vendor: %s\n",  platform.getInfo<CL_PLATFORM_VENDOR>().c_str());
    LOG_I("Found %lu OpenCL devices.\n", devices.size());

    if (devices.size() == 1) {
        GpuDev = devices[0];
        FpgaDev = devices[0];
    } else if (devices[1].getInfo<CL_DEVICE_TYPE>() & CL_DEVICE_TYPE_CUSTOM) {
        GpuDev = devices[0];
        FpgaDev = devices[1];
    } else {
        GpuDev = devices[1];
        FpgaDev = devices[0];
    }

    ClContext = cl::Context(devices, nullptr, nullptr, nullptr, &err);
    CHECK_CL_ERROR(err, "Context creation failed\n");

    GpuQueue = cl::CommandQueue(ClContext, GpuDev, 0, &err); // , CL_QUEUE_PROFILING_ENABLE
    CHECK_CL_ERROR(err, "CmdQueue creation failed\n");
    if (devices.size() == 1)
        FpgaQueue = GpuQueue;
    else {
        FpgaQueue = cl::CommandQueue(ClContext, FpgaDev, 0, &err); // , CL_QUEUE_PROFILING_ENABLE
        CHECK_CL_ERROR(err, "CmdQueue creation failed\n");
    }

    std::vector<cl::Device> GpuDevs = {GpuDev};
    GpuProgram = cl::Program{ClContext, DOWNSAMPLE_SOURCE, false, &err};
    CHECK_CL_ERROR(err, "Program creation failed\n");
    err = GpuProgram.build(GpuDevs);
    CHECK_CL_ERROR(err, "Program build failed\n");
    GpuKernel = cl::Kernel(GpuProgram, "downsample_image", &err);
    CHECK_CL_ERROR(err, "Kernel creation failed\n");

    std::string FpgaBuiltinKernels = FpgaDev.getInfo<CL_DEVICE_BUILT_IN_KERNELS>();
    std::string RedPixelKernelName{"pocl.countred"};
    std::vector<cl::Device> FpgaDevs = {FpgaDev};
    if (FpgaBuiltinKernels.find(RedPixelKernelName) != std::string::npos)
      {
        UsingFpgaBuiltinKernel = true;
        FpgaProgram = cl::Program{ClContext, FpgaDevs, RedPixelKernelName, &err};
        CHECK_CL_ERROR(err, "Program creation failed\n");
      }
    else
      {
        FpgaProgram = cl::Program{ClContext, PX_COUNT_SOURCE, false, &err};
        RedPixelKernelName = "count_red_pixels";
        CHECK_CL_ERROR(err, "Program creation failed\n");
      }

    err = FpgaProgram.build(FpgaDevs);
    CHECK_CL_ERROR(err, "Program build failed\n");
    FpgaKernel = cl::Kernel(FpgaProgram, RedPixelKernelName.c_str(), &err);
    CHECK_CL_ERROR(err, "Kernel creation failed\n");

    GpuInputBuffer = cl::Buffer(ClContext, CL_MEM_READ_WRITE, (cl::size_type)(InputBufferSize), nullptr, &err);
    CHECK_CL_ERROR(err, "Input buffer creation failed\n");
    InterBuffer = cl::Buffer(ClContext, CL_MEM_READ_WRITE, (cl::size_type)(InputBufferSize/4), nullptr, &err);
    CHECK_CL_ERROR(err, "Inter buffer creation failed\n");
    FpgaOutputBuffer = cl::Buffer(ClContext, CL_MEM_READ_WRITE, OUTPUT_SIZE, nullptr, &err);
    CHECK_CL_ERROR(err, "Output buffer creation failed\n");

    GpuKernel.setArg(0, GpuInputBuffer);
    GpuKernel.setArg(1, InterBuffer);

    FpgaKernel.setArg(0, InterBuffer);
    FpgaKernel.setArg(1, FpgaOutputBuffer);

    initialized = true;
    return true;
}

bool OpenCL_Context::processCameraFrame(unsigned char* input, unsigned long *output) {
    if (!isAvailable()) {
        LOG_E("Device not available");
        return false;
    }

    std::unique_lock<std::mutex> lock(OclMutex);

#ifdef TIMING
    LOG_D("OpenCL: start processCameraFrame\n");
    auto start_time = std::chrono::steady_clock::now();
#endif

    cl_int err;
    *output = 0;

    std::vector<cl::Event> evts;
    cl::Event ev1, ev2, ev3, ev4;

    err = GpuQueue.enqueueWriteBuffer(GpuInputBuffer, CL_FALSE, 0, InputBufferSize, input, nullptr, &ev1);
    if (err != CL_SUCCESS)
        return false;

    evts.push_back(ev1);
    err = GpuQueue.enqueueNDRangeKernel(GpuKernel, Offset, Global, Local, &evts, &ev2);
    if (err != CL_SUCCESS)
        return false;

    evts.clear();
    evts.push_back(ev2);

    if (!UsingFpgaBuiltinKernel) {
    /* clear the output buffer. only required for some devices */
    err = FpgaQueue.enqueueWriteBuffer(FpgaOutputBuffer, CL_FALSE, 0, sizeof(cl_ulong), output, nullptr, &ev3);
    if (err != CL_SUCCESS)
        return false;
    evts.push_back(ev3);
    }

    err = FpgaQueue.enqueueNDRangeKernel(FpgaKernel, Offset, Global, Local, &evts, &ev4);
    if (err != CL_SUCCESS)
        return false;

    evts.clear();
    evts.push_back(ev4);
    err = FpgaQueue.enqueueReadBuffer(FpgaOutputBuffer, CL_TRUE, 0, sizeof(unsigned long), output, &evts);
    if (err != CL_SUCCESS)
        return false;

#ifdef DUMP_FRAMES
    char filename[1024];
    std::snprintf(filename, 1024, "/tmp/carla_%u_%zu.raw", imgID, *output);
    FILE* outfile = std::fopen(filename, "w");
    std::fwrite(input, 1, InputBufferSize, outfile);
    std::fclose(outfile);
    ++imgID;
#endif

#ifdef TIMING
    auto end_time = std::chrono::steady_clock::now();
    std::chrono::duration<float> diff = end_time - start_time;
    float s = diff.count() * 1000.0f;
    LOG_D("OpenCL: end processCameraFrame: %03.1f ms\n", s);
#endif
    return true;
}


void OpenCL_Context::shutdown() {
    if (GpuQueue())
            GpuQueue.finish();
}