Search code examples
copensslmpibroadcast

MPi multiple broadcasting BIGNUM


I have the following piece of code:

int MPIbcastBigNum(BIGNUM *num, int rank, char* purpoce){
    int size = BN_num_bytes(num);
    unsigned char *message = OPENSSL_malloc(size);

    if(!BN_bn2bin(num, message)) {
     fprintf(stderr, "RANK %d: Fail to allocate an array for key bytes \"%s\" \n", rank, purpoce);
     fflush(stderr);
     OPENSSL_free(message);
     return -1;    
    }

    //Do the actual Broadcast && Debug
    printf("RANK %d: Broaccasting bignum for purpoce \"%s\" \n", rank, purpoce);
    fflush(stdout);
    int value = MPI_Bcast(message, size, MPI_BYTE, rank, MPI_COMM_WORLD);
    switch(value) {
        case MPI_ERR_COMM:
         fprintf(stderr, "RANK %d: COMMUNICATIPN ERROR on BIGNUM sending for purpoce \"%s\" \n", rank, purpoce);
         fflush(stderr);
         OPENSSL_free(message);
         return -1;

        case MPI_ERR_COUNT:
         fprintf(stderr, "RANK %d: Invalid Size Count on BIGNUM sending for purpoce \"%s\" \n", rank, purpoce);
         fflush(stderr);
         OPENSSL_free(message);
         return -1;

        case MPI_ERR_TYPE:
         fprintf(stderr, "RANK %d: Invalid Data Type on BIGNUM sending for purpoce \"%s\" \n", rank, purpoce);
         fflush(stderr);
         OPENSSL_free(message);
         return -1;

        case MPI_ERR_BUFFER:
         fprintf(stderr, "RANK %d: Invalid Buffer on BIGNUM sending for purpoce \"%s\" \n", rank, purpoce);
         fflush(stderr);
         OPENSSL_free(message);
         return -1;

        default:
          printf("RANK %d: ALL OK on BIGNUM sending on purpoce \"%s\" \n", rank, purpoce);
          fflush(stdout);
    }

    //Cleanups
    OPENSSL_free(message);
    return 0;
}

What I try to do is to send a OpenSSL BIGNUM via MPI. But my issue is that each BIGNUM may not have the same size. So I keed to know to notify the receiving processes the lenngth of my data.

So far what I thought is to broadcast the size first as MPI_INT and then Broadcast the actual data as n-sized MPI_BYTE array. But how these will not get out of order when I will try to receive both the size and the BigNum?


Solution

  • Broadcasting the size first and then the data is the right thing to do. The ordering is not an issue in this case. Collective communications cannot overtake each other. Even point to point messages guarantees that messages are non-overtaking for a pair of senders and receivers.