dining_philosophers.c 2.8 KB

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