disk_pack.c 6.5 KB

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