Possible fix for [#36086] Activating the opencl option in the compositor causes blend...
[blender.git] / source / blender / compositor / intern / COM_WorkScheduler.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor: 
19  *              Jeroen Bakker 
20  *              Monique Dewanchand
21  */
22
23 #include <list>
24 #include <stdio.h>
25
26 #include "COM_compositor.h"
27 #include "COM_WorkScheduler.h"
28 #include "COM_CPUDevice.h"
29 #include "COM_OpenCLDevice.h"
30 #include "COM_OpenCLKernels.cl.h"
31 #include "OCL_opencl.h"
32 #include "COM_WriteBufferOperation.h"
33
34 #include "MEM_guardedalloc.h"
35
36 #include "PIL_time.h"
37 #include "BLI_threads.h"
38
39 #include "BKE_global.h"
40
41 #if COM_CURRENT_THREADING_MODEL == COM_TM_NOTHREAD
42 #  ifndef DEBUG  /* test this so we dont get warnings in debug builds */
43 #    warning COM_CURRENT_THREADING_MODEL COM_TM_NOTHREAD is activated. Use only for debugging.
44 #  endif
45 #elif COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
46    /* do nothing - default */
47 #else
48 #  error COM_CURRENT_THREADING_MODEL No threading model selected
49 #endif
50
51
52 /// @brief list of all CPUDevices. for every hardware thread an instance of CPUDevice is created
53 static vector<CPUDevice *> g_cpudevices;
54
55 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
56 /// @brief list of all thread for every CPUDevice in cpudevices a thread exists
57 static ListBase g_cputhreads;
58 static bool g_cpuInitialized = false;
59 /// @brief all scheduled work for the cpu
60 static ThreadQueue *g_cpuqueue;
61 static ThreadQueue *g_gpuqueue;
62 #ifdef COM_OPENCL_ENABLED
63 static cl_context g_context;
64 static cl_program g_program;
65 /// @brief list of all OpenCLDevices. for every OpenCL GPU device an instance of OpenCLDevice is created
66 static vector<OpenCLDevice *> g_gpudevices;
67 /// @brief list of all thread for every GPUDevice in cpudevices a thread exists
68 static ListBase g_gputhreads;
69 /// @brief all scheduled work for the gpu
70 #ifdef COM_OPENCL_ENABLED
71 static bool g_openclActive = false;
72 static bool g_openclInitialized = false;
73 #endif
74 #endif
75 #endif
76
77 #define MAX_HIGHLIGHT 8
78 static bool g_highlightInitialized = false;
79 extern "C" {
80 int g_highlightIndex;
81 void **g_highlightedNodes;
82 void **g_highlightedNodesRead;
83
84 #define HIGHLIGHT(wp) \
85 { \
86         ExecutionGroup *group = wp->getExecutionGroup(); \
87         if (group->isComplex()) { \
88                 NodeOperation *operation = group->getOutputNodeOperation(); \
89                 if (operation->isWriteBufferOperation()) { \
90                         WriteBufferOperation *writeOperation = (WriteBufferOperation *)operation; \
91                         NodeOperation *complexOperation = writeOperation->getInput(); \
92                         bNode *node = complexOperation->getbNode(); \
93                         if (node) { \
94                                 if (node->original) { \
95                                         node = node->original; \
96                                 } \
97                                 if (g_highlightInitialized && g_highlightedNodes) { \
98                                         if (g_highlightIndex < MAX_HIGHLIGHT) { \
99                                                 g_highlightedNodes[g_highlightIndex++] = node; \
100                                         } \
101                                 } \
102                         } \
103                 } \
104         } \
105 }
106
107 void COM_startReadHighlights()
108 {
109         if (!g_highlightInitialized) {
110                 return;
111         }
112         
113         if (g_highlightedNodesRead) {
114                 MEM_freeN(g_highlightedNodesRead);
115         }
116         
117         g_highlightedNodesRead = g_highlightedNodes;
118         g_highlightedNodes = (void **)MEM_callocN(sizeof(void *) * MAX_HIGHLIGHT, __func__);
119         g_highlightIndex = 0;
120 }
121
122 int COM_isHighlightedbNode(bNode *bnode)
123 {
124         if (!g_highlightInitialized) {
125                 return false;
126         }
127         
128         if (!g_highlightedNodesRead) {
129                 return false;
130         }
131
132         for (int i = 0; i < MAX_HIGHLIGHT; i++) {
133                 void *p = g_highlightedNodesRead[i];
134                 if (!p) return false;
135                 if (p == bnode) return true;
136         }
137         return false;
138 }
139 } // end extern "C"
140
141 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
142 void *WorkScheduler::thread_execute_cpu(void *data)
143 {
144         Device *device = (Device *)data;
145         WorkPackage *work;
146         
147         while ((work = (WorkPackage *)BLI_thread_queue_pop(g_cpuqueue))) {
148                 HIGHLIGHT(work);
149                 device->execute(work);
150                 delete work;
151         }
152         
153         return NULL;
154 }
155
156 void *WorkScheduler::thread_execute_gpu(void *data)
157 {
158         Device *device = (Device *)data;
159         WorkPackage *work;
160         
161         while ((work = (WorkPackage *)BLI_thread_queue_pop(g_gpuqueue))) {
162                 HIGHLIGHT(work);
163                 device->execute(work);
164                 delete work;
165         }
166         
167         return NULL;
168 }
169 #endif
170
171
172
173 void WorkScheduler::schedule(ExecutionGroup *group, int chunkNumber)
174 {
175         WorkPackage *package = new WorkPackage(group, chunkNumber);
176 #if COM_CURRENT_THREADING_MODEL == COM_TM_NOTHREAD
177         CPUDevice device;
178         device.execute(package);
179         delete package;
180 #elif COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
181 #ifdef COM_OPENCL_ENABLED
182         if (group->isOpenCL() && g_openclActive) {
183                 BLI_thread_queue_push(g_gpuqueue, package);
184         }
185         else {
186                 BLI_thread_queue_push(g_cpuqueue, package);
187         }
188 #else
189         BLI_thread_queue_push(cpuqueue, package);
190 #endif
191 #endif
192 }
193
194 void WorkScheduler::start(CompositorContext &context)
195 {
196 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
197         unsigned int index;
198         g_cpuqueue = BLI_thread_queue_init();
199         BLI_init_threads(&g_cputhreads, thread_execute_cpu, g_cpudevices.size());
200         for (index = 0; index < g_cpudevices.size(); index++) {
201                 Device *device = g_cpudevices[index];
202                 BLI_insert_thread(&g_cputhreads, device);
203         }
204 #ifdef COM_OPENCL_ENABLED
205         if (context.getHasActiveOpenCLDevices()) {
206                 g_gpuqueue = BLI_thread_queue_init();
207                 BLI_init_threads(&g_gputhreads, thread_execute_gpu, g_gpudevices.size());
208                 for (index = 0; index < g_gpudevices.size(); index++) {
209                         Device *device = g_gpudevices[index];
210                         BLI_insert_thread(&g_gputhreads, device);
211                 }
212                 g_openclActive = true;
213         }
214         else {
215                 g_openclActive = false;
216         }
217 #endif
218 #endif
219 }
220 void WorkScheduler::finish()
221 {
222 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
223 #ifdef COM_OPENCL_ENABLED
224         if (g_openclActive) {
225                 BLI_thread_queue_wait_finish(g_gpuqueue);
226                 BLI_thread_queue_wait_finish(g_cpuqueue);
227         }
228         else {
229                 BLI_thread_queue_wait_finish(g_cpuqueue);
230         }
231 #else
232         BLI_thread_queue_wait_finish(cpuqueue);
233 #endif
234 #endif
235 }
236 void WorkScheduler::stop()
237 {
238 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
239         BLI_thread_queue_nowait(g_cpuqueue);
240         BLI_end_threads(&g_cputhreads);
241         BLI_thread_queue_free(g_cpuqueue);
242         g_cpuqueue = NULL;
243 #ifdef COM_OPENCL_ENABLED
244         if (g_openclActive) {
245                 BLI_thread_queue_nowait(g_gpuqueue);
246                 BLI_end_threads(&g_gputhreads);
247                 BLI_thread_queue_free(g_gpuqueue);
248                 g_gpuqueue = NULL;
249         }
250 #endif
251 #endif
252 }
253
254 bool WorkScheduler::hasGPUDevices()
255 {
256 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
257 #ifdef COM_OPENCL_ENABLED
258         return g_gpudevices.size() > 0;
259 #else
260         return 0;
261 #endif
262 #else
263         return 0;
264 #endif
265 }
266
267 static void clContextError(const char *errinfo, const void *private_info, size_t cb, void *user_data)
268 {
269         printf("OPENCL error: %s\n", errinfo);
270 }
271
272 void WorkScheduler::initialize(bool use_opencl)
273 {
274         /* initialize highlighting */
275         if (!g_highlightInitialized) {
276                 if (g_highlightedNodesRead) MEM_freeN(g_highlightedNodesRead);
277                 if (g_highlightedNodes)     MEM_freeN(g_highlightedNodes);
278
279                 g_highlightedNodesRead = NULL;
280                 g_highlightedNodes = NULL;
281
282                 COM_startReadHighlights();
283
284                 g_highlightInitialized = true;
285         }
286
287 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
288         /* initialize CPU threads */
289         if (!g_cpuInitialized) {
290                 int numberOfCPUThreads = BLI_system_thread_count();
291
292                 for (int index = 0; index < numberOfCPUThreads; index++) {
293                         CPUDevice *device = new CPUDevice();
294                         device->initialize();
295                         g_cpudevices.push_back(device);
296                 }
297
298                 g_cpuInitialized = true;
299         }
300
301 #ifdef COM_OPENCL_ENABLED
302         /* deinitialize OpenCL GPU's */
303         if (use_opencl && !g_openclInitialized) {
304                 g_context = NULL;
305                 g_program = NULL;
306
307                 if(!OCL_init()) /* this will check for errors and skip if already initialized */
308                         return;
309
310                 if (clCreateContextFromType) {
311                         cl_uint numberOfPlatforms = 0;
312                         cl_int error;
313                         error = clGetPlatformIDs(0, 0, &numberOfPlatforms);
314                         if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
315                         if (G.f & G_DEBUG) printf("%d number of platforms\n", numberOfPlatforms);
316                         cl_platform_id *platforms = (cl_platform_id *)MEM_mallocN(sizeof(cl_platform_id) * numberOfPlatforms, __func__);
317                         error = clGetPlatformIDs(numberOfPlatforms, platforms, 0);
318                         unsigned int indexPlatform;
319                         for (indexPlatform = 0; indexPlatform < numberOfPlatforms; indexPlatform++) {
320                                 cl_platform_id platform = platforms[indexPlatform];
321                                 cl_uint numberOfDevices = 0;
322                                 clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, 0, &numberOfDevices);
323                                 if (numberOfDevices <= 0)
324                                         continue;
325
326                                 cl_device_id *cldevices = (cl_device_id *)MEM_mallocN(sizeof(cl_device_id) * numberOfDevices, __func__);
327                                 clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numberOfDevices, cldevices, 0);
328
329                                 g_context = clCreateContext(NULL, numberOfDevices, cldevices, clContextError, NULL, &error);
330                                 if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
331                                 const char *cl_str[2] = {datatoc_COM_OpenCLKernels_cl, NULL};
332                                 g_program = clCreateProgramWithSource(g_context, 1, cl_str, 0, &error);
333                                 error = clBuildProgram(g_program, numberOfDevices, cldevices, 0, 0, 0);
334                                 if (error != CL_SUCCESS) {
335                                         cl_int error2;
336                                         size_t ret_val_size = 0;
337                                         printf("CLERROR[%d]: %s\n", error, clewErrorString(error));
338                                         error2 = clGetProgramBuildInfo(g_program, cldevices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
339                                         if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error)); }
340                                         char *build_log = (char *)MEM_mallocN(sizeof(char) * ret_val_size + 1, __func__);
341                                         error2 = clGetProgramBuildInfo(g_program, cldevices[0], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
342                                         if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error)); }
343                                         build_log[ret_val_size] = '\0';
344                                         printf("%s", build_log);
345                                         MEM_freeN(build_log);
346                                 }
347                                 else {
348                                         unsigned int indexDevices;
349                                         for (indexDevices = 0; indexDevices < numberOfDevices; indexDevices++) {
350                                                 cl_device_id device = cldevices[indexDevices];
351                                                 cl_int vendorID = 0;
352                                                 cl_int error2 = clGetDeviceInfo(device, CL_DEVICE_VENDOR_ID, sizeof(cl_int), &vendorID, NULL);
353                                                 if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error2, clewErrorString(error2)); }
354                                                 OpenCLDevice *clDevice = new OpenCLDevice(g_context, device, g_program, vendorID);
355                                                 clDevice->initialize();
356                                                 g_gpudevices.push_back(clDevice);
357                                         }
358                                 }
359                                 MEM_freeN(cldevices);
360                         }
361                         MEM_freeN(platforms);
362                 }
363
364                 g_openclInitialized = true;
365         }
366 #endif
367 #endif
368 }
369
370 void WorkScheduler::deinitialize()
371 {
372 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
373         /* deinitialize CPU threads */
374         if (g_cpuInitialized) {
375                 Device *device;
376                 while (g_cpudevices.size() > 0) {
377                         device = g_cpudevices.back();
378                         g_cpudevices.pop_back();
379                         device->deinitialize();
380                         delete device;
381                 }
382
383                 g_cpuInitialized = false;
384         }
385
386 #ifdef COM_OPENCL_ENABLED
387         /* deinitialize OpenCL GPU's */
388         if (g_openclInitialized) {
389                 Device *device;
390                 while (g_gpudevices.size() > 0) {
391                         device = g_gpudevices.back();
392                         g_gpudevices.pop_back();
393                         device->deinitialize();
394                         delete device;
395                 }
396                 if (g_program) {
397                         clReleaseProgram(g_program);
398                         g_program = NULL;
399                 }
400                 if (g_context) {
401                         clReleaseContext(g_context);
402                         g_context = NULL;
403                 }
404
405                 g_openclInitialized = false;
406         }
407 #endif
408 #endif
409
410         /* deinitialize highlighting */
411         if (g_highlightInitialized) {
412                 g_highlightInitialized = false;
413                 if (g_highlightedNodes) {
414                         MEM_freeN(g_highlightedNodes);
415                         g_highlightedNodes = NULL;
416                 }
417
418                 if (g_highlightedNodesRead) {
419                         MEM_freeN(g_highlightedNodesRead);
420                         g_highlightedNodesRead = NULL;
421                 }
422         }
423 }
424