mem_reclaim.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2015-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Corentin Salingue
  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 <fcntl.h>
  18. #include <starpu.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <math.h>
  26. #include "../helper.h"
  27. /*
  28. * Try to write into disk memory
  29. * Use mechanism to push datas from main ram to disk ram
  30. * Here we stress the memory with more tasks than what the RAM can fit.
  31. */
  32. #ifdef STARPU_HAVE_MEMCHECK_H
  33. #include <valgrind/memcheck.h>
  34. #else
  35. #define VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(addr, size) (void)0
  36. #endif
  37. #ifdef STARPU_QUICK_CHECK
  38. # define NDATA 4
  39. # define NITER 8
  40. #elif !defined(STARPU_LONG_CHECK)
  41. # define NDATA 32
  42. # define NITER 128
  43. #else
  44. # define NDATA 128
  45. # define NITER 512
  46. #endif
  47. # define MEMSIZE 1
  48. # define MEMSIZE_STR "1"
  49. #if !defined(STARPU_HAVE_SETENV)
  50. #warning setenv is not defined. Skipping test
  51. int main(void)
  52. {
  53. return STARPU_TEST_SKIPPED;
  54. }
  55. #else
  56. static int (*any_to_any)(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node, void *async_data);
  57. /* We need a ram-to-ram copy for NUMA machine, use any_to_any for that */
  58. static int ram_to_ram(void *src_interface, unsigned src_node, void *dst_interface, unsigned dst_node)
  59. {
  60. return any_to_any(src_interface, src_node, dst_interface, dst_node, NULL);
  61. }
  62. const struct starpu_data_copy_methods my_vector_copy_data_methods_s =
  63. {
  64. .ram_to_ram = ram_to_ram
  65. };
  66. struct starpu_data_interface_ops starpu_interface_my_vector_ops;
  67. void starpu_my_vector_data_register(starpu_data_handle_t *handleptr, int home_node,
  68. uintptr_t ptr, uint32_t nx, size_t elemsize)
  69. {
  70. struct starpu_vector_interface vector =
  71. {
  72. .id = STARPU_VECTOR_INTERFACE_ID,
  73. .ptr = ptr,
  74. .nx = nx,
  75. .elemsize = elemsize,
  76. .dev_handle = ptr,
  77. .slice_base = 0,
  78. .offset = 0,
  79. .allocsize = nx * elemsize,
  80. };
  81. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_my_vector_ops);
  82. }
  83. static unsigned values[NDATA];
  84. static void zero(void *buffers[], void *args)
  85. {
  86. (void)args;
  87. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  88. unsigned *val = (unsigned*) STARPU_VECTOR_GET_PTR(vector);
  89. *val = 0;
  90. VALGRIND_MAKE_MEM_DEFINED_IF_ADDRESSABLE(val, STARPU_VECTOR_GET_NX(vector) * STARPU_VECTOR_GET_ELEMSIZE(vector));
  91. }
  92. static void inc(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. (*val)++;
  99. STARPU_ATOMIC_ADD(&values[i], 1);
  100. }
  101. static void check(void *buffers[], void *args)
  102. {
  103. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  104. unsigned *val = (unsigned*) STARPU_VECTOR_GET_PTR(vector);
  105. unsigned i;
  106. starpu_codelet_unpack_args(args, &i);
  107. STARPU_ASSERT_MSG(*val == values[i], "Incorrect value. Value %u should be %u (index %u)", *val, values[i], i);
  108. }
  109. static struct starpu_codelet zero_cl =
  110. {
  111. .cpu_funcs = { zero },
  112. .nbuffers = 1,
  113. .modes = { STARPU_W },
  114. };
  115. static struct starpu_codelet inc_cl =
  116. {
  117. .cpu_funcs = { inc },
  118. .nbuffers = 1,
  119. .modes = { STARPU_RW },
  120. };
  121. static struct starpu_codelet check_cl =
  122. {
  123. .cpu_funcs = { check },
  124. .nbuffers = 1,
  125. .modes = { STARPU_R },
  126. };
  127. int dotest(struct starpu_disk_ops *ops, char *base, void (*vector_data_register)(starpu_data_handle_t *handleptr, int home_node, uintptr_t ptr, uint32_t nx, size_t elemsize), const char *text)
  128. {
  129. starpu_data_handle_t handles[NDATA];
  130. if (starpu_get_env_number_default("STARPU_DIDUSE_BARRIER", 0))
  131. /* This would hang */
  132. return STARPU_TEST_SKIPPED;
  133. FPRINTF(stderr, "Testing <%s>\n", text);
  134. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  135. // Ignore environment variables as we want to force the exact number of workers
  136. struct starpu_conf conf;
  137. int ret = starpu_conf_init(&conf);
  138. if (ret == -EINVAL)
  139. return EXIT_FAILURE;
  140. conf.precedence_over_environment_variables = 1;
  141. starpu_conf_noworker(&conf);
  142. conf.ncpus = -1;
  143. conf.nmpi_ms = -1;
  144. ret = starpu_init(&conf);
  145. if (ret == -ENODEV) goto enodev;
  146. /* Initialize path and name */
  147. /* register swap disk */
  148. int new_dd = starpu_disk_register(ops, (void *) base, STARPU_DISK_SIZE_MIN);
  149. /* can't write on /tmp/ */
  150. if (new_dd == -ENOENT) goto enoent;
  151. unsigned int i, j;
  152. /* Initialize twice as much data as available memory */
  153. for (i = 0; i < NDATA; i++)
  154. {
  155. vector_data_register(&handles[i], -1, 0, (MEMSIZE*1024*1024*2) / NDATA, sizeof(char));
  156. starpu_task_insert(&zero_cl, STARPU_W, handles[i], 0);
  157. }
  158. memset(values, 0, sizeof(values));
  159. /* Work out of core */
  160. for (i = 0; i < NITER; i++)
  161. {
  162. j = rand()%NDATA;
  163. starpu_task_insert(&inc_cl, STARPU_RW, handles[j], STARPU_VALUE, &j, sizeof(j), 0);
  164. }
  165. starpu_task_wait_for_all();
  166. /* forcibly evict some data, just for fun */
  167. for (i = 0; i < NDATA; i++)
  168. {
  169. if ((rand() % 2) == 0)
  170. starpu_data_evict_from_node(handles[i], STARPU_MAIN_RAM);
  171. }
  172. /* And work out of core again */
  173. for (i = 0; i < NITER; i++)
  174. {
  175. j = rand()%NDATA;
  176. starpu_task_insert(&inc_cl, STARPU_RW, handles[j], STARPU_VALUE, &j, sizeof(j), 0);
  177. }
  178. /* Check and free data */
  179. for (i = 0; i < NDATA; i++)
  180. {
  181. starpu_task_insert(&check_cl, STARPU_R, handles[i], STARPU_VALUE, &i, sizeof(i), 0);
  182. starpu_data_unregister(handles[i]);
  183. }
  184. /* terminate StarPU, no task can be submitted after */
  185. starpu_shutdown();
  186. return EXIT_SUCCESS;
  187. enoent:
  188. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  189. starpu_shutdown();
  190. enodev:
  191. return STARPU_TEST_SKIPPED;
  192. }
  193. static int merge_result(int old, int new)
  194. {
  195. if (new == EXIT_FAILURE)
  196. return EXIT_FAILURE;
  197. if (old == 0)
  198. return 0;
  199. return new;
  200. }
  201. int main(void)
  202. {
  203. int ret = 0;
  204. int ret2;
  205. char s[128];
  206. char *ptr;
  207. #ifdef STARPU_HAVE_SETENV
  208. setenv("STARPU_CALIBRATE_MINIMUM", "1", 1);
  209. #endif
  210. snprintf(s, sizeof(s), "/tmp/%s-disk-XXXXXX", getenv("USER"));
  211. ptr = _starpu_mkdtemp(s);
  212. if (!ptr)
  213. {
  214. FPRINTF(stderr, "Cannot make directory '%s'\n", s);
  215. return STARPU_TEST_SKIPPED;
  216. }
  217. setenv("STARPU_LIMIT_CPU_MEM", MEMSIZE_STR, 1);
  218. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  219. any_to_any = starpu_interface_vector_ops.copy_methods->any_to_any;
  220. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  221. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  222. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_vector_data_register, "Stdio with read/write vector ops"));
  223. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_my_vector_data_register, "Stdio with pack/unpack vector ops"));
  224. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_vector_data_register, "unistd with read/write vector ops"));
  225. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_my_vector_data_register, "unistd with pack/unpack vector ops"));
  226. #ifdef STARPU_LINUX_SYS
  227. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_vector_data_register, "unistd_direct with read/write vector ops"));
  228. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_my_vector_data_register, "unistd_direct with pack/unpack vector ops"));
  229. #endif
  230. ret2 = rmdir(s);
  231. STARPU_CHECK_RETURN_VALUE(ret2, "rmdir '%s'\n", s);
  232. return ret;
  233. }
  234. #endif