123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- #include <stdlib.h>
- #include <stdio.h>
- #include <assert.h>
- #include <math.h>
- #include <starpu.h>
- #include <starpu_mpi.h>
- #include "helper.h"
- #define VERBOSE 0
- static int N = 16;
- static int BS = 4;
- #define NB ((N)/(BS))
- static double *A = NULL;
- static double *B = NULL;
- static double *C = NULL;
- static starpu_data_handle_t *A_h;
- static starpu_data_handle_t *B_h;
- static starpu_data_handle_t *C_h;
- static int comm_rank;
- static int comm_size;
- static void alloc_matrices(void)
- {
-
- starpu_malloc((void **)&A, N*N*sizeof(double));
- starpu_malloc((void **)&B, N*N*sizeof(double));
- starpu_malloc((void **)&C, N*N*sizeof(double));
- }
- static void free_matrices(void)
- {
- starpu_free(A);
- starpu_free(B);
- starpu_free(C);
- }
- static void init_matrices(void)
- {
- int row,col;
- for (row = 0; row < N; row++)
- {
- for (col = 0; col < N; col++)
- {
- A[row*N+col] = (row==col)?2:0;
- B[row*N+col] = row*N+col;
- C[row*N+col] = 0;
- }
- }
- }
- #if VERBOSE
- static void disp_matrix(double *m)
- {
- int row,col;
- for (row = 0; row < N; row++)
- {
- for (col = 0; col < N; col++)
- {
- printf("\t%.2lf", m[row*N+col]);
- }
- printf("\n");
- }
- }
- #endif
- static void check_result(void)
- {
- int row,col;
- for (row = 0; row < N; row++)
- {
- for (col = 0; col < N; col++)
- {
- if (fabs(C[row*N+col] - 2*(row*N+col)) > 1.0)
- {
- fprintf(stderr, "check failed\n");
- exit(1);
- }
- }
- }
- #if VERBOSE
- printf("success\n");
- #endif
- }
- static void register_matrices()
- {
- A_h = calloc(NB, sizeof(starpu_data_handle_t));
- B_h = calloc(NB, sizeof(starpu_data_handle_t));
- C_h = calloc(NB*NB, sizeof(starpu_data_handle_t));
-
- int mr = (comm_rank == 0) ? STARPU_MAIN_RAM : -1;
-
- int tag = 0;
- int b_row,b_col;
- for (b_row = 0; b_row < NB; b_row++)
- {
-
- starpu_matrix_data_register(&A_h[b_row],
- mr,
- (comm_rank == 0)?(uintptr_t)(A+b_row*BS*N):0, N, N, BS,
- sizeof(double));
-
- starpu_data_set_coordinates(A_h[b_row], 2, 0, b_row);
- starpu_mpi_data_register(A_h[b_row], tag++, 0);
- }
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_matrix_data_register(&B_h[b_col],
- mr,
- (comm_rank == 0)?(uintptr_t)(B+b_col*BS):0, N, BS, N,
- sizeof(double));
- starpu_data_set_coordinates(B_h[b_col], 2, b_col, 0);
- starpu_mpi_data_register(B_h[b_col], tag++, 0);
- }
- for (b_row = 0; b_row < NB; b_row++)
- {
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_matrix_data_register(&C_h[b_row*NB+b_col],
- mr,
- (comm_rank == 0)?(uintptr_t)(C+b_row*BS*N+b_col*BS):0, N, BS, BS,
- sizeof(double));
- starpu_data_set_coordinates(C_h[b_row*NB+b_col], 2, b_col, b_row);
- starpu_mpi_data_register(C_h[b_row*NB+b_col], tag++, 0);
- }
- }
- }
- static void distribute_matrix_C(void)
- {
- int b_row,b_col;
- for (b_row = 0; b_row < NB; b_row++)
- {
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_data_handle_t h = C_h[b_row*NB+b_col];
-
- int target_rank = (b_row+b_col)%comm_size;
-
- starpu_mpi_data_migrate(MPI_COMM_WORLD, h, target_rank);
- }
- }
- }
- static void undistribute_matrix_C(void)
- {
- int b_row,b_col;
- for (b_row = 0; b_row < NB; b_row++)
- {
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_data_handle_t h = C_h[b_row*NB+b_col];
- starpu_mpi_data_migrate(MPI_COMM_WORLD, h, 0);
- }
- }
- }
- static void unregister_matrices()
- {
- int b_row,b_col;
- for (b_row = 0; b_row < NB; b_row++)
- {
- starpu_data_unregister(A_h[b_row]);
- }
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_data_unregister(B_h[b_col]);
- }
- for (b_row = 0; b_row < NB; b_row++)
- {
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_data_unregister(C_h[b_row*NB+b_col]);
- }
- }
- free(A_h);
- free(B_h);
- free(C_h);
- }
- static void cpu_mult(void *handles[], STARPU_ATTRIBUTE_UNUSED void *arg)
- {
- double *block_A = (double *)STARPU_MATRIX_GET_PTR(handles[0]);
- double *block_B = (double *)STARPU_MATRIX_GET_PTR(handles[1]);
- double *block_C = (double *)STARPU_MATRIX_GET_PTR(handles[2]);
- unsigned n_col_A = STARPU_MATRIX_GET_NX(handles[0]);
- unsigned n_col_B = STARPU_MATRIX_GET_NX(handles[1]);
- unsigned n_col_C = STARPU_MATRIX_GET_NX(handles[2]);
- unsigned n_row_A = STARPU_MATRIX_GET_NY(handles[0]);
- unsigned n_row_B = STARPU_MATRIX_GET_NY(handles[1]);
- unsigned n_row_C = STARPU_MATRIX_GET_NY(handles[2]);
- unsigned ld_A = STARPU_MATRIX_GET_LD(handles[0]);
- unsigned ld_B = STARPU_MATRIX_GET_LD(handles[1]);
- unsigned ld_C = STARPU_MATRIX_GET_LD(handles[2]);
-
- assert(n_col_C == n_col_B);
- assert(n_row_C == n_row_A);
- assert(n_col_A == n_row_B);
- unsigned i,j,k;
- for (k = 0; k < n_row_C; k++)
- {
- for (j = 0; j < n_col_C; j++)
- {
- for (i = 0; i < n_col_A; i++)
- {
- block_C[k*ld_C+j] += block_A[k*ld_A+i] * block_B[i*ld_B+j];
- }
- #if VERBOSE
-
- block_C[k*ld_C+j] += comm_rank / 100.0;
- #endif
- }
- }
- }
- static struct starpu_codelet gemm_cl =
- {
- .cpu_funcs = {cpu_mult},
- .nbuffers = 3,
- .modes = {STARPU_R, STARPU_R, STARPU_RW}
- };
- int main(int argc, char *argv[])
- {
-
- int ret = starpu_init(NULL);
- STARPU_CHECK_RETURN_VALUE(ret, "starpu_init");
-
- ret = starpu_mpi_init(&argc, &argv, 1);
- STARPU_CHECK_RETURN_VALUE(ret, "starpu_mpi_init");
- if (starpu_cpu_worker_get_count() == 0)
- {
- FPRINTF(stderr, "We need at least 1 CPU worker.\n");
- starpu_mpi_shutdown();
- starpu_shutdown();
- return STARPU_TEST_SKIPPED;
- }
-
- if (argc > 1)
- {
- N = atoi(argv[1]);
- if (N < 1)
- {
- fprintf(stderr, "invalid matrix size\n");
- exit(1);
- }
- if (argc > 2)
- {
- BS = atoi(argv[2]);
- }
- if (BS < 1 || N % BS != 0)
- {
- fprintf(stderr, "invalid block size\n");
- exit(1);
- }
- }
-
- starpu_mpi_comm_rank(MPI_COMM_WORLD, &comm_rank);
- starpu_mpi_comm_size(MPI_COMM_WORLD, &comm_size);
- if (comm_rank == 0)
- {
- #if VERBOSE
- printf("N = %d\n", N);
- printf("BS = %d\n", BS);
- printf("NB = %d\n", NB);
- printf("comm_size = %d\n", comm_size);
- #endif
-
- alloc_matrices();
- init_matrices();
- }
-
- register_matrices();
-
- distribute_matrix_C();
- int b_row,b_col;
- for (b_row = 0; b_row < NB; b_row++)
- {
- for (b_col = 0; b_col < NB; b_col++)
- {
- starpu_mpi_task_insert(MPI_COMM_WORLD, &gemm_cl,
- STARPU_R, A_h[b_row],
- STARPU_R, B_h[b_col],
- STARPU_RW, C_h[b_row*NB+b_col],
- 0);
- }
- }
- starpu_task_wait_for_all();
- undistribute_matrix_C();
- unregister_matrices();
- if (comm_rank == 0)
- {
- #if VERBOSE
- disp_matrix(C);
- #endif
- check_result();
- free_matrices();
- }
- starpu_mpi_shutdown();
- starpu_shutdown();
- return 0;
- }
|