multithreaded.c 3.1 KB

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