Cycles: Change the way how we pass requested capabilities to the device
[blender.git] / intern / cycles / device / device_multi.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdlib.h>
18 #include <sstream>
19
20 #include "device.h"
21 #include "device_intern.h"
22 #include "device_network.h"
23
24 #include "buffers.h"
25
26 #include "util_foreach.h"
27 #include "util_list.h"
28 #include "util_logging.h"
29 #include "util_map.h"
30 #include "util_time.h"
31
32 CCL_NAMESPACE_BEGIN
33
34 class MultiDevice : public Device
35 {
36 public:
37         struct SubDevice {
38                 SubDevice(Device *device_)
39                 : device(device_) {}
40
41                 Device *device;
42                 map<device_ptr, device_ptr> ptr_map;
43         };
44
45         list<SubDevice> devices;
46         device_ptr unique_ptr;
47
48         MultiDevice(DeviceInfo& info, Stats &stats, bool background_)
49         : Device(info, stats, background_), unique_ptr(1)
50         {
51                 Device *device;
52
53                 foreach(DeviceInfo& subinfo, info.multi_devices) {
54                         device = Device::create(subinfo, stats, background);
55                         devices.push_back(SubDevice(device));
56                 }
57
58 #ifdef WITH_NETWORK
59                 /* try to add network devices */
60                 ServerDiscovery discovery(true);
61                 time_sleep(1.0);
62
63                 vector<string> servers = discovery.get_server_list();
64
65                 foreach(string& server, servers) {
66                         device = device_network_create(info, stats, server.c_str());
67                         if(device)
68                                 devices.push_back(SubDevice(device));
69                 }
70 #endif
71         }
72
73         ~MultiDevice()
74         {
75                 foreach(SubDevice& sub, devices)
76                         delete sub.device;
77         }
78
79         const string& error_message()
80         {
81                 foreach(SubDevice& sub, devices) {
82                         if(sub.device->error_message() != "") {
83                                 if(error_msg == "")
84                                         error_msg = sub.device->error_message();
85                                 break;
86                         }
87                 }
88
89                 return error_msg;
90         }
91
92         bool load_kernels(const DeviceRequestedFeatures& requested_features)
93         {
94                 foreach(SubDevice& sub, devices)
95                         if(!sub.device->load_kernels(requested_features))
96                                 return false;
97
98                 return true;
99         }
100
101         void mem_alloc(device_memory& mem, MemoryType type)
102         {
103                 foreach(SubDevice& sub, devices) {
104                         mem.device_pointer = 0;
105                         sub.device->mem_alloc(mem, type);
106                         sub.ptr_map[unique_ptr] = mem.device_pointer;
107                 }
108
109                 mem.device_pointer = unique_ptr++;
110         }
111
112         void mem_copy_to(device_memory& mem)
113         {
114                 device_ptr tmp = mem.device_pointer;
115
116                 foreach(SubDevice& sub, devices) {
117                         mem.device_pointer = sub.ptr_map[tmp];
118                         sub.device->mem_copy_to(mem);
119                 }
120
121                 mem.device_pointer = tmp;
122         }
123
124         void mem_copy_from(device_memory& mem, int y, int w, int h, int elem)
125         {
126                 device_ptr tmp = mem.device_pointer;
127                 int i = 0, sub_h = h/devices.size();
128
129                 foreach(SubDevice& sub, devices) {
130                         int sy = y + i*sub_h;
131                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
132
133                         mem.device_pointer = sub.ptr_map[tmp];
134                         sub.device->mem_copy_from(mem, sy, w, sh, elem);
135                         i++;
136                 }
137
138                 mem.device_pointer = tmp;
139         }
140
141         void mem_zero(device_memory& mem)
142         {
143                 device_ptr tmp = mem.device_pointer;
144
145                 foreach(SubDevice& sub, devices) {
146                         mem.device_pointer = sub.ptr_map[tmp];
147                         sub.device->mem_zero(mem);
148                 }
149
150                 mem.device_pointer = tmp;
151         }
152
153         void mem_free(device_memory& mem)
154         {
155                 device_ptr tmp = mem.device_pointer;
156
157                 foreach(SubDevice& sub, devices) {
158                         mem.device_pointer = sub.ptr_map[tmp];
159                         sub.device->mem_free(mem);
160                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
161                 }
162
163                 mem.device_pointer = 0;
164         }
165
166         void const_copy_to(const char *name, void *host, size_t size)
167         {
168                 foreach(SubDevice& sub, devices)
169                         sub.device->const_copy_to(name, host, size);
170         }
171
172         void tex_alloc(const char *name, device_memory& mem, InterpolationType interpolation, bool periodic)
173         {
174                 VLOG(1) << "Texture allocate: " << name << ", " << mem.memory_size() << " bytes.";
175
176                 foreach(SubDevice& sub, devices) {
177                         mem.device_pointer = 0;
178                         sub.device->tex_alloc(name, mem, interpolation, periodic);
179                         sub.ptr_map[unique_ptr] = mem.device_pointer;
180                 }
181
182                 mem.device_pointer = unique_ptr++;
183         }
184
185         void tex_free(device_memory& mem)
186         {
187                 device_ptr tmp = mem.device_pointer;
188
189                 foreach(SubDevice& sub, devices) {
190                         mem.device_pointer = sub.ptr_map[tmp];
191                         sub.device->tex_free(mem);
192                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
193                 }
194
195                 mem.device_pointer = 0;
196         }
197
198         void pixels_alloc(device_memory& mem)
199         {
200                 foreach(SubDevice& sub, devices) {
201                         mem.device_pointer = 0;
202                         sub.device->pixels_alloc(mem);
203                         sub.ptr_map[unique_ptr] = mem.device_pointer;
204                 }
205
206                 mem.device_pointer = unique_ptr++;
207         }
208
209         void pixels_free(device_memory& mem)
210         {
211                 device_ptr tmp = mem.device_pointer;
212
213                 foreach(SubDevice& sub, devices) {
214                         mem.device_pointer = sub.ptr_map[tmp];
215                         sub.device->pixels_free(mem);
216                         sub.ptr_map.erase(sub.ptr_map.find(tmp));
217                 }
218
219                 mem.device_pointer = 0;
220         }
221
222         void pixels_copy_from(device_memory& mem, int y, int w, int h)
223         {
224                 device_ptr tmp = mem.device_pointer;
225                 int i = 0, sub_h = h/devices.size();
226
227                 foreach(SubDevice& sub, devices) {
228                         int sy = y + i*sub_h;
229                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
230
231                         mem.device_pointer = sub.ptr_map[tmp];
232                         sub.device->pixels_copy_from(mem, sy, w, sh);
233                         i++;
234                 }
235
236                 mem.device_pointer = tmp;
237         }
238
239         void draw_pixels(device_memory& rgba, int y, int w, int h, int dy, int width, int height, bool transparent,
240                 const DeviceDrawParams &draw_params)
241         {
242                 device_ptr tmp = rgba.device_pointer;
243                 int i = 0, sub_h = h/devices.size();
244                 int sub_height = height/devices.size();
245
246                 foreach(SubDevice& sub, devices) {
247                         int sy = y + i*sub_h;
248                         int sh = (i == (int)devices.size() - 1)? h - sub_h*i: sub_h;
249                         int sheight = (i == (int)devices.size() - 1)? height - sub_height*i: sub_height;
250                         int sdy = dy + i*sub_height;
251                         /* adjust math for w/width */
252
253                         rgba.device_pointer = sub.ptr_map[tmp];
254                         sub.device->draw_pixels(rgba, sy, w, sh, sdy, width, sheight, transparent, draw_params);
255                         i++;
256                 }
257
258                 rgba.device_pointer = tmp;
259         }
260
261         void map_tile(Device *sub_device, RenderTile& tile)
262         {
263                 foreach(SubDevice& sub, devices) {
264                         if(sub.device == sub_device) {
265                                 if(tile.buffer) tile.buffer = sub.ptr_map[tile.buffer];
266                                 if(tile.rng_state) tile.rng_state = sub.ptr_map[tile.rng_state];
267                         }
268                 }
269         }
270
271         int device_number(Device *sub_device)
272         {
273                 int i = 0;
274
275                 foreach(SubDevice& sub, devices) {
276                         if(sub.device == sub_device)
277                                 return i;
278                         i++;
279                 }
280
281                 return -1;
282         }
283
284         int get_split_task_count(DeviceTask& task)
285         {
286                 int total_tasks = 0;
287                 list<DeviceTask> tasks;
288                 task.split(tasks, devices.size());
289                 foreach(SubDevice& sub, devices) {
290                         if(!tasks.empty()) {
291                                 DeviceTask subtask = tasks.front();
292                                 tasks.pop_front();
293
294                                 total_tasks += sub.device->get_split_task_count(subtask);
295                         }
296                 }
297                 return total_tasks;
298         }
299
300         void task_add(DeviceTask& task)
301         {
302                 list<DeviceTask> tasks;
303                 task.split(tasks, devices.size());
304
305                 foreach(SubDevice& sub, devices) {
306                         if(!tasks.empty()) {
307                                 DeviceTask subtask = tasks.front();
308                                 tasks.pop_front();
309
310                                 if(task.buffer) subtask.buffer = sub.ptr_map[task.buffer];
311                                 if(task.rgba_byte) subtask.rgba_byte = sub.ptr_map[task.rgba_byte];
312                                 if(task.rgba_half) subtask.rgba_half = sub.ptr_map[task.rgba_half];
313                                 if(task.shader_input) subtask.shader_input = sub.ptr_map[task.shader_input];
314                                 if(task.shader_output) subtask.shader_output = sub.ptr_map[task.shader_output];
315
316                                 sub.device->task_add(subtask);
317                         }
318                 }
319         }
320
321         void task_wait()
322         {
323                 foreach(SubDevice& sub, devices)
324                         sub.device->task_wait();
325         }
326
327         void task_cancel()
328         {
329                 foreach(SubDevice& sub, devices)
330                         sub.device->task_cancel();
331         }
332 };
333
334 Device *device_multi_create(DeviceInfo& info, Stats &stats, bool background)
335 {
336         return new MultiDevice(info, stats, background);
337 }
338
339 static bool device_multi_add(vector<DeviceInfo>& devices, DeviceType type, bool with_display, bool with_advanced_shading, const char *id_fmt, int num)
340 {
341         DeviceInfo info;
342
343         /* create map to find duplicate descriptions */
344         map<string, int> dupli_map;
345         map<string, int>::iterator dt;
346         int num_added = 0, num_display = 0;
347
348         info.advanced_shading = with_advanced_shading;
349         info.pack_images = false;
350         info.extended_images = true;
351
352         foreach(DeviceInfo& subinfo, devices) {
353                 if(subinfo.type == type) {
354                         if(subinfo.advanced_shading != info.advanced_shading)
355                                 continue;
356                         if(subinfo.display_device) {
357                                 if(with_display)
358                                         num_display++;
359                                 else
360                                         continue;
361                         }
362
363                         string key = subinfo.description;
364
365                         if(dupli_map.find(key) == dupli_map.end())
366                                 dupli_map[key] = 1;
367                         else
368                                 dupli_map[key]++;
369
370                         info.multi_devices.push_back(subinfo);
371                         if(subinfo.display_device)
372                                 info.display_device = true;
373                         info.pack_images = info.pack_images || subinfo.pack_images;
374                         info.extended_images = info.extended_images && subinfo.extended_images;
375                         num_added++;
376                 }
377         }
378
379         if(num_added <= 1 || (with_display && num_display == 0))
380                 return false;
381
382         /* generate string */
383         stringstream desc;
384         vector<string> last_tokens;
385         bool first = true;
386
387         for(dt = dupli_map.begin(); dt != dupli_map.end(); dt++) {
388                 if(!first) desc << " + ";
389                 first = false;
390
391                 /* get name and count */
392                 string name = dt->first;
393                 int count = dt->second;
394
395                 /* strip common prefixes */
396                 vector<string> tokens;
397                 string_split(tokens, dt->first);
398
399                 if(tokens.size() > 1) {
400                         int i;
401
402                         for(i = 0; i < tokens.size() && i < last_tokens.size(); i++)
403                                 if(tokens[i] != last_tokens[i])
404                                         break;
405
406                         name = "";
407                         for(; i < tokens.size(); i++) {
408                                 name += tokens[i];
409                                 if(i != tokens.size() - 1)
410                                         name += " ";
411                         }
412                 }
413
414                 last_tokens = tokens;
415
416                 /* add */
417                 if(count > 1)
418                         desc << name << " (" << count << "x)";
419                 else
420                         desc << name;
421         }
422
423         /* add info */
424         info.type = DEVICE_MULTI;
425         info.description = desc.str();
426         info.id = string_printf(id_fmt, num);
427         info.display_device = with_display;
428         info.num = 0;
429
430         if(with_display)
431                 devices.push_back(info);
432         else
433                 devices.insert(devices.begin(), info);
434         
435         return true;
436 }
437
438 void device_multi_info(vector<DeviceInfo>& devices)
439 {
440         int num = 0;
441
442         if(!device_multi_add(devices, DEVICE_CUDA, false, true, "CUDA_MULTI_%d", num++))
443                 device_multi_add(devices, DEVICE_CUDA, false, false, "CUDA_MULTI_%d", num++);
444         if(!device_multi_add(devices, DEVICE_CUDA, true, true, "CUDA_MULTI_%d", num++))
445                 device_multi_add(devices, DEVICE_CUDA, true, false, "CUDA_MULTI_%d", num++);
446
447         num = 0;
448         if(!device_multi_add(devices, DEVICE_OPENCL, false, true, "OPENCL_MULTI_%d", num++))
449                 device_multi_add(devices, DEVICE_OPENCL, false, false, "OPENCL_MULTI_%d", num++);
450         if(!device_multi_add(devices, DEVICE_OPENCL, true, true, "OPENCL_MULTI_%d", num++))
451                 device_multi_add(devices, DEVICE_OPENCL, true, false, "OPENCL_MULTI_%d", num++);
452 }
453
454 CCL_NAMESPACE_END
455