disk_compute.c 5.6 KB

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