Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <mpi.h>
- #include <math.h>
- #include <string.h>
- #include <unistd.h>
- double **allocMatrix(int dim) {
- int i;
- double **matrix;
- matrix = (double **)malloc(dim*sizeof(double *));
- for(i=0; i < dim; i++) {
- matrix[i] = (double *)malloc(dim*sizeof(double));
- }
- return matrix;
- }
- void printMatrix(double **values, int size) {
- int i, j;
- for (i = 0; i < size; i++) {
- for (j = 0; j < size; j++) {
- printf("%10lf ", values[i][j]);
- }
- printf("n");
- }
- }
- int main(int argc, char* argv[]) {
- MPI_Init(&argc, &argv);
- int size, rank, i, j;
- int dimensions = 7;
- MPI_Comm_size(MPI_COMM_WORLD, &size);//number of processes
- MPI_Comm_rank(MPI_COMM_WORLD, &rank);//rank for each process
- double **send = allocMatrix(dimensions);
- double **recv = allocMatrix(dimensions);
- int count = 0;
- for (i=0; i<dimensions; i++) {
- for (j=0; j<dimensions; j++) {
- if (rank == 0) {
- recv[i][j] = 0;
- } else {
- send[i][j] = ++count;
- }
- }
- }
- MPI_Datatype arrType;
- MPI_Type_vector(1, dimensions, 0, MPI_DOUBLE, &arrType);
- MPI_Type_commit(&arrType);
- int recvCounts[size];
- int displs[size];
- recvCounts[0] = 0;
- displs[0] = 0;
- recvCounts[1] = 1;
- displs[1] = 0;
- MPI_Gatherv(&(send[0][0]), 1, arrType,
- &(recv[0][0]), recvCounts, displs, arrType,
- 0, MPI_COMM_WORLD);
- if (rank == 0) {
- printMatrix(recv, dimensions);
- }
- MPI_Finalize();
- return 0;
- }
- make gatherv
- mpicc -Wall -o gatherv gatherv.c && ./gather
- 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000
- 8.000000 9.000000 10.00000 11.00000 12.00000 13.00000 14.00000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
- 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement