reclaim.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  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. #ifdef STARPU_HAVE_HWLOC
  19. #include <hwloc.h>
  20. #endif
  21. #include "../helper.h"
  22. #if !defined(STARPU_HAVE_SETENV)
  23. #warning setenv is not defined. Skipping test
  24. int main(void)
  25. {
  26. return STARPU_TEST_SKIPPED;
  27. }
  28. #else
  29. /*
  30. * Stress the memory allocation system and force StarPU to reclaim memory from
  31. * time to time.
  32. */
  33. #ifdef STARPU_QUICK_CHECK
  34. # define BLOCK_SIZE (64*1024)
  35. static unsigned ntasks = 250;
  36. #else
  37. # define BLOCK_SIZE (64*1024*1024)
  38. static unsigned ntasks = 1000;
  39. #endif
  40. #ifdef STARPU_HAVE_HWLOC
  41. static uint64_t get_total_memory_size(void)
  42. {
  43. uint64_t size;
  44. hwloc_topology_t hwtopology;
  45. hwloc_topology_init(&hwtopology);
  46. hwloc_topology_load(hwtopology);
  47. hwloc_obj_t root = hwloc_get_root_obj(hwtopology);
  48. #if HWLOC_API_VERSION >= 0x00020000
  49. size = root->total_memory;
  50. #else
  51. size = root->memory.total_memory;
  52. #endif
  53. hwloc_topology_destroy(hwtopology);
  54. return size;
  55. }
  56. #endif
  57. void dummy_func(void *descr[], void *_args)
  58. {
  59. }
  60. static unsigned int i = 0;
  61. void f(void *arg)
  62. {
  63. printf("%u\n", ++i);
  64. }
  65. static struct starpu_codelet dummy_cl =
  66. {
  67. .cpu_funcs = {dummy_func},
  68. .cuda_funcs = {dummy_func},
  69. .opencl_funcs = {dummy_func},
  70. .cpu_funcs_name = {"dummy_func"},
  71. .nbuffers = 3,
  72. .modes = {STARPU_RW, STARPU_R, STARPU_R}
  73. };
  74. /* Number of chunks */
  75. static unsigned mb = 16;
  76. int main(int argc, char **argv)
  77. {
  78. unsigned j, taskid;
  79. int ret;
  80. #ifdef STARPU_HAVE_HWLOC
  81. /* We allocate 50% of the memory */
  82. uint64_t total_size = get_total_memory_size();
  83. /* On x86_64-freebsd8.2, hwloc 1.3 returns 0 as the total memory
  84. * size, so sanity-check what we have. */
  85. if (total_size > 0)
  86. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  87. #endif
  88. setenv("STARPU_LIMIT_OPENCL_MEM", "1000", 1);
  89. ret = starpu_initialize(NULL, &argc, &argv);
  90. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  91. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  92. /* An optional argument indicates the number of MB to allocate */
  93. if (argc > 1)
  94. mb = atoi(argv[1]);
  95. if (2*mb > ntasks)
  96. ntasks = 2*mb;
  97. #ifdef STARPU_QUICK_CHECK
  98. mb /= 100;
  99. if (mb == 0)
  100. mb = 1;
  101. #endif
  102. FPRINTF(stderr, "Allocate %u buffers of size %d and create %u tasks\n", mb, BLOCK_SIZE, ntasks);
  103. float **host_ptr_array;
  104. starpu_data_handle_t *handle_array;
  105. host_ptr_array = calloc(mb, sizeof(float *));
  106. STARPU_ASSERT(host_ptr_array);
  107. handle_array = calloc(mb, sizeof(starpu_data_handle_t));
  108. STARPU_ASSERT(handle_array);
  109. /* Register mb buffers of 1MB */
  110. for (j = 0; j < mb; j++)
  111. {
  112. size_t size = starpu_lrand48()%BLOCK_SIZE + 1;
  113. host_ptr_array[j] = calloc(size, 1);
  114. if (host_ptr_array[j] == NULL)
  115. {
  116. mb = j;
  117. FPRINTF(stderr, "Cannot allocate more than %u buffers\n", mb);
  118. break;
  119. }
  120. starpu_variable_data_register(&handle_array[j], STARPU_MAIN_RAM, (uintptr_t)host_ptr_array[j], size);
  121. STARPU_ASSERT(handle_array[j]);
  122. }
  123. for (taskid = 0; taskid < ntasks; taskid++)
  124. {
  125. struct starpu_task *task = starpu_task_create();
  126. task->cl = &dummy_cl;
  127. task->handles[0] = handle_array[taskid%mb];
  128. task->handles[1] = handle_array[(taskid+1)%mb];
  129. task->handles[2] = handle_array[(taskid+2)%mb];
  130. task->callback_func = f;
  131. task->callback_arg = NULL;
  132. ret = starpu_task_submit(task);
  133. if (ret == -ENODEV) goto enodev;
  134. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  135. }
  136. for (j = 0; j < mb; j++)
  137. {
  138. if ( j%20 == 0 )
  139. starpu_data_unregister_submit(handle_array[j]);
  140. }
  141. ret = starpu_task_wait_for_all();
  142. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  143. for (j = 0; j < mb; j++)
  144. {
  145. if ( j%20 != 0 )
  146. starpu_data_unregister(handle_array[j]);
  147. free(host_ptr_array[j]);
  148. }
  149. free(host_ptr_array);
  150. free(handle_array);
  151. starpu_shutdown();
  152. return EXIT_SUCCESS;
  153. enodev:
  154. fprintf(stderr, "WARNING: No one can execute this task\n");
  155. /* yes, we do not perform the computation but we did detect that no one
  156. * could perform the kernel, so this is not an error from StarPU */
  157. starpu_shutdown();
  158. return STARPU_TEST_SKIPPED;
  159. }
  160. #endif