noreclaim.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 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. void dummy_func(void *descr[], STARPU_ATTRIBUTE_UNUSED void *_args)
  24. {
  25. }
  26. static struct starpu_codelet dummy_cl =
  27. {
  28. .cpu_funcs = {dummy_func},
  29. .nbuffers = 1,
  30. .modes = {STARPU_W}
  31. };
  32. static void emit_task(starpu_data_handle_t handle)
  33. {
  34. struct starpu_task *task = starpu_task_create();
  35. int ret;
  36. task->cl = &dummy_cl;
  37. task->handles[0] = handle;
  38. ret = starpu_task_submit(task);
  39. STARPU_ASSERT(ret == 0);
  40. }
  41. static struct starpu_codelet empty_cl =
  42. {
  43. .cpu_funcs = {dummy_func},
  44. .nbuffers = 0,
  45. };
  46. static void emit_empty_task(void)
  47. {
  48. struct starpu_task *task = starpu_task_create();
  49. int ret;
  50. task->cl = &empty_cl;
  51. ret = starpu_task_submit(task);
  52. STARPU_ASSERT(ret == 0);
  53. }
  54. #define TOTAL "100"
  55. #define FILL (99*1024*1024)
  56. int main(int argc, char **argv)
  57. {
  58. int i, ret;
  59. struct starpu_conf conf;
  60. starpu_data_handle_t handle;
  61. void *allocated;
  62. setenv("STARPU_LIMIT_CPU_MEM", TOTAL, 1);
  63. starpu_conf_init(&conf);
  64. conf.ncpus = 1;
  65. conf.ncuda = 0;
  66. conf.nopencl = 0;
  67. conf.nmic = 0;
  68. conf.nscc = 0;
  69. ret = starpu_initialize(&conf, &argc, &argv);
  70. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  71. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  72. starpu_variable_data_register(&handle, -1, 0, FILL);
  73. /* This makes the data allocated */
  74. emit_task(handle);
  75. ret = starpu_task_wait_for_all();
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  77. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  78. /* Room should be busy due to data */
  79. STARPU_ASSERT(ret == -ENOMEM);
  80. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT|STARPU_MALLOC_NORECLAIM);
  81. /* But we should be able to tell we don't care */
  82. STARPU_ASSERT(ret == 0);
  83. ((char*)allocated)[FILL-1] = 0;
  84. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  85. /* Release the automatically allocated data */
  86. starpu_data_unregister(handle);
  87. /* Memory may not be available immediately, make sure the driver has
  88. * the opportunity to release it */
  89. emit_empty_task();
  90. ret = starpu_task_wait_for_all();
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  92. emit_empty_task();
  93. ret = starpu_task_wait_for_all();
  94. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  95. ret = starpu_malloc_flags(&allocated, FILL, STARPU_MALLOC_COUNT);
  96. /* Room should now be available */
  97. STARPU_ASSERT(ret == 0);
  98. starpu_free_flags(allocated, FILL, STARPU_MALLOC_COUNT);
  99. starpu_shutdown();
  100. return EXIT_SUCCESS;
  101. enodev:
  102. fprintf(stderr, "WARNING: No one can execute this task\n");
  103. /* yes, we do not perform the computation but we did detect that no one
  104. * could perform the kernel, so this is not an error from StarPU */
  105. starpu_shutdown();
  106. return STARPU_TEST_SKIPPED;
  107. }