disk_compute.c 3.7 KB

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