disk_pack.c 7.7 KB

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