disk_pack.c 7.0 KB

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