multithreaded.c 3.0 KB

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