gpu_concurrency.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2021 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. *
  5. * StarPU 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. * StarPU 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. #include "../helper.h"
  22. #include <common/thread.h>
  23. /*
  24. * Check that concurrency does happen when using multi-stream CUDA.
  25. */
  26. #ifdef STARPU_QUICK_CHECK
  27. #define NITERS 100000
  28. #else
  29. #define NITERS 1000000
  30. #endif
  31. #define NTASKS 64
  32. #define SYNC 16
  33. #ifdef STARPU_USE_CUDA
  34. extern void long_kernel_cuda(unsigned long niters);
  35. void codelet_long_kernel_async(void *descr[], void *arg)
  36. {
  37. (void)descr;
  38. (void)arg;
  39. long_kernel_cuda(NITERS);
  40. }
  41. void codelet_long_kernel_sync(void *descr[], void *arg)
  42. {
  43. (void)descr;
  44. (void)arg;
  45. long_kernel_cuda(NITERS);
  46. cudaStreamSynchronize(starpu_cuda_get_local_stream());
  47. }
  48. static struct starpu_perfmodel model_async =
  49. {
  50. .type = STARPU_HISTORY_BASED,
  51. .symbol = "long_kernel_async",
  52. };
  53. static struct starpu_perfmodel model_sync =
  54. {
  55. .type = STARPU_HISTORY_BASED,
  56. .symbol = "long_kernel_sync",
  57. };
  58. static struct starpu_codelet cl_async =
  59. {
  60. .cuda_funcs = {codelet_long_kernel_async},
  61. .cuda_flags = {STARPU_CUDA_ASYNC},
  62. .nbuffers = 0,
  63. .model = &model_async,
  64. };
  65. static struct starpu_codelet cl =
  66. {
  67. .cuda_funcs = {codelet_long_kernel_sync},
  68. .nbuffers = 0,
  69. .model = &model_sync,
  70. };
  71. #endif
  72. int main(int argc, char **argv)
  73. {
  74. #ifndef STARPU_USE_CUDA
  75. return STARPU_TEST_SKIPPED;
  76. #else
  77. setenv("STARPU_NWORKER_PER_CUDA", "4", 1);
  78. int ret = starpu_initialize(NULL, &argc, &argv);
  79. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  80. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  81. if (starpu_cuda_worker_get_count() == 0)
  82. {
  83. starpu_shutdown();
  84. return STARPU_TEST_SKIPPED;
  85. }
  86. unsigned iter;
  87. for (iter = 0; iter < NTASKS; iter++)
  88. {
  89. struct starpu_task *task = starpu_task_create();
  90. if (!(iter % SYNC))
  91. /* Insert a synchronous task, just for fun */
  92. task->cl = &cl;
  93. else
  94. task->cl = &cl_async;
  95. ret = starpu_task_submit(task);
  96. if (ret == -ENODEV) goto enodev;
  97. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  98. }
  99. starpu_shutdown();
  100. STARPU_RETURN(EXIT_SUCCESS);
  101. enodev:
  102. fprintf(stderr, "WARNING: No one can execute this task\n");
  103. /* yes, we do not perform the computation but we did detect that no one
  104. * could perform the kernel, so this is not an error from StarPU */
  105. starpu_shutdown();
  106. STARPU_RETURN(STARPU_TEST_SKIPPED);
  107. #endif
  108. }