dsm_stress.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011 Centre National de la Recherche Scientifique
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. #include <errno.h>
  20. #include <starpu.h>
  21. #include <stdlib.h>
  22. #include <pthread.h>
  23. #define N 10000
  24. #define VECTORSIZE 1024
  25. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  26. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  27. static unsigned finished = 0;
  28. static unsigned cnt = N;
  29. starpu_data_handle v_handle, v_handle2;
  30. static unsigned *v;
  31. static unsigned *v2;
  32. static void callback(void *arg)
  33. {
  34. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  35. if (res == 0)
  36. {
  37. pthread_mutex_lock(&mutex);
  38. finished = 1;
  39. pthread_cond_signal(&cond);
  40. pthread_mutex_unlock(&mutex);
  41. }
  42. }
  43. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  44. {
  45. }
  46. static void opencl_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  47. {
  48. }
  49. static void cpu_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  50. {
  51. }
  52. static starpu_access_mode select_random_mode(void)
  53. {
  54. int r = rand();
  55. switch (r % 3) {
  56. case 0:
  57. return STARPU_R;
  58. case 1:
  59. return STARPU_W;
  60. case 2:
  61. return STARPU_RW;
  62. };
  63. return STARPU_RW;
  64. }
  65. static starpu_codelet cl = {
  66. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  67. .cpu_func = cpu_codelet_null,
  68. .cuda_func = cuda_codelet_null,
  69. .opencl_func = opencl_codelet_null,
  70. .nbuffers = 2
  71. };
  72. int main(int argc, char **argv)
  73. {
  74. starpu_init(NULL);
  75. starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  76. starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  77. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  78. starpu_vector_data_register(&v_handle2, 0, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  79. unsigned iter;
  80. for (iter = 0; iter < N; iter++)
  81. {
  82. struct starpu_task *task = starpu_task_create();
  83. task->cl = &cl;
  84. task->buffers[0].handle = v_handle;
  85. task->buffers[0].mode = select_random_mode();
  86. task->buffers[1].handle = v_handle2;
  87. task->buffers[1].mode = select_random_mode();
  88. task->callback_func = callback;
  89. task->callback_arg = NULL;
  90. int ret = starpu_task_submit(task);
  91. if (ret == -ENODEV)
  92. goto enodev;
  93. }
  94. pthread_mutex_lock(&mutex);
  95. if (!finished)
  96. pthread_cond_wait(&cond, &mutex);
  97. pthread_mutex_unlock(&mutex);
  98. starpu_shutdown();
  99. return 0;
  100. enodev:
  101. fprintf(stderr, "WARNING: No one can execute this task\n");
  102. /* yes, we do not perform the computation but we did detect that no one
  103. * could perform the kernel, so this is not an error from StarPU */
  104. return 0;
  105. }