noreclaim.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * StarPU is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <assert.h>
  17. #include <starpu.h>
  18. #include "../helper.h"
  19. /*
  20. * Stress the memory allocation system and force StarPU to reclaim memory from
  21. * time to time.
  22. */
  23. #if !defined(STARPU_HAVE_SETENV)
  24. #warning setenv is not defined. Skipping test
  25. int main(void)
  26. {
  27. return STARPU_TEST_SKIPPED;
  28. }
  29. #else
  30. void dummy_func(void *descr[], void *_args)
  31. {
  32. (void)descr;
  33. (void)_args;
  34. }
  35. static struct starpu_codelet dummy_cl =
  36. {
  37. .cpu_funcs = {dummy_func},
  38. .cpu_funcs_name = {"dummy_func"},
  39. .nbuffers = 1,
  40. .modes = {STARPU_W}
  41. };
  42. static void emit_task(starpu_data_handle_t handle)
  43. {
  44. struct starpu_task *task = starpu_task_create();
  45. int ret;
  46. task->cl = &dummy_cl;
  47. task->handles[0] = handle;
  48. ret = starpu_task_submit(task);
  49. STARPU_ASSERT(ret == 0);
  50. }
  51. static struct starpu_codelet empty_cl =
  52. {
  53. .cpu_funcs = {dummy_func},
  54. .cpu_funcs_name = {"dummy_func"},
  55. .nbuffers = 0,
  56. };
  57. static void emit_empty_task(void)
  58. {
  59. struct starpu_task *task = starpu_task_create();
  60. int ret;
  61. task->cl = &empty_cl;
  62. ret = starpu_task_submit(task);
  63. STARPU_ASSERT(ret == 0);
  64. }
  65. #define TOTAL "100"
  66. #define FILL (99*1024*1024)
  67. int main(int argc, char **argv)
  68. {
  69. int ret;
  70. struct starpu_conf conf;
  71. starpu_data_handle_t handle;
  72. void *allocated;
  73. setenv("STARPU_LIMIT_CPU_NUMA_MEM", TOTAL, 1);
  74. starpu_conf_init(&conf);
  75. conf.ncpus = 1;
  76. conf.ncuda = 0;
  77. conf.nopencl = 0;
  78. conf.nmic = 0;
  79. conf.nmpi_ms = 0;
  80. ret = starpu_initialize(&conf, &argc, &argv);
  81. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  83. if (starpu_cpu_worker_get_count() == 0)
  84. {
  85. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  86. starpu_shutdown();
  87. return STARPU_TEST_SKIPPED;
  88. }
  89. starpu_variable_data_register(&handle, -1, 0, FILL);
  90. /* This makes the data allocated */
  91. emit_task(handle);
  92. ret = starpu_task_wait_for_all();
  93. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  94. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  95. /* Room should be busy due to data */
  96. STARPU_ASSERT(ret == -ENOMEM);
  97. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT|STARPU_MALLOC_NORECLAIM);
  98. /* But we should be able to tell we don't care */
  99. STARPU_ASSERT(ret == 0);
  100. ((char*)allocated)[FILL-1] = 0;
  101. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  102. /* Release the automatically allocated data */
  103. starpu_data_unregister(handle);
  104. /* Memory may not be available immediately, make sure the driver has
  105. * the opportunity to release it */
  106. emit_empty_task();
  107. ret = starpu_task_wait_for_all();
  108. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  109. emit_empty_task();
  110. ret = starpu_task_wait_for_all();
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  112. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  113. /* Room should now be available */
  114. STARPU_ASSERT(ret == 0);
  115. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  116. starpu_shutdown();
  117. return EXIT_SUCCESS;
  118. }
  119. #endif