disk_hdf5.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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 <errno.h>
  18. #include <common/config.h>
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <time.h>
  23. #include <hdf5.h>
  24. #include <starpu.h>
  25. #include <core/disk.h>
  26. #include <core/perfmodel/perfmodel.h>
  27. #ifndef O_BINARY
  28. #define O_BINARY 0
  29. #endif
  30. #define NITER _starpu_calibration_minimum
  31. #define STARPU_CHUNK_DIM 4096
  32. /* ------------------- use HDF5 to write on disk ------------------- */
  33. #ifndef H5_HAVE_THREADSAFE
  34. static int nb_disk_open = 0;
  35. static volatile int init_finished = 0;
  36. static starpu_pthread_t global_thread; /* This thread will perform each write/read because we don't have asynchronous functions */
  37. static volatile int global_run; /* Ask to the thread if he can continue */
  38. static starpu_pthread_mutex_t global_mutex; /* Mutex is used to protect work_list and if HDF5 library is not safe */
  39. static starpu_pthread_cond_t global_cond;
  40. static struct _starpu_hdf5_work_list global_work_list; /* This list contains the work for the hdf5 thread */
  41. #endif
  42. #ifdef H5_HAVE_THREADSAFE
  43. #define HDF5_VAR_THREAD fileBase->thread
  44. #define HDF5_VAR_RUN fileBase->run
  45. #define HDF5_VAR_MUTEX fileBase->mutex
  46. #define HDF5_VAR_COND fileBase->cond
  47. #define HDF5_VAR_WORK_LIST fileBase->work_list
  48. #else
  49. #define HDF5_VAR_THREAD global_thread
  50. #define HDF5_VAR_RUN global_run
  51. #define HDF5_VAR_MUTEX global_mutex
  52. #define HDF5_VAR_COND global_cond
  53. #define HDF5_VAR_WORK_LIST global_work_list
  54. #endif
  55. enum hdf5_work_type { READ, WRITE, FULL_READ, FULL_WRITE, COPY };
  56. LIST_TYPE(_starpu_hdf5_work,
  57. enum hdf5_work_type type;
  58. struct starpu_hdf5_base * base_src;
  59. struct starpu_hdf5_obj * obj_src;
  60. off_t offset_src;
  61. struct starpu_hdf5_base * base_dst;
  62. struct starpu_hdf5_obj * obj_dst;
  63. off_t offset_dst;
  64. void * ptr;
  65. size_t size;
  66. void * event;
  67. );
  68. struct starpu_hdf5_base
  69. {
  70. hid_t fileID;
  71. char * path;
  72. unsigned created; /* StarPU creates the HDF5 file */
  73. unsigned next_dataset_id;
  74. starpu_pthread_t thread; /* This thread will perform each write/read because we don't have asynchronous functions */
  75. int run; /* Ask to the thread if he can continue */
  76. starpu_pthread_mutex_t mutex; /* Mutex is used to protect work_list and if HDF5 library is not safe */
  77. starpu_pthread_cond_t cond;
  78. struct _starpu_hdf5_work_list work_list; /* This list contains the work for the hdf5 thread */
  79. };
  80. struct starpu_hdf5_obj
  81. {
  82. hid_t dataset; /* describe this object in HDF5 file */
  83. char * path; /* path where data are stored in HDF5 file */
  84. size_t size;
  85. };
  86. static inline void _starpu_hdf5_protect_start(void * base STARPU_ATTRIBUTE_UNUSED)
  87. {
  88. #ifndef H5_HAVE_THREADSAFE
  89. if (base != NULL)
  90. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  91. #endif
  92. }
  93. static inline void _starpu_hdf5_protect_stop(void * base STARPU_ATTRIBUTE_UNUSED)
  94. {
  95. #ifndef H5_HAVE_THREADSAFE
  96. if (base != NULL)
  97. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  98. #endif
  99. }
  100. /* ------------------ Functions for internal thread -------------------- */
  101. /* TODO : Dataspace may not be NATIVE_CHAR for opened data */
  102. static void starpu_hdf5_full_read_internal(struct _starpu_hdf5_work * work)
  103. {
  104. herr_t status;
  105. status = H5Dread(work->obj_src->dataset, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, work->ptr);
  106. STARPU_ASSERT_MSG(status >= 0, "Can not read data associed to this dataset (%s)\n", work->obj_src->path);
  107. }
  108. /* TODO : Dataspace may not be NATIVE_CHAR for opened data */
  109. static void starpu_hdf5_full_write_internal(struct _starpu_hdf5_work * work)
  110. {
  111. herr_t status;
  112. /* Update size of dataspace */
  113. if (work->size > work->obj_dst->size)
  114. {
  115. /* Get official datatype */
  116. hid_t datatype = H5Dget_type(work->obj_dst->dataset);
  117. hsize_t sizeDatatype = H5Tget_size(datatype);
  118. /* Count in number of elements */
  119. hsize_t extendsdim[1] = {work->size/sizeDatatype};
  120. status = H5Dset_extent (work->obj_dst->dataset, extendsdim);
  121. STARPU_ASSERT_MSG(status >= 0, "Error when extending HDF5 dataspace !\n");
  122. work->obj_dst->size = work->size;
  123. }
  124. /* Write ALL the dataspace */
  125. status = H5Dwrite(work->obj_dst->dataset, H5T_NATIVE_CHAR, H5S_ALL, H5S_ALL, H5P_DEFAULT, work->ptr);
  126. STARPU_ASSERT_MSG(status >= 0, "Can not write data to this dataset (%s)\n", work->obj_dst->path);
  127. }
  128. static void starpu_hdf5_read_internal(struct _starpu_hdf5_work * work)
  129. {
  130. herr_t status;
  131. /* Get official datatype */
  132. hid_t datatype = H5Dget_type(work->obj_src->dataset);
  133. hsize_t sizeDatatype = H5Tget_size(datatype);
  134. /* count in element, not in byte */
  135. work->offset_src /= sizeDatatype;
  136. work->size /= sizeDatatype;
  137. /* duplicate the dataspace in the dataset */
  138. hid_t dataspace_select = H5Dget_space(work->obj_src->dataset);
  139. STARPU_ASSERT_MSG(dataspace_select >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  140. /* Select what we want of the duplicated dataspace (it's called an hyperslab). This operation is done on place */
  141. hsize_t offsets[1] = {work->offset_src};
  142. hsize_t count[1] = {work->size};
  143. /* stride and block size are NULL which is equivalent of a shift of 1 */
  144. status = H5Sselect_hyperslab(dataspace_select, H5S_SELECT_SET, offsets, NULL, count, NULL);
  145. STARPU_ASSERT_MSG(status >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  146. /* create the dataspace for the received data which describes ptr */
  147. hsize_t dims_receive[1] = {work->size};
  148. hid_t dataspace_receive = H5Screate_simple(1, dims_receive, NULL);
  149. STARPU_ASSERT_MSG(dataspace_receive >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  150. /* Receiver has to be an hyperslabs */
  151. offsets[0] = 0;
  152. count[0] = work->size;
  153. H5Sselect_hyperslab(dataspace_receive, H5S_SELECT_SET, offsets, NULL, count, NULL);
  154. STARPU_ASSERT_MSG(dataspace_receive >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  155. status = H5Dread(work->obj_src->dataset, datatype, dataspace_receive, dataspace_select, H5P_DEFAULT, work->ptr);
  156. STARPU_ASSERT_MSG(status >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  157. /* don't need these dataspaces */
  158. status = H5Sclose(dataspace_select);
  159. STARPU_ASSERT_MSG(status >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  160. status = H5Sclose(dataspace_receive);
  161. STARPU_ASSERT_MSG(status >= 0, "Error when reading this HDF5 dataset (%s)\n", work->obj_src->path);
  162. }
  163. static void starpu_hdf5_write_internal(struct _starpu_hdf5_work * work)
  164. {
  165. herr_t status;
  166. /* Get official datatype */
  167. hid_t datatype = H5Dget_type(work->obj_dst->dataset);
  168. hsize_t sizeDatatype = H5Tget_size(datatype);
  169. /* Update size of dataspace */
  170. if (work->size + work->offset_dst > work->obj_dst->size)
  171. {
  172. /* Count in number of elements */
  173. hsize_t extendsdim[1] = {(work->offset_dst + work->size)/sizeDatatype};
  174. status = H5Dset_extent (work->obj_dst->dataset, extendsdim);
  175. STARPU_ASSERT_MSG(status >= 0, "Error when extending HDF5 dataspace !\n");
  176. work->obj_dst->size = work->offset_dst + work->size;
  177. }
  178. /* count in element, not in byte */
  179. work->offset_dst /= sizeDatatype;
  180. work->size /= sizeDatatype;
  181. /* duplicate the dataspace in the dataset */
  182. hid_t dataspace_select = H5Dget_space(work->obj_dst->dataset);
  183. STARPU_ASSERT_MSG(dataspace_select >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  184. /* Select what we want of the duplicated dataspace (it's called an hyperslab). This operation is done on place */
  185. hsize_t offsets[1] = {work->offset_dst};
  186. hsize_t count[1] = {work->size};
  187. /* stride and block size are NULL which is equivalent of a shift of 1 */
  188. status = H5Sselect_hyperslab(dataspace_select, H5S_SELECT_SET, offsets, NULL, count, NULL);
  189. STARPU_ASSERT_MSG(status >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  190. /* create the dataspace for the received data which describes ptr */
  191. hsize_t dims_send[1] = {work->size};
  192. hid_t dataspace_send = H5Screate_simple(1, dims_send, NULL);
  193. STARPU_ASSERT_MSG(dataspace_send >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  194. /* Receiver has to be an hyperslabs */
  195. offsets[0] = 0;
  196. count[0] = work->size;
  197. H5Sselect_hyperslab(dataspace_send, H5S_SELECT_SET, offsets, NULL, count, NULL);
  198. STARPU_ASSERT_MSG(dataspace_send >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  199. status = H5Dwrite(work->obj_dst->dataset, datatype, dataspace_send, dataspace_select, H5P_DEFAULT, work->ptr);
  200. STARPU_ASSERT_MSG(status >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  201. /* don't need these dataspaces */
  202. status = H5Sclose(dataspace_select);
  203. STARPU_ASSERT_MSG(status >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  204. status = H5Sclose(dataspace_send);
  205. STARPU_ASSERT_MSG(status >= 0, "Error when writing this HDF5 dataset (%s)\n", work->obj_dst->path);
  206. }
  207. unsigned warned = 0;
  208. static void starpu_hdf5_copy_internal(struct _starpu_hdf5_work * work)
  209. {
  210. herr_t status;
  211. /* HDF5 H50copy supports only same size in both areas and copies the entire object */
  212. if (work->offset_src == 0 && work->offset_dst == 0 && work->size == work->obj_src->size && work->size == work->obj_dst->size)
  213. {
  214. H5Dclose(work->obj_dst->dataset);
  215. /* Dirty : Delete dataspace because H5Ocopy only works if destination does not exist */
  216. H5Ldelete(work->base_dst->fileID, work->obj_dst->path, H5P_DEFAULT);
  217. status = H5Ocopy(work->base_src->fileID, work->obj_src->path, work->base_dst->fileID, work->obj_dst->path, H5P_DEFAULT, H5P_DEFAULT);
  218. STARPU_ASSERT_MSG(status >= 0, "Can not copy data (%s) associed to this disk (%s) to the data (%s) on this disk (%s)\n", work->obj_src->path, work->base_src->path, work->obj_dst->path, work->base_dst->path);
  219. work->obj_dst->dataset = H5Dopen2(work->base_dst->fileID, work->obj_dst->path, H5P_DEFAULT);
  220. }
  221. else
  222. {
  223. if (!warned)
  224. {
  225. _STARPU_DISP("Direct disk to disk copy is not supported for a piece of data. Data will be transfered to RAM memory and then, be pushed on disk \n");
  226. warned = 1;
  227. }
  228. void * ptr;
  229. int ret = _starpu_malloc_flags_on_node(STARPU_MAIN_RAM, &ptr, work->size, 0);
  230. STARPU_ASSERT_MSG(ret == 0, "Cannot allocate %lu bytes to perform disk to disk operation", (unsigned long)work->size);
  231. /* buffer is only used internally to store intermediate data */
  232. work->ptr = ptr;
  233. starpu_hdf5_read_internal(work);
  234. starpu_hdf5_write_internal(work);
  235. _starpu_free_flags_on_node(STARPU_MAIN_RAM, ptr, work->size, 0);
  236. }
  237. }
  238. static void * _starpu_hdf5_internal_thread(void * arg)
  239. {
  240. #ifdef H5_HAVE_THREADSAFE
  241. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) arg;
  242. #endif
  243. while (HDF5_VAR_RUN || !_starpu_hdf5_work_list_empty(&HDF5_VAR_WORK_LIST))
  244. {
  245. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  246. if (_starpu_hdf5_work_list_empty(&HDF5_VAR_WORK_LIST) && HDF5_VAR_RUN)
  247. STARPU_PTHREAD_COND_WAIT(&HDF5_VAR_COND, &HDF5_VAR_MUTEX);
  248. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  249. /* We are the only consummer here, don't need to protect here */
  250. if (!_starpu_hdf5_work_list_empty(&HDF5_VAR_WORK_LIST))
  251. {
  252. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  253. struct _starpu_hdf5_work * work = _starpu_hdf5_work_list_pop_back(&HDF5_VAR_WORK_LIST);
  254. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  255. if (work->base_src < work->base_dst)
  256. {
  257. _starpu_hdf5_protect_start(work->base_src);
  258. #ifdef H5_HAVE_THREADSAFE
  259. _starpu_hdf5_protect_start(work->base_dst);
  260. #endif
  261. }
  262. else
  263. {
  264. _starpu_hdf5_protect_start(work->base_dst);
  265. #ifdef H5_HAVE_THREADSAFE
  266. if (work->base_src != work->base_dst)
  267. _starpu_hdf5_protect_start(work->base_src);
  268. #endif
  269. }
  270. switch(work->type)
  271. {
  272. case READ:
  273. starpu_hdf5_read_internal(work);
  274. break;
  275. case WRITE:
  276. starpu_hdf5_write_internal(work);
  277. break;
  278. case FULL_READ:
  279. starpu_hdf5_full_read_internal(work);
  280. break;
  281. case FULL_WRITE:
  282. starpu_hdf5_full_write_internal(work);
  283. break;
  284. case COPY:
  285. starpu_hdf5_copy_internal(work);
  286. break;
  287. default:
  288. STARPU_ABORT();
  289. }
  290. if (work->base_src < work->base_dst)
  291. {
  292. _starpu_hdf5_protect_stop(work->base_src);
  293. #ifdef H5_HAVE_THREADSAFE
  294. _starpu_hdf5_protect_stop(work->base_dst);
  295. #endif
  296. }
  297. else
  298. {
  299. _starpu_hdf5_protect_stop(work->base_dst);
  300. #ifdef H5_HAVE_THREADSAFE
  301. if (work->base_src != work->base_dst)
  302. _starpu_hdf5_protect_stop(work->base_src);
  303. #endif
  304. }
  305. /* Update event to tell it's finished */
  306. starpu_sem_post((starpu_sem_t *) work->event);
  307. free(work);
  308. }
  309. }
  310. return NULL;
  311. }
  312. static void _starpu_hdf5_create_thread(struct starpu_hdf5_base * fileBase)
  313. {
  314. _starpu_hdf5_work_list_init(&HDF5_VAR_WORK_LIST);
  315. HDF5_VAR_RUN = 1;
  316. STARPU_PTHREAD_COND_INIT(&HDF5_VAR_COND, NULL);
  317. STARPU_PTHREAD_CREATE(&HDF5_VAR_THREAD, NULL, _starpu_hdf5_internal_thread, (void *) fileBase);
  318. }
  319. /* returns the size in BYTES */
  320. static hsize_t _starpu_get_size_obj(struct starpu_hdf5_obj * obj)
  321. {
  322. herr_t status;
  323. hid_t dataspace = H5Dget_space(obj->dataset);
  324. STARPU_ASSERT_MSG(dataspace >= 0, "Can not get the size of this HDF5 dataset (%s)\n", obj->path);
  325. hsize_t dims[1];
  326. status = H5Sget_simple_extent_dims(dataspace, dims, NULL);
  327. STARPU_ASSERT_MSG(status >= 0, "Can not get the size of this HDF5 dataset (%s)\n", obj->path);
  328. hid_t datatype = H5Dget_type(obj->dataset);
  329. STARPU_ASSERT_MSG(datatype >= 0, "Can not get the size of this HDF5 dataset (%s)\n", obj->path);
  330. hsize_t sizeDatatype = H5Tget_size(datatype);
  331. STARPU_ASSERT_MSG(sizeDatatype > 0, "Can not get the size of this HDF5 dataset (%s)\n", obj->path);
  332. H5Sclose(dataspace);
  333. H5Tclose(datatype);
  334. return dims[0]*sizeDatatype;
  335. }
  336. static void starpu_hdf5_send_work(void *base_src, void *obj_src, off_t offset_src, void *base_dst, void *obj_dst, off_t offset_dst, void *buf, size_t size, void * event, enum hdf5_work_type type)
  337. {
  338. struct starpu_hdf5_obj * dataObj_src = (struct starpu_hdf5_obj *) obj_src;
  339. struct starpu_hdf5_obj * dataObj_dst = (struct starpu_hdf5_obj *) obj_dst;
  340. struct starpu_hdf5_base * fileBase_src = (struct starpu_hdf5_base *) base_src;
  341. struct starpu_hdf5_base * fileBase_dst = (struct starpu_hdf5_base *) base_dst;
  342. struct _starpu_hdf5_work * work;
  343. _STARPU_MALLOC(work, sizeof(*work));
  344. work->type = type;
  345. work->base_src = fileBase_src;
  346. work->obj_src = dataObj_src;
  347. work->offset_src = offset_src;
  348. work->base_dst = fileBase_dst;
  349. work->obj_dst = dataObj_dst;
  350. work->offset_dst = offset_dst;
  351. work->ptr = buf;
  352. work->size = size;
  353. work->event = event;
  354. #ifdef H5_HAVE_THREADSAFE
  355. struct starpu_hdf5_base * fileBase;
  356. if (fileBase_src != NULL)
  357. fileBase = fileBase_src;
  358. else
  359. fileBase = fileBase_dst;
  360. #endif
  361. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  362. _starpu_hdf5_work_list_push_front(&HDF5_VAR_WORK_LIST, work);
  363. /* Wake up internal thread */
  364. STARPU_PTHREAD_COND_BROADCAST(&HDF5_VAR_COND);
  365. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  366. }
  367. static struct starpu_hdf5_obj * _starpu_hdf5_data_alloc(struct starpu_hdf5_base * fileBase, char * name, size_t size)
  368. {
  369. struct starpu_hdf5_obj * obj;
  370. _STARPU_MALLOC(obj, sizeof(*obj));
  371. _starpu_hdf5_protect_start((void *) fileBase);
  372. /* create a dataspace with one dimension of size elements */
  373. hsize_t dim[1] = {size};
  374. hsize_t maxdim[1] = {H5S_UNLIMITED};
  375. hid_t dataspace = H5Screate_simple(1, dim, maxdim);
  376. if (dataspace < 0)
  377. {
  378. free(obj);
  379. return NULL;
  380. }
  381. hsize_t chunkdim[1] = {STARPU_CHUNK_DIM};
  382. hid_t prop = H5Pcreate (H5P_DATASET_CREATE);
  383. herr_t status = H5Pset_chunk (prop, 1, chunkdim);
  384. STARPU_ASSERT_MSG(status >= 0, "Error when setting HDF5 property \n");
  385. /* create a dataset at location name, with data described by the dataspace.
  386. * Each element are like char in C (expected one byte)
  387. */
  388. obj->dataset = H5Dcreate2(fileBase->fileID, name, H5T_NATIVE_CHAR, dataspace, H5P_DEFAULT, prop, H5P_DEFAULT);
  389. H5Sclose(dataspace);
  390. H5Pclose(prop);
  391. if (obj->dataset < 0)
  392. {
  393. free(obj);
  394. return NULL;
  395. }
  396. obj->path = name;
  397. obj->size = size;
  398. _starpu_hdf5_protect_stop((void *) fileBase);
  399. return obj;
  400. }
  401. static struct starpu_hdf5_obj * _starpu_hdf5_data_open(struct starpu_hdf5_base * fileBase, char * name, size_t size)
  402. {
  403. struct starpu_hdf5_obj * obj;
  404. _STARPU_MALLOC(obj, sizeof(*obj));
  405. _starpu_hdf5_protect_start((void *) fileBase);
  406. /* create a dataset at location name, with data described by the dataspace.
  407. * Each element are like char in C (expected one byte)
  408. */
  409. obj->dataset = H5Dopen2(fileBase->fileID, name, H5P_DEFAULT);
  410. _starpu_hdf5_protect_stop((void *) fileBase);
  411. if (obj->dataset < 0)
  412. {
  413. free(obj);
  414. return NULL;
  415. }
  416. obj->path = name;
  417. obj->size = size;
  418. return obj;
  419. }
  420. static void *starpu_hdf5_plug(void *parameter, starpu_ssize_t size STARPU_ATTRIBUTE_UNUSED)
  421. {
  422. struct starpu_hdf5_base * fileBase;
  423. _STARPU_MALLOC(fileBase, sizeof(struct starpu_hdf5_base));
  424. #ifndef H5_HAVE_THREADSAFE
  425. int actual_nb_disk = STARPU_ATOMIC_ADD(&nb_disk_open, 1);
  426. if (actual_nb_disk == 1)
  427. {
  428. #endif
  429. STARPU_PTHREAD_MUTEX_INIT(&HDF5_VAR_MUTEX, NULL);
  430. #ifndef H5_HAVE_THREADSAFE
  431. }
  432. else
  433. {
  434. while (!init_finished)
  435. STARPU_UYIELD();
  436. }
  437. #endif
  438. _starpu_hdf5_protect_start(fileBase);
  439. struct stat buf;
  440. if (stat(parameter, &buf) != 0 || !S_ISREG(buf.st_mode))
  441. {
  442. /* The file doesn't exist or the directory exists => create the datafile */
  443. int id;
  444. _starpu_mkpath(parameter, S_IRWXU);
  445. fileBase->path = _starpu_mktemp(parameter, O_RDWR | O_BINARY, &id);
  446. if (!fileBase->path)
  447. {
  448. free(fileBase);
  449. _STARPU_ERROR("Can not create the HDF5 file (%s)", (char *) parameter);
  450. return NULL;
  451. }
  452. /* just use _starpu_mktemp_many to create a file, close the file descriptor */
  453. close(id);
  454. /* Truncate it */
  455. fileBase->fileID = H5Fcreate((char *)fileBase->path, H5F_ACC_TRUNC, H5P_DEFAULT, H5P_DEFAULT);
  456. if (fileBase->fileID < 0)
  457. {
  458. free(fileBase);
  459. _STARPU_ERROR("Can not create the HDF5 file (%s)", (char *) parameter);
  460. return NULL;
  461. }
  462. fileBase->created = 1;
  463. }
  464. else
  465. {
  466. /* Well, open it ! */
  467. char *path = strdup((char *)parameter);
  468. STARPU_ASSERT(path);
  469. fileBase->fileID = H5Fopen((char *)parameter, H5F_ACC_RDWR, H5P_DEFAULT);
  470. if (fileBase->fileID < 0)
  471. {
  472. free(fileBase);
  473. free(path);
  474. _STARPU_ERROR("Can not open the HDF5 file (%s)", (char *) parameter);
  475. return NULL;
  476. }
  477. fileBase->created = 0;
  478. fileBase->path = path;
  479. }
  480. #ifndef H5_HAVE_THREADSAFE
  481. if (actual_nb_disk == 1)
  482. {
  483. #endif
  484. _starpu_hdf5_create_thread(fileBase);
  485. #ifndef H5_HAVE_THREADSAFE
  486. init_finished = 1;
  487. }
  488. #endif
  489. #if H5_VERS_MAJOR > 1 || (H5_VERS_MAJOR == 1 && H5_VERS_MINOR > 10) || (H5_VERS_MAJOR == 1 && H5_VERS_MINOR == 10 && H5_VERS_RELEASE > 0)
  490. H5Pset_file_space_strategy(fileBase->fileID, H5F_FSPACE_STRATEGY_FSM_AGGR, 0, 0);
  491. #endif
  492. _starpu_hdf5_protect_stop(fileBase);
  493. fileBase->next_dataset_id = 0;
  494. return (void *) fileBase;
  495. }
  496. /* free memory allocated for the base */
  497. static void starpu_hdf5_unplug(void *base)
  498. {
  499. #ifndef H5_HAVE_THREADSAFE
  500. int actual_nb_disk = STARPU_ATOMIC_ADD(&nb_disk_open, -1);
  501. #endif
  502. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
  503. herr_t status;
  504. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  505. #ifndef H5_HAVE_THREADSAFE
  506. if (actual_nb_disk == 0)
  507. {
  508. #endif
  509. HDF5_VAR_RUN = 0;
  510. STARPU_PTHREAD_COND_BROADCAST(&HDF5_VAR_COND);
  511. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  512. STARPU_PTHREAD_JOIN(HDF5_VAR_THREAD, NULL);
  513. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  514. STARPU_PTHREAD_COND_DESTROY(&HDF5_VAR_COND);
  515. STARPU_ASSERT(_starpu_hdf5_work_list_empty(&HDF5_VAR_WORK_LIST));
  516. /* the internal thread is deleted */
  517. #ifndef H5_HAVE_THREADSAFE
  518. }
  519. #endif
  520. status = H5Fclose(fileBase->fileID);
  521. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  522. #ifndef H5_HAVE_THREADSAFE
  523. if (actual_nb_disk == 0)
  524. {
  525. #endif
  526. STARPU_PTHREAD_MUTEX_DESTROY(&HDF5_VAR_MUTEX);
  527. #ifndef H5_HAVE_THREADSAFE
  528. init_finished = 0;
  529. }
  530. #endif
  531. STARPU_ASSERT_MSG(status >= 0, "Can not unplug this HDF5 disk (%s)\n", fileBase->path);
  532. if (fileBase->created)
  533. {
  534. unlink(fileBase->path);
  535. }
  536. else
  537. {
  538. /* Warn user about repack, because unlink dataset doesn't delete data in file */
  539. _STARPU_DISP("This disk (%s) was used to store temporary data. You may use the h5repack command to reduce the size of the file... \n", fileBase->path);
  540. }
  541. free(fileBase->path);
  542. free(fileBase);
  543. }
  544. static void *starpu_hdf5_alloc(void *base, size_t size)
  545. {
  546. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
  547. struct starpu_hdf5_obj * obj;
  548. char * name;
  549. char * prefix = "STARPU_";
  550. char name_id[16];
  551. /* Save the name of the dataset */
  552. STARPU_PTHREAD_MUTEX_LOCK(&HDF5_VAR_MUTEX);
  553. snprintf(name_id, sizeof(name_id), "%u", fileBase->next_dataset_id);
  554. fileBase->next_dataset_id++;
  555. STARPU_PTHREAD_MUTEX_UNLOCK(&HDF5_VAR_MUTEX);
  556. /* name in HDF5 is like a path */
  557. _STARPU_MALLOC(name, 1+strlen(prefix)+strlen(name_id)+1);
  558. snprintf(name, 1+strlen(prefix)+strlen(name_id)+1, "/%s%s", prefix, name_id);
  559. obj = _starpu_hdf5_data_alloc(fileBase, name, size);
  560. if (!obj)
  561. {
  562. free(name);
  563. }
  564. return (void *) obj;
  565. }
  566. static void starpu_hdf5_free(void *base, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  567. {
  568. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
  569. struct starpu_hdf5_obj * dataObj = (struct starpu_hdf5_obj *) obj;
  570. herr_t status;
  571. _starpu_hdf5_protect_start(base);
  572. status = H5Dclose(dataObj->dataset);
  573. STARPU_ASSERT_MSG(status >= 0, "Can not free this HDF5 dataset (%s)\n", dataObj->path);
  574. /* remove the dataset link in the HDF5
  575. * But it doesn't delete the space in the file */
  576. status = H5Ldelete(fileBase->fileID, dataObj->path, H5P_DEFAULT);
  577. STARPU_ASSERT_MSG(status >= 0, "Can not delete the link associed to this dataset (%s)\n", dataObj->path);
  578. _starpu_hdf5_protect_stop(base);
  579. free(dataObj->path);
  580. free(dataObj);
  581. }
  582. static void *starpu_hdf5_open(void *base, void *pos, size_t size)
  583. {
  584. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
  585. struct starpu_hdf5_obj * obj;
  586. char *name;
  587. name = strdup((char *)pos);
  588. STARPU_ASSERT(name);
  589. obj = _starpu_hdf5_data_open(fileBase, name, size);
  590. if (!obj)
  591. {
  592. free(name);
  593. }
  594. return (void *) obj;
  595. }
  596. static void starpu_hdf5_close(void *base, void *obj, size_t size STARPU_ATTRIBUTE_UNUSED)
  597. {
  598. struct starpu_hdf5_obj * dataObj = (struct starpu_hdf5_obj *) obj;
  599. herr_t status;
  600. _starpu_hdf5_protect_start(base);
  601. status = H5Dclose(dataObj->dataset);
  602. STARPU_ASSERT_MSG(status >= 0, "Can not close this HDF5 dataset (%s)\n", dataObj->path);
  603. _starpu_hdf5_protect_stop(base);
  604. free(dataObj->path);
  605. free(dataObj);
  606. }
  607. static void starpu_hdf5_wait(void * event)
  608. {
  609. starpu_sem_t * finished = (starpu_sem_t *) event;
  610. starpu_sem_wait(finished);
  611. }
  612. static int starpu_hdf5_test(void * event)
  613. {
  614. starpu_sem_t * finished = (starpu_sem_t *) event;
  615. return starpu_sem_trywait(finished) == 0;
  616. }
  617. static int starpu_hdf5_full_read(void *base, void *obj, void **ptr, size_t *size, unsigned dst_node)
  618. {
  619. struct starpu_hdf5_obj * dataObj = (struct starpu_hdf5_obj *) obj;
  620. starpu_sem_t finished;
  621. starpu_sem_init(&finished, 0, 0);
  622. _starpu_hdf5_protect_start(base);
  623. *size = _starpu_get_size_obj(dataObj);
  624. _starpu_hdf5_protect_stop(base);
  625. _starpu_malloc_flags_on_node(dst_node, ptr, *size, 0);
  626. starpu_hdf5_send_work(base, obj, 0, NULL, NULL, 0, *ptr, *size, (void*) &finished, FULL_READ);
  627. starpu_hdf5_wait(&finished);
  628. starpu_sem_destroy(&finished);
  629. return 0;
  630. }
  631. static int starpu_hdf5_full_write(void *base, void *obj, void *ptr, size_t size)
  632. {
  633. starpu_sem_t finished;
  634. starpu_sem_init(&finished, 0, 0);
  635. starpu_hdf5_send_work(NULL, NULL, 0, base, obj, 0, ptr, size, (void*) &finished, FULL_WRITE);
  636. starpu_hdf5_wait(&finished);
  637. starpu_sem_destroy(&finished);
  638. return 0;
  639. }
  640. static int starpu_hdf5_read(void *base, void *obj, void *buf, off_t offset, size_t size)
  641. {
  642. starpu_sem_t finished;
  643. starpu_sem_init(&finished, 0, 0);
  644. starpu_hdf5_send_work(base, obj, offset, NULL, NULL, 0, buf, size, (void*) &finished, READ);
  645. starpu_hdf5_wait(&finished);
  646. starpu_sem_destroy(&finished);
  647. return 0;
  648. }
  649. static int starpu_hdf5_write(void *base, void *obj, const void *buf, off_t offset, size_t size)
  650. {
  651. starpu_sem_t finished;
  652. starpu_sem_init(&finished, 0, 0);
  653. starpu_hdf5_send_work(NULL, NULL, 0, base, obj, offset, (void *) buf, size, (void*) &finished, WRITE);
  654. starpu_hdf5_wait(&finished);
  655. starpu_sem_destroy(&finished);
  656. return 0;
  657. }
  658. static void * starpu_hdf5_async_read(void *base, void *obj, void *buf, off_t offset, size_t size)
  659. {
  660. starpu_sem_t * finished;
  661. _STARPU_MALLOC(finished, sizeof(*finished));
  662. starpu_sem_init(finished, 0, 0);
  663. starpu_hdf5_send_work(base, obj, offset, NULL, NULL, 0, buf, size, (void*) finished, READ);
  664. return finished;
  665. }
  666. static void * starpu_hdf5_async_write(void *base, void *obj, void *buf, off_t offset, size_t size)
  667. {
  668. starpu_sem_t * finished;
  669. _STARPU_MALLOC(finished, sizeof(*finished));
  670. starpu_sem_init(finished, 0, 0);
  671. starpu_hdf5_send_work(NULL, NULL, 0, base, obj, offset, (void *) buf, size, (void*) finished, WRITE);
  672. return finished;
  673. }
  674. void * starpu_hdf5_async_full_read (void * base, void * obj, void ** ptr, size_t * size, unsigned dst_node)
  675. {
  676. struct starpu_hdf5_obj * dataObj = (struct starpu_hdf5_obj *) obj;
  677. starpu_sem_t * finished;
  678. _STARPU_MALLOC(finished, sizeof(*finished));
  679. starpu_sem_init(finished, 0, 0);
  680. _starpu_hdf5_protect_start(base);
  681. *size = _starpu_get_size_obj(dataObj);
  682. _starpu_hdf5_protect_stop(base);
  683. _starpu_malloc_flags_on_node(dst_node, ptr, *size, 0);
  684. starpu_hdf5_send_work(base, obj, 0, NULL, NULL, 0, *ptr, *size, (void*) finished, FULL_READ);
  685. return finished;
  686. }
  687. void * starpu_hdf5_async_full_write (void * base, void * obj, void * ptr, size_t size)
  688. {
  689. starpu_sem_t * finished;
  690. _STARPU_MALLOC(finished, sizeof(*finished));
  691. starpu_sem_init(finished, 0, 0);
  692. starpu_hdf5_send_work(NULL, NULL, 0, base, obj, 0, ptr, size, (void*) finished, FULL_WRITE);
  693. return finished;
  694. }
  695. void * starpu_hdf5_copy(void *base_src, void* obj_src, off_t offset_src, void *base_dst, void* obj_dst, off_t offset_dst, size_t size)
  696. {
  697. starpu_sem_t * finished;
  698. _STARPU_MALLOC(finished, sizeof(*finished));
  699. starpu_sem_init(finished, 0, 0);
  700. starpu_hdf5_send_work(base_src, obj_src, offset_src, base_dst, obj_dst, offset_dst, NULL, size, (void*) finished, COPY);
  701. return finished;
  702. }
  703. static void starpu_hdf5_free_request(void * event)
  704. {
  705. starpu_sem_destroy(event);
  706. free(event);
  707. }
  708. static int get_hdf5_bandwidth_between_disk_and_main_ram(unsigned node, void *base)
  709. {
  710. unsigned iter;
  711. double timing_slowness, timing_latency;
  712. double start;
  713. double end;
  714. char *buf;
  715. struct starpu_hdf5_base * fileBase = (struct starpu_hdf5_base *) base;
  716. srand(time(NULL));
  717. starpu_malloc_flags((void **) &buf, STARPU_DISK_SIZE_MIN, 0);
  718. STARPU_ASSERT(buf != NULL);
  719. /* allocate memory */
  720. void *mem = _starpu_disk_alloc(node, STARPU_DISK_SIZE_MIN);
  721. /* fail to alloc */
  722. if (mem == NULL)
  723. return 0;
  724. memset(buf, 0, STARPU_DISK_SIZE_MIN);
  725. /* Measure upload slowness */
  726. start = starpu_timing_now();
  727. for (iter = 0; iter < NITER; ++iter)
  728. {
  729. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, 0, STARPU_DISK_SIZE_MIN, NULL);
  730. }
  731. end = starpu_timing_now();
  732. timing_slowness = end - start;
  733. /* free memory */
  734. starpu_free_flags(buf, STARPU_DISK_SIZE_MIN, 0);
  735. starpu_malloc_flags((void**) &buf, sizeof(char), 0);
  736. STARPU_ASSERT(buf != NULL);
  737. *buf = 0;
  738. /* Measure latency */
  739. start = starpu_timing_now();
  740. for (iter = 0; iter < NITER; ++iter)
  741. {
  742. _starpu_disk_write(STARPU_MAIN_RAM, node, mem, buf, rand() % (STARPU_DISK_SIZE_MIN -1) , 1, NULL);
  743. }
  744. end = starpu_timing_now();
  745. timing_latency = end - start;
  746. _starpu_disk_free(node, mem, STARPU_DISK_SIZE_MIN);
  747. starpu_free_flags(buf, sizeof(char), 0);
  748. _starpu_save_bandwidth_and_latency_disk((NITER/timing_slowness)*STARPU_DISK_SIZE_MIN, (NITER/timing_slowness)*STARPU_DISK_SIZE_MIN,
  749. timing_latency/NITER, timing_latency/NITER, node, fileBase->path);
  750. return 1;
  751. }
  752. struct starpu_disk_ops starpu_disk_hdf5_ops =
  753. {
  754. .alloc = starpu_hdf5_alloc,
  755. .free = starpu_hdf5_free,
  756. .open = starpu_hdf5_open,
  757. .close = starpu_hdf5_close,
  758. .read = starpu_hdf5_read,
  759. .write = starpu_hdf5_write,
  760. .plug = starpu_hdf5_plug,
  761. .unplug = starpu_hdf5_unplug,
  762. .copy = starpu_hdf5_copy,
  763. .bandwidth = get_hdf5_bandwidth_between_disk_and_main_ram,
  764. .full_read = starpu_hdf5_full_read,
  765. .full_write = starpu_hdf5_full_write,
  766. .async_read = starpu_hdf5_async_read,
  767. .async_write = starpu_hdf5_async_write,
  768. .async_full_read = starpu_hdf5_async_full_read,
  769. .async_full_write = starpu_hdf5_async_full_write,
  770. .wait_request = starpu_hdf5_wait,
  771. .test_request = starpu_hdf5_test,
  772. .free_request = starpu_hdf5_free_request
  773. };