disk_pack.c 7.5 KB

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