dining_philosophers.c 1.9 KB

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