noreclaim.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2015 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. /*
  17. * This test stress the memory allocation system and should force StarPU to
  18. * reclaim memory from time to time.
  19. */
  20. #include <assert.h>
  21. #include <starpu.h>
  22. #include "../helper.h"
  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. starpu_variable_data_register(&handle, -1, 0, FILL);
  82. /* This makes the data allocated */
  83. emit_task(handle);
  84. ret = starpu_task_wait_for_all();
  85. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  86. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  87. /* Room should be busy due to data */
  88. STARPU_ASSERT(ret == -ENOMEM);
  89. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT|STARPU_MALLOC_NORECLAIM);
  90. /* But we should be able to tell we don't care */
  91. STARPU_ASSERT(ret == 0);
  92. ((char*)allocated)[FILL-1] = 0;
  93. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  94. /* Release the automatically allocated data */
  95. starpu_data_unregister(handle);
  96. /* Memory may not be available immediately, make sure the driver has
  97. * the opportunity to release it */
  98. emit_empty_task();
  99. ret = starpu_task_wait_for_all();
  100. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  101. emit_empty_task();
  102. ret = starpu_task_wait_for_all();
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  104. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  105. /* Room should now be available */
  106. STARPU_ASSERT(ret == 0);
  107. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  108. starpu_shutdown();
  109. return EXIT_SUCCESS;
  110. enodev:
  111. fprintf(stderr, "WARNING: No one can execute this task\n");
  112. /* yes, we do not perform the computation but we did detect that no one
  113. * could perform the kernel, so this is not an error from StarPU */
  114. starpu_shutdown();
  115. return STARPU_TEST_SKIPPED;
  116. }
  117. #endif