0
votes

So what i want to do is print the input string "HELO" as "HEELLLOOOO" So far, i have come up with this code

#include <mpi.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
    int i=0,rank,size;
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&size);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    char buf[20]="HELO",a[50],b[50];
    //int len = strlen(buf);
    MPI_Scatter(buf,1,MPI_CHAR,a,1,MPI_CHAR,0,MPI_COMM_WORLD);

    while(i<rank)
    {   
        a[i+1]=a[i];
        i++;
    }

    MPI_Gather(a,rank+1,MPI_CHAR,b,rank+1,MPI_CHAR,0,MPI_COMM_WORLD);

    if(rank==0)
    {
        printf("%s\n",b );
    }

    MPI_Finalize();
    return 0;
}

When i try and run this with 4 processes, i get the error :

Fatal error in PMPI_Gather: Message truncated, error stack: PMPI_Gather(904)........................: MPI_Gather(sbuf=0x7fffa92bac30, scount=3, MPI_CHAR, rbuf=0x7fffa92bac10, rcount=3, MPI_CHAR, root=0, MPI_COMM_WORLD) failed MPIR_Gather_impl(726)...................: MPIR_Gather(686)........................: MPIR_Gather_intra(294)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 3 and tag 3 truncated; 4 bytes received but buffer size is 3 Fatal error in PMPI_Gather: Message truncated, error stack: PMPI_Gather(904)........................: MPI_Gather(sbuf=0x7ffd15c31980, scount=1, MPI_CHAR, rbuf=0x7ffd15c31960, rcount=1, MPI_CHAR, root=0, MPI_COMM_WORLD) failed MPIR_Gather_impl(726)...................: MPIR_Gather(686)........................: MPIR_Gather_intra(230)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 1 and tag 3 truncated; 2 bytes received but buffer size is 1 MPIR_Gather_intra(230)..................: MPIDI_CH3_PktHandler_EagerShortSend(363): Message from rank 2 and tag 1073741827 truncated; 6 bytes received but buffer size is 2

Can someone tell me where am i going wrong?

1

1 Answers

0
votes

You cannot use MPI_Gather() here. Since you use MPI_CHAR as both send and receive datatype, sendcount and recvcount must be the same on all ranks.

MPI_Gatherv() is what you are looking for.

Last but not least, do not forget to NULL terminate b before invoking printf()