dining_philosophers.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 = STARPU_CPU|STARPU_CUDA,
  26. .cuda_func = eat_kernel,
  27. .cpu_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. task->buffers[0].handle = fork_handles[left];
  37. task->buffers[0].mode = STARPU_RW;
  38. task->buffers[1].handle = fork_handles[right];
  39. task->buffers[1].mode = STARPU_RW;
  40. int ret = starpu_submit_task(task);
  41. STARPU_ASSERT(!ret);
  42. }
  43. int main(int argc, int argv)
  44. {
  45. starpu_init(NULL);
  46. /* initialize the forks */
  47. unsigned f;
  48. for (f = 0; f < N; f++)
  49. {
  50. forks[f] = 0;
  51. starpu_register_vector_data(&fork_handles[f], 0, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  52. }
  53. unsigned ntasks = 1024;
  54. unsigned t;
  55. for (t = 0; t < ntasks; t++)
  56. {
  57. /* select one philosopher randomly */
  58. unsigned philosopher = rand() % N;
  59. submit_one_task(philosopher);
  60. }
  61. starpu_wait_all_tasks();
  62. starpu_shutdown();
  63. return 0;
  64. }