dining_philosophers.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * StarPU
  3. * Copyright (C) Université Bordeaux 1, CNRS 2008-2010 (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 = STARPU_CPU|STARPU_CUDA|STARPU_OPENCL,
  26. .cuda_func = eat_kernel,
  27. .cpu_func = eat_kernel,
  28. .opencl_func = eat_kernel,
  29. .nbuffers = 2
  30. };
  31. void submit_one_task(unsigned p)
  32. {
  33. struct starpu_task *task = starpu_task_create();
  34. task->cl = &eating_cl;
  35. unsigned left = p;
  36. unsigned right = (p+1)%N;
  37. task->buffers[0].handle = fork_handles[left];
  38. task->buffers[0].mode = STARPU_RW;
  39. task->buffers[1].handle = fork_handles[right];
  40. task->buffers[1].mode = STARPU_RW;
  41. int ret = starpu_task_submit(task);
  42. STARPU_ASSERT(!ret);
  43. }
  44. int main(int argc, int argv)
  45. {
  46. starpu_init(NULL);
  47. /* initialize the forks */
  48. unsigned f;
  49. for (f = 0; f < N; f++)
  50. {
  51. forks[f] = 0;
  52. starpu_vector_data_register(&fork_handles[f], 0, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  53. }
  54. unsigned ntasks = 1024;
  55. unsigned t;
  56. for (t = 0; t < ntasks; t++)
  57. {
  58. /* select one philosopher randomly */
  59. unsigned philosopher = rand() % N;
  60. submit_one_task(philosopher);
  61. }
  62. starpu_task_wait_for_all();
  63. starpu_shutdown();
  64. return 0;
  65. }