disk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <fcntl.h>
  18. #include <stdbool.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <time.h>
  24. #include <common/config.h>
  25. #include <core/debug.h>
  26. #include <core/disk.h>
  27. #include <core/workers.h>
  28. #include <core/perfmodel/perfmodel.h>
  29. #include <core/topology.h>
  30. #include <datawizard/memory_nodes.h>
  31. #include <datawizard/memory_manager.h>
  32. #include <datawizard/memalloc.h>
  33. #include <drivers/cuda/driver_cuda.h>
  34. #include <drivers/opencl/driver_opencl.h>
  35. #include <profiling/profiling.h>
  36. #include <common/uthash.h>
  37. struct disk_register
  38. {
  39. unsigned node;
  40. void *base;
  41. struct starpu_disk_ops *functions;
  42. /* disk condition (1 = all authorizations, */
  43. int flag;
  44. };
  45. static void add_disk_in_list(unsigned node, struct starpu_disk_ops *func, void *base);
  46. static int get_location_with_node(unsigned node);
  47. static struct disk_register **disk_register_list = NULL;
  48. static int disk_number = -1;
  49. static int size_register_list = 2;
  50. int starpu_disk_swap_node = -1;
  51. int starpu_disk_register(struct starpu_disk_ops *func, void *parameter, starpu_ssize_t size)
  52. {
  53. STARPU_ASSERT_MSG(size < 0 || size >= SIZE_DISK_MIN,"Minimum disk size is %u Bytes ! (Here %u) \n", (int) SIZE_DISK_MIN, (int) size);
  54. /* register disk */
  55. unsigned memory_node = _starpu_memory_node_register(STARPU_DISK_RAM, 0);
  56. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  57. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  58. /* connect disk */
  59. void *base = func->plug(parameter, size);
  60. /* remember it */
  61. add_disk_in_list(memory_node,func,base);
  62. int ret = func->bandwidth(memory_node);
  63. /* have a problem with the disk */
  64. if (ret == 0)
  65. return -ENOENT;
  66. if (size >= 0)
  67. _starpu_memory_manager_set_global_memory_size(memory_node, size);
  68. return memory_node;
  69. }
  70. void _starpu_disk_unregister(void)
  71. {
  72. int i;
  73. /* search disk and delete it */
  74. for (i = 0; i <= disk_number; ++i)
  75. {
  76. _starpu_set_disk_flag(disk_register_list[i]->node, STARPU_DISK_NO_RECLAIM);
  77. _starpu_free_all_automatically_allocated_buffers(disk_register_list[i]->node);
  78. /* don't forget to unplug */
  79. disk_register_list[i]->functions->unplug(disk_register_list[i]->base);
  80. free(disk_register_list[i]);
  81. }
  82. /* no disk in the list -> delete the list */
  83. disk_number--;
  84. if (disk_register_list != NULL && disk_number == -1)
  85. {
  86. free(disk_register_list);
  87. disk_register_list = NULL;
  88. }
  89. }
  90. /* interface between user and disk memory */
  91. void *_starpu_disk_alloc(unsigned node, size_t size)
  92. {
  93. int pos = get_location_with_node(node);
  94. return disk_register_list[pos]->functions->alloc(disk_register_list[pos]->base, size);
  95. }
  96. void _starpu_disk_free(unsigned node, void *obj, size_t size)
  97. {
  98. int pos = get_location_with_node(node);
  99. disk_register_list[pos]->functions->free(disk_register_list[pos]->base, obj, size);
  100. }
  101. /* src_node == disk node and dst_node == STARPU_MAIN_RAM */
  102. int _starpu_disk_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size, struct _starpu_async_channel *channel)
  103. {
  104. int pos = get_location_with_node(src_node);
  105. if (channel != NULL)
  106. {
  107. if (disk_register_list[pos]->functions->async_read == NULL)
  108. channel = NULL;
  109. else
  110. {
  111. channel->type = STARPU_DISK_RAM;
  112. channel->event.disk_event.memory_node = src_node;
  113. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  114. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_read(disk_register_list[pos]->base, obj, buf, offset, size);
  115. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  116. }
  117. }
  118. /* asynchronous request failed or synchronous request is asked */
  119. if (channel == NULL || !channel->event.disk_event.backend_event)
  120. {
  121. disk_register_list[pos]->functions->read(disk_register_list[pos]->base, obj, buf, offset, size);
  122. return 0;
  123. }
  124. return -EAGAIN;
  125. }
  126. /* src_node == STARPU_MAIN_RAM and dst_node == disk node */
  127. int _starpu_disk_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void *obj, void *buf, off_t offset, size_t size, struct _starpu_async_channel *channel)
  128. {
  129. int pos = get_location_with_node(dst_node);
  130. if (channel != NULL)
  131. {
  132. if (disk_register_list[pos]->functions->async_write == NULL)
  133. channel = NULL;
  134. else
  135. {
  136. channel->type = STARPU_DISK_RAM;
  137. channel->event.disk_event.memory_node = dst_node;
  138. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  139. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_write(disk_register_list[pos]->base, obj, buf, offset, size);
  140. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  141. }
  142. }
  143. /* asynchronous request failed or synchronous request is asked */
  144. if (channel == NULL || !channel->event.disk_event.backend_event)
  145. {
  146. disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size);
  147. return 0;
  148. }
  149. return -EAGAIN;
  150. }
  151. int _starpu_disk_copy(unsigned node_src, void *obj_src, off_t offset_src, unsigned node_dst, void *obj_dst, off_t offset_dst, size_t size, struct _starpu_async_channel *channel)
  152. {
  153. int pos_src = get_location_with_node(node_src);
  154. int pos_dst = get_location_with_node(node_dst);
  155. /* both nodes have same copy function */
  156. channel->event.disk_event.memory_node = node_src;
  157. channel->event.disk_event.backend_event = disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  158. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  159. size);
  160. STARPU_ASSERT(channel->event.disk_event.backend_event);
  161. return -EAGAIN;
  162. }
  163. int _starpu_disk_full_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void *obj, void **ptr, size_t *size, struct _starpu_async_channel *channel)
  164. {
  165. int pos = get_location_with_node(src_node);
  166. if (channel != NULL)
  167. {
  168. if (disk_register_list[pos]->functions->async_full_read == NULL)
  169. channel = NULL;
  170. else
  171. {
  172. channel->type = STARPU_DISK_RAM;
  173. channel->event.disk_event.memory_node = src_node;
  174. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  175. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_read(disk_register_list[pos]->base, obj, ptr, size);
  176. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  177. }
  178. }
  179. /* asynchronous request failed or synchronous request is asked */
  180. if (channel == NULL || !channel->event.disk_event.backend_event)
  181. {
  182. disk_register_list[pos]->functions->full_read(disk_register_list[pos]->base, obj, ptr, size);
  183. return 0;
  184. }
  185. return -EAGAIN;
  186. }
  187. int _starpu_disk_full_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void *obj, void *ptr, size_t size, struct _starpu_async_channel *channel)
  188. {
  189. int pos = get_location_with_node(dst_node);
  190. if (channel != NULL)
  191. {
  192. if (disk_register_list[pos]->functions->async_full_write == NULL)
  193. channel = NULL;
  194. else
  195. {
  196. channel->type = STARPU_DISK_RAM;
  197. channel->event.disk_event.memory_node = dst_node;
  198. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  199. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_write(disk_register_list[pos]->base, obj, ptr, size);
  200. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  201. }
  202. }
  203. /* asynchronous request failed or synchronous request is asked */
  204. if (channel == NULL || !channel->event.disk_event.backend_event)
  205. {
  206. disk_register_list[pos]->functions->full_write(disk_register_list[pos]->base, obj, ptr, size);
  207. return 0;
  208. }
  209. return -EAGAIN;
  210. }
  211. void *starpu_disk_open(unsigned node, void *pos, size_t size)
  212. {
  213. int position = get_location_with_node(node);
  214. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  215. }
  216. void starpu_disk_close(unsigned node, void *obj, size_t size)
  217. {
  218. int position = get_location_with_node(node);
  219. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  220. }
  221. void starpu_disk_wait_request(struct _starpu_async_channel *async_channel)
  222. {
  223. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  224. disk_register_list[position]->functions->wait_request(async_channel->event.disk_event.backend_event);
  225. }
  226. int starpu_disk_test_request(struct _starpu_async_channel *async_channel)
  227. {
  228. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  229. return disk_register_list[position]->functions->test_request(async_channel->event.disk_event.backend_event);
  230. }
  231. void starpu_disk_free_request(struct _starpu_async_channel *async_channel)
  232. {
  233. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  234. if (async_channel->event.disk_event.backend_event)
  235. disk_register_list[position]->functions->free_request(async_channel->event.disk_event.backend_event);
  236. }
  237. static void add_disk_in_list(unsigned node, struct starpu_disk_ops *func, void *base)
  238. {
  239. /* initialization */
  240. if (disk_register_list == NULL)
  241. {
  242. _STARPU_MALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  243. }
  244. /* small size -> new size */
  245. if ((disk_number+1) > size_register_list)
  246. {
  247. size_register_list *= 2;
  248. _STARPU_REALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  249. }
  250. struct disk_register *dr;
  251. _STARPU_MALLOC(dr, sizeof(struct disk_register));
  252. dr->node = node;
  253. dr->base = base;
  254. dr->flag = STARPU_DISK_ALL;
  255. dr->functions = func;
  256. disk_register_list[++disk_number] = dr;
  257. }
  258. static int get_location_with_node(unsigned node)
  259. {
  260. #ifdef STARPU_DEVEL
  261. #warning optimize with a MAXNODE array
  262. #endif
  263. int i;
  264. for (i = 0; i <= disk_number; ++i)
  265. if (disk_register_list[i]->node == node)
  266. return i;
  267. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  268. return -1;
  269. }
  270. int _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  271. {
  272. if (starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  273. {
  274. int pos1 = get_location_with_node(node1);
  275. int pos2 = get_location_with_node(node2);
  276. if (disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  277. /* they must have a copy function */
  278. if (disk_register_list[pos1]->functions->copy != NULL)
  279. return 1;
  280. }
  281. return 0;
  282. }
  283. void _starpu_set_disk_flag(unsigned node, int flag)
  284. {
  285. int pos = get_location_with_node(node);
  286. disk_register_list[pos]->flag = flag;
  287. }
  288. int _starpu_get_disk_flag(unsigned node)
  289. {
  290. int pos = get_location_with_node(node);
  291. return disk_register_list[pos]->flag;
  292. }
  293. void _starpu_swap_init(void)
  294. {
  295. char *backend;
  296. char *path;
  297. starpu_ssize_t size;
  298. struct starpu_disk_ops *ops;
  299. path = starpu_getenv("STARPU_DISK_SWAP");
  300. if (!path)
  301. return;
  302. backend = starpu_getenv("STARPU_DISK_SWAP_BACKEND");
  303. if (!backend)
  304. {
  305. _starpu_mkpath(path, S_IRWXU);
  306. ops = &starpu_disk_unistd_ops;
  307. }
  308. else if (!strcmp(backend, "stdio"))
  309. {
  310. _starpu_mkpath(path, S_IRWXU);
  311. ops = &starpu_disk_stdio_ops;
  312. }
  313. else if (!strcmp(backend, "unistd"))
  314. {
  315. _starpu_mkpath(path, S_IRWXU);
  316. ops = &starpu_disk_unistd_ops;
  317. }
  318. else if (!strcmp(backend, "unistd_o_direct"))
  319. {
  320. #ifdef STARPU_LINUX_SYS
  321. _starpu_mkpath(path, S_IRWXU);
  322. ops = &starpu_disk_unistd_o_direct_ops;
  323. #else
  324. _STARPU_DISP("Warning: o_direct support is not compiled in, could not enable disk swap");
  325. return;
  326. #endif
  327. }
  328. else if (!strcmp(backend, "leveldb"))
  329. {
  330. #ifdef STARPU_HAVE_LEVELDB
  331. ops = &starpu_disk_leveldb_ops;
  332. #else
  333. _STARPU_DISP("Warning: leveldb support is not compiled in, could not enable disk swap");
  334. return;
  335. #endif
  336. }
  337. else
  338. {
  339. _STARPU_DISP("Warning: unknown disk swap backend %s, could not enable disk swap", backend);
  340. return;
  341. }
  342. size = starpu_get_env_number_default("STARPU_DISK_SWAP_SIZE", -1);
  343. starpu_disk_swap_node = starpu_disk_register(ops, path, ((size_t) size) << 20);
  344. if (starpu_disk_swap_node < 0)
  345. {
  346. _STARPU_DISP("Warning: could not enable disk swap %s on %s with size %ld, could not enable disk swap", backend, path, (long) size);
  347. return;
  348. }
  349. }