dining_philosophers.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009, 2010, 2016 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013 CNRS
  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. #include "../helper.h"
  19. /*
  20. * Test Dijkstra's Dining Philosophers problem
  21. */
  22. /* TODO: try to use an arbiter and check improved concurrency */
  23. /* number of philosophers */
  24. #define N 16
  25. starpu_data_handle_t fork_handles[N];
  26. unsigned forks[N];
  27. void eat_kernel(void *descr[], void *arg)
  28. {
  29. (void)descr;
  30. (void)arg;
  31. }
  32. static struct starpu_codelet eating_cl =
  33. {
  34. .modes = { STARPU_RW, STARPU_RW },
  35. .cuda_funcs = {eat_kernel},
  36. .cpu_funcs = {eat_kernel},
  37. .opencl_funcs = {eat_kernel},
  38. .cpu_funcs_name = {"eat_kernel"},
  39. .nbuffers = 2
  40. };
  41. static
  42. int submit_one_task(unsigned p)
  43. {
  44. struct starpu_task *task = starpu_task_create();
  45. task->cl = &eating_cl;
  46. unsigned left = p;
  47. unsigned right = (p+1)%N;
  48. task->handles[0] = fork_handles[left];
  49. task->handles[1] = fork_handles[right];
  50. int ret = starpu_task_submit(task);
  51. return ret;
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. int ret;
  56. ret = starpu_initialize(NULL, &argc, &argv);
  57. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  58. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  59. /* initialize the forks */
  60. unsigned f;
  61. for (f = 0; f < N; f++)
  62. {
  63. forks[f] = 0;
  64. starpu_vector_data_register(&fork_handles[f], STARPU_MAIN_RAM, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  65. starpu_data_set_sequential_consistency_flag(fork_handles[f], 0);
  66. }
  67. unsigned ntasks = 1024;
  68. unsigned t;
  69. for (t = 0; t < ntasks; t++)
  70. {
  71. /* select one philosopher randomly */
  72. unsigned philosopher = rand() % N;
  73. ret = submit_one_task(philosopher);
  74. if (ret == -ENODEV) goto enodev;
  75. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  76. }
  77. ret = starpu_task_wait_for_all();
  78. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  79. FPRINTF(stderr, "waiting done\n");
  80. for (f = 0; f < N; f++)
  81. {
  82. starpu_data_unregister(fork_handles[f]);
  83. }
  84. starpu_shutdown();
  85. return EXIT_SUCCESS;
  86. enodev:
  87. for (f = 0; f < N; f++)
  88. {
  89. starpu_data_unregister(fork_handles[f]);
  90. }
  91. fprintf(stderr, "WARNING: No one can execute this task\n");
  92. /* yes, we do not perform the computation but we did detect that no one
  93. * could perform the kernel, so this is not an error from StarPU */
  94. starpu_shutdown();
  95. return STARPU_TEST_SKIPPED;
  96. }