]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - tidl_api/src/ocl_device.h
Use DSP Built-in Kernels in TIDL-API
[tidl/tidl-api.git] / tidl_api / src / ocl_device.h
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, const char *name);
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 = 5;  // max: 1 DSP device + 4 EVE devices
83         cl_mem CreateBuffer(const DeviceArgInfo &Arg);
84         void   ReleaseBuffer(cl_mem M);
85         static bool GetDevices(DeviceType device_type,
86                                cl_device_id cl_d_ids[],
87                                cl_uint *p_num_devices,
88                                cl_uint *p_num_compute_units);
90               cl_context        context_m;
91               cl_program        program_m;
92               cl_command_queue  queue_m[MAX_DEVICES];
93         const cl_device_type    device_type_m;
94         const DeviceIds         device_ids_m;
95               cl_uint           freq_in_mhz_m;
97         friend Kernel;
98 };
100 class DspDevice: public Device
102     public:
103         DspDevice(const DeviceIds& ids, const std::string &kernel_names);
104         virtual ~DspDevice() {}
106         DspDevice()                            = delete;
107         DspDevice(const DspDevice&)            = delete;
108         DspDevice& operator=(const DspDevice&) = delete;
110         virtual std::string GetDeviceName() { return "DSP"; }
112     protected:
113         bool BuildBuiltInProgram(const std::string &kernel_names,
114                                  cl_device_id device_ids[],
115                                  int num_devices);
116 };
118 class EveDevice : public Device
120     public:
121         EveDevice(const DeviceIds& ids, const std::string &kernel_names);
122         virtual ~EveDevice() {}
124         EveDevice()                            = delete;
125         EveDevice(const EveDevice&)            = delete;
126         EveDevice& operator=(const EveDevice&) = delete;
128         virtual std::string GetDeviceName() { return "EVE"; }
130     protected:
131         bool BuildBuiltInProgram(const std::string &kernel_names,
132                                  cl_device_id device_ids[],
133                                  int num_devices);
134 };
137 /*! \brief OpenCL kernels
138  *
139  * Create and execute OpenCL kernels
140  */
141 class Kernel
143     public:
144         Kernel(Device *device, const std::string &Name,
145                const KernelArgs &args, uint8_t device_index);
146         ~Kernel();
148         bool UpdateScalarArg(uint32_t index, size_t size, const void *value);
149         Kernel& RunAsync(uint32_t context_idx = 0);
150         bool Wait(uint32_t context_idx = 0);
151         bool AddCallback(void *user_data, uint32_t context_idx = 0);
153     private:
154         cl_kernel           kernel_m;
155         cl_event            event_m[tidl::internal::NUM_CONTEXTS];
156         std::vector<cl_mem> buffers_m;
157         const std::string   name_m;
159         Device*             device_m;
160         uint8_t             device_index_m;
161 };
164 } // namespace oa