disk_compute.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 (30*1000000)
  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*40);
  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[NX];
  37. int C[NX];
  38. unsigned int j;
  39. /* you register them in a vector */
  40. for(j = 0; j < NX; ++j)
  41. {
  42. A[j] = j;
  43. }
  44. /* you create a file to store the vector ON the disk */
  45. FILE * f = fopen("/tmp/STARPU_DISK_COMPUTE_DATA", "rb+");
  46. /* fail */
  47. if (f == NULL)
  48. goto enoent;
  49. /* store it in the file */
  50. fwrite((void *) A, sizeof(int), NX, f);
  51. /* close the file */
  52. fclose(f);
  53. /* And now, you want to use your datas in StarPU */
  54. /* Open the file ON the disk */
  55. void * data = starpu_disk_open(dd, (void *) "STARPU_DISK_COMPUTE_DATA", NX);
  56. starpu_data_handle_t vector_handleA, vector_handleB;
  57. /* register vector in starpu */
  58. starpu_vector_data_register(&vector_handleA, dd, (uintptr_t)A, NX, sizeof(int));
  59. /* and do what you want with it, here we copy it into an other vector */
  60. starpu_vector_data_register(&vector_handleB, STARPU_MAIN_RAM, (uintptr_t) NULL, NX, sizeof(int));
  61. starpu_data_cpy(vector_handleB, vector_handleA, 0, NULL, NULL);
  62. /* free them */
  63. starpu_data_unregister(vector_handleA);
  64. starpu_data_unregister(vector_handleB);
  65. /* close it in StarPU */
  66. starpu_disk_close(dd, data, NX*sizeof(int));
  67. /* check if it's correct */
  68. f = fopen("/tmp/STARPU_DISK_COMPUTE_DATA", "rb+");
  69. /* fail */
  70. if (f == NULL)
  71. goto enoent;
  72. int size = fread(C, sizeof(int), NX, f);
  73. int try = 1;
  74. for (j = 0; j < NX; ++j)
  75. if (A[j] != C[j])
  76. {
  77. printf("Fail A %d != C %d \n", A[j], C[j]);
  78. try = 0;
  79. }
  80. /* terminate StarPU, no task can be submitted after */
  81. starpu_shutdown();
  82. if(try)
  83. printf("TEST SUCCESS\n");
  84. else
  85. printf("TEST FAIL\n");
  86. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  87. enodev:
  88. return 77;
  89. enoent:
  90. return 77;
  91. }