disk_compute.c 4.3 KB

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