Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mpi.h>
- #include <vector>
- using namespace std;
- typedef struct three_args_struct {
- int first;
- int second;
- int third;
- } triplet;
- void buildDerivedType(MPI_Datatype* mpi_triplet_type) {
- int nitems = 3;
- int block_lengths[3] = { 1, 1, 1 };
- MPI_Aint offsets[3];
- MPI_Datatype typelist[3] = { MPI_INT, MPI_INT, MPI_INT };
- offsets[0] = offsetof(triplet, first);
- offsets[1] = offsetof(triplet, second);
- offsets[2] = offsetof(triplet, third);
- MPI_Type_create_struct(nitems, block_lengths, offsets, typelist, mpi_triplet_type);
- MPI_Type_commit(mpi_triplet_type);
- }
- void initializeTriplet(triplet* triplet, int rank) {
- triplet->first = rank;
- triplet->second = rank * rank;
- triplet->third = rank * rank * rank;
- }
- int main(int argc, char* argv[]) {
- MPI_Init(&argc, &argv);
- double startTime = MPI_Wtime();
- int rank;
- int threads_count;
- MPI_Comm_size(MPI_COMM_WORLD, &threads_count);
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);
- int side_threads_count = threads_count - 1;
- MPI_Comm newcomm;
- int color;
- color = rank % 3;
- MPI_Comm_split(MPI_COMM_WORLD, color, rank, &newcomm);
- int newsize;
- MPI_Comm_size(newcomm, &newsize);
- //cout << "newSize = " << newsize << endl;
- if (rank % 3 == 0) {
- triplet sendBuf;
- initializeTriplet(&sendBuf, rank);
- cout << rank << ". first = " << sendBuf.first << ", second = " << sendBuf.second << ", third = " << sendBuf.third << endl;
- MPI_Datatype* mpi_newtype = new MPI_Datatype;
- buildDerivedType(mpi_newtype);
- triplet* recvBuf = new triplet[newsize];
- MPI_Gather(&sendBuf, 1, *mpi_newtype, recvBuf, newsize, *mpi_newtype, 0, newcomm);
- if (rank == 0) {
- cout << "Printing received array: " << endl;
- for (int i = 0; i < newsize; i++) {
- cout << i * 3 << " structure: first = " << recvBuf[i].first << ", second = " << recvBuf[i].second << ", third = " << recvBuf[i].third << endl;
- }
- }
- }
- double endTime = MPI_Wtime();
- double elapsed = endTime - startTime;
- //cout << "Program runtime: " << elapsed << endl;
- MPI_Finalize();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement