reclaim.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (see AUTHORS file)
  4. *
  5. * This program 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. * This program 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. static unsigned ntasks = 10000;
  23. static void dummy_func(void *descr[], __attribute__ ((unused)) void *_args)
  24. {
  25. }
  26. static starpu_codelet dummy_cl = {
  27. .where = STARPU_CPU|STARPU_CUDA,
  28. .cpu_func = dummy_func,
  29. .cuda_func = dummy_func,
  30. .nbuffers = 3
  31. };
  32. /* Number of 1MB chunks */
  33. static int mb = 256;
  34. int main(int argc, char **argv)
  35. {
  36. int i;
  37. int task;
  38. /* An optional argument indicates the number of MB to allocate */
  39. if (argc > 1)
  40. mb = atoi(argv[1]);
  41. if (2*mb > ntasks)
  42. ntasks = 2*mb;
  43. fprintf(stderr, "Allocate %d buffers and create %d tasks\n", mb, ntasks);
  44. starpu_init(NULL);
  45. float **host_ptr_array;
  46. starpu_data_handle *handle_array;
  47. host_ptr_array = calloc(mb, sizeof(float *));
  48. handle_array = calloc(mb, sizeof(starpu_data_handle));
  49. /* Register mb buffers of 1MB */
  50. for (i = 0; i < mb; i++)
  51. {
  52. host_ptr_array[i] = malloc(1024*1024);
  53. assert(host_ptr_array[i]);
  54. starpu_variable_data_register(&handle_array[i], 0,
  55. (uintptr_t)host_ptr_array[i], 1024*1024);
  56. assert(handle_array[i]);
  57. }
  58. for (task = 0; task < ntasks; task++)
  59. {
  60. struct starpu_task *task = starpu_task_create();
  61. task->cl = &dummy_cl;
  62. task->buffers[0].handle = handle_array[i%mb];
  63. task->buffers[0].mode = STARPU_RW;
  64. task->buffers[1].handle = handle_array[(i+1)%mb];
  65. task->buffers[1].mode = STARPU_R;
  66. task->buffers[2].handle = handle_array[(i+2)%mb];
  67. task->buffers[2].mode = STARPU_R;
  68. starpu_task_submit(task);
  69. }
  70. starpu_task_wait_for_all();
  71. for (i = 0; i < mb; i++)
  72. {
  73. starpu_data_unregister(handle_array[i]);
  74. free(host_ptr_array[i]);
  75. }
  76. starpu_shutdown();
  77. return 0;
  78. }