mem_reclaim.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. /* Try to write into disk memory
  18. * Use mechanism to push datas from main ram to disk ram
  19. */
  20. #include <fcntl.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <unistd.h>
  28. #include <math.h>
  29. #include <common/config.h>
  30. #include "../helper.h"
  31. #ifdef STARPU_HAVE_MEMCHECK_H
  32. #include <valgrind/memcheck.h>
  33. #else
  34. #define VALGRIND_MAKE_MEM_DEFINED(addr, size) (void)0
  35. #endif
  36. #ifdef STARPU_HAVE_WINDOWS
  37. # include <io.h>
  38. # if defined(_WIN32) && !defined(__CYGWIN__)
  39. # define mkdir(path, mode) mkdir(path)
  40. # endif
  41. #endif
  42. #ifdef STARPU_QUICK_CHECK
  43. # define NDATA 4
  44. # define NITER 16
  45. #else
  46. # define NDATA 128
  47. # define NITER 1024
  48. #endif
  49. # define MEMSIZE 1
  50. # define MEMSIZE_STR "1"
  51. #if !defined(STARPU_HAVE_SETENV)
  52. #warning setenv is not defined. Skipping test
  53. int main(int argc, char **argv)
  54. {
  55. return STARPU_TEST_SKIPPED;
  56. }
  57. #else
  58. const struct starpu_data_copy_methods my_vector_copy_data_methods_s;
  59. struct starpu_data_interface_ops starpu_interface_my_vector_ops;
  60. void starpu_my_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  61. uintptr_t ptr, uint32_t nx, size_t elemsize)
  62. {
  63. struct starpu_vector_interface vector =
  64. {
  65. .id = STARPU_VECTOR_INTERFACE_ID,
  66. .ptr = ptr,
  67. .nx = nx,
  68. .elemsize = elemsize,
  69. .dev_handle = ptr,
  70. .slice_base = 0,
  71. .offset = 0
  72. };
  73. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_my_vector_ops);
  74. }
  75. static unsigned values[NDATA];
  76. static void zero(void *buffers[], void *args)
  77. {
  78. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  79. unsigned *val = (unsigned*) STARPU_VECTOR_GET_PTR(vector);
  80. *val = 0;
  81. VALGRIND_MAKE_MEM_DEFINED(val, STARPU_VECTOR_GET_NX(vector) * STARPU_VECTOR_GET_ELEMSIZE(vector));
  82. }
  83. static void inc(void *buffers[], void *args)
  84. {
  85. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  86. unsigned *val = (unsigned*) STARPU_VECTOR_GET_PTR(vector);
  87. unsigned i;
  88. starpu_codelet_unpack_args(args, &i);
  89. (*val)++;
  90. STARPU_ATOMIC_ADD(&values[i], 1);
  91. }
  92. static void check(void *buffers[], void *args)
  93. {
  94. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  95. unsigned *val = (unsigned*) STARPU_VECTOR_GET_PTR(vector);
  96. unsigned i;
  97. starpu_codelet_unpack_args(args, &i);
  98. STARPU_ASSERT(*val == values[i]);
  99. }
  100. static struct starpu_codelet zero_cl =
  101. {
  102. .cpu_funcs = { zero },
  103. .nbuffers = 1,
  104. .modes = { STARPU_W },
  105. };
  106. static struct starpu_codelet inc_cl =
  107. {
  108. .cpu_funcs = { inc },
  109. .nbuffers = 1,
  110. .modes = { STARPU_RW },
  111. };
  112. static struct starpu_codelet check_cl =
  113. {
  114. .cpu_funcs = { check },
  115. .nbuffers = 1,
  116. .modes = { STARPU_R },
  117. };
  118. int dotest(struct starpu_disk_ops *ops, char *base, void (*vector_data_register)(starpu_data_handle_t *handleptr, unsigned home_node, uintptr_t ptr, uint32_t nx, size_t elemsize))
  119. {
  120. int *A, *C;
  121. starpu_data_handle_t handles[NDATA];
  122. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  123. struct starpu_conf conf;
  124. int ret = starpu_conf_init(&conf);
  125. if (ret == -EINVAL)
  126. return EXIT_FAILURE;
  127. conf.ncuda = 0;
  128. conf.nopencl = 0;
  129. ret = starpu_init(&conf);
  130. if (ret == -ENODEV) goto enodev;
  131. /* Initialize path and name */
  132. /* register swap disk */
  133. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*16);
  134. /* can't write on /tmp/ */
  135. if (new_dd == -ENOENT) goto enoent;
  136. unsigned int i, j;
  137. /* Initialize twice as much data as available memory */
  138. for (i = 0; i < NDATA; i++)
  139. {
  140. vector_data_register(&handles[i], -1, 0, (MEMSIZE*1024*1024*2) / NDATA, sizeof(char));
  141. starpu_task_insert(&zero_cl, STARPU_W, handles[i], 0);
  142. }
  143. memset(values, 0, sizeof(values));
  144. for (i = 0; i < NITER; i++)
  145. {
  146. j = rand()%NDATA;
  147. starpu_task_insert(&inc_cl, STARPU_RW, handles[j], STARPU_VALUE, &j, sizeof(j), 0);
  148. }
  149. /* Check and free data */
  150. for (i = 0; i < NDATA; i++)
  151. {
  152. starpu_task_insert(&check_cl, STARPU_R, handles[i], STARPU_VALUE, &i, sizeof(i), 0);
  153. starpu_data_unregister(handles[i]);
  154. }
  155. /* terminate StarPU, no task can be submitted after */
  156. starpu_shutdown();
  157. return EXIT_SUCCESS;
  158. enoent:
  159. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  160. starpu_shutdown();
  161. enodev:
  162. return STARPU_TEST_SKIPPED;
  163. }
  164. static int merge_result(int old, int new)
  165. {
  166. if (new == EXIT_FAILURE)
  167. return EXIT_FAILURE;
  168. if (old == 0)
  169. return 0;
  170. return new;
  171. }
  172. int main(void)
  173. {
  174. int ret = 0;
  175. char s[128];
  176. snprintf(s, sizeof(s), "/tmp/%s-disk-%d", getenv("USER"), getpid());
  177. mkdir(s, 0777);
  178. setenv("STARPU_LIMIT_CPU_MEM", MEMSIZE_STR, 1);
  179. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  180. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  181. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  182. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_vector_data_register));
  183. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_my_vector_data_register));
  184. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_vector_data_register));
  185. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_my_vector_data_register));
  186. #ifdef STARPU_LINUX_SYS
  187. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_vector_data_register));
  188. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_my_vector_data_register));
  189. #endif
  190. rmdir(s);
  191. return ret;
  192. }
  193. #endif