disk_compute.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #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, 16, "%d", pid);
  37. char * base = "/tmp/";
  38. char * name_file_start = malloc(128*sizeof(char));
  39. strcpy(name_file_start, "STARPU_DISK_COMPUTE_DATA_");
  40. strcat(name_file_start, pid_str);
  41. char * name_file_end = malloc(128*sizeof(char));
  42. strcpy(name_file_end, "STARPU_DISK_COMPUTE_DATA_RESULT_");
  43. strcat(name_file_end, pid_str);
  44. char * path_file_start = malloc(128*sizeof(char));
  45. strcpy(path_file_start, base);
  46. strcat(path_file_start, name_file_start);
  47. char * path_file_end = malloc(128*sizeof(char));
  48. strcpy(path_file_end, base);
  49. strcat(path_file_end, name_file_end);
  50. /* register a disk */
  51. int new_dd = starpu_disk_register(&starpu_disk_unistd_ops, (void *) base, 1024*1024*1);
  52. /* can't write on /tmp/ */
  53. if (new_dd == -ENOENT) goto enoent;
  54. unsigned dd = (unsigned) new_dd;
  55. printf("TEST DISK MEMORY \n");
  56. /* Imagine, you want to compute datas */
  57. int *A;
  58. int *C;
  59. starpu_malloc_flags((void **)&A, NX*sizeof(int), STARPU_MALLOC_COUNT);
  60. starpu_malloc_flags((void **)&C, NX*sizeof(int), STARPU_MALLOC_COUNT);
  61. unsigned int j;
  62. /* you register them in a vector */
  63. for(j = 0; j < NX; ++j)
  64. {
  65. A[j] = j;
  66. C[j] = 0;
  67. }
  68. /* you create a file to store the vector ON the disk */
  69. FILE * f = fopen(path_file_start, "wb+");
  70. if (f == NULL)
  71. goto enoent;
  72. /* store it in the file */
  73. fwrite(A, sizeof(int), NX, f);
  74. /* close the file */
  75. fclose(f);
  76. int descriptor = open(path_file_start, O_RDWR);
  77. fdatasync(descriptor);
  78. close(descriptor);
  79. /* create a file to store result */
  80. f = fopen(path_file_end, "wb+");
  81. if (f == NULL)
  82. goto enoent;
  83. /* replace all datas by 0 */
  84. fwrite(C, sizeof(int), NX, f);
  85. /* close the file */
  86. fclose(f);
  87. descriptor = open(path_file_end, O_RDWR);
  88. fdatasync(descriptor);
  89. close(descriptor);
  90. /* And now, you want to use your datas in StarPU */
  91. /* Open the file ON the disk */
  92. void * data = starpu_disk_open(dd, (void *) name_file_start, NX*sizeof(int));
  93. void * data_result = starpu_disk_open(dd, (void *) name_file_end, NX*sizeof(int));
  94. starpu_data_handle_t vector_handleA, vector_handleC;
  95. /* register vector in starpu */
  96. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  97. /* and do what you want with it, here we copy it into an other vector */
  98. starpu_vector_data_register(&vector_handleC, dd, (uintptr_t) data_result, NX, sizeof(int));
  99. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  100. /* free them */
  101. starpu_data_unregister(vector_handleA);
  102. starpu_data_unregister(vector_handleC);
  103. /* close them in StarPU */
  104. starpu_disk_close(dd, data, NX*sizeof(int));
  105. starpu_disk_close(dd, data_result, NX*sizeof(int));
  106. /* check results */
  107. f = fopen(path_file_end, "rb+");
  108. if (f == NULL)
  109. goto enoent;
  110. /* take datas */
  111. int size = fread(C, sizeof(int), NX, f);
  112. /* close the file */
  113. fclose(f);
  114. int try = 1;
  115. for (j = 0; j < NX; ++j)
  116. if (A[j] != C[j])
  117. {
  118. printf("Fail A %d != C %d \n", A[j], C[j]);
  119. try = 0;
  120. }
  121. starpu_free_flags(A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  122. starpu_free_flags(C, NX*sizeof(double), STARPU_MALLOC_COUNT);
  123. unlink(path_file_start);
  124. unlink(path_file_end);
  125. free(name_file_start);
  126. free(name_file_end);
  127. free(path_file_start);
  128. free(path_file_end);
  129. /* terminate StarPU, no task can be submitted after */
  130. starpu_shutdown();
  131. if(try)
  132. printf("TEST SUCCESS\n");
  133. else
  134. printf("TEST FAIL\n");
  135. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  136. enodev:
  137. return 77;
  138. enoent:
  139. return 77;
  140. }