disk.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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 <stdlib.h>
  17. #include <stdio.h>
  18. #include <stdbool.h>
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #include <fcntl.h>
  22. #include <time.h>
  23. #include <common/config.h>
  24. #include <core/workers.h>
  25. #include <core/debug.h>
  26. #include <core/perfmodel/perfmodel.h>
  27. #include <datawizard/memory_manager.h>
  28. #include <core/topology.h>
  29. #include <drivers/cuda/driver_cuda.h>
  30. #include <drivers/opencl/driver_opencl.h>
  31. #include <profiling/profiling.h>
  32. #include <common/uthash.h>
  33. #define SIZE (1024*1024)
  34. #define NITER 64
  35. struct disk_register {
  36. unsigned node;
  37. void * base;
  38. struct disk_ops * functions;
  39. };
  40. static void add_disk_in_list(unsigned node, struct disk_ops * func, void * base);
  41. static int get_location_with_node(unsigned node);
  42. static struct disk_register ** disk_register_list = NULL;
  43. static int disk_number = -1;
  44. static int size_register_list = 2;
  45. unsigned
  46. starpu_disk_register(struct disk_ops * func, void *parameter, size_t size)
  47. {
  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. func->bandwidth(base,memory_node);
  57. _starpu_memory_manager_set_global_memory_size(memory_node, size);
  58. return memory_node;
  59. }
  60. void
  61. starpu_disk_unregister(unsigned node)
  62. {
  63. bool find = false;
  64. int i;
  65. /* search disk and delete it */
  66. for (i = 0; i <= disk_number; ++i)
  67. {
  68. if (find)
  69. disk_register_list[i-1] = disk_register_list[i];
  70. if (disk_register_list[i]->node == node)
  71. {
  72. /* don't forget to unplug */
  73. disk_register_list[i]->functions->unplug(disk_register_list[i]->base);
  74. free(disk_register_list[i]);
  75. find = true;
  76. }
  77. }
  78. /* no disk in the list -> delete the list */
  79. STARPU_ASSERT_MSG(find, "Disk node not found !(%u) ", node);
  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. static void
  101. add_disk_in_list(unsigned node, struct disk_ops * func, void * base)
  102. {
  103. /* initialization */
  104. if(disk_register_list == NULL)
  105. {
  106. disk_register_list = malloc(size_register_list*sizeof(struct disk_register *));
  107. STARPU_ASSERT(disk_register_list != NULL);
  108. }
  109. /* small size -> new size */
  110. if((disk_number+1) > size_register_list)
  111. {
  112. struct disk_register ** ptr_realloc = realloc(disk_register_list, 2*size_register_list*sizeof(struct disk_register *));
  113. if (ptr_realloc != NULL)
  114. {
  115. size_register_list *= 2;
  116. disk_register_list = ptr_realloc;
  117. }
  118. else
  119. {
  120. STARPU_ASSERT(ptr_realloc != NULL);
  121. }
  122. }
  123. struct disk_register * dr = malloc(sizeof(struct disk_register));
  124. STARPU_ASSERT(dr != NULL);
  125. dr->node = node;
  126. dr->base = base;
  127. dr->functions = func;
  128. disk_register_list[++disk_number] = dr;
  129. }
  130. static int
  131. get_location_with_node(unsigned node)
  132. {
  133. int i;
  134. for(i = 0; i <= disk_number; ++i)
  135. if (disk_register_list[i]->node == node)
  136. return i;
  137. STARPU_ASSERT_MSG(false, "Disk node not found !(%u) ", node);
  138. return -1;
  139. }
  140. /* use STDIO to write on disk */
  141. struct starpu_stdio_obj {
  142. int descriptor;
  143. FILE * file;
  144. char * path;
  145. double size;
  146. };
  147. /* allocation memory on disk */
  148. static void *
  149. starpu_stdio_alloc (void *base, size_t size)
  150. {
  151. struct starpu_stdio_obj * obj = malloc(sizeof(struct starpu_stdio_obj));
  152. STARPU_ASSERT(obj != NULL);
  153. int id = -1;
  154. /* create template for mkstemp */
  155. unsigned int sizeBase = 16;
  156. while(sizeBase < (strlen(base)+7))
  157. sizeBase *= 2;
  158. char * baseCpy = malloc(sizeBase*sizeof(char));
  159. STARPU_ASSERT(baseCpy != NULL);
  160. char * tmp = "XXXXXX";
  161. strcpy(baseCpy, (char *) base);
  162. strcat(baseCpy,tmp);
  163. id = mkstemp(baseCpy);
  164. STARPU_ASSERT_MSG(id >= 0, "Stdio allocation failed");
  165. FILE * f = fdopen(id, "rb+");
  166. STARPU_ASSERT_MSG(f != NULL, "Stdio allocation failed");
  167. int val = ftruncate(id,size);
  168. STARPU_ASSERT_MSG(val >= 0, "Stdio allocation failed");
  169. obj->descriptor = id;
  170. obj->file = f;
  171. obj->path = baseCpy;
  172. obj->size = size;
  173. return (void *) obj;
  174. }
  175. /* free memory on disk */
  176. static void
  177. starpu_stdio_free (void *base, void *obj, size_t size)
  178. {
  179. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  180. unlink(tmp->path);
  181. fclose(tmp->file);
  182. close(tmp->descriptor);
  183. free(tmp->path);
  184. free(tmp);
  185. }
  186. /* open an existing memory on disk */
  187. static void *
  188. starpu_unistd_open (void *base, void *pos, size_t size)
  189. {
  190. struct starpu_stdio_obj * obj = malloc(sizeof(struct starpu_stdio_obj));
  191. STARPU_ASSERT(obj != NULL);
  192. /* create template for mkstemp */
  193. unsigned int sizeBase = 16;
  194. while(sizeBase < (strlen(base)+strlen(pos)+1))
  195. sizeBase *= 2;
  196. char * baseCpy = malloc(sizeBase*sizeof(char));
  197. STARPU_ASSERT(baseCpy != NULL);
  198. strcpy(baseCpy,(char *) base);
  199. strcat(baseCpy,(char *) pos);
  200. int id = open(baseCpy, O_RDONLY);
  201. STARPU_ASSERT_MSG(id >= 0, "Unistd open failed");
  202. FILE * f = fdopen(id,"rb+");
  203. STARPU_ASSERT_MSG(f != NULL, "Unistd open failed");
  204. obj->descriptor = id;
  205. obj->file = f;
  206. obj->path = baseCpy;
  207. obj->size = size;
  208. return (void *) obj;
  209. }
  210. /* free memory without delete it */
  211. static void
  212. starpu_unistd_close (void *base, void *obj, size_t size)
  213. {
  214. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  215. fclose(tmp->file);
  216. close(tmp->descriptor);
  217. free(tmp->path);
  218. free(tmp);
  219. }
  220. /* read the memory disk */
  221. static ssize_t
  222. starpu_stdio_read (void *base, void *obj, void *buf, off_t offset, size_t size)
  223. {
  224. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  225. int res = fseek(tmp->file, offset, SEEK_SET);
  226. STARPU_ASSERT_MSG(res == 0, "Stdio read failed");
  227. ssize_t nb = fread (buf, 1, size, tmp->file);
  228. return nb;
  229. }
  230. /* write on the memory disk */
  231. static ssize_t
  232. starpu_stdio_write (void *base, void *obj, const void *buf, off_t offset, size_t size)
  233. {
  234. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) obj;
  235. int res = fseek(tmp->file, offset, SEEK_SET);
  236. STARPU_ASSERT_MSG(res == 0, "Stdio read failed");
  237. ssize_t nb = fwrite (buf, 1, size, tmp->file);
  238. return nb;
  239. }
  240. /* create a new copy of parameter == base */
  241. static void *
  242. starpu_stdio_plug (void *parameter)
  243. {
  244. char * tmp = malloc(sizeof(char)*(strlen(parameter)+1));
  245. STARPU_ASSERT(tmp != NULL);
  246. strcpy(tmp,(char *) parameter);
  247. return (void *) tmp;
  248. }
  249. /* free memory allocated for the base */
  250. static void
  251. starpu_stdio_unplug (void *base)
  252. {
  253. free(base);
  254. }
  255. static void
  256. get_stdio_bandwidth_between_disk_and_main_ram(void * base, unsigned node)
  257. {
  258. unsigned iter;
  259. double timing_slowness, timing_latency;
  260. struct timeval start;
  261. struct timeval end;
  262. srand (time (NULL));
  263. int pos = get_location_with_node(node);
  264. char * buf = malloc(SIZE*sizeof(char));
  265. STARPU_ASSERT(buf != NULL);
  266. /* allocate memory */
  267. void * mem = disk_register_list[pos]->functions->alloc(base, SIZE);
  268. struct starpu_stdio_obj * tmp = (struct starpu_stdio_obj *) mem;
  269. /* Measure upload slowness */
  270. gettimeofday(&start, NULL);
  271. for (iter = 0; iter < NITER; ++iter)
  272. {
  273. disk_register_list[pos]->functions->write(base, mem, buf, 0, SIZE);
  274. /* clean cache memory */
  275. int res = fflush (tmp->file);
  276. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed");
  277. res = fsync(tmp->descriptor);
  278. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed");
  279. }
  280. gettimeofday(&end, NULL);
  281. timing_slowness = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  282. /* free memory */
  283. disk_register_list[pos]->functions->free(base, mem, SIZE);
  284. free(buf);
  285. mem = disk_register_list[pos]->functions->alloc(base, 2*SIZE);
  286. tmp = (struct starpu_stdio_obj *) mem;
  287. buf = malloc(sizeof(char));
  288. STARPU_ASSERT(buf != NULL);
  289. /* Measure latency */
  290. gettimeofday(&start, NULL);
  291. for (iter = 0; iter < NITER; ++iter)
  292. {
  293. disk_register_list[pos]->functions->write(base, mem, buf, rand() % ((2*SIZE)-1) +1 , 1);
  294. int res = fflush (tmp->file);
  295. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  296. res = fsync(tmp->descriptor);
  297. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  298. }
  299. gettimeofday(&end, NULL);
  300. timing_latency = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  301. disk_register_list[pos]->functions->free(base, mem, SIZE);
  302. free(buf);
  303. _starpu_save_bandwidth_and_latency_disk((NITER/timing_slowness)*1000000, (NITER/timing_slowness)*1000000,
  304. timing_latency/NITER, timing_latency/NITER, node);
  305. }
  306. struct disk_ops write_on_file = {
  307. .alloc = starpu_stdio_alloc,
  308. .free = starpu_stdio_free,
  309. .open = starpu_unistd_open,
  310. .close = starpu_unistd_close,
  311. .read = starpu_stdio_read,
  312. .write = starpu_stdio_write,
  313. .plug = starpu_stdio_plug,
  314. .unplug = starpu_stdio_unplug,
  315. .bandwidth = get_stdio_bandwidth_between_disk_and_main_ram
  316. };