dining_philosophers.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2011,2013,2014,2016 Université de Bordeaux
  4. * Copyright (C) 2011-2013 Inria
  5. * Copyright (C) 2010-2013,2015,2017 CNRS
  6. *
  7. * StarPU is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation; either version 2.1 of the License, or (at
  10. * your option) any later version.
  11. *
  12. * StarPU is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. *
  16. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. */
  18. #include <starpu.h>
  19. #include "../helper.h"
  20. /*
  21. * Test Dijkstra's Dining Philosophers problem
  22. */
  23. /* TODO: try to use an arbiter and check improved concurrency */
  24. /* number of philosophers */
  25. #define N 16
  26. starpu_data_handle_t fork_handles[N];
  27. unsigned forks[N];
  28. void eat_kernel(void *descr[], void *arg)
  29. {
  30. (void)descr;
  31. (void)arg;
  32. }
  33. static struct starpu_codelet eating_cl =
  34. {
  35. .modes = { STARPU_RW, STARPU_RW },
  36. .cuda_funcs = {eat_kernel},
  37. .cpu_funcs = {eat_kernel},
  38. .opencl_funcs = {eat_kernel},
  39. .cpu_funcs_name = {"eat_kernel"},
  40. .nbuffers = 2
  41. };
  42. static
  43. int submit_one_task(unsigned p)
  44. {
  45. struct starpu_task *task = starpu_task_create();
  46. task->cl = &eating_cl;
  47. unsigned left = p;
  48. unsigned right = (p+1)%N;
  49. task->handles[0] = fork_handles[left];
  50. task->handles[1] = fork_handles[right];
  51. int ret = starpu_task_submit(task);
  52. return ret;
  53. }
  54. int main(int argc, char **argv)
  55. {
  56. int ret;
  57. ret = starpu_initialize(NULL, &argc, &argv);
  58. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  60. /* initialize the forks */
  61. unsigned f;
  62. for (f = 0; f < N; f++)
  63. {
  64. forks[f] = 0;
  65. starpu_vector_data_register(&fork_handles[f], STARPU_MAIN_RAM, (uintptr_t)&forks[f], 1, sizeof(unsigned));
  66. starpu_data_set_sequential_consistency_flag(fork_handles[f], 0);
  67. }
  68. unsigned ntasks = 1024;
  69. unsigned t;
  70. for (t = 0; t < ntasks; t++)
  71. {
  72. /* select one philosopher randomly */
  73. unsigned philosopher = rand() % N;
  74. ret = submit_one_task(philosopher);
  75. if (ret == -ENODEV) goto enodev;
  76. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  77. }
  78. ret = starpu_task_wait_for_all();
  79. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_wait_for_all");
  80. FPRINTF(stderr, "waiting done\n");
  81. for (f = 0; f < N; f++)
  82. {
  83. starpu_data_unregister(fork_handles[f]);
  84. }
  85. starpu_shutdown();
  86. return EXIT_SUCCESS;
  87. enodev:
  88. for (f = 0; f < N; f++)
  89. {
  90. starpu_data_unregister(fork_handles[f]);
  91. }
  92. fprintf(stderr, "WARNING: No one can execute this task\n");
  93. /* yes, we do not perform the computation but we did detect that no one
  94. * could perform the kernel, so this is not an error from StarPU */
  95. starpu_shutdown();
  96. return STARPU_TEST_SKIPPED;
  97. }