disk.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 <datawizard/memory_manager.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 <drivers/cuda/driver_cuda.h>
  31. #include <drivers/opencl/driver_opencl.h>
  32. #include <profiling/profiling.h>
  33. #include <common/uthash.h>
  34. struct disk_register {
  35. unsigned node;
  36. void * base;
  37. struct disk_ops * functions;
  38. };
  39. static void add_disk_in_list(unsigned node, struct disk_ops * func, void * base);
  40. static int get_location_with_node(unsigned node);
  41. static struct disk_register ** disk_register_list = NULL;
  42. static int disk_number = -1;
  43. static int size_register_list = 2;
  44. int
  45. starpu_disk_register(struct disk_ops * func, void *parameter, size_t size)
  46. {
  47. STARPU_ASSERT_MSG(size >= SIZE_DISK_MIN,"Minimum disk size is %u Bytes ! (Here %u) \n", (int) SIZE_DISK_MIN, (int) size);
  48. /* register disk */
  49. unsigned memory_node = _starpu_memory_node_register(STARPU_DISK_RAM, 0);
  50. _starpu_register_bus(STARPU_MAIN_RAM, memory_node);
  51. _starpu_register_bus(memory_node, STARPU_MAIN_RAM);
  52. /* connect disk */
  53. void * base = func->plug(parameter);
  54. /* remember it */
  55. add_disk_in_list(memory_node,func,base);
  56. int ret = func->bandwidth(memory_node);
  57. /* have a problem with the disk */
  58. if(ret == 0)
  59. return -ENOENT;
  60. _starpu_memory_manager_set_global_memory_size(memory_node, size);
  61. return memory_node;
  62. }
  63. void
  64. starpu_disk_unregister(unsigned node)
  65. {
  66. bool find = false;
  67. int i;
  68. /* search disk and delete it */
  69. for (i = 0; i <= disk_number; ++i)
  70. {
  71. if (find)
  72. disk_register_list[i-1] = disk_register_list[i];
  73. if (disk_register_list[i]->node == node)
  74. {
  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. find = true;
  79. }
  80. }
  81. /* no disk in the list -> delete the list */
  82. STARPU_ASSERT_MSG(find, "Disk node not found !(%u) ", node);
  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 *
  92. _starpu_disk_alloc(unsigned node, size_t size)
  93. {
  94. int pos = get_location_with_node(node);
  95. return disk_register_list[pos]->functions->alloc(disk_register_list[pos]->base, size);
  96. }
  97. void
  98. _starpu_disk_free(unsigned node, void *obj, size_t size)
  99. {
  100. int pos = get_location_with_node(node);
  101. disk_register_list[pos]->functions->free(disk_register_list[pos]->base, obj, size);
  102. }
  103. ssize_t
  104. _starpu_disk_read(unsigned node, void *obj, void *buf, off_t offset, size_t size)
  105. {
  106. int pos = get_location_with_node(node);
  107. return disk_register_list[pos]->functions->read(disk_register_list[pos]->base, obj, buf, offset, size);
  108. }
  109. ssize_t
  110. _starpu_disk_write(unsigned node, void *obj, const void *buf, off_t offset, size_t size)
  111. {
  112. int pos = get_location_with_node(node);
  113. return disk_register_list[pos]->functions->write(disk_register_list[pos]->base, obj, buf, offset, size);
  114. }
  115. int
  116. _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)
  117. {
  118. int pos_src = get_location_with_node(node_src);
  119. int pos_dst = get_location_with_node(node_dst);
  120. /* both nodes have same copy function */
  121. return disk_register_list[pos_src]->functions->copy(disk_register_list[pos_src]->base, obj_src, offset_src,
  122. disk_register_list[pos_dst]->base, obj_dst, offset_dst,
  123. size);
  124. }
  125. void *
  126. _starpu_disk_open(unsigned node, void *pos, size_t size)
  127. {
  128. int position = get_location_with_node(node);
  129. return disk_register_list[position]->functions->open(disk_register_list[position]->base, pos, size);
  130. }
  131. void
  132. _starpu_disk_close(unsigned node, void *obj, size_t size)
  133. {
  134. int position = get_location_with_node(node);
  135. disk_register_list[position]->functions->close(disk_register_list[position]->base, obj, size);
  136. }
  137. static void
  138. add_disk_in_list(unsigned node, struct disk_ops * func, void * base)
  139. {
  140. /* initialization */
  141. if(disk_register_list == NULL)
  142. {
  143. disk_register_list = malloc(size_register_list*sizeof(struct disk_register *));
  144. STARPU_ASSERT(disk_register_list != NULL);
  145. }
  146. /* small size -> new size */
  147. if((disk_number+1) > size_register_list)
  148. {
  149. struct disk_register ** ptr_realloc = realloc(disk_register_list, 2*size_register_list*sizeof(struct disk_register *));
  150. if (ptr_realloc != NULL)
  151. {
  152. size_register_list *= 2;
  153. disk_register_list = ptr_realloc;
  154. }
  155. else
  156. {
  157. STARPU_ASSERT(ptr_realloc != NULL);
  158. }
  159. }
  160. struct disk_register * dr = malloc(sizeof(struct disk_register));
  161. STARPU_ASSERT(dr != NULL);
  162. dr->node = node;
  163. dr->base = base;
  164. dr->functions = func;
  165. disk_register_list[++disk_number] = dr;
  166. }
  167. static int
  168. get_location_with_node(unsigned node)
  169. {
  170. int i;
  171. for(i = 0; i <= disk_number; ++i)
  172. if (disk_register_list[i]->node == node)
  173. return i;
  174. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  175. return -1;
  176. }
  177. int
  178. _starpu_is_same_kind_disk(unsigned node1, unsigned node2)
  179. {
  180. if(starpu_node_get_kind(node1) == STARPU_DISK_RAM && starpu_node_get_kind(node2) == STARPU_DISK_RAM)
  181. {
  182. int pos1 = get_location_with_node(node1);
  183. int pos2 = get_location_with_node(node2);
  184. if(disk_register_list[pos1]->functions == disk_register_list[pos2]->functions)
  185. /* they must have a copy function */
  186. if(disk_register_list[pos1]->functions->copy != NULL)
  187. return 1;
  188. }
  189. return 0;
  190. }