reclaim.c 3.0 KB

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