disk_pack.c 7.5 KB

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