commute2.c 2.8 KB

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