noreclaim.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2016 Université de Bordeaux
  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(int argc, char **argv)
  26. {
  27. return STARPU_TEST_SKIPPED;
  28. }
  29. #else
  30. void dummy_func(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  31. {
  32. }
  33. static struct starpu_codelet dummy_cl =
  34. {
  35. .cpu_funcs = {dummy_func},
  36. .cpu_funcs_name = {"dummy_func"},
  37. .nbuffers = 1,
  38. .modes = {STARPU_W}
  39. };
  40. static void emit_task(starpu_data_handle_t handle)
  41. {
  42. struct starpu_task *task = starpu_task_create();
  43. int ret;
  44. task->cl = &dummy_cl;
  45. task->handles[0] = handle;
  46. ret = starpu_task_submit(task);
  47. STARPU_ASSERT(ret == 0);
  48. }
  49. static struct starpu_codelet empty_cl =
  50. {
  51. .cpu_funcs = {dummy_func},
  52. .cpu_funcs_name = {"dummy_func"},
  53. .nbuffers = 0,
  54. };
  55. static void emit_empty_task(void)
  56. {
  57. struct starpu_task *task = starpu_task_create();
  58. int ret;
  59. task->cl = &empty_cl;
  60. ret = starpu_task_submit(task);
  61. STARPU_ASSERT(ret == 0);
  62. }
  63. #define TOTAL "100"
  64. #define FILL (99*1024*1024)
  65. int main(int argc, char **argv)
  66. {
  67. int i, ret;
  68. struct starpu_conf conf;
  69. starpu_data_handle_t handle;
  70. void *allocated;
  71. setenv("STARPU_LIMIT_CPU_MEM", TOTAL, 1);
  72. starpu_conf_init(&conf);
  73. conf.ncpus = 1;
  74. conf.ncuda = 0;
  75. conf.nopencl = 0;
  76. conf.nmic = 0;
  77. conf.nscc = 0;
  78. ret = starpu_initialize(&conf, &argc, &argv);
  79. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. if (starpu_cpu_worker_get_count() == 0)
  82. {
  83. FPRINTF(stderr, "We need at least 1 CPU worker.\n");
  84. starpu_shutdown();
  85. return STARPU_TEST_SKIPPED;
  86. }
  87. starpu_variable_data_register(&handle, -1, 0, FILL);
  88. /* This makes the data allocated */
  89. emit_task(handle);
  90. ret = starpu_task_wait_for_all();
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  92. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  93. /* Room should be busy due to data */
  94. STARPU_ASSERT(ret == -ENOMEM);
  95. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT|STARPU_MALLOC_NORECLAIM);
  96. /* But we should be able to tell we don't care */
  97. STARPU_ASSERT(ret == 0);
  98. ((char*)allocated)[FILL-1] = 0;
  99. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  100. /* Release the automatically allocated data */
  101. starpu_data_unregister(handle);
  102. /* Memory may not be available immediately, make sure the driver has
  103. * the opportunity to release it */
  104. emit_empty_task();
  105. ret = starpu_task_wait_for_all();
  106. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  107. emit_empty_task();
  108. ret = starpu_task_wait_for_all();
  109. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  110. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  111. /* Room should now be available */
  112. STARPU_ASSERT(ret == 0);
  113. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  114. starpu_shutdown();
  115. return EXIT_SUCCESS;
  116. }
  117. #endif