disk_compute.c 5.3 KB

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