#include #include #include #include using namespace std; /******************************/ /* function to create my struct */ void // JMS: Need to pass references to the variables, not the pointers Build_derived_type(double &a, double &b, int &n, MPI_Datatype * mesg_mpi_t_ptr) { //start Build_derived_type int block_lengths[3]; MPI_Aint displacements[3]; MPI_Datatype typelist[3]; MPI_Aint start_address; MPI_Aint address; block_lengths[0] = block_lengths[1] = 10; block_lengths[2] = 1; typelist[0] = MPI_DOUBLE; typelist[1] = MPI_DOUBLE; typelist[2] = MPI_INT; displacements[0] = 0; // JMS: MPI_Address(&a, &start_address); MPI_Address(&b, &address); displacements[1] = address - start_address; MPI_Address(&n, &address); displacements[2] = address - start_address; MPI_Type_struct(3, block_lengths, displacements, typelist, mesg_mpi_t_ptr); MPI_Type_commit(mesg_mpi_t_ptr); } //end Build_derived_type /********************************/ int main(int argc, char *argv[]) { //start main int numprocs, myrank, namelen, i, n; // double *A,* B; MPI_Datatype mytype; MPI_Request Reqs; char processor_name[MPI_MAX_PROCESSOR_NAME]; char greeting[MPI_MAX_PROCESSOR_NAME + 80]; MPI_Status status; MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD, &numprocs); MPI_Comm_rank(MPI_COMM_WORLD, &myrank); MPI_Get_processor_name(processor_name, &namelen); /* pointer to an array */ double *A = new double[10]; double *B = new double[10]; // JMS: Pass the variables themselves, not pointers to them -- so // then the reference is taken. Build_derived_type(A[0], B[0], myrank, &mytype); if (myrank == 0) { for (i = 0; i < 10; i++) { A[i] = (double) i; B[i] = (double) i + 10; } for (i = 0; i < 10; i++) cout << "A [ " << i << " ] = " << A[i] << " **** B [ " << i << " ]= " << B[i] << endl; // JMS: The count should be "1" MPI_Isend(A, 1, mytype, 1, 1, MPI_COMM_WORLD, &Reqs); MPI_Wait(&Reqs, &status); } else if (myrank == 1) { // JMS: The count should be "1" MPI_Irecv(A, 1, mytype, 0, 1, MPI_COMM_WORLD, &Reqs); MPI_Wait(&Reqs, &status); for (i = 0; i < 10; i++) cout << "A [ " << i << " ] = " << A[i] << " **** B [ " << i << " ]= " << B[i] << endl; } MPI_Finalize(); return (0); } //end main