disk_copy.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. #include <math.h>
  22. #include <common/config.h>
  23. #include "../helper.h"
  24. /*
  25. * Try to write into disk memory
  26. * Use mechanism to push datas from main ram to disk ram
  27. * Here we make copies between buffers, that StarPU has to evict while
  28. * progressing because there is not enough room for all of them.
  29. */
  30. #ifdef STARPU_HAVE_WINDOWS
  31. # include <io.h>
  32. # if defined(_WIN32) && !defined(__CYGWIN__)
  33. # define mkdir(path, mode) mkdir(path)
  34. # endif
  35. #endif
  36. /* RAM is not enough to hold 6 times NX
  37. * DISK is just enough to hold 6 times NX */
  38. /* size of one vector */
  39. #ifdef STARPU_QUICK_CHECK
  40. # define RAM "1"
  41. # define DISK 2
  42. # define NX (256*1024/sizeof(double))
  43. #else
  44. # define NX (32*1048576/sizeof(double))
  45. # define RAM "160"
  46. # define DISK 200
  47. #endif
  48. #if !defined(STARPU_HAVE_SETENV)
  49. #warning setenv is not defined. Skipping test
  50. int main(int argc, char **argv)
  51. {
  52. return STARPU_TEST_SKIPPED;
  53. }
  54. #else
  55. int dotest(struct starpu_disk_ops *ops, void *param)
  56. {
  57. double *A,*B,*C,*D,*E,*F;
  58. int ret;
  59. /* limit main ram to force to push in disk */
  60. setenv("STARPU_LIMIT_CPU_MEM", RAM, 1);
  61. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  62. struct starpu_conf conf;
  63. ret = starpu_conf_init(&conf);
  64. if (ret == -EINVAL)
  65. return EXIT_FAILURE;
  66. conf.ncuda = 0;
  67. conf.nopencl = 0;
  68. conf.nmic = 0;
  69. conf.nscc = 0;
  70. ret = starpu_init(&conf);
  71. if (ret == -ENODEV) goto enodev;
  72. /* register a disk */
  73. int new_dd = starpu_disk_register(ops, param, 1024*1024*DISK);
  74. /* can't write on /tmp/ */
  75. if (new_dd == -ENOENT) goto enoent;
  76. /* allocate two memory spaces */
  77. starpu_malloc_flags((void **)&A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  78. starpu_malloc_flags((void **)&F, NX*sizeof(double), STARPU_MALLOC_COUNT);
  79. FPRINTF(stderr, "TEST DISK MEMORY \n");
  80. unsigned int j;
  81. /* initialization with bad values */
  82. for(j = 0; j < NX; ++j)
  83. {
  84. A[j] = j;
  85. F[j] = -j;
  86. }
  87. starpu_data_handle_t vector_handleA, vector_handleB, vector_handleC, vector_handleD, vector_handleE, vector_handleF;
  88. /* register vector in starpu */
  89. starpu_vector_data_register(&vector_handleA, STARPU_MAIN_RAM, (uintptr_t)A, NX, sizeof(double));
  90. starpu_vector_data_register(&vector_handleB, -1, (uintptr_t) NULL, NX, sizeof(double));
  91. starpu_vector_data_register(&vector_handleC, -1, (uintptr_t) NULL, NX, sizeof(double));
  92. starpu_vector_data_register(&vector_handleD, -1, (uintptr_t) NULL, NX, sizeof(double));
  93. starpu_vector_data_register(&vector_handleE, -1, (uintptr_t) NULL, NX, sizeof(double));
  94. starpu_vector_data_register(&vector_handleF, STARPU_MAIN_RAM, (uintptr_t)F, NX, sizeof(double));
  95. /* copy vector A->B, B->C... */
  96. starpu_data_cpy(vector_handleB, vector_handleA, 0, NULL, NULL);
  97. starpu_data_cpy(vector_handleC, vector_handleB, 0, NULL, NULL);
  98. starpu_data_cpy(vector_handleD, vector_handleC, 0, NULL, NULL);
  99. starpu_data_cpy(vector_handleE, vector_handleD, 0, NULL, NULL);
  100. starpu_data_cpy(vector_handleF, vector_handleE, 0, NULL, NULL);
  101. /* StarPU does not need to manipulate the array anymore so we can stop
  102. * monitoring it */
  103. /* free them */
  104. starpu_data_unregister(vector_handleA);
  105. starpu_data_unregister(vector_handleB);
  106. starpu_data_unregister(vector_handleC);
  107. starpu_data_unregister(vector_handleD);
  108. starpu_data_unregister(vector_handleE);
  109. starpu_data_unregister(vector_handleF);
  110. /* check if computation is correct */
  111. int try = 1;
  112. for (j = 0; j < NX; ++j)
  113. if (A[j] != F[j])
  114. {
  115. FPRINTF(stderr, "Fail A %f != F %f \n", A[j], F[j]);
  116. try = 0;
  117. }
  118. starpu_free_flags(A, NX*sizeof(double), STARPU_MALLOC_COUNT);
  119. starpu_free_flags(F, NX*sizeof(double), STARPU_MALLOC_COUNT);
  120. /* terminate StarPU, no task can be submitted after */
  121. starpu_shutdown();
  122. if(try)
  123. FPRINTF(stderr, "TEST SUCCESS\n");
  124. else
  125. FPRINTF(stderr, "TEST FAIL\n");
  126. return (try ? EXIT_SUCCESS : EXIT_FAILURE);
  127. enodev:
  128. return STARPU_TEST_SKIPPED;
  129. enoent:
  130. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  131. starpu_shutdown();
  132. return STARPU_TEST_SKIPPED;
  133. }
  134. static int merge_result(int old, int new)
  135. {
  136. if (new == EXIT_FAILURE)
  137. return EXIT_FAILURE;
  138. if (old == 0)
  139. return 0;
  140. return new;
  141. }
  142. int main(void)
  143. {
  144. int ret = 0;
  145. char s[128];
  146. snprintf(s, sizeof(s), "/tmp/%s-disk-%d", getenv("USER"), getpid());
  147. ret = mkdir(s, 0777);
  148. if (ret)
  149. {
  150. FPRINTF(stderr, "Cannot make directory <%s>\n", s);
  151. return STARPU_TEST_SKIPPED;
  152. }
  153. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s));
  154. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s));
  155. #ifdef STARPU_LINUX_SYS
  156. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s));
  157. #endif
  158. rmdir(s);
  159. return ret;
  160. }
  161. #endif