disk_stdio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015-2017 CNRS
  4. * Copyright (C) 2013,2017 Inria
  5. * Copyright (C) 2013-2017 Université de Bordeaux
  6. * Copyright (C) 2013 Corentin Salingue
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <sys/stat.h>
  23. #include <errno.h>
  24. #include <time.h>
  25. #include <starpu.h>
  26. #include <core/disk.h>
  27. #include <core/perfmodel/perfmodel.h>
  28. #include <datawizard/copy_driver.h>
  29. #include <datawizard/memory_manager.h>
  30. #include <datawizard/memory_nodes.h>
  31. #include <starpu_parameters.h>
  32. #ifdef STARPU_HAVE_WINDOWS
  33. # include <io.h>
  34. #endif
  35. #define NITER _starpu_calibration_minimum
  36. #ifndef O_BINARY
  37. #define O_BINARY 0
  38. #endif
  39. #define MAX_OPEN_FILES 64
  40. #define TEMP_HIERARCHY_DEPTH 2
  41. /* ------------------- use STDIO to write on disk ------------------- */
  42. static unsigned starpu_stdio_opened_files;
  43. struct starpu_stdio_obj
  44. {
  45. int descriptor;
  46. FILE * file;
  47. char * path;
  48. size_t size;
  49. starpu_pthread_mutex_t mutex;
  50. };
  51. struct starpu_stdio_base
  52. {
  53. char * path;
  54. int created;
  55. };
  56. static struct starpu_stdio_obj *_starpu_stdio_init(int descriptor, char *path, size_t size)
  57. {
  58. struct starpu_stdio_obj *obj;
  59. _STARPU_MALLOC(obj, sizeof(struct starpu_stdio_obj));
  60. FILE *f = fdopen(descriptor,"rb+");
  61. if (f == NULL)
  62. {
  63. free(obj);
  64. return NULL;
  65. }
  66. STARPU_HG_DISABLE_CHECKING(starpu_stdio_opened_files);
  67. if (starpu_stdio_opened_files >= MAX_OPEN_FILES)
  68. {
  69. /* Too many opened files, avoid keeping this one opened */
  70. fclose(f);
  71. f = NULL;
  72. descriptor = -1;
  73. }
  74. else
  75. (void) STARPU_ATOMIC_ADD(&starpu_stdio_opened_files, 1);
  76. STARPU_PTHREAD_MUTEX_INIT(&obj->mutex, NULL);
  77. obj->descriptor = descriptor;
  78. obj->file = f;
  79. obj->path = path;
  80. obj->size = size;
  81. return (void *) obj;
  82. }
  83. static FILE *_starpu_stdio_reopen(struct starpu_stdio_obj *obj)
  84. {
  85. int id = open(obj->path, O_RDWR);
  86. STARPU_ASSERT(id >= 0);
  87. FILE *f = fdopen(id,"rb+");
  88. STARPU_ASSERT(f);
  89. return f;
  90. }
  91. static void _starpu_stdio_reclose(FILE *f)
  92. {
  93. fclose(f);
  94. }
  95. static void _starpu_stdio_close(struct starpu_stdio_obj *obj)
  96. {
  97. if (obj->descriptor < 0)
  98. return;
  99. if (starpu_stdio_opened_files < MAX_OPEN_FILES)
  100. (void) STARPU_ATOMIC_ADD(&starpu_stdio_opened_files, -1);
  101. fclose(obj->file);
  102. }
  103. static void _starpu_stdio_fini(struct starpu_stdio_obj *obj)
  104. {
  105. STARPU_PTHREAD_MUTEX_DESTROY(&obj->mutex);
  106. free(obj->path);
  107. free(obj);
  108. }
  109. /* allocation memory on disk */
  110. static void *starpu_stdio_alloc(void *base, size_t size)
  111. {
  112. struct starpu_stdio_obj *obj;
  113. struct starpu_stdio_base * fileBase = (struct starpu_stdio_base *) base;
  114. int id;
  115. char *baseCpy = _starpu_mktemp_many(fileBase->path, TEMP_HIERARCHY_DEPTH, O_RDWR | O_BINARY, &id);
  116. /* fail */
  117. if (!baseCpy)
  118. return NULL;
  119. int val = _starpu_ftruncate(id,size);
  120. /* fail */
  121. if (val < 0)
  122. {
  123. _STARPU_DISP("Could not truncate file, ftruncate failed with error '%s'\n", strerror(errno));
  124. close(id);
  125. unlink(baseCpy);
  126. free(baseCpy);
  127. return NULL;
  128. }
  129. obj = _starpu_stdio_init(id, baseCpy, size);
  130. if (!obj)
  131. {
  132. close(id);
  133. unlink(baseCpy);
  134. free(baseCpy);
  135. }
  136. return obj;
  137. }
  138. /* free memory on disk */
  139. static void starpu_stdio_free(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  140. {
  141. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  142. _starpu_stdio_close(tmp);
  143. unlink(tmp->path);
  144. _starpu_rmtemp_many(tmp->path, TEMP_HIERARCHY_DEPTH);
  145. _starpu_stdio_fini(tmp);
  146. }
  147. /* open an existing memory on disk */
  148. static void *starpu_stdio_open(void *base, void *pos, size_t size)
  149. {
  150. struct starpu_stdio_base * fileBase = (struct starpu_stdio_base *) base;
  151. struct starpu_stdio_obj *obj;
  152. /* create template */
  153. char *baseCpy;
  154. _STARPU_MALLOC(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1);
  155. snprintf(baseCpy, strlen(fileBase->path)+1+strlen(pos)+1, "%s/%s", fileBase->path, (char *)pos);
  156. int id = open(baseCpy, O_RDWR);
  157. if (id < 0)
  158. {
  159. free(baseCpy);
  160. return NULL;
  161. }
  162. obj = _starpu_stdio_init(id, baseCpy, size);
  163. if (!obj)
  164. free(baseCpy);
  165. return obj;
  166. }
  167. /* free memory without delete it */
  168. static void starpu_stdio_close(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  169. {
  170. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  171. _starpu_stdio_close(tmp);
  172. _starpu_stdio_fini(tmp);
  173. }
  174. /* read the memory disk */
  175. static int starpu_stdio_read(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void *buf, off_t offset, size_t size)
  176. {
  177. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  178. FILE *f = tmp->file;
  179. if (f)
  180. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  181. else
  182. f = _starpu_stdio_reopen(obj);
  183. int res = fseek(f, offset, SEEK_SET);
  184. STARPU_ASSERT_MSG(res == 0, "Stdio read failed");
  185. starpu_ssize_t nb = fread(buf, 1, size, f);
  186. STARPU_ASSERT_MSG(nb >= 0, "Stdio read failed");
  187. if (tmp->file)
  188. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  189. else
  190. _starpu_stdio_reclose(f);
  191. return 0;
  192. }
  193. static int starpu_stdio_full_read(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void **ptr, size_t *size, unsigned dst_node)
  194. {
  195. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  196. FILE *f = tmp->file;
  197. starpu_ssize_t ssize;
  198. if (f)
  199. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  200. else
  201. f = _starpu_stdio_reopen(obj);
  202. int res = fseek(f, 0, SEEK_END);
  203. STARPU_ASSERT_MSG(res == 0, "Stdio write failed");
  204. ssize = ftell(f);
  205. STARPU_ASSERT_MSG(ssize >= 0, "Stdio write failed");
  206. *size = ssize;
  207. if (tmp->file)
  208. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  209. /* Alloc aligned buffer */
  210. _starpu_malloc_flags_on_node(dst_node, ptr, *size, 0);
  211. if (tmp->file)
  212. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  213. res = fseek(f, 0, SEEK_SET);
  214. STARPU_ASSERT_MSG(res == 0, "Stdio read failed");
  215. starpu_ssize_t nb = fread(*ptr, 1, *size, f);
  216. STARPU_ASSERT_MSG(nb >= 0, "Stdio read failed");
  217. if (tmp->file)
  218. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  219. else
  220. _starpu_stdio_reclose(f);
  221. return 0;
  222. }
  223. /* write on the memory disk */
  224. static int starpu_stdio_write(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, const void *buf, off_t offset, size_t size)
  225. {
  226. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  227. FILE *f = tmp->file;
  228. if (f)
  229. STARPU_PTHREAD_MUTEX_LOCK(&tmp->mutex);
  230. else
  231. f = _starpu_stdio_reopen(obj);
  232. int res = fseek(f, offset, SEEK_SET);
  233. STARPU_ASSERT_MSG(res == 0, "Stdio write failed");
  234. fwrite(buf, 1, size, f);
  235. if (tmp->file)
  236. STARPU_PTHREAD_MUTEX_UNLOCK(&tmp->mutex);
  237. else
  238. _starpu_stdio_reclose(f);
  239. return 0;
  240. }
  241. static int starpu_stdio_full_write(void *base STARPU_ATTRIBUTE_UNUSED, void *obj, void *ptr, size_t size)
  242. {
  243. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) obj;
  244. FILE *f = tmp->file;
  245. if (!f)
  246. f = _starpu_stdio_reopen(obj);
  247. /* update file size to realise the next good full_read */
  248. if(size != tmp->size)
  249. {
  250. int val = _starpu_fftruncate(f,size);
  251. STARPU_ASSERT(val == 0);
  252. tmp->size = size;
  253. }
  254. int res = fseek(f, 0, SEEK_SET);
  255. STARPU_ASSERT_MSG(res == 0, "Stdio write failed");
  256. fwrite(ptr, 1, size, f);
  257. if (!tmp->file)
  258. _starpu_stdio_reclose(f);
  259. return 0;
  260. }
  261. static void *starpu_stdio_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIBUTE_UNUSED)
  262. {
  263. struct starpu_stdio_base * base;
  264. struct stat buf;
  265. _STARPU_MALLOC(base, sizeof(*base));
  266. base->created = 0;
  267. base->path = strdup((char *) parameter);
  268. STARPU_ASSERT(base->path);
  269. if (!(stat(base->path, &buf) == 0 && S_ISDIR(buf.st_mode)))
  270. {
  271. _starpu_mkpath(base->path, S_IRWXU);
  272. base->created = 1;
  273. }
  274. return (void *) base;
  275. }
  276. /* free memory allocated for the base */
  277. static void starpu_stdio_unplug(void *base)
  278. {
  279. struct starpu_stdio_base * fileBase = (struct starpu_stdio_base *) base;
  280. if (fileBase->created)
  281. rmdir(fileBase->path);
  282. free(fileBase->path);
  283. free(fileBase);
  284. }
  285. static int get_stdio_bandwidth_between_disk_and_main_ram(unsigned node, void *base)
  286. {
  287. unsigned iter;
  288. double timing_slowness, timing_latency;
  289. double start;
  290. double end;
  291. char *buf;
  292. struct starpu_stdio_base * fileBase = (struct starpu_stdio_base *) base;
  293. srand(time(NULL));
  294. starpu_malloc_flags((void **) &buf, STARPU_DISK_SIZE_MIN, 0);
  295. STARPU_ASSERT(buf != NULL);
  296. /* allocate memory */
  297. void *mem = _starpu_disk_alloc(node, STARPU_DISK_SIZE_MIN);
  298. /* fail to alloc */
  299. if (mem == NULL)
  300. return 0;
  301. struct starpu_stdio_obj *tmp = (struct starpu_stdio_obj *) mem;
  302. memset(buf, 0, STARPU_DISK_SIZE_MIN);
  303. /* Measure upload slowness */
  304. start = starpu_timing_now();
  305. for (iter = 0; iter < NITER; ++iter)
  306. {
  307. FILE *f = tmp->file;
  308. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, 0, STARPU_DISK_SIZE_MIN, NULL);
  309. if (!f)
  310. f = _starpu_stdio_reopen(tmp);
  311. /* clean cache memory */
  312. int res = fflush(f);
  313. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed \n");
  314. #ifdef STARPU_HAVE_WINDOWS
  315. res = _commit(fileno(f));
  316. #else
  317. res = fsync(fileno(f));
  318. #endif
  319. STARPU_ASSERT_MSG(res == 0, "Slowness computation failed \n");
  320. if (!tmp->file)
  321. _starpu_stdio_reclose(f);
  322. }
  323. end = starpu_timing_now();
  324. timing_slowness = end - start;
  325. /* free memory */
  326. starpu_free_flags(buf, STARPU_DISK_SIZE_MIN, 0);
  327. starpu_malloc_flags((void**) &buf, sizeof(char), 0);
  328. STARPU_ASSERT(buf != NULL);
  329. *buf = 0;
  330. /* Measure latency */
  331. start = starpu_timing_now();
  332. for (iter = 0; iter < NITER; ++iter)
  333. {
  334. FILE *f = tmp->file;
  335. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, rand() % (STARPU_DISK_SIZE_MIN -1) , 1, NULL);
  336. if (!f)
  337. f = _starpu_stdio_reopen(tmp);
  338. int res = fflush(f);
  339. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  340. #ifdef STARPU_HAVE_WINDOWS
  341. res = _commit(fileno(f));
  342. #else
  343. res = fsync(fileno(f));
  344. #endif
  345. STARPU_ASSERT_MSG(res == 0, "Latency computation failed");
  346. if (!tmp->file)
  347. _starpu_stdio_reclose(f);
  348. }
  349. end = starpu_timing_now();
  350. timing_latency = end - start;
  351. _starpu_disk_free(node, mem, STARPU_DISK_SIZE_MIN);
  352. starpu_free_flags(buf, sizeof(char), 0);
  353. _starpu_save_bandwidth_and_latency_disk((NITER/timing_slowness)*STARPU_DISK_SIZE_MIN, (NITER/timing_slowness)*STARPU_DISK_SIZE_MIN,
  354. timing_latency/NITER, timing_latency/NITER, node, fileBase->path);
  355. return 1;
  356. }
  357. struct starpu_disk_ops starpu_disk_stdio_ops =
  358. {
  359. .alloc = starpu_stdio_alloc,
  360. .free = starpu_stdio_free,
  361. .open = starpu_stdio_open,
  362. .close = starpu_stdio_close,
  363. .read = starpu_stdio_read,
  364. .write = starpu_stdio_write,
  365. .plug = starpu_stdio_plug,
  366. .unplug = starpu_stdio_unplug,
  367. .copy = NULL,
  368. .bandwidth = get_stdio_bandwidth_between_disk_and_main_ram,
  369. .full_read = starpu_stdio_full_read,
  370. .full_write = starpu_stdio_full_write
  371. };