commute2.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2014 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. #ifdef STARPU_USE_CUDA
  21. #include <starpu_cuda.h>
  22. #endif
  23. static unsigned cnt;
  24. static void cpu_memcpy(void *descr[], void *cl_arg)
  25. {
  26. double *res = (double *)STARPU_VECTOR_GET_PTR(descr[0]);
  27. double *dst = (double *)STARPU_VECTOR_GET_PTR(descr[1]);
  28. int n = (int) STARPU_VECTOR_GET_NX(descr[0]);
  29. int me = (uintptr_t)cl_arg;
  30. if (me == 0)
  31. {
  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. memcpy(dst, res, n*sizeof(double));
  38. }
  39. static struct starpu_codelet my_cl = {
  40. .where = STARPU_CPU,
  41. .cpu_funcs = {cpu_memcpy, NULL},
  42. .nbuffers = STARPU_VARIABLE_NBUFFERS
  43. };
  44. int
  45. main()
  46. {
  47. double *res, *a;
  48. unsigned n=100000, i;
  49. starpu_data_handle_t res_handle, a_handle;
  50. unsigned nb_tasks = 10, worker;
  51. if (starpu_init(NULL))
  52. exit(-1);
  53. starpu_malloc((void**)&res, n*sizeof(double));
  54. starpu_malloc((void**)&a, n*sizeof(double));
  55. for(i=0; i < n; i++)
  56. res[i] = a[i] = 1.0;
  57. starpu_vector_data_register(&res_handle, 0, (uintptr_t)res, (uint32_t)n, sizeof(double));
  58. starpu_vector_data_register(&a_handle, 0, (uintptr_t)a, (uint32_t)n, sizeof(double));
  59. for (i = 0; i < nb_tasks; i++) {
  60. struct starpu_task *task = starpu_task_create();
  61. task->cl=&my_cl;
  62. task->nbuffers = 2;
  63. task->handles[0] = res_handle;
  64. if (i == 0)
  65. task->modes[0] = STARPU_RW;
  66. else
  67. task->modes[0] = STARPU_RW | STARPU_COMMUTE;
  68. task->handles[1] = a_handle;
  69. task->modes[1] = STARPU_R;
  70. task->cl_arg = (void*)(uintptr_t)i;
  71. if (starpu_task_submit(task)) {
  72. fprintf(stderr,"taks submmition failed!");
  73. exit(EXIT_FAILURE);
  74. }
  75. }
  76. starpu_task_wait_for_all ();
  77. starpu_data_unregister(res_handle);
  78. starpu_data_unregister(a_handle);
  79. starpu_free(res);
  80. starpu_free(a);
  81. starpu_shutdown();
  82. return EXIT_SUCCESS;
  83. }