File: opencl.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (188 lines) | stat: -rw-r--r-- 7,253 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
#include "opencl.h"
#include "logger.h"
#include "core/config.h"
#include <fstream>
#include "core/exception.h"

#ifdef USE_OPENCL
namespace satdump
{
    namespace opencl
    {
        cl_context ocl_context;
        cl_device_id ocl_device;

        bool context_is_init = false;
        int platform_id, device_id;
        std::map<std::string, cl_program> cached_kernels;

        std::vector<OCLDevice> getAllDevices()
        {
            std::vector<OCLDevice> devs;
            cl_platform_id platforms_ids[100];
            cl_uint platforms_cnt = 0;
            cl_device_id devices_ids[100];
            cl_uint devices_cnt = 0;
            char device_name[200];
            size_t device_name_len = 0;

            if (clGetPlatformIDs(100, platforms_ids, &platforms_cnt) != CL_SUCCESS)
                return devs;

            for (int p = 0; p < (int)platforms_cnt; p++)
            {
                if (clGetDeviceIDs(platforms_ids[p], CL_DEVICE_TYPE_ALL, 100, devices_ids, &devices_cnt) != CL_SUCCESS)
                    continue;

                for (int d = 0; d < (int)devices_cnt; d++)
                    if (clGetDeviceInfo(devices_ids[d], CL_DEVICE_NAME, 200, device_name, &device_name_len) == CL_SUCCESS)
                        devs.push_back({p, d, std::string(&device_name[0], &device_name[device_name_len])});
            }

            return devs;
        }

        void initOpenCL()
        {
#ifdef __ANDROID__
            if (OpenCLHelper::Loader::Init())
            {
                logger->debug("Failed to init OpenCL!");
                platform_id = device_id = -1;
                return;
            }
#endif
            std::vector<OCLDevice> devices = resetOCLContext();
            logger->info("Found OpenCL Devices (%d) :", devices.size());
            for (OCLDevice &d : devices)
                logger->debug(" - " + d.name.substr(0, d.name.size() - 1));
        }

        void setupOCLContext()
        {
            if (context_is_init)
            {
                logger->trace("OpenCL context already initilized.");
                return;
            }

            if (platform_id == -1)
                throw satdump_exception("User specified CPU processing");

            cl_platform_id platforms_ids[100];
            cl_uint platforms_cnt = 0;
            cl_device_id devices_ids[100];
            cl_uint devices_cnt = 0;
            char device_platform_name[200];
            size_t device_platform_name_len = 0;
            cl_int err = 0;

            logger->trace("First OpenCL context request. Initializing...");

            err = clGetPlatformIDs(100, platforms_ids, &platforms_cnt);
            if (err != CL_SUCCESS)
                throw satdump_exception("Could not get OpenCL platform IDs! Code " + std::to_string(err));

            if (platforms_cnt == 0)
                throw satdump_exception("No platforms found. Check OpenCL installation!");

            cl_platform_id platform = platforms_ids[platform_id];
            err = clGetPlatformInfo(platform, CL_PLATFORM_NAME, 200, device_platform_name, &device_platform_name_len);
            if (err == CL_SUCCESS)
                logger->info("Using platform: %s", std::string(&device_platform_name[0], &device_platform_name[device_platform_name_len]).c_str());
            else
                logger->error("Could not get platform name! Code %d", err);

            err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 100, devices_ids, &devices_cnt);
            if (err != CL_SUCCESS)
                throw satdump_exception("Could not get OpenCL devices IDs! Code " + std::to_string(err));

            if (devices_cnt == 0)
                throw satdump_exception("No devices found. Check OpenCL installation!");

            ocl_device = devices_ids[device_id];
            if (clGetDeviceInfo(ocl_device, CL_DEVICE_NAME, 200, device_platform_name, &device_platform_name_len) == CL_SUCCESS)
                logger->info("Using device: %s", std::string(&device_platform_name[0], &device_platform_name[device_platform_name_len]).c_str());

            ocl_context = clCreateContext(NULL, 1, &ocl_device, NULL, NULL, &err);
            if (err != CL_SUCCESS)
                throw satdump_exception("Could not init OpenCL context! Code " + std::to_string(err));

            context_is_init = true;
        }

        std::vector<OCLDevice> resetOCLContext()
        {
            if (context_is_init)
            {
                context_is_init = false;
                for (auto &kernel : cached_kernels)
                {
                    cl_int ret = clReleaseProgram(kernel.second);
                    if (ret != CL_SUCCESS)
                        logger->error("Could not release CL program! Code %d", ret);
                }

                cached_kernels.clear();
                cl_int ret = clReleaseContext(ocl_context);
                if (ret != CL_SUCCESS)
                    logger->error("Could not release old context! Code %d", ret);
            }

            platform_id = satdump::config::main_cfg["satdump_general"]["opencl_device"]["platform"].get<int>();
            device_id = satdump::config::main_cfg["satdump_general"]["opencl_device"]["device"].get<int>();

            std::vector<OCLDevice> devices = getAllDevices();
            if (devices.empty())
                platform_id = device_id = -1;

            return devices;
        }

        bool useCL()
        {
            return platform_id >= 0;
        }

        cl_program buildCLKernel(std::string path, bool use_cache)
        {
            if (use_cache)                          // If cache enabled...
                if (cached_kernels.count(path) > 0) // ...check if we already have this kernel
                    return cached_kernels[path];

            std::ifstream isf(path);
            std::string kernel_src(std::istreambuf_iterator<char>{isf}, {});

            const char *srcs[1] = {kernel_src.c_str()};
            const size_t lens[1] = {kernel_src.length()};
            cl_int err = 0;

            cl_program prg = clCreateProgramWithSource(ocl_context, 1, srcs, lens, &err);
            err = clBuildProgram(prg, 1, &ocl_device, NULL, NULL, NULL);

            if (err != CL_SUCCESS)
            {
                char* error_msg = new char[100000];
                size_t error_len = 0;
                if (clGetProgramBuildInfo(prg, ocl_device, CL_PROGRAM_BUILD_LOG, 100000, error_msg, &error_len) == CL_SUCCESS)
                {
                    std::string err_str = std::string(&error_msg[0], &error_msg[error_len]);
                    delete[] error_msg;
                    throw satdump_exception((std::string)"Error building: " + err_str);
                }
                else
                {
                    delete[] error_msg;
                    throw satdump_exception("Error building, and could not read error log!");
                }
            }

            if (use_cache)                              // If cache enabled...
                if (cached_kernels.count(path) == 0)    // ...and we don't already have the kernel...
                    cached_kernels.insert({path, prg}); // ...return it

            return prg;
        }
    }
}
#endif