disk_pack.c 7.5 KB

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