disk_compute.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015, 2016, 2017 CNRS
  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. #include <fcntl.h>
  18. #include <starpu.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <math.h>
  26. #include <common/config.h>
  27. #include "../helper.h"
  28. /*
  29. * Try to write into disk memory
  30. * Use mechanism to push datas from main ram to disk ram
  31. * Here we just simulate performing a dumb computation C=A+0, i.e. a mere copy
  32. * actually
  33. */
  34. #define NX (1024)
  35. int dotest(struct starpu_disk_ops *ops, char *base)
  36. {
  37. int *A, *C;
  38. /* Initialize StarPU with default configuration */
  39. int ret = starpu_init(NULL);
  40. if (ret == -ENODEV) goto enodev;
  41. /* Initialize path and name */
  42. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  43. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  44. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  45. strcpy(path_file_start, base);
  46. strcat(path_file_start, "/");
  47. strcat(path_file_start, name_file_start);
  48. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  49. strcpy(path_file_end, base);
  50. strcat(path_file_end, "/");
  51. strcat(path_file_end, name_file_end);
  52. /* register a disk */
  53. int new_dd = starpu_disk_register(ops, (void *) base, STARPU_DISK_SIZE_MIN);
  54. /* can't write on /tmp/ */
  55. if (new_dd == -ENOENT) goto enoent;
  56. unsigned dd = (unsigned) new_dd;
  57. /* allocate two memory spaces */
  58. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  59. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  60. FPRINTF(stderr, "TEST DISK MEMORY \n");
  61. unsigned int j;
  62. /* you register them in a vector */
  63. for(j = 0; j < NX; ++j)
  64. {
  65. A[j] = j;
  66. C[j] = 0;
  67. }
  68. /* you create a file to store the vector ON the disk */
  69. FILE * f = fopen(path_file_start, "wb+");
  70. if (f == NULL)
  71. goto enoent2;
  72. /* store it in the file */
  73. fwrite(A, sizeof(int), NX, f);
  74. /* close the file */
  75. fclose(f);
  76. int descriptor = open(path_file_start, O_RDWR);
  77. if (descriptor < 0)
  78. goto enoent2;
  79. #ifdef STARPU_HAVE_WINDOWS
  80. _commit(descriptor);
  81. #else
  82. fsync(descriptor);
  83. #endif
  84. close(descriptor);
  85. /* create a file to store result */
  86. f = fopen(path_file_end, "wb+");
  87. if (f == NULL)
  88. goto enoent2;
  89. /* replace all datas by 0 */
  90. fwrite(C, sizeof(int), NX, f);
  91. /* close the file */
  92. fclose(f);
  93. descriptor = open(path_file_end, O_RDWR);
  94. #ifdef STARPU_HAVE_WINDOWS
  95. _commit(descriptor);
  96. #else
  97. fsync(descriptor);
  98. #endif
  99. close(descriptor);
  100. /* And now, you want to use your datas in StarPU */
  101. /* Open the file ON the disk */
  102. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  103. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  104. starpu_data_handle_t vector_handleA, vector_handleC;
  105. /* register vector in starpu */
  106. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  107. /* and do what you want with it, here we copy it into an other vector */
  108. starpu_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  109. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  110. /* free them */
  111. starpu_data_unregister(vector_handleA);
  112. starpu_data_unregister(vector_handleC);
  113. /* close them in StarPU */
  114. starpu_disk_close(dd, data, NX*sizeof(int));
  115. starpu_disk_close(dd, data_result, NX*sizeof(int));
  116. /* check results */
  117. f = fopen(path_file_end, "rb+");
  118. if (f == NULL)
  119. goto enoent2;
  120. /* take datas */
  121. size_t read = fread(C, sizeof(int), NX, f);
  122. STARPU_ASSERT(read == NX);
  123. /* close the file */
  124. fclose(f);
  125. int try = 1;
  126. for (j = 0; j < NX; ++j)
  127. if (A[j] != C[j])
  128. {
  129. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  130. try = 0;
  131. }
  132. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  133. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  134. unlink(path_file_start);
  135. unlink(path_file_end);
  136. free(path_file_start);
  137. free(path_file_end);
  138. /* terminate StarPU, no task can be submitted after */
  139. starpu_shutdown();
  140. if(try)
  141. FPRINTF(stderr, "TEST SUCCESS\n");
  142. else
  143. FPRINTF(stderr, "TEST FAIL\n");
  144. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  145. enodev:
  146. return STARPU_TEST_SKIPPED;
  147. enoent2:
  148. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  149. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  150. enoent:
  151. unlink(path_file_start);
  152. unlink(path_file_end);
  153. free(path_file_start);
  154. free(path_file_end);
  155. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  156. starpu_shutdown();
  157. return STARPU_TEST_SKIPPED;
  158. }
  159. static int merge_result(int old, int new)
  160. {
  161. if (new == EXIT_FAILURE)
  162. return EXIT_FAILURE;
  163. if (old == 0)
  164. return 0;
  165. return new;
  166. }
  167. int main(void)
  168. {
  169. int ret = 0;
  170. int ret2;
  171. char s[128];
  172. char *ptr;
  173. snprintf(s, sizeof(s), "/tmp/%s-disk-XXXXXX", getenv("USER"));
  174. ptr = _starpu_mkdtemp(s);
  175. if (!ptr)
  176. {
  177. FPRINTF(stderr, "Cannot make directory '%s'\n", s);
  178. return STARPU_TEST_SKIPPED;
  179. }
  180. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  181. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  182. #ifdef STARPU_LINUX_SYS
  183. if ((NX * sizeof(int)) % getpagesize() == 0)
  184. {
  185. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  186. }
  187. else
  188. {
  189. ret = merge_result(ret, STARPU_TEST_SKIPPED);
  190. }
  191. #endif
  192. ret2 = rmdir(s);
  193. STARPU_CHECK_RETURN_VALUE(ret2, "rmdir '%s'\n", s);
  194. return ret;
  195. }