disk_pack.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015 Centre National de la Recherche Scientifique
  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. /* Try to write into disk memory
  18. * Use mechanism to push datas from main ram to disk ram
  19. */
  20. #include <fcntl.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #include <common/config.h>
  30. #include "../helper.h"
  31. #ifdef STARPU_HAVE_WINDOWS
  32. # include <io.h>
  33. # if defined(_WIN32) && !defined(__CYGWIN__)
  34. # define mkdir(path, mode) mkdir(path)
  35. # endif
  36. #endif
  37. #ifdef STARPU_QUICK_CHECK
  38. # define NX (128)
  39. #else
  40. # define NX (1024)
  41. #endif
  42. const struct starpu_data_copy_methods my_vector_copy_data_methods_s;
  43. struct starpu_data_interface_ops starpu_interface_my_vector_ops;
  44. void starpu_my_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  45. uintptr_t ptr, uint32_t nx, size_t elemsize)
  46. {
  47. struct starpu_vector_interface vector =
  48. {
  49. .id = STARPU_VECTOR_INTERFACE_ID,
  50. .ptr = ptr,
  51. .nx = nx,
  52. .elemsize = elemsize,
  53. .dev_handle = ptr,
  54. .slice_base = 0,
  55. .offset = 0
  56. };
  57. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_my_vector_ops);
  58. }
  59. int dotest(struct starpu_disk_ops *ops, char *base)
  60. {
  61. int *A, *C;
  62. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  63. struct starpu_conf conf;
  64. int ret = starpu_conf_init(&conf);
  65. if (ret == -EINVAL)
  66. return EXIT_FAILURE;
  67. conf.ncuda = 0;
  68. conf.nopencl = 0;
  69. ret = starpu_init(&conf);
  70. if (ret == -ENODEV) goto enodev;
  71. /* Initialize path and name */
  72. char pid_str[16];
  73. int pid = getpid();
  74. snprintf(pid_str, 16, "%d", pid);
  75. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  76. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  77. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  78. strcpy(path_file_start, base);
  79. strcat(path_file_start, "/");
  80. strcat(path_file_start, name_file_start);
  81. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  82. strcpy(path_file_end, base);
  83. strcat(path_file_end, "/");
  84. strcat(path_file_end, name_file_end);
  85. /* register a disk */
  86. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*1);
  87. /* can't write on /tmp/ */
  88. if (new_dd == -ENOENT) goto enoent;
  89. unsigned dd = (unsigned) new_dd;
  90. /* allocate two memory spaces */
  91. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  92. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  93. FPRINTF(stderr, "TEST DISK MEMORY \n");
  94. unsigned int j;
  95. /* you register them in a vector */
  96. for(j = 0; j < NX; ++j)
  97. {
  98. A[j] = j;
  99. C[j] = 0;
  100. }
  101. /* you create a file to store the vector ON the disk */
  102. FILE * f = fopen(path_file_start, "wb+");
  103. if (f == NULL)
  104. goto enoent2;
  105. /* store it in the file */
  106. fwrite(A, sizeof(int), NX, f);
  107. /* close the file */
  108. fclose(f);
  109. int descriptor = open(path_file_start, O_RDWR);
  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. #ifdef STARPU_HAVE_WINDOWS
  126. _commit(descriptor);
  127. #else
  128. fsync(descriptor);
  129. #endif
  130. close(descriptor);
  131. /* And now, you want to use your datas in StarPU */
  132. /* Open the file ON the disk */
  133. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  134. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  135. starpu_data_handle_t vector_handleA, vector_handleC;
  136. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  137. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  138. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  139. /* register vector in starpu */
  140. starpu_my_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  141. /* and do what you want with it, here we copy it into an other vector */
  142. starpu_my_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  143. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  144. /* free them */
  145. starpu_data_unregister(vector_handleA);
  146. starpu_data_unregister(vector_handleC);
  147. /* close them in StarPU */
  148. starpu_disk_close(dd, data, NX*sizeof(int));
  149. starpu_disk_close(dd, data_result, NX*sizeof(int));
  150. /* check results */
  151. f = fopen(path_file_end, "rb+");
  152. if (f == NULL)
  153. goto enoent2;
  154. /* take datas */
  155. int size = fread(C, sizeof(int), NX, f);
  156. /* close the file */
  157. fclose(f);
  158. int try = 1;
  159. for (j = 0; j < NX; ++j)
  160. if (A[j] != C[j])
  161. {
  162. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  163. try = 0;
  164. }
  165. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  166. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  167. unlink(path_file_start);
  168. unlink(path_file_end);
  169. free(path_file_start);
  170. free(path_file_end);
  171. /* terminate StarPU, no task can be submitted after */
  172. starpu_shutdown();
  173. if(try)
  174. FPRINTF(stderr, "TEST SUCCESS\n");
  175. else
  176. FPRINTF(stderr, "TEST FAIL\n");
  177. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  178. enodev:
  179. return STARPU_TEST_SKIPPED;
  180. enoent2:
  181. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  182. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  183. enoent:
  184. unlink(path_file_start);
  185. unlink(path_file_end);
  186. free(path_file_start);
  187. free(path_file_end);
  188. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  189. starpu_shutdown();
  190. return STARPU_TEST_SKIPPED;
  191. }
  192. static int merge_result(int old, int new)
  193. {
  194. if (new == EXIT_FAILURE)
  195. return EXIT_FAILURE;
  196. if (old == 0)
  197. return 0;
  198. return new;
  199. }
  200. int main(void)
  201. {
  202. int ret = 0;
  203. char s[128];
  204. snprintf(s, sizeof(s), "/tmp/%s-disk-%d", getenv("USER"), getpid());
  205. mkdir(s, 0777);
  206. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  207. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  208. #ifdef STARPU_LINUX_SYS
  209. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  210. #endif
  211. rmdir(s);
  212. return ret;
  213. }