multithreaded.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010-2011, 2013-2014, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2017 CNRS
  5. * Copyright (C) 2011 CNRS
  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 <starpu.h>
  21. #include "../helper.h"
  22. /*
  23. * Try submitting tasks from different threads
  24. */
  25. starpu_pthread_t threads[16];
  26. #ifdef STARPU_QUICK_CHECK
  27. static unsigned ntasks = 64;
  28. #else
  29. static unsigned ntasks = 65536;
  30. #endif
  31. static unsigned nthreads = 2;
  32. void dummy_func(void *descr[] STARPU_ATTRIBUTE_UNUSED, void *arg STARPU_ATTRIBUTE_UNUSED)
  33. {
  34. }
  35. static struct starpu_codelet dummy_codelet =
  36. {
  37. .cpu_funcs = {dummy_func},
  38. .cuda_funcs = {dummy_func},
  39. .opencl_funcs = {dummy_func},
  40. .cpu_funcs_name = {"dummy_func"},
  41. .model = NULL,
  42. .nbuffers = 0
  43. };
  44. static
  45. void *thread_func(void *arg STARPU_ATTRIBUTE_UNUSED)
  46. {
  47. int ret;
  48. unsigned i;
  49. for (i = 0; i < ntasks; i++)
  50. {
  51. struct starpu_task *task = starpu_task_create();
  52. task->cl = &dummy_codelet;
  53. task->cl_arg = NULL;
  54. task->callback_func = NULL;
  55. task->callback_arg = NULL;
  56. ret = starpu_task_submit(task);
  57. STARPU_ASSERT_MSG(!ret, "task submission failed with error code %d", ret);
  58. }
  59. ret = starpu_task_wait_for_all();
  60. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  61. return NULL;
  62. }
  63. static void usage(char **argv)
  64. {
  65. FPRINTF(stderr, "%s [-i ntasks] [-t nthreads] [-h]\n", argv[0]);
  66. exit(-1);
  67. }
  68. static void parse_args(int argc, char **argv)
  69. {
  70. int c;
  71. while ((c = getopt(argc, argv, "i:t:h")) != -1)
  72. switch(c)
  73. {
  74. case 'i':
  75. ntasks = atoi(optarg);
  76. break;
  77. case 't':
  78. nthreads = atoi(optarg);
  79. break;
  80. case 'h':
  81. usage(argv);
  82. break;
  83. }
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. // unsigned i;
  88. double timing;
  89. double start;
  90. double end;
  91. int ret;
  92. parse_args(argc, argv);
  93. ret = starpu_initialize(NULL, &argc, &argv);
  94. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  95. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  96. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  97. start = starpu_timing_now();
  98. unsigned t;
  99. for (t = 0; t < nthreads; t++)
  100. {
  101. STARPU_PTHREAD_CREATE(&threads[t], NULL, thread_func, NULL);
  102. }
  103. for (t = 0; t < nthreads; t++)
  104. {
  105. STARPU_PTHREAD_JOIN(threads[t], NULL);
  106. }
  107. end = starpu_timing_now();
  108. timing = end - start;
  109. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  110. FPRINTF(stderr, "Per task: %f usecs\n", timing/(nthreads*ntasks));
  111. starpu_shutdown();
  112. return EXIT_SUCCESS;
  113. }