disk_pack.c 6.7 KB

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