reclaim.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  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. #ifdef STARPU_HAVE_HWLOC
  23. #include <hwloc.h>
  24. #endif
  25. #define BLOCK_SIZE (64*1024*1024)
  26. static unsigned ntasks = 1000;
  27. #ifdef STARPU_HAVE_HWLOC
  28. static uint64_t get_total_memory_size(void)
  29. {
  30. hwloc_topology_t hwtopology;
  31. hwloc_topology_init(&hwtopology);
  32. hwloc_topology_load(hwtopology);
  33. hwloc_obj_t root = hwloc_get_root_obj(hwtopology);
  34. return root->memory.total_memory;
  35. }
  36. #endif
  37. static void dummy_func(void *descr[], __attribute__ ((unused)) void *_args)
  38. {
  39. }
  40. static starpu_codelet dummy_cl = {
  41. .where = STARPU_CPU|STARPU_CUDA,
  42. .cpu_func = dummy_func,
  43. .cuda_func = dummy_func,
  44. .nbuffers = 3
  45. };
  46. /* Number of chunks */
  47. static int mb = 256;
  48. int main(int argc, char **argv)
  49. {
  50. int i;
  51. int task;
  52. #ifdef STARPU_HAVE_HWLOC
  53. /* We allocate 50% of the memory */
  54. uint64_t total_size = get_total_memory_size();
  55. mb = (int)((0.50 * total_size)/(BLOCK_SIZE));
  56. #endif
  57. /* An optional argument indicates the number of MB to allocate */
  58. if (argc > 1)
  59. mb = atoi(argv[1]);
  60. if (2*mb > ntasks)
  61. ntasks = 2*mb;
  62. fprintf(stderr, "Allocate %d buffers and create %d tasks\n", mb, ntasks);
  63. starpu_init(NULL);
  64. float **host_ptr_array;
  65. starpu_data_handle *handle_array;
  66. host_ptr_array = calloc(mb, sizeof(float *));
  67. handle_array = calloc(mb, sizeof(starpu_data_handle));
  68. /* Register mb buffers of 1MB */
  69. for (i = 0; i < mb; i++)
  70. {
  71. host_ptr_array[i] = malloc(BLOCK_SIZE);
  72. assert(host_ptr_array[i]);
  73. starpu_variable_data_register(&handle_array[i], 0,
  74. (uintptr_t)host_ptr_array[i], BLOCK_SIZE);
  75. assert(handle_array[i]);
  76. }
  77. for (task = 0; task < ntasks; task++)
  78. {
  79. struct starpu_task *task = starpu_task_create();
  80. task->cl = &dummy_cl;
  81. task->buffers[0].handle = handle_array[i%mb];
  82. task->buffers[0].mode = STARPU_RW;
  83. task->buffers[1].handle = handle_array[(i+1)%mb];
  84. task->buffers[1].mode = STARPU_R;
  85. task->buffers[2].handle = handle_array[(i+2)%mb];
  86. task->buffers[2].mode = STARPU_R;
  87. starpu_task_submit(task);
  88. }
  89. starpu_task_wait_for_all();
  90. for (i = 0; i < mb; i++)
  91. {
  92. starpu_data_unregister(handle_array[i]);
  93. free(host_ptr_array[i]);
  94. }
  95. starpu_shutdown();
  96. return 0;
  97. }