disk_pack.c 7.4 KB

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