disk_compute.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2017,2018 CNRS
  4. * Copyright (C) 2013 Inria
  5. * Copyright (C) 2014 Université de Bordeaux
  6. * Copyright (C) 2013 Corentin Salingue
  7. *
  8. * StarPU is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation; either version 2.1 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * StarPU is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  18. */
  19. //! [To be included. You should update doxygen if you see this text.]
  20. /* Try to write into disk memory
  21. * Use mechanism to push datas from main ram to disk ram
  22. */
  23. #include <starpu.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #define NX (1024)
  30. int main(int argc, char **argv)
  31. {
  32. /* Initialize StarPU with default configuration */
  33. int ret = starpu_init(NULL);
  34. if (ret == -ENODEV) goto enodev;
  35. /* Initialize path and name */
  36. char pid_str[16];
  37. int pid = getpid();
  38. snprintf(pid_str, sizeof(pid_str), "%d", pid);
  39. const char *name_file_start = "STARPU_DISK_COMPUTE_DATA_";
  40. const char *name_file_end = "STARPU_DISK_COMPUTE_DATA_RESULT_";
  41. char * path_file_start = malloc(strlen(base) + 1 + strlen(name_file_start) + 1);
  42. strcpy(path_file_start, base);
  43. strcat(path_file_start, "/");
  44. strcat(path_file_start, name_file_start);
  45. char * path_file_end = malloc(strlen(base) + 1 + strlen(name_file_end) + 1);
  46. strcpy(path_file_end, base);
  47. strcat(path_file_end, "/");
  48. strcat(path_file_end, name_file_end);
  49. /* register a disk */
  50. int new_dd = starpu_disk_register(&starpu_disk_unistd_ops, (void *) base, 1024*1024*1);
  51. /* can't write on /tmp/ */
  52. if (new_dd == -ENOENT) goto enoent;
  53. unsigned dd = (unsigned) new_dd;
  54. printf("TEST DISK MEMORY \n");
  55. /* Imagine, you want to compute datas */
  56. int *A;
  57. int *C;
  58. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  59. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  60. unsigned int j;
  61. /* you register them in a vector */
  62. for(j = 0; j < NX; ++j)
  63. {
  64. A[j] = j;
  65. C[j] = 0;
  66. }
  67. /* you create a file to store the vector ON the disk */
  68. FILE * f = fopen(path_file_start, "wb+");
  69. if (f == NULL)
  70. goto enoent2;
  71. /* store it in the file */
  72. fwrite(A, sizeof(int), NX, f);
  73. /* close the file */
  74. fclose(f);
  75. /* create a file to store result */
  76. f = fopen(path_file_end, "wb+");
  77. if (f == NULL)
  78. goto enoent2;
  79. /* replace all datas by 0 */
  80. fwrite(C, sizeof(int), NX, f);
  81. /* close the file */
  82. fclose(f);
  83. /* And now, you want to use your datas in StarPU */
  84. /* Open the file ON the disk */
  85. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  86. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  87. starpu_data_handle_t vector_handleA, vector_handleC;
  88. /* register vector in starpu */
  89. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  90. /* and do what you want with it, here we copy it into an other vector */
  91. starpu_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  92. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  93. /* free them */
  94. starpu_data_unregister(vector_handleA);
  95. starpu_data_unregister(vector_handleC);
  96. /* close them in StarPU */
  97. starpu_disk_close(dd, data, NX*sizeof(int));
  98. starpu_disk_close(dd, data_result, NX*sizeof(int));
  99. /* check results */
  100. f = fopen(path_file_end, "rb+");
  101. if (f == NULL)
  102. goto enoent;
  103. /* take datas */
  104. int size = fread(C, sizeof(int), NX, f);
  105. /* close the file */
  106. fclose(f);
  107. int try = 1;
  108. for (j = 0; j < NX; ++j)
  109. if (A[j] != C[j])
  110. {
  111. printf("Fail A %d != C %d \n", A[j], C[j]);
  112. try = 0;
  113. }
  114. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  115. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  116. unlink(path_file_start);
  117. unlink(path_file_end);
  118. free(path_file_start);
  119. free(path_file_end);
  120. /* terminate StarPU, no task can be submitted after */
  121. starpu_shutdown();
  122. if(try)
  123. printf("TEST SUCCESS\n");
  124. else
  125. printf("TEST FAIL\n");
  126. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  127. enodev:
  128. return 77;
  129. enoent2:
  130. starpu_free_flags(A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  131. starpu_free_flags(C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  132. enoent:
  133. unlink(path_file_start);
  134. unlink(path_file_end);
  135. free(path_file_start);
  136. free(path_file_end);
  137. starpu_shutdown();
  138. return 77;
  139. }
  140. //! [To be included. You should update doxygen if you see this text.]