disk_copy.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #include <common/config.h>
  24. #include "../helper.h"
  25. #ifdef STARPU_HAVE_WINDOWS
  26. #if defined(_WIN32) && !defined(__CYGWIN__)
  27. #define mkdir(path, mode) mkdir(path)
  28. #endif
  29. #endif
  30. /* size of one vector */
  31. #if SIZEOF_VOID_P == 4
  32. #define NX (32*1024/sizeof(double))
  33. #else
  34. #define NX (32*1048576/sizeof(double))
  35. #endif
  36. #if !defined(STARPU_HAVE_SETENV)
  37. #warning setenv is not defined. Skipping test
  38. int main(int argc, char **argv)
  39. {
  40. return STARPU_TEST_SKIPPED;
  41. }
  42. #else
  43. int dotest(struct starpu_disk_ops *ops, void *param)
  44. {
  45. double *A,*B,*C,*D,*E,*F;
  46. int ret;
  47. /* limit main ram to force to push in disk */
  48. setenv("STARPU_LIMIT_CPU_MEM", "160", 1);
  49. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  50. struct starpu_conf conf;
  51. ret = starpu_conf_init(&conf);
  52. if (ret == -EINVAL)
  53. return EXIT_FAILURE;
  54. conf.ncuda = 0;
  55. conf.nopencl = 0;
  56. ret = starpu_init(&conf);
  57. if (ret == -ENODEV) goto enodev;
  58. /* register a disk */
  59. int new_dd = starpu_disk_register(ops, param, 1024*1024*200);
  60. /* can't write on /tmp/ */
  61. if (new_dd == -ENOENT) goto enoent;
  62. /* allocate two memory spaces */
  63. starpu_malloc_flags((void **)&A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  64. starpu_malloc_flags((void **)&F, NX*sizeof(double), STARPU_MALLOC_COUNT);
  65. FPRINTF(stderr, "TEST DISK MEMORY \n");
  66. unsigned int j;
  67. /* initialization with bad values */
  68. for(j = 0; j < NX; ++j)
  69. {
  70. A[j] = j;
  71. F[j] = -j;
  72. }
  73. starpu_data_handle_t vector_handleA, vector_handleB, vector_handleC, vector_handleD, vector_handleE, vector_handleF;
  74. /* register vector in starpu */
  75. starpu_vector_data_register(&vector_handleA, STARPU_MAIN_RAM, (uintptr_t)A, NX, sizeof(double));
  76. starpu_vector_data_register(&vector_handleB, -1, (uintptr_t) NULL, NX, sizeof(double));
  77. starpu_vector_data_register(&vector_handleC, -1, (uintptr_t) NULL, NX, sizeof(double));
  78. starpu_vector_data_register(&vector_handleD, -1, (uintptr_t) NULL, NX, sizeof(double));
  79. starpu_vector_data_register(&vector_handleE, -1, (uintptr_t) NULL, NX, sizeof(double));
  80. starpu_vector_data_register(&vector_handleF, STARPU_MAIN_RAM, (uintptr_t)F, NX, sizeof(double));
  81. /* copy vector A->B, B->C... */
  82. starpu_data_cpy(vector_handleB, vector_handleA, 0, NULL, NULL);
  83. starpu_data_cpy(vector_handleC, vector_handleB, 0, NULL, NULL);
  84. starpu_data_cpy(vector_handleD, vector_handleC, 0, NULL, NULL);
  85. starpu_data_cpy(vector_handleE, vector_handleD, 0, NULL, NULL);
  86. starpu_data_cpy(vector_handleF, vector_handleE, 0, NULL, NULL);
  87. /* StarPU does not need to manipulate the array anymore so we can stop
  88. * monitoring it */
  89. /* free them */
  90. starpu_data_unregister(vector_handleA);
  91. starpu_data_unregister(vector_handleB);
  92. starpu_data_unregister(vector_handleC);
  93. starpu_data_unregister(vector_handleD);
  94. starpu_data_unregister(vector_handleE);
  95. starpu_data_unregister(vector_handleF);
  96. /* check if computation is correct */
  97. int try = 1;
  98. for (j = 0; j < NX; ++j)
  99. if (A[j] != F[j])
  100. {
  101. FPRINTF(stderr, "Fail A %f != F %f \n", A[j], F[j]);
  102. try = 0;
  103. }
  104. starpu_free_flags(A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  105. starpu_free_flags(F, NX*sizeof(double), STARPU_MALLOC_COUNT);
  106. /* terminate StarPU, no task can be submitted after */
  107. starpu_shutdown();
  108. if(try)
  109. FPRINTF(stderr, "TEST SUCCESS\n");
  110. else
  111. FPRINTF(stderr, "TEST FAIL\n");
  112. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  113. enodev:
  114. return STARPU_TEST_SKIPPED;
  115. enoent:
  116. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  117. starpu_shutdown();
  118. return STARPU_TEST_SKIPPED;
  119. }
  120. static int merge_result(int old, int new)
  121. {
  122. if (new == EXIT_FAILURE)
  123. return EXIT_FAILURE;
  124. if (old == 0)
  125. return 0;
  126. return new;
  127. }
  128. int main(void)
  129. {
  130. int ret = 0;
  131. char s[128];
  132. snprintf(s, sizeof(s), "/tmp/%s-disk", getenv("USER"));
  133. mkdir(s, 0777);
  134. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  135. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  136. #ifdef STARPU_LINUX_SYS
  137. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  138. #endif
  139. return ret;
  140. }
  141. #endif