commute2.c 2.8 KB

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