commute2.c 2.7 KB

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