frecursive.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2018 CNRS
  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 FPRINTF(ofile, fmt, ...) do { if (!getenv("STARPU_SSILENT")) {fprintf(ofile, fmt, ## __VA_ARGS__); }} while(0)
  18. void cpu_codelet(void *buffers[], void *cl_arg)
  19. {
  20. unsigned i, j;
  21. int factor;
  22. starpu_codelet_unpack_args(cl_arg, &factor, 0);
  23. /* length of the matrix */
  24. unsigned nx = STARPU_MATRIX_GET_NX(buffers[0]);
  25. unsigned ny = STARPU_MATRIX_GET_NY(buffers[0]);
  26. unsigned ld = STARPU_MATRIX_GET_LD(buffers[0]);
  27. /* local copy of the matrix pointer */
  28. int *val = (int *)STARPU_MATRIX_GET_PTR(buffers[0]);
  29. FPRINTF(stderr, "computing on matrix with nx=%u, ny=%u, ld=%u\n", nx, ny, ld);
  30. for(j=0; j<ny ; j++)
  31. {
  32. for(i=0; i<nx ; i++)
  33. val[(j*ld)+i] *= factor;
  34. }
  35. }
  36. static struct starpu_codelet cl =
  37. {
  38. .cpu_funcs[0] = cpu_codelet,
  39. .nbuffers = 1,
  40. .modes[0] = STARPU_RW,
  41. };
  42. #define NX 400
  43. #define NY 80
  44. #define LD NX
  45. #define PARTS 4
  46. int main(void)
  47. {
  48. int *matrix;
  49. starpu_data_handle_t matrix_handle;
  50. starpu_data_handle_t subhandle_l1[PARTS];
  51. starpu_data_handle_t subhandle_l2[PARTS][PARTS];
  52. starpu_data_handle_t subhandle_l3[PARTS][PARTS][PARTS];
  53. int ret;
  54. int factor = 12;
  55. int n=1;
  56. int i,j,k;
  57. ret = starpu_init(NULL);
  58. if (STARPU_UNLIKELY(ret == -ENODEV))
  59. {
  60. return 77;
  61. }
  62. if (starpu_cpu_worker_get_count() < 1)
  63. {
  64. FPRINTF(stderr, "This application requires at least 1 cpu worker\n");
  65. starpu_shutdown();
  66. return 77;
  67. }
  68. matrix = (int*)malloc(NX * NY * sizeof(int));
  69. assert(matrix);
  70. starpu_matrix_data_register(&matrix_handle, STARPU_MAIN_RAM, (uintptr_t)matrix, LD, NX, NY, sizeof(int));
  71. for(j=0 ; j<NY ; j++)
  72. {
  73. for(i=0 ; i<NX ; i++)
  74. {
  75. matrix[(j*LD)+i] = n++;
  76. }
  77. }
  78. /* Split the matrix in PARTS sub-matrices, each sub-matrix in PARTS sub-sub-matrices, and each sub-sub matrix in PARTS sub-sub-sub-matrices */
  79. struct starpu_data_filter f =
  80. {
  81. .filter_func = starpu_matrix_filter_block,
  82. .nchildren = PARTS
  83. };
  84. struct starpu_data_filter f2 =
  85. {
  86. .filter_func = starpu_matrix_filter_vertical_block,
  87. .nchildren = PARTS
  88. };
  89. starpu_data_partition_plan(matrix_handle, &f, subhandle_l1);
  90. for(i=0 ; i<PARTS ; i++)
  91. {
  92. starpu_data_partition_plan(subhandle_l1[i], &f2, subhandle_l2[i]);
  93. for(j=0 ; j<PARTS ; j++)
  94. {
  95. starpu_data_partition_plan(subhandle_l2[i][j], &f, subhandle_l3[i][j]);
  96. }
  97. }
  98. /* Submit a task on the first sub-matrix and sub-sub matrix, and on all others sub-sub-matrices */
  99. ret = starpu_task_insert(&cl,
  100. STARPU_RW, subhandle_l1[0],
  101. STARPU_VALUE, &factor, sizeof(factor),
  102. 0);
  103. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  104. for (i=1; i<PARTS; i++)
  105. {
  106. ret = starpu_task_insert(&cl,
  107. STARPU_RW, subhandle_l2[i][0],
  108. STARPU_VALUE, &factor, sizeof(factor),
  109. 0);
  110. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  111. for (j=1; j<PARTS; j++)
  112. {
  113. for (k=0; k<PARTS; k++)
  114. {
  115. ret = starpu_task_insert(&cl,
  116. STARPU_RW, subhandle_l3[i][j][k],
  117. STARPU_VALUE, &factor, sizeof(factor),
  118. 0);
  119. STARPU_CHECK_RETURN_VALUE(ret, "starpu_task_insert");
  120. }
  121. }
  122. }
  123. for(i=0 ; i<PARTS ; i++)
  124. {
  125. for(j=0 ; j<PARTS ; j++)
  126. {
  127. starpu_data_partition_clean(subhandle_l2[i][j], PARTS, subhandle_l3[i][j]);
  128. }
  129. starpu_data_partition_clean(subhandle_l1[i], PARTS, subhandle_l2[i]);
  130. }
  131. starpu_data_partition_clean(matrix_handle, PARTS, subhandle_l1);
  132. starpu_data_unregister(matrix_handle);
  133. /* Print result matrix */
  134. n=1;
  135. for(j=0 ; j<NY ; j++)
  136. {
  137. for(i=0 ; i<NX ; i++)
  138. {
  139. if (matrix[(j*LD)+i] != (int) n*12)
  140. {
  141. FPRINTF(stderr, "Incorrect result %4d != %4d", matrix[(j*LD)+i], n*12);
  142. ret=1;
  143. }
  144. n++;
  145. }
  146. }
  147. free(matrix);
  148. starpu_shutdown();
  149. return ret;
  150. }