dsm_stress.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (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. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <starpu.h>
  20. #include <stdlib.h>
  21. #define N 10000
  22. #define VECTORSIZE 1024
  23. static pthread_mutex_t mutex;
  24. static pthread_cond_t cond;
  25. static unsigned finished = 0;
  26. static unsigned cnt = N;
  27. starpu_data_handle v_handle, v_handle2;
  28. static unsigned *v;
  29. static unsigned *v2;
  30. static void callback(void *arg)
  31. {
  32. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  33. if (res == 0)
  34. {
  35. pthread_mutex_lock(&mutex);
  36. finished = 1;
  37. pthread_cond_signal(&cond);
  38. pthread_mutex_unlock(&mutex);
  39. }
  40. }
  41. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  42. {
  43. }
  44. static void core_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  45. {
  46. }
  47. static starpu_access_mode select_random_mode(void)
  48. {
  49. int r = rand();
  50. switch (r % 3) {
  51. case 0:
  52. return STARPU_R;
  53. case 1:
  54. return STARPU_W;
  55. case 2:
  56. return STARPU_RW;
  57. };
  58. }
  59. static starpu_codelet cl = {
  60. .where = CORE|CUDA,
  61. .core_func = core_codelet_null,
  62. .cuda_func = cuda_codelet_null,
  63. .nbuffers = 2
  64. };
  65. int main(int argc, char **argv)
  66. {
  67. starpu_init(NULL);
  68. starpu_malloc_pinned_if_possible((void **)&v, VECTORSIZE*sizeof(unsigned));
  69. starpu_malloc_pinned_if_possible((void **)&v2, VECTORSIZE*sizeof(unsigned));
  70. starpu_register_vector_data(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  71. starpu_register_vector_data(&v_handle2, 0, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  72. unsigned iter;
  73. for (iter = 0; iter < N; iter++)
  74. {
  75. struct starpu_task *task = starpu_task_create();
  76. task->cl = &cl;
  77. task->buffers[0].handle = v_handle;
  78. task->buffers[0].mode = select_random_mode();
  79. task->buffers[1].handle = v_handle2;
  80. task->buffers[1].mode = select_random_mode();
  81. task->callback_func = callback;
  82. task->callback_arg = NULL;
  83. int ret = starpu_submit_task(task);
  84. if (ret == -ENODEV)
  85. goto enodev;
  86. }
  87. pthread_mutex_lock(&mutex);
  88. if (!finished)
  89. pthread_cond_wait(&cond, &mutex);
  90. pthread_mutex_unlock(&mutex);
  91. starpu_shutdown();
  92. return 0;
  93. enodev:
  94. fprintf(stderr, "WARNING: No one can execute this task\n");
  95. /* yes, we do not perform the computation but we did detect that no one
  96. * could perform the kernel, so this is not an error from StarPU */
  97. return 0;
  98. }