reclaim.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2012 Université de Bordeaux 1
  4. * Copyright (C) 2012 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. /*
  18. * This test stress the memory allocation system and should force StarPU to
  19. * reclaim memory from time to time.
  20. */
  21. #include <assert.h>
  22. #include <starpu.h>
  23. #include <common/config.h>
  24. #ifdef STARPU_HAVE_HWLOC
  25. #include <hwloc.h>
  26. #endif
  27. #include "../helper.h"
  28. #ifdef STARPU_SLOW_MACHINE
  29. # define BLOCK_SIZE (64*1024)
  30. static unsigned ntasks = 250;
  31. #else
  32. # define BLOCK_SIZE (64*1024*1024)
  33. static unsigned ntasks = 1000;
  34. #endif
  35. #ifdef STARPU_HAVE_HWLOC
  36. static uint64_t get_total_memory_size(void)
  37. {
  38. hwloc_topology_t hwtopology;
  39. hwloc_topology_init(&hwtopology);
  40. hwloc_topology_load(hwtopology);
  41. hwloc_obj_t root = hwloc_get_root_obj(hwtopology);
  42. return root->memory.total_memory;
  43. }
  44. #endif
  45. static void dummy_func(void *descr[], __attribute__ ((unused)) void *_args)
  46. {
  47. }
  48. static struct starpu_codelet dummy_cl =
  49. {
  50. .cpu_funcs = {dummy_func, NULL},
  51. .cuda_funcs = {dummy_func, NULL},
  52. .opencl_funcs = {dummy_func, NULL},
  53. .nbuffers = 3,
  54. .modes = {STARPU_RW, STARPU_R, STARPU_R}
  55. };
  56. /* Number of chunks */
  57. static int mb = 16;
  58. int main(int argc, char **argv)
  59. {
  60. int i, ret;
  61. int taskid;
  62. #ifdef STARPU_HAVE_HWLOC
  63. /* We allocate 50% of the memory */
  64. uint64_t total_size = get_total_memory_size();
  65. /* On x86_64-freebsd8.2, hwloc 1.3 returns 0 as the total memory
  66. * size, so sanity-check what we have. */
  67. if (total_size > 0)
  68. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  69. #endif
  70. /* An optional argument indicates the number of MB to allocate */
  71. if (argc > 1)
  72. mb = atoi(argv[1]);
  73. if (2*mb > ntasks)
  74. ntasks = 2*mb;
  75. #ifdef STARPU_SLOW_MACHINE
  76. mb /= 100;
  77. if (mb == 0)
  78. mb = 1;
  79. ntasks /= 100;
  80. #endif
  81. FPRINTF(stderr, "Allocate %d buffers and create %u tasks\n", mb, ntasks);
  82. ret = starpu_init(NULL);
  83. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  84. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  85. float **host_ptr_array;
  86. starpu_data_handle_t *handle_array;
  87. host_ptr_array = (float **) calloc(mb, sizeof(float *));
  88. handle_array = (starpu_data_handle_t *) calloc(mb, sizeof(starpu_data_handle_t));
  89. /* Register mb buffers of 1MB */
  90. for (i = 0; i < mb; i++)
  91. {
  92. host_ptr_array[i] = (float *) malloc(BLOCK_SIZE);
  93. if (host_ptr_array[i] == NULL)
  94. {
  95. mb = i;
  96. FPRINTF(stderr, "Cannot allocate more than %d buffers\n", mb);
  97. break;
  98. }
  99. starpu_variable_data_register(&handle_array[i], 0, (uintptr_t)host_ptr_array[i], BLOCK_SIZE);
  100. STARPU_ASSERT(handle_array[i]);
  101. }
  102. for (taskid = 0; taskid < ntasks; taskid++)
  103. {
  104. struct starpu_task *task = starpu_task_create();
  105. task->cl = &dummy_cl;
  106. task->handles[0] = handle_array[taskid%mb];
  107. task->handles[1] = handle_array[(taskid+1)%mb];
  108. task->handles[2] = handle_array[(taskid+2)%mb];
  109. ret = starpu_task_submit(task);
  110. if (ret == -ENODEV) goto enodev;
  111. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  112. }
  113. ret = starpu_task_wait_for_all();
  114. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  115. for (i = 0; i < mb; i++)
  116. {
  117. starpu_data_unregister(handle_array[i]);
  118. free(host_ptr_array[i]);
  119. }
  120. free(host_ptr_array);
  121. free(handle_array);
  122. starpu_shutdown();
  123. return EXIT_SUCCESS;
  124. enodev:
  125. fprintf(stderr, "WARNING: No one can execute this task\n");
  126. /* yes, we do not perform the computation but we did detect that no one
  127. * could perform the kernel, so this is not an error from StarPU */
  128. starpu_shutdown();
  129. return STARPU_TEST_SKIPPED;
  130. }