commute2.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014-2021 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 <stdlib.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include "../helper.h"
  21. /*
  22. * Test that STARPU_RW vs STARPU_RW|STARPU_COMMUTE get proper dependency
  23. */
  24. static unsigned cnt;
  25. static void cpu_memcpy(void *descr[], void *cl_arg)
  26. {
  27. int me = (uintptr_t)cl_arg;
  28. int res;
  29. (void)descr;
  30. FPRINTF(stderr,"%d\n", me);
  31. if (me == 0)
  32. {
  33. /* let commute tasks potentially happen */
  34. usleep(100000);
  35. res = STARPU_ATOMIC_ADD(&cnt,1);
  36. STARPU_ASSERT(res == 1);
  37. }
  38. else
  39. {
  40. res = STARPU_ATOMIC_ADD(&cnt,1);
  41. STARPU_ASSERT(res != 1);
  42. }
  43. }
  44. static struct starpu_codelet my_cl =
  45. {
  46. .where = STARPU_CPU,
  47. .cpu_funcs = {cpu_memcpy},
  48. .nbuffers = STARPU_VARIABLE_NBUFFERS
  49. };
  50. int main(void)
  51. {
  52. double *res, *a;
  53. unsigned n=100000, i;
  54. starpu_data_handle_t res_handle, a_handle;
  55. unsigned nb_tasks = 10;
  56. int ret;
  57. ret = starpu_init(NULL);
  58. if (ret == -ENODEV) return STARPU_TEST_SKIPPED;
  59. STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
  60. starpu_malloc((void**)&res, n*sizeof(double));
  61. starpu_malloc((void**)&a, n*sizeof(double));
  62. for(i=0; i < n; i++)
  63. res[i] = a[i] = 1.0;
  64. starpu_vector_data_register(&res_handle, 0, (uintptr_t)res, (uint32_t)n, sizeof(double));
  65. starpu_vector_data_register(&a_handle, 0, (uintptr_t)a, (uint32_t)n, sizeof(double));
  66. starpu_data_acquire(a_handle, STARPU_RW);
  67. for (i = 0; i < nb_tasks; i++)
  68. {
  69. struct starpu_task *task = starpu_task_create();
  70. task->cl=&my_cl;
  71. task->nbuffers = i == 0 ? 2 : 1;
  72. task->handles[0] = res_handle;
  73. if (i == 0)
  74. task->modes[0] = STARPU_RW;
  75. else
  76. task->modes[0] = STARPU_RW | STARPU_COMMUTE;
  77. task->handles[1] = a_handle;
  78. task->modes[1] = STARPU_R;
  79. task->cl_arg = (void*)(uintptr_t)i;
  80. ret = starpu_task_submit(task);
  81. if (ret == -ENODEV)
  82. {
  83. starpu_data_release(a_handle);
  84. goto enodev;
  85. }
  86. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_submit");
  87. }
  88. /* let commute tasks potentially happen */
  89. usleep(100000);
  90. starpu_data_release(a_handle);
  91. starpu_task_wait_for_all ();
  92. enodev:
  93. starpu_data_unregister(res_handle);
  94. starpu_data_unregister(a_handle);
  95. starpu_free_noflag(res, n*sizeof(double));
  96. starpu_free_noflag(a, n*sizeof(double));
  97. starpu_shutdown();
  98. return ret == -ENODEV ? STARPU_TEST_SKIPPED : EXIT_SUCCESS;
  99. }