disk.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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);
  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.memory_node = src_node;
  113. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  114. channel->event.disk_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)
  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.memory_node = dst_node;
  139. _STARPU_TRACE_START_DRIVER_COPY_ASYNC(src_node, dst_node);
  140. channel->event.disk_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)
  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 = disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  159. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  160. size);
  161. STARPU_ASSERT(channel->event.disk_event);
  162. return -EAGAIN;
  163. }
  164. int _starpu_disk_full_read(unsigned src_node, unsigned dst_node STARPU_ATTRIBUTE_UNUSED, void * obj, void ** ptr, size_t * size)
  165. {
  166. int pos = get_location_with_node(src_node);
  167. return disk_register_list[pos]->functions->full_read(src_node, disk_register_list[pos]->base, obj, ptr, size);
  168. }
  169. int _starpu_disk_full_write(unsigned src_node STARPU_ATTRIBUTE_UNUSED, unsigned dst_node, void * obj, void * ptr, size_t size)
  170. {
  171. int pos = get_location_with_node(dst_node);
  172. return disk_register_list[pos]->functions->full_write(dst_node, disk_register_list[pos]->base, obj, ptr, size);
  173. }
  174. void *
  175. starpu_disk_open(unsigned node, void *pos, size_t size)
  176. {
  177. int position = get_location_with_node(node);
  178. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  179. }
  180. void
  181. starpu_disk_close(unsigned node, void *obj, size_t size)
  182. {
  183. int position = get_location_with_node(node);
  184. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  185. }
  186. void starpu_disk_wait_request(struct _starpu_async_channel *async_channel)
  187. {
  188. int position = get_location_with_node(async_channel->event.memory_node);
  189. disk_register_list[position]->functions->wait_request(async_channel->event.disk_event);
  190. }
  191. int starpu_disk_test_request(struct _starpu_async_channel *async_channel)
  192. {
  193. int position = get_location_with_node(async_channel->event.memory_node);
  194. return disk_register_list[position]->functions->test_request(async_channel->event.disk_event);
  195. }
  196. void starpu_disk_free_request(struct _starpu_async_channel *async_channel)
  197. {
  198. int position = get_location_with_node(async_channel->event.memory_node);
  199. if (async_channel->event.disk_event)
  200. disk_register_list[position]->functions->free_request(async_channel->event.disk_event);
  201. }
  202. static void
  203. add_disk_in_list(unsigned node, struct starpu_disk_ops * func, void * base)
  204. {
  205. /* initialization */
  206. if(disk_register_list == NULL)
  207. {
  208. disk_register_list = malloc(size_register_list*sizeof(struct disk_register *));
  209. STARPU_ASSERT(disk_register_list != NULL);
  210. }
  211. /* small size -> new size */
  212. if((disk_number+1) > size_register_list)
  213. {
  214. struct disk_register ** ptr_realloc = realloc(disk_register_list, 2*size_register_list*sizeof(struct disk_register *));
  215. if (ptr_realloc != NULL)
  216. {
  217. size_register_list *= 2;
  218. disk_register_list = ptr_realloc;
  219. }
  220. else
  221. {
  222. STARPU_ASSERT(ptr_realloc != NULL);
  223. }
  224. }
  225. struct disk_register * dr = malloc(sizeof(struct disk_register));
  226. STARPU_ASSERT(dr != NULL);
  227. dr->node = node;
  228. dr->base = base;
  229. dr->flag = STARPU_DISK_ALL;
  230. dr->functions = func;
  231. disk_register_list[++disk_number] = dr;
  232. }
  233. static int
  234. get_location_with_node(unsigned node)
  235. {
  236. #ifdef STARPU_DEVEL
  237. #warning optimize with a MAXNODE array
  238. #endif
  239. int i;
  240. for(i = 0; i <= disk_number; ++i)
  241. if (disk_register_list[i]->node == node)
  242. return i;
  243. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  244. return -1;
  245. }
  246. int
  247. _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  248. {
  249. if(starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  250. {
  251. int pos1 = get_location_with_node(node1);
  252. int pos2 = get_location_with_node(node2);
  253. if(disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  254. /* they must have a copy function */
  255. if(disk_register_list[pos1]->functions->copy != NULL)
  256. return 1;
  257. }
  258. return 0;
  259. }
  260. void
  261. _starpu_set_disk_flag(unsigned node, int flag)
  262. {
  263. int pos = get_location_with_node(node);
  264. disk_register_list[pos]->flag = flag;
  265. }
  266. int
  267. _starpu_get_disk_flag(unsigned node)
  268. {
  269. int pos = get_location_with_node(node);
  270. return disk_register_list[pos]->flag;
  271. }