disk_compute.c 5.8 KB

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