disk_pack.c 7.4 KB

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