#include #include #include #include typedef struct buffers_s { double *buf1; double *buf2; int size; } Buffers; void BuffersMPI(Buffers *buf, MPI_Datatype *bufmpi) { int block_lengths[2]; // # of elt. in each block MPI_Aint displacements[2]; // displac. MPI_Datatype typelist[2]; // list of types MPI_Aint start_address, address; // use for calculating displac. MPI_Datatype myType; block_lengths[0] = buf->size; block_lengths[1] = buf->size; typelist[0] = MPI_DOUBLE; typelist[1] = MPI_DOUBLE; displacements[0] = 0; MPI_Address(buf->buf1, &start_address); MPI_Address(buf->buf2, &address); displacements[1] = address - start_address; MPI_Type_struct(2,block_lengths, displacements,typelist,bufmpi); MPI_Type_commit(bufmpi); } void buffersmpisum(Buffers *in, Buffers *out, int *len, MPI_Datatype *typeptr) { int i; for (i=0; i < *len; i++) { out->buf1[i] += in->buf1[i]; out->buf2[i] += in->buf2[i]; } } int main(int argc, char **argv) { Buffers buffers_1, buffers_2; double *pack, *unpack; double size; double start, end; int pos; int i; int rank; int cnt; int ierr; MPI_Datatype Buffers_mpi; MPI_Op buffersumop; MPI_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); cnt = 100000; size = 10; buffers_1.buf1 = calloc(size, sizeof *buffers_1.buf1); buffers_1.buf2 = calloc(size, sizeof *buffers_1.buf2); buffers_2.buf1 = calloc(size, sizeof *buffers_2.buf1); buffers_2.buf2 = calloc(size, sizeof *buffers_2.buf2); pack = calloc(3*size, sizeof *pack); unpack = calloc(3*size, sizeof *unpack); for (i=0; i