disk_compute.c 3.7 KB

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