multithreaded.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010 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. pthread_t threads[16];
  25. static unsigned ntasks = 65536;
  26. static unsigned nthreads = 2;
  27. static void dummy_func(void *descr[] __attribute__ ((unused)), void *arg __attribute__ ((unused)))
  28. {
  29. }
  30. static starpu_codelet dummy_codelet =
  31. {
  32. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  33. .cpu_func = dummy_func,
  34. .cuda_func = dummy_func,
  35. .opencl_func = dummy_func,
  36. .model = NULL,
  37. .nbuffers = 0
  38. };
  39. void *thread_func(void *arg __attribute__((unused)))
  40. {
  41. int i;
  42. for (i = 0; i < ntasks; i++)
  43. {
  44. struct starpu_task *task = starpu_task_create();
  45. task->cl = &dummy_codelet;
  46. task->cl_arg = NULL;
  47. task->callback_func = NULL;
  48. task->callback_arg = NULL;
  49. int ret = starpu_task_submit(task);
  50. STARPU_ASSERT(!ret);
  51. }
  52. starpu_task_wait_for_all();
  53. return NULL;
  54. }
  55. static void usage(char **argv)
  56. {
  57. fprintf(stderr, "%s [-i ntasks] [-t nthreads] [-h]\n", argv[0]);
  58. exit(-1);
  59. }
  60. static void parse_args(int argc, char **argv)
  61. {
  62. int c;
  63. while ((c = getopt(argc, argv, "i:t:h")) != -1)
  64. switch(c) {
  65. case 'i':
  66. ntasks = atoi(optarg);
  67. break;
  68. case 't':
  69. nthreads = atoi(optarg);
  70. break;
  71. case 'h':
  72. usage(argv);
  73. break;
  74. }
  75. }
  76. int main(int argc, char **argv)
  77. {
  78. // unsigned i;
  79. double timing;
  80. struct timeval start;
  81. struct timeval end;
  82. parse_args(argc, argv);
  83. starpu_init(NULL);
  84. fprintf(stderr, "#tasks : %d\n", ntasks);
  85. gettimeofday(&start, NULL);
  86. unsigned t;
  87. for (t = 0; t < nthreads; t++)
  88. {
  89. int ret = pthread_create(&threads[t], NULL, thread_func, NULL);
  90. STARPU_ASSERT(ret == 0);
  91. }
  92. for (t = 0; t < nthreads; t++)
  93. {
  94. int ret = pthread_join(threads[t], NULL);
  95. STARPU_ASSERT(ret == 0);
  96. }
  97. gettimeofday(&end, NULL);
  98. timing = (double)((end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec));
  99. fprintf(stderr, "Total: %lf secs\n", timing/1000000);
  100. fprintf(stderr, "Per task: %lf usecs\n", timing/(nthreads*ntasks));
  101. starpu_shutdown();
  102. return 0;
  103. }