disk_compute.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. }
  46. /* you create a file to store the vector ON the disk */
  47. FILE * f = fopen("/tmp/STARPU_DISK_COMPUTE_DATA", "wb+");
  48. /* fail */
  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. /* And now, you want to use your datas in StarPU */
  56. /* Open the file ON the disk */
  57. void * data = starpu_disk_open(dd, (void *) "STARPU_DISK_COMPUTE_DATA", NX*sizeof(int));
  58. starpu_data_handle_t vector_handleA, vector_handleC;
  59. /* register vector in starpu */
  60. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t) data, NX, sizeof(int));
  61. /* and do what you want with it, here we copy it into an other vector */
  62. starpu_vector_data_register(&vector_handleC, STARPU_MAIN_RAM, (uintptr_t) C, NX, sizeof(int));
  63. starpu_data_cpy(vector_handleC, vector_handleA, 0, NULL, NULL);
  64. /* free them */
  65. starpu_data_unregister(vector_handleA);
  66. starpu_data_unregister(vector_handleC);
  67. /* close it in StarPU */
  68. starpu_disk_close(dd, data, NX*sizeof(int));
  69. int try = 1;
  70. for (j = 0; j < NX; ++j)
  71. if (A[j] != C[j])
  72. {
  73. printf("Fail A %d != C %d \n", A[j], C[j]);
  74. try = 0;
  75. }
  76. starpu_free_flags(A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  77. starpu_free_flags(C, NX*sizeof(double), STARPU_MALLOC_COUNT);
  78. /* terminate StarPU, no task can be submitted after */
  79. starpu_shutdown();
  80. if(try)
  81. printf("TEST SUCCESS\n");
  82. else
  83. printf("TEST FAIL\n");
  84. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  85. enodev:
  86. return 77;
  87. enoent:
  88. return 77;
  89. }