dining_philosophers.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * StarPU
  3. * Copyright (C) INRIA 2008-2009 (see AUTHORS file)
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 2.1 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  15. */
  16. #include <starpu.h>
  17. /* number of philosophers */
  18. #define N 16
  19. starpu_data_handle fork_handles[N];
  20. unsigned forks[N];
  21. static void eat_kernel(void *descr[], void *arg)
  22. {
  23. }
  24. static starpu_codelet eating_cl = {
  25. .where = CORE|CUDA,
  26. .cuda_func = eat_kernel,
  27. .core_func = eat_kernel,
  28. .nbuffers = 2
  29. };
  30. void submit_one_task(unsigned p)
  31. {
  32. struct starpu_task *task = starpu_task_create();
  33. task->cl = &eating_cl;
  34. unsigned left = p;
  35. unsigned right = (p+1)%N;
  36. /* TODO we should not have to order these ressources ! */
  37. /* the last philosopher is left-handed ;) */
  38. if (p == (N - 1))
  39. {
  40. unsigned tmp;
  41. tmp = right;
  42. right = left;
  43. left = tmp;
  44. }
  45. task->buffers[0].handle = fork_handles[left];
  46. task->buffers[0].mode = STARPU_RW;
  47. task->buffers[1].handle = fork_handles[right];
  48. task->buffers[1].mode = STARPU_RW;
  49. int ret = starpu_submit_task(task);
  50. STARPU_ASSERT(!ret);
  51. }
  52. int main(int argc, int argv)
  53. {
  54. starpu_init(NULL);
  55. /* initialize the forks */
  56. unsigned f;
  57. for (f = 0; f < N; f++)
  58. {
  59. forks[f] = 0;
  60. starpu_register_vector_data(&fork_handles[f], 0, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  61. }
  62. unsigned ntasks = 1024;
  63. unsigned t;
  64. for (t = 0; t < ntasks; t++)
  65. {
  66. /* select one philosopher randomly */
  67. unsigned philosopher = rand() % N;
  68. submit_one_task(philosopher);
  69. }
  70. starpu_wait_all_tasks();
  71. starpu_shutdown();
  72. return 0;
  73. }