c - MPI user defined type for parts of three different matrices -
i doing particle simulation, , need send part of 3 different arrays other processes. how use mpi user defined types this?
for example, suppose, have 3 matrixes datatype being double, a, b, , c on process 1. want send first 2 rows of a, b, , c process 2. how use mpi user defined type this, assuming c type storage these matrices? thank you.
currently, copying first 2 rows of these matrices single buffer, , perform mpi send. these involves following steps:
copy first 2 rows of a, b, , c send_buffer on process 1. send send_buffer process 1 process 2. on process 2, use recv_buffer receive data process 1. on process 2, copy data recv_buffer a, b, c on process 2. i hope there better way this. thanks.
in code below, mpi data type defined communicate range of rows of matrix. if there 3 matrices there 3 send/receive. can compare following code own code see 1 better.
if think transferring matrices 1 one not efficient might put matrices inside struct , make mpi data type or consider using mpi_pack.
#include <mpi.h> #include <stdio.h> #include <stdlib.h> void make_layout(int row_begin, int row_end, int ncol, mpi_datatype* mpi_dtype) { int nblock = 1; int block_count = (row_end - row_begin + 1) * ncol; mpi_aint lb, extent; mpi_type_get_extent(mpi_double, &lb, &extent); mpi_aint offset = row_begin * ncol * extent; mpi_datatype block_type = mpi_double; mpi_type_create_struct(nblock, &block_count, &offset, &block_type, mpi_dtype); mpi_type_commit(mpi_dtype); } double** allocate(int nrow, int ncol) { double *data = (double *)malloc(nrow*ncol*sizeof(double)); double **array= (double **)malloc(nrow*sizeof(double*)); (int i=0; i<nrow; i++) array[i] = &(data[ncol*i]); return array; } int main() { mpi_init(null, null); int rank; mpi_comm_rank(mpi_comm_world, &rank); // make 3x3 matrix. int nrow = 3; int ncol = 3; double** = allocate(nrow, ncol); // make mpi datatype send rows [0, 1] excluding row 2. // can send range of rows i.e. rows [row_begin, row_end]. int row_begin = 0; int row_end = 1; // inclusive. mpi_datatype mpi_dtype; make_layout(row_begin, row_end, ncol, &mpi_dtype); if (rank == 0) mpi_send(&(a[0][0]), 1, mpi_dtype, 1, 0, mpi_comm_world); else mpi_recv(&(a[0][0]), 1, mpi_dtype, 0, 0, mpi_comm_world, mpi_status_ignore); mpi_type_free(&mpi_dtype); mpi_finalize(); return 0; }
Comments
Post a Comment