disk_compute.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. /* Try to write into disk memory
  17. * Use mechanism to push datas from main ram to disk ram
  18. */
  19. #include <fcntl.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <math.h>
  27. #include <common/config.h>
  28. #include "../helper.h"
  29. #ifdef STARPU_HAVE_WINDOWS
  30. #include <io.h>
  31. #if defined(_WIN32) && !defined(__CYGWIN__)
  32. #define mkdir(path, mode) mkdir(path)
  33. #endif
  34. #endif
  35. #define NX (1024)
  36. int dotest(struct starpu_disk_ops *ops, char *base)
  37. {
  38. int *A, *C;
  39. /* Initialize StarPU with default configuration */
  40. int ret = starpu_init(NULL);
  41. if (ret == -ENODEV) goto enodev;
  42. /* Initialize path and name */
  43. char pid_str[16];
  44. int pid = getpid();
  45. snprintf(pid_str, 16, "%d", pid);
  46. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  47. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  48. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  49. strcpy(path_file_start, base);
  50. strcat(path_file_start, "/");
  51. strcat(path_file_start, name_file_start);
  52. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  53. strcpy(path_file_end, base);
  54. strcat(path_file_end, "/");
  55. strcat(path_file_end, name_file_end);
  56. /* register a disk */
  57. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*1);
  58. /* can't write on /tmp/ */
  59. if (new_dd == -ENOENT) goto enoent;
  60. unsigned dd = (unsigned) new_dd;
  61. /* allocate two memory spaces */
  62. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  63. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  64. FPRINTF(stderr, "TEST DISK MEMORY \n");
  65. unsigned int j;
  66. /* you register them in a vector */
  67. for(j = 0; j < NX; ++j)
  68. {
  69. A[j] = j;
  70. C[j] = 0;
  71. }
  72. /* you create a file to store the vector ON the disk */
  73. FILE * f = fopen(path_file_start, "wb+");
  74. if (f == NULL)
  75. goto enoent2;
  76. /* store it in the file */
  77. fwrite(A, sizeof(int), NX, f);
  78. /* close the file */
  79. fclose(f);
  80. int descriptor = open(path_file_start, O_RDWR);
  81. #ifdef STARPU_HAVE_WINDOWS
  82. _commit(descriptor);
  83. #else
  84. fsync(descriptor);
  85. #endif
  86. close(descriptor);
  87. /* create a file to store result */
  88. f = fopen(path_file_end, "wb+");
  89. if (f == NULL)
  90. goto enoent2;
  91. /* replace all datas by 0 */
  92. fwrite(C, sizeof(int), NX, f);
  93. /* close the file */
  94. fclose(f);
  95. descriptor = open(path_file_end, O_RDWR);
  96. #ifdef STARPU_HAVE_WINDOWS
  97. _commit(descriptor);
  98. #else
  99. fsync(descriptor);
  100. #endif
  101. close(descriptor);
  102. /* And now, you want to use your datas in StarPU */
  103. /* Open the file ON the disk */
  104. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  105. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  106. starpu_data_handle_t vector_handleA, vector_handleC;
  107. /* register vector in starpu */
  108. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  109. /* and do what you want with it, here we copy it into an other vector */
  110. starpu_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  111. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  112. /* free them */
  113. starpu_data_unregister(vector_handleA);
  114. starpu_data_unregister(vector_handleC);
  115. /* close them in StarPU */
  116. starpu_disk_close(dd, data, NX*sizeof(int));
  117. starpu_disk_close(dd, data_result, NX*sizeof(int));
  118. /* check results */
  119. f = fopen(path_file_end, "rb+");
  120. if (f == NULL)
  121. goto enoent2;
  122. /* take datas */
  123. int size = fread(C, sizeof(int), NX, f);
  124. /* close the file */
  125. fclose(f);
  126. int try = 1;
  127. for (j = 0; j < NX; ++j)
  128. if (A[j] != C[j])
  129. {
  130. FPRINTF(stderr, "Fail A %d != C %d \n", A[j], C[j]);
  131. try = 0;
  132. }
  133. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  134. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  135. unlink(path_file_start);
  136. unlink(path_file_end);
  137. free(path_file_start);
  138. free(path_file_end);
  139. /* terminate StarPU, no task can be submitted after */
  140. starpu_shutdown();
  141. if(try)
  142. FPRINTF(stderr, "TEST SUCCESS\n");
  143. else
  144. FPRINTF(stderr, "TEST FAIL\n");
  145. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  146. enodev:
  147. return STARPU_TEST_SKIPPED;
  148. enoent2:
  149. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  150. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  151. enoent:
  152. unlink(path_file_start);
  153. unlink(path_file_end);
  154. free(path_file_start);
  155. free(path_file_end);
  156. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  157. starpu_shutdown();
  158. return STARPU_TEST_SKIPPED;
  159. }
  160. static int merge_result(int old, int new)
  161. {
  162. if (new == EXIT_FAILURE)
  163. return EXIT_FAILURE;
  164. if (old == 0)
  165. return 0;
  166. return new;
  167. }
  168. int main(void)
  169. {
  170. int ret = 0;
  171. char s[128];
  172. snprintf(s, sizeof(s), "/tmp/%s-disk", getenv("USER"));
  173. mkdir(s, 0777);
  174. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  175. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  176. #ifdef STARPU_LINUX_SYS
  177. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  178. #endif
  179. return ret;
  180. }