disk.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <fcntl.h>
  17. #include <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <sys/stat.h>
  22. #include <time.h>
  23. #include <common/config.h>
  24. #include <core/debug.h>
  25. #include <core/disk.h>
  26. #include <core/workers.h>
  27. #include <core/perfmodel/perfmodel.h>
  28. #include <core/topology.h>
  29. #include <datawizard/memory_manager.h>
  30. #include <datawizard/memalloc.h>
  31. #include <drivers/cuda/driver_cuda.h>
  32. #include <drivers/opencl/driver_opencl.h>
  33. #include <profiling/profiling.h>
  34. #include <common/uthash.h>
  35. struct disk_register {
  36. unsigned node;
  37. void * base;
  38. struct starpu_disk_ops * functions;
  39. /* disk condition (1 = all authorizations, */
  40. int flag;
  41. };
  42. static void add_disk_in_list(unsigned node, struct starpu_disk_ops * func, void * base);
  43. static int get_location_with_node(unsigned node);
  44. static struct disk_register ** disk_register_list = NULL;
  45. static int disk_number = -1;
  46. static int size_register_list = 2;
  47. int
  48. starpu_disk_register(struct starpu_disk_ops * func, void *parameter, size_t size)
  49. {
  50. STARPU_ASSERT_MSG(size >= SIZE_DISK_MIN,"Minimum disk size is %u Bytes ! (Here %u) \n", (int) SIZE_DISK_MIN, (int) size);
  51. /* register disk */
  52. unsigned memory_node = _starpu_memory_node_register(STARPU_DISK_RAM, 0);
  53. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  54. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  55. /* connect disk */
  56. void * base = func->plug(parameter, size);
  57. /* remember it */
  58. add_disk_in_list(memory_node,func,base);
  59. int ret = func->bandwidth(memory_node);
  60. /* have a problem with the disk */
  61. if(ret == 0)
  62. return -ENOENT;
  63. _starpu_memory_manager_set_global_memory_size(memory_node, size);
  64. return memory_node;
  65. }
  66. void
  67. _starpu_disk_unregister(void)
  68. {
  69. int i;
  70. /* search disk and delete it */
  71. for (i = 0; i <= disk_number; ++i)
  72. {
  73. _starpu_set_disk_flag(disk_register_list[i]->node, STARPU_DISK_NO_RECLAIM);
  74. _starpu_free_all_automatically_allocated_buffers(disk_register_list[i]->node);
  75. /* don't forget to unplug */
  76. disk_register_list[i]->functions->unplug(disk_register_list[i]->base);
  77. free(disk_register_list[i]);
  78. }
  79. /* no disk in the list -> delete the list */
  80. disk_number--;
  81. if (disk_register_list != NULL && disk_number == -1)
  82. {
  83. free(disk_register_list);
  84. disk_register_list = NULL;
  85. }
  86. }
  87. /* interface between user and disk memory */
  88. void *
  89. _starpu_disk_alloc(unsigned node, size_t size)
  90. {
  91. int pos = get_location_with_node(node);
  92. return disk_register_list[pos]->functions->alloc(disk_register_list[pos]->base, size);
  93. }
  94. void
  95. _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
  102. _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
  128. _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)
  129. {
  130. int pos = get_location_with_node(dst_node);
  131. if (channel != NULL)
  132. {
  133. if (disk_register_list[pos]->functions->async_write == NULL)
  134. channel = NULL;
  135. else
  136. {
  137. channel->type = STARPU_DISK_RAM;
  138. channel->event.disk_event.memory_node = dst_node;
  139. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  140. channel->event.disk_event.backend_event = disk_register_list[pos]->functions->async_write(disk_register_list[pos]->base, obj, buf, offset, size);
  141. _STARPU_TRACE_END_DRIVER_COPY_ASYNC(src_node, dst_node);
  142. }
  143. }
  144. /* asynchronous request failed or synchronous request is asked */
  145. if (channel == NULL || !channel->event.disk_event.backend_event)
  146. {
  147. disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size);
  148. return 0;
  149. }
  150. return -EAGAIN;
  151. }
  152. int
  153. _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)
  154. {
  155. int pos_src = get_location_with_node(node_src);
  156. int pos_dst = get_location_with_node(node_dst);
  157. /* both nodes have same copy function */
  158. channel->event.disk_event.memory_node = node_src;
  159. channel->event.disk_event.backend_event = disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  160. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  161. size);
  162. STARPU_ASSERT(channel->event.disk_event.backend_event);
  163. return -EAGAIN;
  164. }
  165. int _starpu_disk_full_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void * obj, void ** ptr, size_t * size)
  166. {
  167. int pos = get_location_with_node(src_node);
  168. return disk_register_list[pos]->functions->full_read(src_node, disk_register_list[pos]->base, obj, ptr, size);
  169. }
  170. int _starpu_disk_full_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void * obj, void * ptr, size_t size)
  171. {
  172. int pos = get_location_with_node(dst_node);
  173. return disk_register_list[pos]->functions->full_write(dst_node, disk_register_list[pos]->base, obj, ptr, size);
  174. }
  175. void *
  176. starpu_disk_open(unsigned node, void *pos, size_t size)
  177. {
  178. int position = get_location_with_node(node);
  179. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  180. }
  181. void
  182. starpu_disk_close(unsigned node, void *obj, size_t size)
  183. {
  184. int position = get_location_with_node(node);
  185. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  186. }
  187. void starpu_disk_wait_request(struct _starpu_async_channel *async_channel)
  188. {
  189. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  190. disk_register_list[position]->functions->wait_request(async_channel->event.disk_event.backend_event);
  191. }
  192. int starpu_disk_test_request(struct _starpu_async_channel *async_channel)
  193. {
  194. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  195. return disk_register_list[position]->functions->test_request(async_channel->event.disk_event.backend_event);
  196. }
  197. void starpu_disk_free_request(struct _starpu_async_channel *async_channel)
  198. {
  199. int position = get_location_with_node(async_channel->event.disk_event.memory_node);
  200. if (async_channel->event.disk_event.backend_event)
  201. disk_register_list[position]->functions->free_request(async_channel->event.disk_event.backend_event);
  202. }
  203. static void
  204. add_disk_in_list(unsigned node, struct starpu_disk_ops * func, void * base)
  205. {
  206. /* initialization */
  207. if(disk_register_list == NULL)
  208. {
  209. disk_register_list = malloc(size_register_list*sizeof(struct disk_register *));
  210. STARPU_ASSERT(disk_register_list != NULL);
  211. }
  212. /* small size -> new size */
  213. if((disk_number+1) > size_register_list)
  214. {
  215. struct disk_register ** ptr_realloc = realloc(disk_register_list, 2*size_register_list*sizeof(struct disk_register *));
  216. if (ptr_realloc != NULL)
  217. {
  218. size_register_list *= 2;
  219. disk_register_list = ptr_realloc;
  220. }
  221. else
  222. {
  223. STARPU_ASSERT(ptr_realloc != NULL);
  224. }
  225. }
  226. struct disk_register * dr = malloc(sizeof(struct disk_register));
  227. STARPU_ASSERT(dr != NULL);
  228. dr->node = node;
  229. dr->base = base;
  230. dr->flag = STARPU_DISK_ALL;
  231. dr->functions = func;
  232. disk_register_list[++disk_number] = dr;
  233. }
  234. static int
  235. get_location_with_node(unsigned node)
  236. {
  237. #ifdef STARPU_DEVEL
  238. #warning optimize with a MAXNODE array
  239. #endif
  240. int i;
  241. for(i = 0; i <= disk_number; ++i)
  242. if (disk_register_list[i]->node == node)
  243. return i;
  244. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  245. return -1;
  246. }
  247. int
  248. _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  249. {
  250. if(starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  251. {
  252. int pos1 = get_location_with_node(node1);
  253. int pos2 = get_location_with_node(node2);
  254. if(disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  255. /* they must have a copy function */
  256. if(disk_register_list[pos1]->functions->copy != NULL)
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. void
  262. _starpu_set_disk_flag(unsigned node, int flag)
  263. {
  264. int pos = get_location_with_node(node);
  265. disk_register_list[pos]->flag = flag;
  266. }
  267. int
  268. _starpu_get_disk_flag(unsigned node)
  269. {
  270. int pos = get_location_with_node(node);
  271. return disk_register_list[pos]->flag;
  272. }