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,2019 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. conf.ncuda = 0;
  73. conf.nopencl = 0;
  74. conf.nmic = 0;
  75. conf.nscc = 0;
  76. ret = starpu_init(&conf);
  77. if (ret == -ENODEV) goto enodev;
  78. if (starpu_cpu_worker_get_count() == 0)
  79. {
  80. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  81. starpu_shutdown();
  82. return STARPU_TEST_SKIPPED;
  83. }
  84. /* Initialize path and name */
  85. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  86. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  87. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  88. strcpy(path_file_start, base);
  89. strcat(path_file_start, "/");
  90. strcat(path_file_start, name_file_start);
  91. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  92. strcpy(path_file_end, base);
  93. strcat(path_file_end, "/");
  94. strcat(path_file_end, name_file_end);
  95. /* register a disk */
  96. int new_dd = starpu_disk_register(ops, (void *) base, STARPU_DISK_SIZE_MIN);
  97. /* can't write on /tmp/ */
  98. if (new_dd == -ENOENT) goto enoent;
  99. unsigned dd = (unsigned) new_dd;
  100. /* allocate two memory spaces */
  101. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  102. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  103. FPRINTF(stderr, "TEST DISK MEMORY \n");
  104. unsigned int j;
  105. /* you register them in a vector */
  106. for(j = 0; j < NX; ++j)
  107. {
  108. A[j] = j;
  109. C[j] = 0;
  110. }
  111. /* you create a file to store the vector ON the disk */
  112. FILE * f = fopen(path_file_start, "wb+");
  113. if (f == NULL)
  114. goto enoent2;
  115. /* store it in the file */
  116. fwrite(A, sizeof(int), NX, f);
  117. /* close the file */
  118. fclose(f);
  119. int descriptor = open(path_file_start, O_RDWR);
  120. if (descriptor < 0)
  121. goto enoent2;
  122. #ifdef STARPU_HAVE_WINDOWS
  123. _commit(descriptor);
  124. #else
  125. fsync(descriptor);
  126. #endif
  127. close(descriptor);
  128. /* create a file to store result */
  129. f = fopen(path_file_end, "wb+");
  130. if (f == NULL)
  131. goto enoent2;
  132. /* replace all datas by 0 */
  133. fwrite(C, sizeof(int), NX, f);
  134. /* close the file */
  135. fclose(f);
  136. descriptor = open(path_file_end, O_RDWR);
  137. if (descriptor < 0)
  138. goto enoent2;
  139. #ifdef STARPU_HAVE_WINDOWS
  140. _commit(descriptor);
  141. #else
  142. fsync(descriptor);
  143. #endif
  144. close(descriptor);
  145. /* And now, you want to use your datas in StarPU */
  146. /* Open the file ON the disk */
  147. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  148. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  149. starpu_data_handle_t vector_handleA, vector_handleC;
  150. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  151. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  152. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  153. /* register vector in starpu */
  154. starpu_my_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  155. /* and do what you want with it, here we copy it into an other vector */
  156. starpu_my_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  157. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  158. /* free them */
  159. starpu_data_unregister(vector_handleA);
  160. starpu_data_unregister(vector_handleC);
  161. /* close them in StarPU */
  162. starpu_disk_close(dd, data, NX*sizeof(int));
  163. starpu_disk_close(dd, data_result, NX*sizeof(int));
  164. /* check results */
  165. f = fopen(path_file_end, "rb+");
  166. if (f == NULL)
  167. goto enoent2;
  168. /* take datas */
  169. size_t read = fread(C, sizeof(int), NX, f);
  170. STARPU_ASSERT(read == NX);
  171. /* close the file */
  172. fclose(f);
  173. int try = 1;
  174. for (j = 0; j < NX; ++j)
  175. if (A[j] != C[j])
  176. {
  177. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  178. try = 0;
  179. }
  180. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  181. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  182. unlink(path_file_start);
  183. unlink(path_file_end);
  184. free(path_file_start);
  185. free(path_file_end);
  186. /* terminate StarPU, no task can be submitted after */
  187. starpu_shutdown();
  188. if(try)
  189. FPRINTF(stderr, "TEST SUCCESS\n");
  190. else
  191. FPRINTF(stderr, "TEST FAIL\n");
  192. return try ? EXIT_SUCCESS : EXIT_FAILURE;
  193. enodev:
  194. return STARPU_TEST_SKIPPED;
  195. enoent2:
  196. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  197. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  198. enoent:
  199. unlink(path_file_start);
  200. unlink(path_file_end);
  201. free(path_file_start);
  202. free(path_file_end);
  203. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  204. starpu_shutdown();
  205. return STARPU_TEST_SKIPPED;
  206. }
  207. static int merge_result(int old, int new)
  208. {
  209. if (new == EXIT_FAILURE)
  210. return EXIT_FAILURE;
  211. if (old == 0)
  212. return 0;
  213. return new;
  214. }
  215. int main(void)
  216. {
  217. int ret = 0;
  218. int ret2;
  219. char s[128];
  220. char *ptr;
  221. #ifdef STARPU_HAVE_SETENV
  222. setenv("STARPU_CALIBRATE_MINIMUM", "1", 1);
  223. #endif
  224. snprintf(s, sizeof(s), "/tmp/%s-disk-XXXXXX", getenv("USER"));
  225. ptr = _starpu_mkdtemp(s);
  226. if (!ptr)
  227. {
  228. FPRINTF(stderr, "Cannot make directory <%s>\n", s);
  229. return STARPU_TEST_SKIPPED;
  230. }
  231. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  232. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  233. #ifdef STARPU_LINUX_SYS
  234. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  235. #endif
  236. ret2 = rmdir(s);
  237. if (ret2 < 0)
  238. STARPU_CHECK_RETURN_VALUE(-errno, "rmdir '%s'\n", s);
  239. return ret;
  240. }
  241. #endif