disk_compute.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015 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. /* Try to write into disk memory
  18. * Use mechanism to push datas from main ram to disk ram
  19. */
  20. #include <fcntl.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #include <common/config.h>
  30. #include "../helper.h"
  31. #ifdef STARPU_HAVE_WINDOWS
  32. # include <io.h>
  33. # if defined(_WIN32) && !defined(__CYGWIN__)
  34. # define mkdir(path, mode) mkdir(path)
  35. # endif
  36. #endif
  37. #define NX (1024)
  38. int dotest(struct starpu_disk_ops *ops, char *base)
  39. {
  40. int *A, *C;
  41. /* Initialize StarPU with default configuration */
  42. int ret = starpu_init(NULL);
  43. if (ret == -ENODEV) goto enodev;
  44. /* Initialize path and name */
  45. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  46. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  47. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  48. strcpy(path_file_start, base);
  49. strcat(path_file_start, "/");
  50. strcat(path_file_start, name_file_start);
  51. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  52. strcpy(path_file_end, base);
  53. strcat(path_file_end, "/");
  54. strcat(path_file_end, name_file_end);
  55. /* register a disk */
  56. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*1);
  57. /* can't write on /tmp/ */
  58. if (new_dd == -ENOENT) goto enoent;
  59. unsigned dd = (unsigned) new_dd;
  60. /* allocate two memory spaces */
  61. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  62. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  63. FPRINTF(stderr, "TEST DISK MEMORY \n");
  64. unsigned int j;
  65. /* you register them in a vector */
  66. for(j = 0; j < NX; ++j)
  67. {
  68. A[j] = j;
  69. C[j] = 0;
  70. }
  71. /* you create a file to store the vector ON the disk */
  72. FILE * f = fopen(path_file_start, "wb+");
  73. if (f == NULL)
  74. goto enoent2;
  75. /* store it in the file */
  76. fwrite(A, sizeof(int), NX, f);
  77. /* close the file */
  78. fclose(f);
  79. int descriptor = open(path_file_start, O_RDWR);
  80. #ifdef STARPU_HAVE_WINDOWS
  81. _commit(descriptor);
  82. #else
  83. fsync(descriptor);
  84. #endif
  85. close(descriptor);
  86. /* create a file to store result */
  87. f = fopen(path_file_end, "wb+");
  88. if (f == NULL)
  89. goto enoent2;
  90. /* replace all datas by 0 */
  91. fwrite(C, sizeof(int), NX, f);
  92. /* close the file */
  93. fclose(f);
  94. descriptor = open(path_file_end, O_RDWR);
  95. #ifdef STARPU_HAVE_WINDOWS
  96. _commit(descriptor);
  97. #else
  98. fsync(descriptor);
  99. #endif
  100. close(descriptor);
  101. /* And now, you want to use your datas in StarPU */
  102. /* Open the file ON the disk */
  103. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  104. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  105. starpu_data_handle_t vector_handleA, vector_handleC;
  106. /* register vector in starpu */
  107. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  108. /* and do what you want with it, here we copy it into an other vector */
  109. starpu_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  110. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  111. /* free them */
  112. starpu_data_unregister(vector_handleA);
  113. starpu_data_unregister(vector_handleC);
  114. /* close them in StarPU */
  115. starpu_disk_close(dd, data, NX*sizeof(int));
  116. starpu_disk_close(dd, data_result, NX*sizeof(int));
  117. /* check results */
  118. f = fopen(path_file_end, "rb+");
  119. if (f == NULL)
  120. goto enoent2;
  121. /* take datas */
  122. fread(C, sizeof(int), NX, f);
  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. char s[128];
  171. snprintf(s, sizeof(s), "/tmp/%s-disk-%d", getenv("USER"), getpid());
  172. mkdir(s, 0777);
  173. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  174. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  175. #ifdef STARPU_LINUX_SYS
  176. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  177. #endif
  178. rmdir(s);
  179. return ret;
  180. }