1 /******************************************************************************
2 * Copyright (c) 2017-2018, Texas Instruments Incorporated - http://www.ti.com/
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Texas Instruments Incorporated nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
27 *****************************************************************************/
29 /*! \file ocl_device.h
30 * \brief Wrapper classes for OpenCL C structures
31 *
32 * Provides a high level abstraction for OpenCL APIs
33 */
35 #pragma once
36 #include <CL/cl.h>
37 #include <CL/cl_ext.h>
38 #include <vector>
39 #include <memory>
40 #include "executor.h"
41 #include "device_arginfo.h"
43 namespace tidl
44 {
46 typedef std::vector<DeviceArgInfo> KernelArgs;
48 class Kernel;
50 /*! \brief Manages OpenCL context, device and command queues
51 *
52 * Inititalizes an OpenCL context, creates devices and command queues to
53 * each device.
54 */
55 class Device
56 {
58 public:
59 typedef std::unique_ptr<Device> Ptr;
61 Device(cl_device_type t, const DeviceIds& ids);
62 virtual ~Device();
65 static Ptr Create(DeviceType core_type, const DeviceIds& ids,
66 const std::string& name);
68 cl_command_queue& GetCommandQueue(uint8_t index)
69 { return queue_m[index]; }
71 cl_device_type type() const { return device_type_m; }
73 float GetFrequencyInMhz() const { return freq_in_mhz_m; }
75 static uint32_t GetNumDevices(DeviceType device_type);
77 virtual std::string GetDeviceName() = 0;
79 protected:
81 static const int MAX_DEVICES = 4;
82 cl_mem CreateBuffer(const DeviceArgInfo &Arg);
83 void ReleaseBuffer(cl_mem M);
86 cl_context context_m;
87 cl_program program_m;
88 cl_command_queue queue_m[MAX_DEVICES];
89 const cl_device_type device_type_m;
90 const DeviceIds device_ids_m;
91 cl_uint freq_in_mhz_m;
93 friend Kernel;
94 };
96 class DspDevice: public Device
97 {
98 public:
99 DspDevice(const DeviceIds& ids, const std::string &binary_filename);
100 virtual ~DspDevice() {}
102 DspDevice() = delete;
103 DspDevice(const DspDevice&) = delete;
104 DspDevice& operator=(const DspDevice&) = delete;
106 virtual std::string GetDeviceName() { return "DSP"; }
108 protected:
109 bool BuildProgramFromBinary(const std::string &binary_filename,
110 cl_device_id device_ids[],
111 int num_devices);
112 };
114 class EveDevice : public Device
115 {
116 public:
117 EveDevice(const DeviceIds& ids, const std::string &kernel_names);
118 virtual ~EveDevice() {}
120 EveDevice() = delete;
121 EveDevice(const EveDevice&) = delete;
122 EveDevice& operator=(const EveDevice&) = delete;
124 virtual std::string GetDeviceName() { return "EVE"; }
126 protected:
127 bool BuildProgramFromBinary(const std::string &kernel_names,
128 cl_device_id device_ids[],
129 int num_devices);
131 };
134 /*! \brief OpenCL kernels
135 *
136 * Create and execute OpenCL kernels
137 */
138 class Kernel
139 {
140 public:
141 Kernel(Device *device, const std::string &Name,
142 const KernelArgs &args, uint8_t device_index);
143 ~Kernel();
145 Kernel& RunAsync();
146 bool Wait(float *host_elapsed_ms = nullptr);
147 bool AddCallback(void *user_data);
149 private:
150 cl_kernel kernel_m;
151 cl_event event_m;
152 std::vector<cl_mem> buffers_m;
153 const std::string name_m;
155 Device* device_m;
156 uint8_t device_index_m;
157 bool is_running_m;
158 };
161 } // namespace oa