gpu_concurrency.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2017 CNRS
  4. * Copyright (C) 2014 Inria
  5. * Copyright (C) 2014-2016 Université de Bordeaux
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <errno.h>
  21. #include <starpu.h>
  22. #include <stdlib.h>
  23. #include "../helper.h"
  24. #include <common/thread.h>
  25. /*
  26. * Check that concurrency does happen when using multi-stream CUDA.
  27. */
  28. #ifdef STARPU_QUICK_CHECK
  29. #define NITERS 100000
  30. #else
  31. #define NITERS 1000000
  32. #endif
  33. #define NTASKS 64
  34. #define SYNC 16
  35. #ifdef STARPU_USE_CUDA
  36. extern void long_kernel_cuda(unsigned long niters);
  37. void codelet_long_kernel_async(void *descr[], void *arg)
  38. {
  39. (void)descr;
  40. (void)arg;
  41. long_kernel_cuda(NITERS);
  42. }
  43. void codelet_long_kernel_sync(void *descr[], void *arg)
  44. {
  45. (void)descr;
  46. (void)arg;
  47. long_kernel_cuda(NITERS);
  48. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  49. }
  50. static struct starpu_perfmodel model_async =
  51. {
  52. .type = STARPU_HISTORY_BASED,
  53. .symbol = "long_kernel_async",
  54. };
  55. static struct starpu_perfmodel model_sync =
  56. {
  57. .type = STARPU_HISTORY_BASED,
  58. .symbol = "long_kernel_sync",
  59. };
  60. static struct starpu_codelet cl_async =
  61. {
  62. .cuda_funcs = {codelet_long_kernel_async},
  63. .cuda_flags = {STARPU_CUDA_ASYNC},
  64. .nbuffers = 0,
  65. .model = &model_async,
  66. };
  67. static struct starpu_codelet cl =
  68. {
  69. .cuda_funcs = {codelet_long_kernel_sync},
  70. .nbuffers = 0,
  71. .model = &model_sync,
  72. };
  73. #endif
  74. int main(int argc, char **argv)
  75. {
  76. #ifndef STARPU_USE_CUDA
  77. return STARPU_TEST_SKIPPED;
  78. #else
  79. setenv("STARPU_NWORKER_PER_CUDA", "4", 1);
  80. int ret = starpu_initialize(NULL, &argc, &argv);
  81. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  82. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  83. if (starpu_cuda_worker_get_count() == 0)
  84. {
  85. starpu_shutdown();
  86. return STARPU_TEST_SKIPPED;
  87. }
  88. unsigned iter;
  89. for (iter = 0; iter < NTASKS; iter++)
  90. {
  91. struct starpu_task *task = starpu_task_create();
  92. if (!(iter % SYNC))
  93. /* Insert a synchronous task, just for fun */
  94. task->cl = &cl;
  95. else
  96. task->cl = &cl_async;
  97. ret = starpu_task_submit(task);
  98. if (ret == -ENODEV) goto enodev;
  99. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  100. }
  101. starpu_shutdown();
  102. STARPU_RETURN(EXIT_SUCCESS);
  103. enodev:
  104. fprintf(stderr, "WARNING: No one can execute this task\n");
  105. /* yes, we do not perform the computation but we did detect that no one
  106. * could perform the kernel, so this is not an error from StarPU */
  107. starpu_shutdown();
  108. STARPU_RETURN(STARPU_TEST_SKIPPED);
  109. #endif
  110. }