dining_philosophers.c 2.7 KB

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