disk_compute.c 5.6 KB

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