device_multi.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. #include <stdlib.h>
  17. #include <sstream>
  18. #include "device/device.h"
  19. #include "device/device_intern.h"
  20. #include "device/device_network.h"
  21. #include "render/buffers.h"
  22. #include "util/util_foreach.h"
  23. #include "util/util_list.h"
  24. #include "util/util_logging.h"
  25. #include "util/util_map.h"
  26. #include "util/util_time.h"
  27. CCL_NAMESPACE_BEGIN
  28. class MultiDevice : public Device {
  29. public:
  30. struct SubDevice {
  31. explicit SubDevice(Device *device_) : device(device_)
  32. {
  33. }
  34. Device *device;
  35. map<device_ptr, device_ptr> ptr_map;
  36. };
  37. list<SubDevice> devices;
  38. device_ptr unique_key;
  39. MultiDevice(DeviceInfo &info, Stats &stats, Profiler &profiler, bool background_)
  40. : Device(info, stats, profiler, background_), unique_key(1)
  41. {
  42. foreach (DeviceInfo &subinfo, info.multi_devices) {
  43. Device *device = Device::create(subinfo, sub_stats_, profiler, background);
  44. /* Always add CPU devices at the back since GPU devices can change
  45. * host memory pointers, which CPU uses as device pointer. */
  46. if (subinfo.type == DEVICE_CPU) {
  47. devices.push_back(SubDevice(device));
  48. }
  49. else {
  50. devices.push_front(SubDevice(device));
  51. }
  52. }
  53. #ifdef WITH_NETWORK
  54. /* try to add network devices */
  55. ServerDiscovery discovery(true);
  56. time_sleep(1.0);
  57. vector<string> servers = discovery.get_server_list();
  58. foreach (string &server, servers) {
  59. Device *device = device_network_create(info, stats, profiler, server.c_str());
  60. if (device)
  61. devices.push_back(SubDevice(device));
  62. }
  63. #endif
  64. }
  65. ~MultiDevice()
  66. {
  67. foreach (SubDevice &sub, devices)
  68. delete sub.device;
  69. }
  70. const string &error_message()
  71. {
  72. foreach (SubDevice &sub, devices) {
  73. if (sub.device->error_message() != "") {
  74. if (error_msg == "")
  75. error_msg = sub.device->error_message();
  76. break;
  77. }
  78. }
  79. return error_msg;
  80. }
  81. virtual bool show_samples() const
  82. {
  83. if (devices.size() > 1) {
  84. return false;
  85. }
  86. return devices.front().device->show_samples();
  87. }
  88. virtual BVHLayoutMask get_bvh_layout_mask() const
  89. {
  90. BVHLayoutMask bvh_layout_mask = BVH_LAYOUT_ALL;
  91. foreach (const SubDevice &sub_device, devices) {
  92. bvh_layout_mask &= sub_device.device->get_bvh_layout_mask();
  93. }
  94. return bvh_layout_mask;
  95. }
  96. bool load_kernels(const DeviceRequestedFeatures &requested_features)
  97. {
  98. foreach (SubDevice &sub, devices)
  99. if (!sub.device->load_kernels(requested_features))
  100. return false;
  101. return true;
  102. }
  103. bool wait_for_availability(const DeviceRequestedFeatures &requested_features)
  104. {
  105. foreach (SubDevice &sub, devices)
  106. if (!sub.device->wait_for_availability(requested_features))
  107. return false;
  108. return true;
  109. }
  110. DeviceKernelStatus get_active_kernel_switch_state()
  111. {
  112. DeviceKernelStatus result = DEVICE_KERNEL_USING_FEATURE_KERNEL;
  113. foreach (SubDevice &sub, devices) {
  114. DeviceKernelStatus subresult = sub.device->get_active_kernel_switch_state();
  115. switch (subresult) {
  116. case DEVICE_KERNEL_WAITING_FOR_FEATURE_KERNEL:
  117. result = subresult;
  118. break;
  119. case DEVICE_KERNEL_FEATURE_KERNEL_INVALID:
  120. case DEVICE_KERNEL_FEATURE_KERNEL_AVAILABLE:
  121. return subresult;
  122. case DEVICE_KERNEL_USING_FEATURE_KERNEL:
  123. case DEVICE_KERNEL_UNKNOWN:
  124. break;
  125. }
  126. }
  127. return result;
  128. }
  129. void mem_alloc(device_memory &mem)
  130. {
  131. device_ptr key = unique_key++;
  132. foreach (SubDevice &sub, devices) {
  133. mem.device = sub.device;
  134. mem.device_pointer = 0;
  135. mem.device_size = 0;
  136. sub.device->mem_alloc(mem);
  137. sub.ptr_map[key] = mem.device_pointer;
  138. }
  139. mem.device = this;
  140. mem.device_pointer = key;
  141. stats.mem_alloc(mem.device_size);
  142. }
  143. void mem_copy_to(device_memory &mem)
  144. {
  145. device_ptr existing_key = mem.device_pointer;
  146. device_ptr key = (existing_key) ? existing_key : unique_key++;
  147. size_t existing_size = mem.device_size;
  148. foreach (SubDevice &sub, devices) {
  149. mem.device = sub.device;
  150. mem.device_pointer = (existing_key) ? sub.ptr_map[existing_key] : 0;
  151. mem.device_size = existing_size;
  152. sub.device->mem_copy_to(mem);
  153. sub.ptr_map[key] = mem.device_pointer;
  154. }
  155. mem.device = this;
  156. mem.device_pointer = key;
  157. stats.mem_alloc(mem.device_size - existing_size);
  158. }
  159. void mem_copy_from(device_memory &mem, int y, int w, int h, int elem)
  160. {
  161. device_ptr key = mem.device_pointer;
  162. int i = 0, sub_h = h / devices.size();
  163. foreach (SubDevice &sub, devices) {
  164. int sy = y + i * sub_h;
  165. int sh = (i == (int)devices.size() - 1) ? h - sub_h * i : sub_h;
  166. mem.device = sub.device;
  167. mem.device_pointer = sub.ptr_map[key];
  168. sub.device->mem_copy_from(mem, sy, w, sh, elem);
  169. i++;
  170. }
  171. mem.device = this;
  172. mem.device_pointer = key;
  173. }
  174. void mem_zero(device_memory &mem)
  175. {
  176. device_ptr existing_key = mem.device_pointer;
  177. device_ptr key = (existing_key) ? existing_key : unique_key++;
  178. size_t existing_size = mem.device_size;
  179. foreach (SubDevice &sub, devices) {
  180. mem.device = sub.device;
  181. mem.device_pointer = (existing_key) ? sub.ptr_map[existing_key] : 0;
  182. mem.device_size = existing_size;
  183. sub.device->mem_zero(mem);
  184. sub.ptr_map[key] = mem.device_pointer;
  185. }
  186. mem.device = this;
  187. mem.device_pointer = key;
  188. stats.mem_alloc(mem.device_size - existing_size);
  189. }
  190. void mem_free(device_memory &mem)
  191. {
  192. device_ptr key = mem.device_pointer;
  193. size_t existing_size = mem.device_size;
  194. foreach (SubDevice &sub, devices) {
  195. mem.device = sub.device;
  196. mem.device_pointer = sub.ptr_map[key];
  197. mem.device_size = existing_size;
  198. sub.device->mem_free(mem);
  199. sub.ptr_map.erase(sub.ptr_map.find(key));
  200. }
  201. mem.device = this;
  202. mem.device_pointer = 0;
  203. mem.device_size = 0;
  204. stats.mem_free(existing_size);
  205. }
  206. void const_copy_to(const char *name, void *host, size_t size)
  207. {
  208. foreach (SubDevice &sub, devices)
  209. sub.device->const_copy_to(name, host, size);
  210. }
  211. void draw_pixels(device_memory &rgba,
  212. int y,
  213. int w,
  214. int h,
  215. int width,
  216. int height,
  217. int dx,
  218. int dy,
  219. int dw,
  220. int dh,
  221. bool transparent,
  222. const DeviceDrawParams &draw_params)
  223. {
  224. device_ptr key = rgba.device_pointer;
  225. int i = 0, sub_h = h / devices.size();
  226. int sub_height = height / devices.size();
  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. int sheight = (i == (int)devices.size() - 1) ? height - sub_height * i : sub_height;
  231. int sdy = dy + i * sub_height;
  232. /* adjust math for w/width */
  233. rgba.device_pointer = sub.ptr_map[key];
  234. sub.device->draw_pixels(
  235. rgba, sy, w, sh, width, sheight, dx, sdy, dw, dh, transparent, draw_params);
  236. i++;
  237. }
  238. rgba.device_pointer = key;
  239. }
  240. void map_tile(Device *sub_device, RenderTile &tile)
  241. {
  242. foreach (SubDevice &sub, devices) {
  243. if (sub.device == sub_device) {
  244. if (tile.buffer)
  245. tile.buffer = sub.ptr_map[tile.buffer];
  246. }
  247. }
  248. }
  249. int device_number(Device *sub_device)
  250. {
  251. int i = 0;
  252. foreach (SubDevice &sub, devices) {
  253. if (sub.device == sub_device)
  254. return i;
  255. i++;
  256. }
  257. return -1;
  258. }
  259. void map_neighbor_tiles(Device *sub_device, RenderTile *tiles)
  260. {
  261. for (int i = 0; i < 9; i++) {
  262. if (!tiles[i].buffers) {
  263. continue;
  264. }
  265. /* If the tile was rendered on another device, copy its memory to
  266. * to the current device now, for the duration of the denoising task.
  267. * Note that this temporarily modifies the RenderBuffers and calls
  268. * the device, so this function is not thread safe. */
  269. device_vector<float> &mem = tiles[i].buffers->buffer;
  270. if (mem.device != sub_device) {
  271. /* Only copy from device to host once. This is faster, but
  272. * also required for the case where a CPU thread is denoising
  273. * a tile rendered on the GPU. In that case we have to avoid
  274. * overwriting the buffer being denoised by the CPU thread. */
  275. if (!tiles[i].buffers->map_neighbor_copied) {
  276. tiles[i].buffers->map_neighbor_copied = true;
  277. mem.copy_from_device(0, mem.data_size, 1);
  278. }
  279. mem.swap_device(sub_device, 0, 0);
  280. mem.copy_to_device();
  281. tiles[i].buffer = mem.device_pointer;
  282. tiles[i].device_size = mem.device_size;
  283. mem.restore_device();
  284. }
  285. }
  286. }
  287. void unmap_neighbor_tiles(Device *sub_device, RenderTile *tiles)
  288. {
  289. /* Copy denoised result back to the host. */
  290. device_vector<float> &mem = tiles[9].buffers->buffer;
  291. mem.swap_device(sub_device, tiles[9].device_size, tiles[9].buffer);
  292. mem.copy_from_device(0, mem.data_size, 1);
  293. mem.restore_device();
  294. /* Copy denoised result to the original device. */
  295. mem.copy_to_device();
  296. for (int i = 0; i < 9; i++) {
  297. if (!tiles[i].buffers) {
  298. continue;
  299. }
  300. device_vector<float> &mem = tiles[i].buffers->buffer;
  301. if (mem.device != sub_device) {
  302. mem.swap_device(sub_device, tiles[i].device_size, tiles[i].buffer);
  303. sub_device->mem_free(mem);
  304. mem.restore_device();
  305. }
  306. }
  307. }
  308. int get_split_task_count(DeviceTask &task)
  309. {
  310. int total_tasks = 0;
  311. list<DeviceTask> tasks;
  312. task.split(tasks, devices.size());
  313. foreach (SubDevice &sub, devices) {
  314. if (!tasks.empty()) {
  315. DeviceTask subtask = tasks.front();
  316. tasks.pop_front();
  317. total_tasks += sub.device->get_split_task_count(subtask);
  318. }
  319. }
  320. return total_tasks;
  321. }
  322. void task_add(DeviceTask &task)
  323. {
  324. list<DeviceTask> tasks;
  325. task.split(tasks, devices.size());
  326. foreach (SubDevice &sub, devices) {
  327. if (!tasks.empty()) {
  328. DeviceTask subtask = tasks.front();
  329. tasks.pop_front();
  330. if (task.buffer)
  331. subtask.buffer = sub.ptr_map[task.buffer];
  332. if (task.rgba_byte)
  333. subtask.rgba_byte = sub.ptr_map[task.rgba_byte];
  334. if (task.rgba_half)
  335. subtask.rgba_half = sub.ptr_map[task.rgba_half];
  336. if (task.shader_input)
  337. subtask.shader_input = sub.ptr_map[task.shader_input];
  338. if (task.shader_output)
  339. subtask.shader_output = sub.ptr_map[task.shader_output];
  340. sub.device->task_add(subtask);
  341. }
  342. }
  343. }
  344. void task_wait()
  345. {
  346. foreach (SubDevice &sub, devices)
  347. sub.device->task_wait();
  348. }
  349. void task_cancel()
  350. {
  351. foreach (SubDevice &sub, devices)
  352. sub.device->task_cancel();
  353. }
  354. protected:
  355. Stats sub_stats_;
  356. };
  357. Device *device_multi_create(DeviceInfo &info, Stats &stats, Profiler &profiler, bool background)
  358. {
  359. return new MultiDevice(info, stats, profiler, background);
  360. }
  361. CCL_NAMESPACE_END