gpu_concurrency.c 2.8 KB

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