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 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 "../common/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 starpu_codelet dummy_codelet =
  32. {
  33. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  34. .cpu_func = dummy_func,
  35. .cuda_func = dummy_func,
  36. .opencl_func = dummy_func,
  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. int 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. case 'i':
  68. ntasks = atoi(optarg);
  69. break;
  70. case 't':
  71. nthreads = atoi(optarg);
  72. break;
  73. case 'h':
  74. usage(argv);
  75. break;
  76. }
  77. }
  78. int main(int argc, char **argv)
  79. {
  80. // unsigned i;
  81. double timing;
  82. struct timeval start;
  83. struct timeval end;
  84. int ret;
  85. parse_args(argc, argv);
  86. ret = starpu_init(NULL);
  87. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  88. #ifdef STARPU_SLOW_MACHINE
  89. ntasks /= 10;
  90. #endif
  91. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  92. gettimeofday(&start, NULL);
  93. unsigned t;
  94. for (t = 0; t < nthreads; t++)
  95. {
  96. int ret = pthread_create(&threads[t], NULL, thread_func, NULL);
  97. STARPU_ASSERT(ret == 0);
  98. }
  99. for (t = 0; t < nthreads; t++)
  100. {
  101. int ret = pthread_join(threads[t], NULL);
  102. STARPU_ASSERT(ret == 0);
  103. }
  104. gettimeofday(&end, NULL);
  105. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  106. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  107. FPRINTF(stderr, "Per task: %f usecs\n", timing/(nthreads*ntasks));
  108. starpu_shutdown();
  109. return EXIT_SUCCESS;
  110. }