disk_pack.c 6.6 KB

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