disk_copy.c 4.8 KB

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