acquire_cb_insert.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2011 Centre National de la Recherche Scientifique
  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. #define N 16
  18. #define M 4
  19. #define X 2
  20. #define FPRINTF(ofile, fmt, args ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ##args); }} while(0)
  21. void which_index_cpu(void *descr[], void *_args)
  22. {
  23. int *x0 = (int *)STARPU_VARIABLE_GET_PTR(descr[0]);
  24. /* A real case would actually compute something */
  25. *x0 = X;
  26. }
  27. starpu_codelet which_index = {
  28. .where = STARPU_CPU,
  29. .cpu_func = which_index_cpu,
  30. .nbuffers = 1
  31. };
  32. void work_cpu(void *descr[], void *_args)
  33. {
  34. int i, n = STARPU_VECTOR_GET_NX(descr[0]);
  35. float *x0 = (float *)STARPU_VECTOR_GET_PTR(descr[0]);
  36. for (i = 0; i < n; i++)
  37. x0[i] = i + 1;
  38. }
  39. starpu_codelet work = {
  40. .where = STARPU_CPU,
  41. .cpu_func = work_cpu,
  42. .nbuffers = 1
  43. };
  44. static int x;
  45. int main(int argc, char **argv)
  46. {
  47. int i, ret;
  48. float *f;
  49. starpu_data_handle x_handle, f_handle;
  50. starpu_init(NULL);
  51. /* Declare x */
  52. starpu_variable_data_register(&x_handle, 0, (uintptr_t)&x, sizeof(x));
  53. /* Allocate and Declare f */
  54. starpu_malloc((void**)&f, N * sizeof(*f));
  55. memset(f, 0, N * sizeof(*f));
  56. starpu_vector_data_register(&f_handle, 0, (uintptr_t)f, N, sizeof(*f));
  57. /* Partition f */
  58. struct starpu_data_filter filter = {
  59. .filter_func = starpu_block_filter_func_vector,
  60. .nchildren = M,
  61. };
  62. starpu_data_partition(f_handle, &filter);
  63. /* Compute which portion we will work on */
  64. ret = starpu_insert_task(&which_index, STARPU_W, x_handle, 0);
  65. if (ret == -ENODEV) goto enodev;
  66. /* And submit the corresponding task */
  67. STARPU_DATA_ACQUIRE_CB(
  68. x_handle,
  69. STARPU_R,
  70. starpu_insert_task(&work, STARPU_W, starpu_data_get_sub_data(f_handle, 1, x), 0)
  71. );
  72. starpu_task_wait_for_all();
  73. starpu_data_unpartition(f_handle, 0);
  74. starpu_data_unregister(f_handle);
  75. starpu_data_unregister(x_handle);
  76. FPRINTF(stderr, "VALUES: %d", x);
  77. for(i=0 ; i<N ; i++) {
  78. FPRINTF(stderr, " %f", f[i]);
  79. }
  80. STARPU_ASSERT(f[X*(N/M)] == 1);
  81. STARPU_ASSERT(f[X*(N/M)+1] == 2);
  82. STARPU_ASSERT(f[X*(N/M)+2] == 3);
  83. STARPU_ASSERT(f[X*(N/M)+3] == 4);
  84. FPRINTF(stderr, "\n");
  85. starpu_shutdown();
  86. return 0;
  87. enodev:
  88. fprintf(stderr, "WARNING: No one can execute this task\n");
  89. /* yes, we do not perform the computation but we did detect that no one
  90. * could perform the kernel, so this is not an error from StarPU */
  91. return 77;
  92. }