c49ac24e018f353f00833c5d39b91c1a77f97ccf
[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 "BKE_global.h"
27
28 #include "COM_WorkScheduler.h"
29 #include "COM_CPUDevice.h"
30 #include "COM_OpenCLDevice.h"
31 #include "COM_OpenCLKernels.cl.h"
32 #include "OCL_opencl.h"
33
34 #include "PIL_time.h"
35 #include "BLI_threads.h"
36
37 #if COM_CURRENT_THREADING_MODEL == COM_TM_NOTHREAD
38 #warning COM_CURRENT_THREADING_MODEL COM_TM_NOTHREAD is activated. Use only for debugging.
39 #elif COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
40 #else
41 #error COM_CURRENT_THREADING_MODEL No threading model selected
42 #endif
43
44
45 /// @brief list of all CPUDevices. for every hardware thread an instance of CPUDevice is created
46 static vector<CPUDevice *> g_cpudevices;
47
48 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
49 /// @brief list of all thread for every CPUDevice in cpudevices a thread exists
50 static ListBase g_cputhreads;
51 /// @brief all scheduled work for the cpu
52 static ThreadQueue *g_cpuqueue;
53 static ThreadQueue *g_gpuqueue;
54 #ifdef COM_OPENCL_ENABLED
55 static cl_context g_context;
56 static cl_program g_program;
57 /// @brief list of all OpenCLDevices. for every OpenCL GPU device an instance of OpenCLDevice is created
58 static vector<OpenCLDevice *> g_gpudevices;
59 /// @brief list of all thread for every GPUDevice in cpudevices a thread exists
60 static ListBase g_gputhreads;
61 /// @brief all scheduled work for the gpu
62 #ifdef COM_OPENCL_ENABLED
63 static bool g_openclActive = false;
64 #endif
65 #endif
66 #endif
67
68
69 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
70 void *WorkScheduler::thread_execute_cpu(void *data)
71 {
72         Device *device = (Device *)data;
73         WorkPackage *work;
74         
75         while ((work = (WorkPackage *)BLI_thread_queue_pop(g_cpuqueue))) {
76                 device->execute(work);
77                 delete work;
78         }
79         
80         return NULL;
81 }
82
83 void *WorkScheduler::thread_execute_gpu(void *data)
84 {
85         Device *device = (Device *)data;
86         WorkPackage *work;
87         
88         while ((work = (WorkPackage *)BLI_thread_queue_pop(g_gpuqueue))) {
89                 device->execute(work);
90                 delete work;
91         }
92         
93         return NULL;
94 }
95 #endif
96
97
98
99 void WorkScheduler::schedule(ExecutionGroup *group, int chunkNumber)
100 {
101         WorkPackage *package = new WorkPackage(group, chunkNumber);
102 #if COM_CURRENT_THREADING_MODEL == COM_TM_NOTHREAD
103         CPUDevice device;
104         device.execute(package);
105         delete package;
106 #elif COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
107 #ifdef COM_OPENCL_ENABLED
108         if (group->isOpenCL() && g_openclActive) {
109                 BLI_thread_queue_push(g_gpuqueue, package);
110         }
111         else {
112                 BLI_thread_queue_push(g_cpuqueue, package);
113         }
114 #else
115         BLI_thread_queue_push(cpuqueue, package);
116 #endif
117 #endif
118 }
119
120 void WorkScheduler::start(CompositorContext &context)
121 {
122 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
123         unsigned int index;
124         g_cpuqueue = BLI_thread_queue_init();
125         BLI_init_threads(&g_cputhreads, thread_execute_cpu, g_cpudevices.size());
126         for (index = 0; index < g_cpudevices.size(); index++) {
127                 Device *device = g_cpudevices[index];
128                 BLI_insert_thread(&g_cputhreads, device);
129         }
130 #ifdef COM_OPENCL_ENABLED
131         if (context.getHasActiveOpenCLDevices()) {
132                 g_gpuqueue = BLI_thread_queue_init();
133                 BLI_init_threads(&g_gputhreads, thread_execute_gpu, g_gpudevices.size());
134                 for (index = 0; index < g_gpudevices.size(); index++) {
135                         Device *device = g_gpudevices[index];
136                         BLI_insert_thread(&g_gputhreads, device);
137                 }
138                 g_openclActive = true;
139         }
140         else {
141                 g_openclActive = false;
142         }
143 #endif
144 #endif
145 }
146 void WorkScheduler::finish()
147 {
148 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
149 #ifdef COM_OPENCL_ENABLED
150         if (g_openclActive) {
151                 BLI_thread_queue_wait_finish(g_gpuqueue);
152                 BLI_thread_queue_wait_finish(g_cpuqueue);
153         }
154         else {
155                 BLI_thread_queue_wait_finish(g_cpuqueue);
156         }
157 #else
158         BLI_thread_queue_wait_finish(cpuqueue);
159 #endif
160 #endif
161 }
162 void WorkScheduler::stop()
163 {
164 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
165         BLI_thread_queue_nowait(g_cpuqueue);
166         BLI_end_threads(&g_cputhreads);
167         BLI_thread_queue_free(g_cpuqueue);
168         g_cpuqueue = NULL;
169 #ifdef COM_OPENCL_ENABLED
170         if (g_openclActive) {
171                 BLI_thread_queue_nowait(g_gpuqueue);
172                 BLI_end_threads(&g_gputhreads);
173                 BLI_thread_queue_free(g_gpuqueue);
174                 g_gpuqueue = NULL;
175         }
176 #endif
177 #endif
178 }
179
180 bool WorkScheduler::hasGPUDevices()
181 {
182 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
183 #ifdef COM_OPENCL_ENABLED
184         return g_gpudevices.size() > 0;
185 #else
186         return 0;
187 #endif
188 #else
189         return 0;
190 #endif
191 }
192
193 extern void clContextError(const char *errinfo, const void *private_info, size_t cb, void *user_data)
194 {
195         printf("OPENCL error: %s\n", errinfo);
196 }
197
198 void WorkScheduler::initialize()
199 {
200 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
201         int numberOfCPUThreads = BLI_system_thread_count();
202
203         for (int index = 0; index < numberOfCPUThreads; index++) {
204                 CPUDevice *device = new CPUDevice();
205                 device->initialize();
206                 g_cpudevices.push_back(device);
207         }
208 #ifdef COM_OPENCL_ENABLED
209         g_context = NULL;
210         g_program = NULL;
211         if (clCreateContextFromType) {
212                 cl_uint numberOfPlatforms = 0;
213                 cl_int error;
214                 error = clGetPlatformIDs(0, 0, &numberOfPlatforms);
215                 if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
216                 if (G.f & G_DEBUG) printf("%d number of platforms\n", numberOfPlatforms);
217                 cl_platform_id *platforms = new cl_platform_id[numberOfPlatforms];
218                 error = clGetPlatformIDs(numberOfPlatforms, platforms, 0);
219                 unsigned int indexPlatform;
220                 for (indexPlatform = 0; indexPlatform < numberOfPlatforms; indexPlatform++) {
221                         cl_platform_id platform = platforms[indexPlatform];
222                         cl_uint numberOfDevices = 0;
223                         clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, 0, &numberOfDevices);
224                         if (numberOfDevices>0) {
225                                 cl_device_id *cldevices = new cl_device_id[numberOfDevices];
226                                 clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numberOfDevices, cldevices, 0);
227
228                                 g_context = clCreateContext(NULL, numberOfDevices, cldevices, clContextError, NULL, &error);
229                                 if (error != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error));  }
230                                 g_program = clCreateProgramWithSource(g_context, 1, &clkernelstoh_COM_OpenCLKernels_cl, 0, &error);
231                                 error = clBuildProgram(g_program, numberOfDevices, cldevices, 0, 0, 0);
232                                 if (error != CL_SUCCESS) { 
233                                         cl_int error2;
234                                         size_t ret_val_size = 0;
235                                         printf("CLERROR[%d]: %s\n", error, clewErrorString(error));     
236                                         error2 = clGetProgramBuildInfo(g_program, cldevices[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &ret_val_size);
237                                         if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error)); }
238                                         char *build_log =  new char[ret_val_size + 1];
239                                         error2 = clGetProgramBuildInfo(g_program, cldevices[0], CL_PROGRAM_BUILD_LOG, ret_val_size, build_log, NULL);
240                                         if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error, clewErrorString(error)); }
241                                         build_log[ret_val_size] = '\0';
242                                         printf("%s", build_log);
243                                         delete build_log;
244                                 }
245                                 else {
246                                         unsigned int indexDevices;
247                                         for (indexDevices = 0; indexDevices < numberOfDevices; indexDevices++) {
248                                                 cl_device_id device = cldevices[indexDevices];
249                                                 cl_int vendorID = 0;
250                                                 cl_int error2 = clGetDeviceInfo(device, CL_DEVICE_VENDOR_ID, sizeof(cl_int), &vendorID, NULL);
251                                                 if (error2 != CL_SUCCESS) { printf("CLERROR[%d]: %s\n", error2, clewErrorString(error2)); }
252                                                 OpenCLDevice *clDevice = new OpenCLDevice(g_context, device, g_program, vendorID);
253                                                 clDevice->initialize();
254                                                 g_gpudevices.push_back(clDevice);
255                                         }
256                                 }
257                                 delete[] cldevices;
258                         }
259                 }
260                 delete[] platforms;
261         }
262 #endif
263 #endif
264 }
265
266 void WorkScheduler::deinitialize()
267 {
268 #if COM_CURRENT_THREADING_MODEL == COM_TM_QUEUE
269         Device *device;
270         while (g_cpudevices.size() > 0) {
271                 device = g_cpudevices.back();
272                 g_cpudevices.pop_back();
273                 device->deinitialize();
274                 delete device;
275         }
276 #ifdef COM_OPENCL_ENABLED
277         while (g_gpudevices.size() > 0) {
278                 device = g_gpudevices.back();
279                 g_gpudevices.pop_back();
280                 device->deinitialize();
281                 delete device;
282         }
283         if (g_program) {
284                 clReleaseProgram(g_program);
285                 g_program = NULL;
286         }
287         if (g_context) {
288                 clReleaseContext(g_context);
289                 g_context = NULL;
290         }
291 #endif
292 #endif
293 }