dsm_stress.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. #include "../common/helper.h"
  24. #define N 10000
  25. #define VECTORSIZE 1024
  26. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  27. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  28. static unsigned finished = 0;
  29. static unsigned cnt = N;
  30. starpu_data_handle v_handle, v_handle2;
  31. static unsigned *v;
  32. static unsigned *v2;
  33. static void callback(void *arg)
  34. {
  35. unsigned res = STARPU_ATOMIC_ADD(&cnt, -1);
  36. if (res == 0)
  37. {
  38. pthread_mutex_lock(&mutex);
  39. finished = 1;
  40. pthread_cond_signal(&cond);
  41. pthread_mutex_unlock(&mutex);
  42. }
  43. }
  44. static void cuda_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  45. {
  46. }
  47. static void opencl_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  48. {
  49. }
  50. static void cpu_codelet_null(void *descr[], __attribute__ ((unused)) void *_args)
  51. {
  52. }
  53. static starpu_access_mode select_random_mode(void)
  54. {
  55. int r = rand();
  56. switch (r % 3) {
  57. case 0:
  58. return STARPU_R;
  59. case 1:
  60. return STARPU_W;
  61. case 2:
  62. return STARPU_RW;
  63. };
  64. return STARPU_RW;
  65. }
  66. static starpu_codelet cl = {
  67. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  68. .cpu_func = cpu_codelet_null,
  69. .cuda_func = cuda_codelet_null,
  70. .opencl_func = opencl_codelet_null,
  71. .nbuffers = 2
  72. };
  73. int main(int argc, char **argv)
  74. {
  75. int ret;
  76. ret = starpu_init(NULL);
  77. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  78. ret = starpu_malloc((void **)&v, VECTORSIZE*sizeof(unsigned));
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  80. ret = starpu_malloc((void **)&v2, VECTORSIZE*sizeof(unsigned));
  81. STARPU_CHECK_RETURN_VALUE(ret, "starpu_malloc");
  82. starpu_vector_data_register(&v_handle, 0, (uintptr_t)v, VECTORSIZE, sizeof(unsigned));
  83. starpu_vector_data_register(&v_handle2, 0, (uintptr_t)v2, VECTORSIZE, sizeof(unsigned));
  84. unsigned iter;
  85. for (iter = 0; iter < N; iter++)
  86. {
  87. struct starpu_task *task = starpu_task_create();
  88. task->cl = &cl;
  89. task->buffers[0].handle = v_handle;
  90. task->buffers[0].mode = select_random_mode();
  91. task->buffers[1].handle = v_handle2;
  92. task->buffers[1].mode = select_random_mode();
  93. task->callback_func = callback;
  94. task->callback_arg = NULL;
  95. int ret = starpu_task_submit(task);
  96. if (ret == -ENODEV) goto enodev;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  98. }
  99. pthread_mutex_lock(&mutex);
  100. if (!finished)
  101. pthread_cond_wait(&cond, &mutex);
  102. pthread_mutex_unlock(&mutex);
  103. starpu_data_unregister(v_handle);
  104. starpu_data_unregister(v_handle2);
  105. starpu_free(v);
  106. starpu_free(v2);
  107. starpu_shutdown();
  108. return 0;
  109. enodev:
  110. starpu_data_unregister(v_handle);
  111. starpu_data_unregister(v_handle2);
  112. starpu_free(v);
  113. starpu_free(v2);
  114. starpu_shutdown();
  115. fprintf(stderr, "WARNING: No one can execute this task\n");
  116. /* yes, we do not perform the computation but we did detect that no one
  117. * could perform the kernel, so this is not an error from StarPU */
  118. return 77;
  119. }