mem_reclaim.c 6.9 KB

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