disk_compute.c 4.7 KB

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