multithreaded.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  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;
  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. starpu_task_wait_for_all();
  54. return NULL;
  55. }
  56. static void usage(char **argv)
  57. {
  58. FPRINTF(stderr, "%s [-i ntasks] [-t nthreads] [-h]\n", argv[0]);
  59. exit(-1);
  60. }
  61. static void parse_args(int argc, char **argv)
  62. {
  63. int c;
  64. while ((c = getopt(argc, argv, "i:t:h")) != -1)
  65. switch(c) {
  66. case 'i':
  67. ntasks = atoi(optarg);
  68. break;
  69. case 't':
  70. nthreads = atoi(optarg);
  71. break;
  72. case 'h':
  73. usage(argv);
  74. break;
  75. }
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. // unsigned i;
  80. double timing;
  81. struct timeval start;
  82. struct timeval end;
  83. parse_args(argc, argv);
  84. starpu_init(NULL);
  85. #ifdef STARPU_SLOW_MACHINE
  86. ntasks /= 10;
  87. #endif
  88. FPRINTF(stderr, "#tasks : %u\n", ntasks);
  89. gettimeofday(&start, NULL);
  90. unsigned t;
  91. for (t = 0; t < nthreads; t++)
  92. {
  93. int ret = pthread_create(&threads[t], NULL, thread_func, NULL);
  94. STARPU_ASSERT(ret == 0);
  95. }
  96. for (t = 0; t < nthreads; t++)
  97. {
  98. int ret = pthread_join(threads[t], NULL);
  99. STARPU_ASSERT(ret == 0);
  100. }
  101. gettimeofday(&end, NULL);
  102. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  103. FPRINTF(stderr, "Total: %f secs\n", timing/1000000);
  104. FPRINTF(stderr, "Per task: %f usecs\n", timing/(nthreads*ntasks));
  105. starpu_shutdown();
  106. return 0;
  107. }