insert_task_block.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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_mpi.h>
  17. #include <math.h>
  18. void func_cpu(void *descr[], __attribute__ ((unused)) void *_args)
  19. {
  20. unsigned *matrix = (unsigned *)STARPU_MATRIX_GET_PTR(descr[0]);
  21. int nx = (int)STARPU_MATRIX_GET_NX(descr[0]);
  22. int ny = (int)STARPU_MATRIX_GET_NY(descr[0]);
  23. int ld = (int)STARPU_MATRIX_GET_LD(descr[0]);
  24. int i, j;
  25. unsigned sum=0;
  26. for (i = 0; i < nx; i++) {
  27. for (j = 0; j < ny; j++) {
  28. sum += matrix[i+j*ld];
  29. }
  30. }
  31. for (i = 0; i < nx; i++) {
  32. for (j = 0; j < ny; j++) {
  33. matrix[i+j*ld] = sum;///(nx*ny);
  34. }
  35. }
  36. }
  37. starpu_codelet mycodelet = {
  38. .where = STARPU_CPU,
  39. .cpu_func = func_cpu,
  40. .nbuffers = 1
  41. };
  42. #define SIZE 6
  43. #define BLOCKS 3
  44. /* Returns the MPI node number where data indexes index is */
  45. int my_distrib(int x, int y, int nb_nodes) {
  46. return x % nb_nodes;
  47. }
  48. int main(int argc, char **argv)
  49. {
  50. int rank, size, x, y, loop;
  51. int value=0;
  52. unsigned matrix[SIZE*SIZE];
  53. starpu_data_handle data_handles[SIZE][SIZE];
  54. starpu_init(NULL);
  55. starpu_mpi_initialize_extended(1, &rank, &size);
  56. for(x = 0; x < SIZE; x++) {
  57. for (y = 0; y < SIZE; y++) {
  58. matrix[x+y*SIZE] = rank*100 + value;
  59. value++;
  60. }
  61. }
  62. #if 1
  63. for(x = 0; x < SIZE; x++) {
  64. fprintf(stdout, "[%d] ", rank);
  65. for (y = 0; y < SIZE; y++) {
  66. fprintf(stdout, "%3d ", matrix[x+y*SIZE]);
  67. }
  68. fprintf(stdout, "\n");
  69. }
  70. #endif
  71. for(x = 0; x < BLOCKS ; x++) {
  72. for (y = 0; y < BLOCKS; y++) {
  73. int mpi_rank = my_distrib(x, y, size);
  74. if (mpi_rank == rank) {
  75. //fprintf(stderr, "[%d] Owning data[%d][%d]\n", rank, x, y);
  76. starpu_matrix_data_register(&data_handles[x][y], 0, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  77. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  78. }
  79. else if (rank == mpi_rank+1 || rank == mpi_rank-1) {
  80. /* I don't own that index, but will need it for my computations */
  81. //fprintf(stderr, "[%d] Neighbour of data[%d][%d]\n", rank, x, y);
  82. starpu_matrix_data_register(&data_handles[x][y], -1, (uintptr_t)&(matrix[((SIZE/BLOCKS)*x) + ((SIZE/BLOCKS)*y) * SIZE]),
  83. SIZE, SIZE/BLOCKS, SIZE/BLOCKS, sizeof(unsigned));
  84. }
  85. else {
  86. /* I know it's useless to allocate anything for this */
  87. data_handles[x][y] = NULL;
  88. }
  89. if (data_handles[x][y])
  90. starpu_data_set_rank(data_handles[x][y], mpi_rank);
  91. }
  92. }
  93. for(x = 0; x < BLOCKS; x++) {
  94. for (y = 0; y < BLOCKS; y++) {
  95. starpu_mpi_insert_task(MPI_COMM_WORLD, &mycodelet,
  96. STARPU_RW, data_handles[x][y],
  97. 0);
  98. }
  99. }
  100. fprintf(stderr, "Waiting ...\n");
  101. starpu_task_wait_for_all();
  102. for(x = 0; x < BLOCKS; x++) {
  103. for (y = 0; y < BLOCKS; y++) {
  104. if (!data_handles[x][y])
  105. starpu_data_release(data_handles[x][y]);
  106. }
  107. }
  108. starpu_mpi_shutdown();
  109. starpu_shutdown();
  110. #if 1
  111. for(x = 0; x < SIZE; x++) {
  112. fprintf(stdout, "[%d] ", rank);
  113. for (y = 0; y < SIZE; y++) {
  114. fprintf(stdout, "%3d ", matrix[x+y*SIZE]);
  115. }
  116. fprintf(stdout, "\n");
  117. }
  118. #endif
  119. return 0;
  120. }