mem_reclaim.c 5.1 KB

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