1 /******************************************************************************
2 * Copyright (c) 2017-18, 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"
42 namespace tidl
43 {
45 typedef std::vector<ArgInfo> KernelArgs;
47 class Kernel;
49 /*! \brief Manages OpenCL context, device and command queues
50 *
51 * Inititalizes an OpenCL context, creates devices and command queues to
52 * each device.
53 */
54 class Device
55 {
57 public:
58 typedef std::unique_ptr<Device> Ptr;
60 Device(cl_device_type t, const DeviceIds& ids);
61 virtual ~Device();
64 static Ptr Create(DeviceType core_type, const DeviceIds& ids,
65 const std::string& name);
67 cl_command_queue& GetCommandQueue(uint8_t index)
68 { return queue_m[index]; }
70 cl_device_type type() const { return device_type_m; }
72 float GetFrequencyInMhz() const { return freq_in_mhz_m; }
74 static uint32_t GetNumDevices(DeviceType device_type);
76 protected:
78 static const int MAX_DEVICES = 4;
79 cl_mem CreateBuffer(const ArgInfo &Arg);
80 void ReleaseBuffer(cl_mem M);
83 cl_context context_m;
84 cl_program program_m;
85 cl_command_queue queue_m[MAX_DEVICES];
86 const cl_device_type device_type_m;
87 const DeviceIds device_ids_m;
88 cl_uint freq_in_mhz_m;
90 friend Kernel;
91 };
93 class DspDevice: public Device
94 {
95 public:
96 DspDevice(const DeviceIds& ids, const std::string &binary_filename);
97 virtual ~DspDevice() {}
99 DspDevice() = delete;
100 DspDevice(const DspDevice&) = delete;
101 DspDevice& operator=(const DspDevice&) = delete;
103 protected:
104 bool BuildProgramFromBinary(const std::string &binary_filename,
105 cl_device_id device_ids[],
106 int num_devices);
107 };
109 class EveDevice : public Device
110 {
111 public:
112 EveDevice(const DeviceIds& ids, const std::string &kernel_names);
113 virtual ~EveDevice() {}
115 EveDevice() = delete;
116 EveDevice(const EveDevice&) = delete;
117 EveDevice& operator=(const EveDevice&) = delete;
119 protected:
120 bool BuildProgramFromBinary(const std::string &kernel_names,
121 cl_device_id device_ids[],
122 int num_devices);
124 };
127 /*! \brief OpenCL kernels
128 *
129 * Create and execute OpenCL kernels
130 */
131 class Kernel
132 {
133 public:
134 Kernel(Device *device, const std::string &Name,
135 const KernelArgs &args, uint8_t device_index);
136 ~Kernel();
138 Kernel& RunAsync();
139 bool Wait();
141 private:
142 cl_kernel kernel_m;
143 cl_event event_m;
144 std::vector<cl_mem> buffers_m;
145 const std::string name_m;
147 Device* device_m;
148 uint8_t device_index_m;
149 bool is_running_m;
150 };
153 } // namespace oa