disk_pack.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 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 <starpu.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <unistd.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <math.h>
  28. #include "../helper.h"
  29. #if !defined(STARPU_HAVE_UNSETENV)
  30. #warning unsetenv is not defined. Skipping test
  31. int main(void)
  32. {
  33. return STARPU_TEST_SKIPPED;
  34. }
  35. #else
  36. /*
  37. * Try to write into disk memory
  38. * Use mechanism to push datas from main ram to disk ram
  39. * Here we force using the pack/unpack mechanism
  40. */
  41. #define NX (16*1024)
  42. const struct starpu_data_copy_methods my_vector_copy_data_methods_s;
  43. struct starpu_data_interface_ops starpu_interface_my_vector_ops;
  44. void starpu_my_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  45. uintptr_t ptr, uint32_t nx, size_t elemsize)
  46. {
  47. struct starpu_vector_interface vector =
  48. {
  49. .id = STARPU_VECTOR_INTERFACE_ID,
  50. .ptr = ptr,
  51. .nx = nx,
  52. .elemsize = elemsize,
  53. .dev_handle = ptr,
  54. .slice_base = 0,
  55. .offset = 0
  56. };
  57. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_my_vector_ops);
  58. }
  59. int dotest(struct starpu_disk_ops *ops, char *base)
  60. {
  61. int *A, *C;
  62. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  63. // Ignore environment variables as we want to force the exact number of workers
  64. unsetenv("STARPU_NCUDA");
  65. unsetenv("STARPU_NOPENCL");
  66. unsetenv("STARPU_NMIC");
  67. unsetenv("STARPU_NSCC");
  68. struct starpu_conf conf;
  69. int ret = starpu_conf_init(&conf);
  70. if (ret == -EINVAL)
  71. return EXIT_FAILURE;
  72. #ifdef STARPU_HAVE_UNSETENV
  73. unsetenv("STARPU_NCUDA");
  74. unsetenv("STARPU_NOPENCL");
  75. #endif
  76. conf.ncuda = 0;
  77. conf.nopencl = 0;
  78. conf.nmic = 0;
  79. conf.nscc = 0;
  80. ret = starpu_init(&conf);
  81. if (ret == -ENODEV) goto enodev;
  82. if (starpu_cpu_worker_get_count() == 0)
  83. {
  84. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  85. starpu_shutdown();
  86. return STARPU_TEST_SKIPPED;
  87. }
  88. /* Initialize path and name */
  89. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  90. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  91. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  92. strcpy(path_file_start, base);
  93. strcat(path_file_start, "/");
  94. strcat(path_file_start, name_file_start);
  95. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  96. strcpy(path_file_end, base);
  97. strcat(path_file_end, "/");
  98. strcat(path_file_end, name_file_end);
  99. /* register a disk */
  100. int new_dd = starpu_disk_register(ops, (void *) base, STARPU_DISK_SIZE_MIN);
  101. /* can't write on /tmp/ */
  102. if (new_dd == -ENOENT) goto enoent;
  103. unsigned dd = (unsigned) new_dd;
  104. /* allocate two memory spaces */
  105. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  106. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  107. FPRINTF(stderr, "TEST DISK MEMORY \n");
  108. unsigned int j;
  109. /* you register them in a vector */
  110. for(j = 0; j < NX; ++j)
  111. {
  112. A[j] = j;
  113. C[j] = 0;
  114. }
  115. /* you create a file to store the vector ON the disk */
  116. FILE * f = fopen(path_file_start, "wb+");
  117. if (f == NULL)
  118. goto enoent2;
  119. /* store it in the file */
  120. fwrite(A, sizeof(int), NX, f);
  121. /* close the file */
  122. fclose(f);
  123. int descriptor = open(path_file_start, O_RDWR);
  124. if (descriptor < 0)
  125. goto enoent2;
  126. #ifdef STARPU_HAVE_WINDOWS
  127. _commit(descriptor);
  128. #else
  129. fsync(descriptor);
  130. #endif
  131. close(descriptor);
  132. /* create a file to store result */
  133. f = fopen(path_file_end, "wb+");
  134. if (f == NULL)
  135. goto enoent2;
  136. /* replace all datas by 0 */
  137. fwrite(C, sizeof(int), NX, f);
  138. /* close the file */
  139. fclose(f);
  140. descriptor = open(path_file_end, O_RDWR);
  141. if (descriptor < 0)
  142. goto enoent2;
  143. #ifdef STARPU_HAVE_WINDOWS
  144. _commit(descriptor);
  145. #else
  146. fsync(descriptor);
  147. #endif
  148. close(descriptor);
  149. /* And now, you want to use your datas in StarPU */
  150. /* Open the file ON the disk */
  151. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  152. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  153. starpu_data_handle_t vector_handleA, vector_handleC;
  154. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  155. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  156. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  157. /* register vector in starpu */
  158. starpu_my_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  159. /* and do what you want with it, here we copy it into an other vector */
  160. starpu_my_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  161. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  162. /* free them */
  163. starpu_data_unregister(vector_handleA);
  164. starpu_data_unregister(vector_handleC);
  165. /* close them in StarPU */
  166. starpu_disk_close(dd, data, NX*sizeof(int));
  167. starpu_disk_close(dd, data_result, NX*sizeof(int));
  168. /* check results */
  169. f = fopen(path_file_end, "rb+");
  170. if (f == NULL)
  171. goto enoent2;
  172. /* take datas */
  173. size_t read = fread(C, sizeof(int), NX, f);
  174. STARPU_ASSERT(read == NX);
  175. /* close the file */
  176. fclose(f);
  177. int try = 1;
  178. for (j = 0; j < NX; ++j)
  179. if (A[j] != C[j])
  180. {
  181. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  182. try = 0;
  183. }
  184. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  185. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  186. unlink(path_file_start);
  187. unlink(path_file_end);
  188. free(path_file_start);
  189. free(path_file_end);
  190. /* terminate StarPU, no task can be submitted after */
  191. starpu_shutdown();
  192. if(try)
  193. FPRINTF(stderr, "TEST SUCCESS\n");
  194. else
  195. FPRINTF(stderr, "TEST FAIL\n");
  196. return try ? EXIT_SUCCESS : EXIT_FAILURE;
  197. enodev:
  198. return STARPU_TEST_SKIPPED;
  199. enoent2:
  200. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  201. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  202. enoent:
  203. unlink(path_file_start);
  204. unlink(path_file_end);
  205. free(path_file_start);
  206. free(path_file_end);
  207. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  208. starpu_shutdown();
  209. return STARPU_TEST_SKIPPED;
  210. }
  211. static int merge_result(int old, int new)
  212. {
  213. if (new == EXIT_FAILURE)
  214. return EXIT_FAILURE;
  215. if (old == 0)
  216. return 0;
  217. return new;
  218. }
  219. int main(void)
  220. {
  221. int ret = 0;
  222. int ret2;
  223. char s[128];
  224. char *ptr;
  225. snprintf(s, sizeof(s), "/tmp/%s-disk-XXXXXX", getenv("USER"));
  226. ptr = _starpu_mkdtemp(s);
  227. if (!ptr)
  228. {
  229. FPRINTF(stderr, "Cannot make directory <%s>\n", s);
  230. return STARPU_TEST_SKIPPED;
  231. }
  232. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  233. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  234. #ifdef STARPU_LINUX_SYS
  235. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  236. #endif
  237. ret2 = rmdir(s);
  238. if (ret2 < 0)
  239. STARPU_CHECK_RETURN_VALUE(-errno, "rmdir '%s'\n", s);
  240. return ret;
  241. }
  242. #endif