disk_compute.c 5.7 KB

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