disk_pack.c 7.3 KB

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