mem_reclaim.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013 Corentin Salingue
  4. * Copyright (C) 2015 Centre National de la Recherche Scientifique
  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_WINDOWS
  32. # include <io.h>
  33. # if defined(_WIN32) && !defined(__CYGWIN__)
  34. # define mkdir(path, mode) mkdir(path)
  35. # endif
  36. #endif
  37. #ifdef STARPU_QUICK_CHECK
  38. # define NDATA 4
  39. # define NITER 16
  40. #else
  41. # define NDATA 128
  42. # define NITER 1024
  43. #endif
  44. # define MEMSIZE 1
  45. # define MEMSIZE_STR "1"
  46. #if !defined(STARPU_HAVE_SETENV)
  47. #warning setenv is not defined. Skipping test
  48. int main(int argc, char **argv)
  49. {
  50. return STARPU_TEST_SKIPPED;
  51. }
  52. #else
  53. const struct starpu_data_copy_methods my_vector_copy_data_methods_s;
  54. struct starpu_data_interface_ops starpu_interface_my_vector_ops;
  55. void starpu_my_vector_data_register(starpu_data_handle_t *handleptr, unsigned home_node,
  56. uintptr_t ptr, uint32_t nx, size_t elemsize)
  57. {
  58. struct starpu_vector_interface vector =
  59. {
  60. .id = STARPU_VECTOR_INTERFACE_ID,
  61. .ptr = ptr,
  62. .nx = nx,
  63. .elemsize = elemsize,
  64. .dev_handle = ptr,
  65. .slice_base = 0,
  66. .offset = 0
  67. };
  68. starpu_data_register(handleptr, home_node, &vector, &starpu_interface_my_vector_ops);
  69. }
  70. static void zero(void *buffers[], void *args)
  71. {
  72. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  73. char *val = (char*) STARPU_VECTOR_GET_PTR(vector);
  74. *val = 0;
  75. }
  76. static void inc(void *buffers[], void *args)
  77. {
  78. struct starpu_vector_interface *vector = (struct starpu_vector_interface *) buffers[0];
  79. char *val = (char*) STARPU_VECTOR_GET_PTR(vector);
  80. *val++;
  81. }
  82. static struct starpu_codelet zero_cl =
  83. {
  84. .cpu_funcs = { zero },
  85. .nbuffers = 1,
  86. .modes = { STARPU_W },
  87. };
  88. static struct starpu_codelet inc_cl =
  89. {
  90. .cpu_funcs = { inc },
  91. .nbuffers = 1,
  92. .modes = { STARPU_RW },
  93. };
  94. 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))
  95. {
  96. int *A, *C;
  97. starpu_data_handle_t handles[NDATA];
  98. /* Initialize StarPU without GPU devices to make sure the memory of the GPU devices will not be used */
  99. struct starpu_conf conf;
  100. int ret = starpu_conf_init(&conf);
  101. if (ret == -EINVAL)
  102. return EXIT_FAILURE;
  103. conf.ncuda = 0;
  104. conf.nopencl = 0;
  105. ret = starpu_init(&conf);
  106. if (ret == -ENODEV) goto enodev;
  107. /* Initialize path and name */
  108. /* register swap disk */
  109. int new_dd = starpu_disk_register(ops, (void *) base, 1024*1024*16);
  110. /* can't write on /tmp/ */
  111. if (new_dd == -ENOENT) goto enoent;
  112. unsigned int i;
  113. /* Initialize twice as much data as available memory */
  114. for (i = 0; i < NDATA; i++)
  115. {
  116. vector_data_register(&handles[i], -1, 0, (MEMSIZE*1024*1024*2) / NDATA, sizeof(char));
  117. starpu_task_insert(&zero_cl, STARPU_W, handles[i], 0);
  118. }
  119. for (i = 0; i < NITER; i++)
  120. starpu_task_insert(&inc_cl, STARPU_RW, handles[rand()%NDATA], 0);
  121. /* Free data */
  122. for (i = 0; i < NDATA; i++)
  123. starpu_data_unregister(handles[i]);
  124. /* terminate StarPU, no task can be submitted after */
  125. starpu_shutdown();
  126. return EXIT_SUCCESS;
  127. enoent:
  128. FPRINTF(stderr, "Couldn't write data: ENOENT\n");
  129. starpu_shutdown();
  130. enodev:
  131. return STARPU_TEST_SKIPPED;
  132. }
  133. static int merge_result(int old, int new)
  134. {
  135. if (new == EXIT_FAILURE)
  136. return EXIT_FAILURE;
  137. if (old == 0)
  138. return 0;
  139. return new;
  140. }
  141. int main(void)
  142. {
  143. int ret = 0;
  144. char s[128];
  145. snprintf(s, sizeof(s), "/tmp/%s-disk-%d", getenv("USER"), getpid());
  146. mkdir(s, 0777);
  147. setenv("STARPU_LIMIT_CPU_MEM", MEMSIZE_STR, 1);
  148. /* Build an vector-like interface which doesn't have the any_to_any helper, to force making use of pack/unpack */
  149. memcpy(&starpu_interface_my_vector_ops, &starpu_interface_vector_ops, sizeof(starpu_interface_my_vector_ops));
  150. starpu_interface_my_vector_ops.copy_methods = &my_vector_copy_data_methods_s;
  151. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_vector_data_register));
  152. ret = merge_result(ret, dotest(&starpu_disk_stdio_ops, s, starpu_my_vector_data_register));
  153. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_vector_data_register));
  154. ret = merge_result(ret, dotest(&starpu_disk_unistd_ops, s, starpu_my_vector_data_register));
  155. #ifdef STARPU_LINUX_SYS
  156. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_vector_data_register));
  157. ret = merge_result(ret, dotest(&starpu_disk_unistd_o_direct_ops, s, starpu_my_vector_data_register));
  158. #endif
  159. rmdir(s);
  160. return ret;
  161. }
  162. #endif