Hi all
I am using MPI and c programming. i want to collect strings from processors to root processor
now i have writen the code as below but does not display the strings. Can anyone help me with this?
The code is as below:
Code:
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main(int argc, char *argv[]){
int numProcs;
int myRank, a, *data, i;
char **DATA;
char *b;
//initialize MPI
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myRank);
MPI_Comm_size(MPI_COMM_WORLD, &numProcs);
// do some computation and MPI communications
a = 1000+myRank;
b = "sdfsd";
if (myRank == 1){
data = (int *) malloc(numProcs*sizeof(int));
DATA = (char **) calloc(numProcs, sizeof(char **));
for (i=0;i<numProcs;i++){
data[i] = 0;
DATA[i] = "helloP";
}
printf("\n contents of data array before gather:\n");
for (i=0;i<numProcs;i++)
printf("%s\n", DATA[i]);
}
MPI_Gather(&b, 5, MPI_CHAR, DATA, 5, MPI_CHAR,1, MPI_COMM_WORLD);
printf("here are contents %s\n",DATA[1]);
if (myRank == 1){
printf("\n contents of data array after gather:\n");
for (i=0;i<numProcs;i++){
printf("%s\n", DATA[i]);
/*printf("2. DATA[%d] = %s \n",i, DATA[i]);*/}}
//shutdown MPI
MPI_Finalize();
return 0;
}