773a27e3900fce547e9ebd42c17cd5d9987effeb
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"
42 #include "parameters.h"
44 namespace tidl
45 {
47 typedef std::vector<DeviceArgInfo> KernelArgs;
49 class Kernel;
51 /*! \brief Manages OpenCL context, device and command queues
52 *
53 * Inititalizes an OpenCL context, creates devices and command queues to
54 * each device.
55 */
56 class Device
57 {
59 public:
60 typedef std::unique_ptr<Device> Ptr;
62 Device(cl_device_type t, const DeviceIds& ids);
63 virtual ~Device();
66 static Ptr Create(DeviceType core_type, const DeviceIds& ids,
67 const std::string& name);
69 cl_command_queue& GetCommandQueue(uint8_t index)
70 { return queue_m[index]; }
72 cl_device_type type() const { return device_type_m; }
74 float GetFrequencyInMhz() const { return freq_in_mhz_m; }
76 static uint32_t GetNumDevices(DeviceType device_type);
78 virtual std::string GetDeviceName() = 0;
80 protected:
82 static const int MAX_DEVICES = 4;
83 cl_mem CreateBuffer(const DeviceArgInfo &Arg);
84 void ReleaseBuffer(cl_mem M);
87 cl_context context_m;
88 cl_program program_m;
89 cl_command_queue queue_m[MAX_DEVICES];
90 const cl_device_type device_type_m;
91 const DeviceIds device_ids_m;
92 cl_uint freq_in_mhz_m;
94 friend Kernel;
95 };
97 class DspDevice: public Device
98 {
99 public:
100 DspDevice(const DeviceIds& ids, const std::string &binary_filename);
101 virtual ~DspDevice() {}
103 DspDevice() = delete;
104 DspDevice(const DspDevice&) = delete;
105 DspDevice& operator=(const DspDevice&) = delete;
107 virtual std::string GetDeviceName() { return "DSP"; }
109 protected:
110 bool BuildProgramFromBinary(const std::string &binary_filename,
111 cl_device_id device_ids[],
112 int num_devices);
113 };
115 class EveDevice : public Device
116 {
117 public:
118 EveDevice(const DeviceIds& ids, const std::string &kernel_names);
119 virtual ~EveDevice() {}
121 EveDevice() = delete;
122 EveDevice(const EveDevice&) = delete;
123 EveDevice& operator=(const EveDevice&) = delete;
125 virtual std::string GetDeviceName() { return "EVE"; }
127 protected:
128 bool BuildProgramFromBinary(const std::string &kernel_names,
129 cl_device_id device_ids[],
130 int num_devices);
132 };
135 /*! \brief OpenCL kernels
136 *
137 * Create and execute OpenCL kernels
138 */
139 class Kernel
140 {
141 public:
142 Kernel(Device *device, const std::string &Name,
143 const KernelArgs &args, uint8_t device_index);
144 ~Kernel();
146 bool UpdateScalarArg(uint32_t index, size_t size, const void *value);
147 Kernel& RunAsync(uint32_t context_idx = 0);
148 bool Wait(uint32_t context_idx = 0);
149 bool AddCallback(void *user_data, uint32_t context_idx = 0);
151 private:
152 cl_kernel kernel_m;
153 cl_event event_m[tidl::internal::NUM_CONTEXTS];
154 std::vector<cl_mem> buffers_m;
155 const std::string name_m;
157 Device* device_m;
158 uint8_t device_index_m;
159 };
162 } // namespace oa