frecursive.c 4.5 KB

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