dining_philosophers.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #include <starpu.h>
  18. /* number of philosophers */
  19. #define N 16
  20. starpu_data_handle fork_handles[N];
  21. unsigned forks[N];
  22. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  23. static void eat_kernel(void *descr[], void *arg)
  24. {
  25. }
  26. static starpu_codelet eating_cl = {
  27. .where = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  28. .cuda_func = eat_kernel,
  29. .cpu_func = eat_kernel,
  30. .opencl_func = eat_kernel,
  31. .nbuffers = 2
  32. };
  33. void submit_one_task(unsigned p)
  34. {
  35. struct starpu_task *task = starpu_task_create();
  36. task->cl = &eating_cl;
  37. unsigned left = p;
  38. unsigned right = (p+1)%N;
  39. task->buffers[0].handle = fork_handles[left];
  40. task->buffers[0].mode = STARPU_RW;
  41. task->buffers[1].handle = fork_handles[right];
  42. task->buffers[1].mode = STARPU_RW;
  43. int ret = starpu_task_submit(task);
  44. STARPU_ASSERT(!ret);
  45. }
  46. int main(int argc, char **argv)
  47. {
  48. starpu_init(NULL);
  49. /* initialize the forks */
  50. unsigned f;
  51. for (f = 0; f < N; f++)
  52. {
  53. forks[f] = 0;
  54. starpu_vector_data_register(&fork_handles[f], 0, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  55. }
  56. unsigned ntasks = 1024;
  57. unsigned t;
  58. for (t = 0; t < ntasks; t++)
  59. {
  60. /* select one philosopher randomly */
  61. unsigned philosopher = rand() % N;
  62. submit_one_task(philosopher);
  63. }
  64. starpu_task_wait_for_all();
  65. FPRINTF(stderr, "waiting done\n");
  66. for (f = 0; f < N; f++)
  67. {
  68. starpu_data_unregister(fork_handles[f]);
  69. }
  70. starpu_shutdown();
  71. return 0;
  72. }