disk.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_register(struct starpu_disk_ops *func, void *parameter, starpu_ssize_t size)
  51. {
  52. STARPU_ASSERT_MSG(size < 0 || size >= SIZE_DISK_MIN,"Minimum disk size is %u Bytes ! (Here %u) \n", (int) SIZE_DISK_MIN, (int) size);
  53. /* register disk */
  54. unsigned memory_node = _starpu_memory_node_register(STARPU_DISK_RAM, 0);
  55. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  56. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  57. /* connect disk */
  58. void *base = func->plug(parameter, size);
  59. /* remember it */
  60. add_disk_in_list(memory_node,func,base);
  61. int ret = func->bandwidth(memory_node);
  62. /* have a problem with the disk */
  63. if (ret == 0)
  64. return -ENOENT;
  65. if (size >= 0)
  66. _starpu_memory_manager_set_global_memory_size(memory_node, size);
  67. return memory_node;
  68. }
  69. void _starpu_disk_unregister(void)
  70. {
  71. int i;
  72. /* search disk and delete it */
  73. for (i = 0; i <= disk_number; ++i)
  74. {
  75. _starpu_set_disk_flag(disk_register_list[i]->node, STARPU_DISK_NO_RECLAIM);
  76. _starpu_free_all_automatically_allocated_buffers(disk_register_list[i]->node);
  77. /* don't forget to unplug */
  78. disk_register_list[i]->functions->unplug(disk_register_list[i]->base);
  79. free(disk_register_list[i]);
  80. }
  81. /* no disk in the list -> delete the list */
  82. disk_number--;
  83. if (disk_register_list != NULL && disk_number == -1)
  84. {
  85. free(disk_register_list);
  86. disk_register_list = NULL;
  87. }
  88. }
  89. /* interface between user and disk memory */
  90. void *_starpu_disk_alloc(unsigned node, size_t size)
  91. {
  92. int pos = get_location_with_node(node);
  93. return disk_register_list[pos]->functions->alloc(disk_register_list[pos]->base, size);
  94. }
  95. void _starpu_disk_free(unsigned node, void *obj, size_t size)
  96. {
  97. int pos = get_location_with_node(node);
  98. disk_register_list[pos]->functions->free(disk_register_list[pos]->base, obj, size);
  99. }
  100. /* src_node == disk node and dst_node == STARPU_MAIN_RAM */
  101. 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)
  102. {
  103. int pos = get_location_with_node(src_node);
  104. if (channel != NULL)
  105. {
  106. if (disk_register_list[pos]->functions->async_read == NULL)
  107. channel = NULL;
  108. else
  109. {
  110. channel->type = STARPU_DISK_RAM;
  111. channel->event.disk_event.memory_node = src_node;
  112. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  113. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_read(disk_register_list[pos]->base, obj, buf, offset, size);
  114. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  115. }
  116. }
  117. /* asynchronous request failed or synchronous request is asked */
  118. if (channel == NULL || !channel->event.disk_event.backend_event)
  119. {
  120. disk_register_list[pos]->functions->read(disk_register_list[pos]->base, obj, buf, offset, size);
  121. return 0;
  122. }
  123. return -EAGAIN;
  124. }
  125. /* src_node == STARPU_MAIN_RAM and dst_node == disk node */
  126. 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)
  127. {
  128. int pos = get_location_with_node(dst_node);
  129. if (channel != NULL)
  130. {
  131. if (disk_register_list[pos]->functions->async_write == NULL)
  132. channel = NULL;
  133. else
  134. {
  135. channel->type = STARPU_DISK_RAM;
  136. channel->event.disk_event.memory_node = dst_node;
  137. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  138. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_write(disk_register_list[pos]->base, obj, buf, offset, size);
  139. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  140. }
  141. }
  142. /* asynchronous request failed or synchronous request is asked */
  143. if (channel == NULL || !channel->event.disk_event.backend_event)
  144. {
  145. disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size);
  146. return 0;
  147. }
  148. return -EAGAIN;
  149. }
  150. 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)
  151. {
  152. int pos_src = get_location_with_node(node_src);
  153. int pos_dst = get_location_with_node(node_dst);
  154. /* both nodes have same copy function */
  155. channel->event.disk_event.memory_node = node_src;
  156. channel->event.disk_event.backend_event = disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  157. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  158. size);
  159. STARPU_ASSERT(channel->event.disk_event.backend_event);
  160. return -EAGAIN;
  161. }
  162. 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)
  163. {
  164. int pos = get_location_with_node(src_node);
  165. if (channel != NULL)
  166. {
  167. if (disk_register_list[pos]->functions->async_full_read == NULL)
  168. channel = NULL;
  169. else
  170. {
  171. channel->type = STARPU_DISK_RAM;
  172. channel->event.disk_event.memory_node = src_node;
  173. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  174. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_read(disk_register_list[pos]->base, obj, ptr, size);
  175. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  176. }
  177. }
  178. /* asynchronous request failed or synchronous request is asked */
  179. if (channel == NULL || !channel->event.disk_event.backend_event)
  180. {
  181. disk_register_list[pos]->functions->full_read(disk_register_list[pos]->base, obj, ptr, size);
  182. return 0;
  183. }
  184. return -EAGAIN;
  185. }
  186. 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)
  187. {
  188. int pos = get_location_with_node(dst_node);
  189. if (channel != NULL)
  190. {
  191. if (disk_register_list[pos]->functions->async_full_write == NULL)
  192. channel = NULL;
  193. else
  194. {
  195. channel->type = STARPU_DISK_RAM;
  196. channel->event.disk_event.memory_node = dst_node;
  197. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  198. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_full_write(disk_register_list[pos]->base, obj, ptr, size);
  199. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  200. }
  201. }
  202. /* asynchronous request failed or synchronous request is asked */
  203. if (channel == NULL || !channel->event.disk_event.backend_event)
  204. {
  205. disk_register_list[pos]->functions->full_write(disk_register_list[pos]->base, obj, ptr, size);
  206. return 0;
  207. }
  208. return -EAGAIN;
  209. }
  210. void *starpu_disk_open(unsigned node, void *pos, size_t size)
  211. {
  212. int position = get_location_with_node(node);
  213. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  214. }
  215. void starpu_disk_close(unsigned node, void *obj, size_t size)
  216. {
  217. int position = get_location_with_node(node);
  218. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  219. }
  220. void starpu_disk_wait_request(struct _starpu_async_channel *async_channel)
  221. {
  222. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  223. disk_register_list[position]->functions->wait_request(async_channel->event.disk_event.backend_event);
  224. }
  225. int starpu_disk_test_request(struct _starpu_async_channel *async_channel)
  226. {
  227. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  228. return disk_register_list[position]->functions->test_request(async_channel->event.disk_event.backend_event);
  229. }
  230. void starpu_disk_free_request(struct _starpu_async_channel *async_channel)
  231. {
  232. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  233. if (async_channel->event.disk_event.backend_event)
  234. disk_register_list[position]->functions->free_request(async_channel->event.disk_event.backend_event);
  235. }
  236. static void add_disk_in_list(unsigned node, struct starpu_disk_ops *func, void *base)
  237. {
  238. /* initialization */
  239. if (disk_register_list == NULL)
  240. {
  241. _STARPU_MALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  242. }
  243. /* small size -> new size */
  244. if ((disk_number+1) > size_register_list)
  245. {
  246. size_register_list *= 2;
  247. _STARPU_REALLOC(disk_register_list, size_register_list*sizeof(struct disk_register *));
  248. }
  249. struct disk_register *dr;
  250. _STARPU_MALLOC(dr, sizeof(struct disk_register));
  251. dr->node = node;
  252. dr->base = base;
  253. dr->flag = STARPU_DISK_ALL;
  254. dr->functions = func;
  255. disk_register_list[++disk_number] = dr;
  256. }
  257. static int get_location_with_node(unsigned node)
  258. {
  259. #ifdef STARPU_DEVEL
  260. #warning optimize with a MAXNODE array
  261. #endif
  262. int i;
  263. for (i = 0; i <= disk_number; ++i)
  264. if (disk_register_list[i]->node == node)
  265. return i;
  266. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  267. return -1;
  268. }
  269. int _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  270. {
  271. if (starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  272. {
  273. int pos1 = get_location_with_node(node1);
  274. int pos2 = get_location_with_node(node2);
  275. if (disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  276. /* they must have a copy function */
  277. if (disk_register_list[pos1]->functions->copy != NULL)
  278. return 1;
  279. }
  280. return 0;
  281. }
  282. void _starpu_set_disk_flag(unsigned node, int flag)
  283. {
  284. int pos = get_location_with_node(node);
  285. disk_register_list[pos]->flag = flag;
  286. }
  287. int _starpu_get_disk_flag(unsigned node)
  288. {
  289. int pos = get_location_with_node(node);
  290. return disk_register_list[pos]->flag;
  291. }
  292. void _starpu_swap_init(void)
  293. {
  294. char *backend;
  295. char *path;
  296. starpu_ssize_t size;
  297. struct starpu_disk_ops *ops;
  298. int dd;
  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. dd = starpu_disk_register(ops, path, size);
  344. if (dd < 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. }