noreclaim.c 3.7 KB

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