disk_pack.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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)
  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. char * base = "/tmp";
  67. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  68. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  69. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  70. strcpy(path_file_start, base);
  71. strcat(path_file_start, "/");
  72. strcat(path_file_start, name_file_start);
  73. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  74. strcpy(path_file_end, base);
  75. strcat(path_file_end, "/");
  76. strcat(path_file_end, name_file_end);
  77. /* register a disk */
  78. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*1);
  79. /* can't write on /tmp/ */
  80. if (new_dd == -ENOENT) goto enoent;
  81. unsigned dd = (unsigned) new_dd;
  82. /* allocate two memory spaces */
  83. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  84. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  85. FPRINTF(stderr, "TEST DISK MEMORY \n");
  86. unsigned int j;
  87. /* you register them in a vector */
  88. for(j = 0; j < NX; ++j)
  89. {
  90. A[j] = j;
  91. C[j] = 0;
  92. }
  93. /* you create a file to store the vector ON the disk */
  94. FILE * f = fopen(path_file_start, "wb+");
  95. if (f == NULL)
  96. goto enoent;
  97. /* store it in the file */
  98. fwrite(A, sizeof(int), NX, f);
  99. /* close the file */
  100. fclose(f);
  101. int descriptor = open(path_file_start, O_RDWR);
  102. #ifdef STARPU_HAVE_WINDOWS
  103. _commit(descriptor);
  104. #else
  105. fsync(descriptor);
  106. #endif
  107. close(descriptor);
  108. /* create a file to store result */
  109. f = fopen(path_file_end, "wb+");
  110. if (f == NULL)
  111. goto enoent;
  112. /* replace all datas by 0 */
  113. fwrite(C, sizeof(int), NX, f);
  114. /* close the file */
  115. fclose(f);
  116. descriptor = open(path_file_end, O_RDWR);
  117. #ifdef STARPU_HAVE_WINDOWS
  118. _commit(descriptor);
  119. #else
  120. fsync(descriptor);
  121. #endif
  122. close(descriptor);
  123. /* And now, you want to use your datas in StarPU */
  124. /* Open the file ON the disk */
  125. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  126. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  127. starpu_data_handle_t vector_handleA, vector_handleC;
  128. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  129. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  130. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  131. /* register vector in starpu */
  132. starpu_my_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  133. /* and do what you want with it, here we copy it into an other vector */
  134. starpu_my_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  135. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  136. /* free them */
  137. starpu_data_unregister(vector_handleA);
  138. starpu_data_unregister(vector_handleC);
  139. /* close them in StarPU */
  140. starpu_disk_close(dd, data, NX*sizeof(int));
  141. starpu_disk_close(dd, data_result, NX*sizeof(int));
  142. /* check results */
  143. f = fopen(path_file_end, "rb+");
  144. if (f == NULL)
  145. goto enoent;
  146. /* take datas */
  147. int size = fread(C, sizeof(int), NX, f);
  148. /* close the file */
  149. fclose(f);
  150. int try = 1;
  151. for (j = 0; j < NX; ++j)
  152. if (A[j] != C[j])
  153. {
  154. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  155. try = 0;
  156. }
  157. starpu_free_flags(A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  158. starpu_free_flags(C, NX*sizeof(double), STARPU_MALLOC_COUNT);
  159. unlink(path_file_start);
  160. unlink(path_file_end);
  161. free(path_file_start);
  162. free(path_file_end);
  163. /* terminate StarPU, no task can be submitted after */
  164. starpu_shutdown();
  165. if(try)
  166. FPRINTF(stderr, "TEST SUCCESS\n");
  167. else
  168. FPRINTF(stderr, "TEST FAIL\n");
  169. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  170. enodev:
  171. return STARPU_TEST_SKIPPED;
  172. enoent:
  173. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  174. starpu_shutdown();
  175. return STARPU_TEST_SKIPPED;
  176. }
  177. static int merge_result(int old, int new)
  178. {
  179. if (new == EXIT_FAILURE)
  180. return EXIT_FAILURE;
  181. if (old == 0)
  182. return 0;
  183. return new;
  184. }
  185. int main(void) {
  186. int ret = 0;
  187. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops));
  188. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops));
  189. #ifdef STARPU_LINUX_SYS
  190. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops));
  191. #endif
  192. return ret;
  193. }