disk.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. ssize_t
  101. _starpu_disk_read(unsigned node, void *obj, void *buf, off_t offset, size_t size, void * _starpu_aiocb_disk)
  102. {
  103. int pos = get_location_with_node(node);
  104. return disk_register_list[pos]->functions->read(disk_register_list[pos]->base, obj, buf, offset, size, _starpu_aiocb_disk);
  105. }
  106. ssize_t
  107. _starpu_disk_write(unsigned node, void *obj, const void *buf, off_t offset, size_t size, void * _starpu_aiocb_disk)
  108. {
  109. int pos = get_location_with_node(node);
  110. return disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size, _starpu_aiocb_disk);
  111. }
  112. int
  113. _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, void * _starpu_aiocb_disk)
  114. {
  115. int pos_src = get_location_with_node(node_src);
  116. int pos_dst = get_location_with_node(node_dst);
  117. /* both nodes have same copy function */
  118. return disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  119. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  120. size);
  121. }
  122. void *
  123. starpu_disk_open(unsigned node, void *pos, size_t size)
  124. {
  125. int position = get_location_with_node(node);
  126. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  127. }
  128. void
  129. starpu_disk_close(unsigned node, void *obj, size_t size)
  130. {
  131. int position = get_location_with_node(node);
  132. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  133. }
  134. static void
  135. add_disk_in_list(unsigned node, struct starpu_disk_ops * func, void * base)
  136. {
  137. /* initialization */
  138. if(disk_register_list == NULL)
  139. {
  140. disk_register_list = malloc(size_register_list*sizeof(struct disk_register *));
  141. STARPU_ASSERT(disk_register_list != NULL);
  142. }
  143. /* small size -> new size */
  144. if((disk_number+1) > size_register_list)
  145. {
  146. struct disk_register ** ptr_realloc = realloc(disk_register_list, 2*size_register_list*sizeof(struct disk_register *));
  147. if (ptr_realloc != NULL)
  148. {
  149. size_register_list *= 2;
  150. disk_register_list = ptr_realloc;
  151. }
  152. else
  153. {
  154. STARPU_ASSERT(ptr_realloc != NULL);
  155. }
  156. }
  157. struct disk_register * dr = malloc(sizeof(struct disk_register));
  158. STARPU_ASSERT(dr != NULL);
  159. dr->node = node;
  160. dr->base = base;
  161. dr->flag = STARPU_DISK_ALL;
  162. dr->functions = func;
  163. disk_register_list[++disk_number] = dr;
  164. }
  165. static int
  166. get_location_with_node(unsigned node)
  167. {
  168. int i;
  169. for(i = 0; i <= disk_number; ++i)
  170. if (disk_register_list[i]->node == node)
  171. return i;
  172. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  173. return -1;
  174. }
  175. int
  176. _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  177. {
  178. if(starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  179. {
  180. int pos1 = get_location_with_node(node1);
  181. int pos2 = get_location_with_node(node2);
  182. if(disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  183. /* they must have a copy function */
  184. if(disk_register_list[pos1]->functions->copy != NULL)
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. void
  190. _starpu_set_disk_flag(unsigned node, int flag)
  191. {
  192. int pos = get_location_with_node(node);
  193. disk_register_list[pos]->flag = flag;
  194. }
  195. int
  196. _starpu_get_disk_flag(unsigned node)
  197. {
  198. int pos = get_location_with_node(node);
  199. return disk_register_list[pos]->flag;
  200. }