1
votes

I know that in C arithmetic of void pointers is not allowed. However I must find a solution to deal with this issue.

I have a function that sends the first half of an array to rank 0 and the second half to rank 1:

void send_expand_multiple(void * data, MPI_Datatype datatype, int size) {
    int my_rank, comm_size, intercomm_size, localSize, factor, dst, iniPart, iniPartBytes, i;
    int datatype_size;
    MPI_Type_size(datatype, &datatype_size);

    int local_size = size / 2;

    //Sending the first half
    MPI_Send(data, local_size, datatype, dst, 0, comm);

    //Sending the second half
    int iniPart = local_size;
    int iniPartBytes = iniPart * datatype_size;
    MPI_Send(((char *) data) + iniPartBytes, local_size, datatype, dst, 1, comm);
}

My solution is based on the principle of "serializing". For the first half, there is no problem, but for the second I have parsed the buffer and moved the pointer iniPartBytes by adding that value.

Finally, the count and the datatype configure the MPI_Send in order to send local_size elements of datatype type.

Is my approach correct?

1
int local_size = size / 2; can loose 1 byte in case of odd valueLPs
This is a simplification of the code, so let's assume that it cannot be odd. Thank you.Bub Espinja
@EugeneSh. You're right. My fault, sorry. Now is edited.Bub Espinja
You did state a problem so it is OT on SO. You can have more luck on codereview.stackexchange.com/LPs
@LPs that is what I though, but a user from there, redirected me...Bub Espinja

1 Answers

1
votes

The two areas I see trouble:

Pointer calculation iniPart * datatype_size may overflow. Suggest use C99 pointer math.

size may be odd - although OP dismisses this, a fix is easy.

void send_expand_multiple(const void * data, MPI_Datatype datatype, int element_count) {
    int dst = tbd();
    MPI_Comm comm = tbd();

    int datatype_size;
    MPI_Type_size(datatype, &datatype_size);
    int first_half_count = element_count / 2;
    int second_half_count = element_count  - first_half_count;

    //Sending the first half
    MPI_Send(data, first_half_count, datatype, dst, 0, comm);

    //Sending the second half
    const char (*first_half)[first_half_count][datatype_size] = data;
    MPI_Send(first_half + 1, second_half_count, datatype, dst, 1, comm);

    // or
    const char (*first_half)[datatype_size] = data;
    MPI_Send(first_half + first_half_count, second_half_count, datatype, dst, 1, comm);
}