Question

So ,i implemented my own mpi library(a simplified version) and I need to send/receive between processes some data. MPI_Send looks like this(void *buf,int count,datatype data,etc...). SO this means i need to send count elements of type data(char,double or int) pointed at the address by buf. I need to send em through a message queue(mq). MPI_Recv takes about the same params. At the moment here is what i do in Send and Recv:

    //Sender part of code
    ret=mq_send(mq,buf,sizeof(buf),0);
        if(ret < 0)
    return MPI_ERR_IO;
    //Receiver part of code
    ret = mq_receive(mq, buf, MSGSIZE, NULL );
    if(ret < 0)
    return MPI_ERR_IO;

Right now i'm only receiving the first element of the array. How would i go about sending the whole thing ? Here is what i have in mind

    //Sender part of pseudocode
    for(i=0,count)
       element=(cast to datatype)buf[i]; 
       mq_send(mq,element,sizeof,0);
    //Receiver part of pseudocode
    //i receive the count number of elements prior to this message
    for(i=0,count)
       mq_receive(mq,local_variable,etc...)
       somehow store them into my void *buf which i receive as an argument ??

If something isn't clear enough , i'll reply

Was it helpful?

Solution

You specify the amount of data you are putting in the queue with the third argument of mq_send. In your case it's:

ret=mq_send(mq,buf,sizeof(buf),0);

Assuming that buf is initialized somewhere along the lines of

float f[2];
void *buf = f;

Then the expression sizeof(buf) means: the size of the pointer called "buf". While it may work on some architectures, the proper way would be

ret=mq_send(mq,buf,sizeof(float) * <number of elements>, 0);

which means, the size of a float, multiplied by the amount of floats stored in the array.

In which case you would put the whole array in the queue. You would also avoid the iteration and use only a constant number of messages, instead of a linear number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top