dining_philosophers.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <starpu.h>
  2. /* number of philosophers */
  3. #define N 16
  4. starpu_data_handle fork_handles[N];
  5. unsigned forks[N];
  6. static void eat_kernel(void *descr[], void *arg)
  7. {
  8. }
  9. static starpu_codelet eating_cl = {
  10. .where = CORE|CUDA,
  11. .cuda_func = eat_kernel,
  12. .core_func = eat_kernel,
  13. .nbuffers = 2
  14. };
  15. void submit_one_task(unsigned p)
  16. {
  17. struct starpu_task *task = starpu_task_create();
  18. task->cl = &eating_cl;
  19. unsigned left = p;
  20. unsigned right = (p+1)%N;
  21. /* TODO we should not have to order these ressources ! */
  22. /* the last philosopher is left-handed ;) */
  23. if (p == (N - 1))
  24. {
  25. unsigned tmp;
  26. tmp = right;
  27. right = left;
  28. left = tmp;
  29. }
  30. task->buffers[0].handle = fork_handles[left];
  31. task->buffers[0].mode = STARPU_RW;
  32. task->buffers[1].handle = fork_handles[right];
  33. task->buffers[1].mode = STARPU_RW;
  34. int ret = starpu_submit_task(task);
  35. STARPU_ASSERT(!ret);
  36. }
  37. int main(int argc, int argv)
  38. {
  39. starpu_init(NULL);
  40. /* initialize the forks */
  41. unsigned f;
  42. for (f = 0; f < N; f++)
  43. {
  44. forks[f] = 0;
  45. starpu_register_vector_data(&fork_handles[f], 0, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  46. }
  47. unsigned ntasks = 1024;
  48. unsigned t;
  49. for (t = 0; t < ntasks; t++)
  50. {
  51. /* select one philosopher randomly */
  52. unsigned philosopher = rand() % N;
  53. submit_one_task(philosopher);
  54. }
  55. starpu_wait_all_tasks();
  56. starpu_shutdown();
  57. return 0;
  58. }