Skip to content
Snippets Groups Projects
cgather.cpp 843 B
Newer Older
  • Learn to ignore specific revisions
  • Ondrej Meca's avatar
    Ondrej Meca committed
    
    #include "mpi.h"
    #include <vector>
    
    int main(int argc, char **argv) {
    	MPI_Init(&argc, &argv);
    
    	int rank, size;
    	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    	MPI_Comm_size(MPI_COMM_WORLD, &size);
    
    	std::vector<int> data = { rank };
    	std::vector<int> gather(size);
    
    //	if (rank == 0) {
    //		data.resize(size);
    //		for (int i = 1; i < size; ++i) {
    //			MPI_Recv(data.data() + i, 1, MPI_DOUBLE, i, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
    //		}
    //	} else {
    //		MPI_Ssend(data.data(), 1, MPI_INT, 0, 0, MPI_COMM_WORLD);
    //	}
    	int ROOT = 0;
    	int SEND_COUNT = 1; // per process
    	int RECV_COUNT = 1; // per process
    	MPI_Gather(data.data(), SEND_COUNT, MPI_INT, gather.data(), RECV_COUNT, MPI_INT, ROOT, MPI_COMM_WORLD);
    
    	if (rank == 0) {
    		for (int i = 0; i < size; ++i) {
    			printf("%d ", gather[i]);
    		}
    		printf("\n");
    	}
    
    	MPI_Finalize();
    	return 0;
    }