mem_reclaim.c 7.8 KB

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