Hello
I've got a problem with sporadic errors coming from time to time at my program.

I basically have to write an own implemetation of the function MPI_Bcast() using only MPI_Send() and MPI_Recv(). It shoud have logarithmic running time, so the root mustn't have to do all the sending work.
I realized it about this way:

Problem with Open-MPI-broadcast_tree-png


Here is the code:
Code:
#include <stdio.h> 
#include <string.h> 
#include <time.h> 
#include <math.h> 
#include <string.h> 
#include "mpi.h" 
  
//At compiling: add -lm at the end for log2() to work 
int MPI_MyBcast(void * message, int count, MPI_Datatype type, int root, MPI_Comm comm) { 
    int my_rank, new_rank, p, tag = 0; 
    MPI_Status status; 
  
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &p); 
  
    int exponent = 0; 
    int sender = 0; 
  
    if (my_rank != root) { 
        exponent = (int) log2((double) my_rank) + 1; 
        sender = my_rank - pow(2, (double) (exponent - 1)); 
    } 
  
    if (my_rank != root) { 
        //printf("Process %i: Waiting for message from %i\n", my_rank, sender); 
        MPI_Recv(message, count, type, sender, 1, comm, &status); 
  
        int k; 
        int receiver = my_rank + (int) pow(2, exponent); 
        for (k = receiver; k < p; k += (int) pow(2, exponent++)) { 
            //printf("Process %i: Sending message to %i\n", my_rank, k); 
            MPI_Send(message, strlen(message)+1, type, k, 1, comm); 
        } 
    } 
    else { 
        int k; 
        int receiver = my_rank + (int) pow(2, exponent); 
        for (k = receiver; k < p; k += (int) pow(2, exponent++)) { 
            //printf("Process %i: Sending message to %i\n", my_rank, k); 
            MPI_Send(message, strlen(message)+1, type, k, 1, comm); 
        } 
    } 
  
    return 0; 
} 
  
int main (int argc, char * argv[]) { 
    const int root = 0;    //atoi(argv[1]); 
  
    int my_rank, p; 
    char msg[6]; 
  
    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &p); 
  
    if (my_rank == 0) { 
        strcpy(msg, "Hello"); 
    } 
  
    MPI_MyBcast(msg, strlen(msg)+1, MPI_CHAR, root, MPI_COMM_WORLD); 
  
  
    printf("Process %i: Got message: %s\n", my_rank, msg); 
  
  
    MPI_Finalize (); 
  
    return 0; 
}
From time to time I receive this error message:
[xyz-desktop:3800] *** An error occurred in MPI_Recv
[txyz-desktop:3800] *** on communicator MPI_COMM_WORLD
[xyz-desktop:3800] *** MPI_ERR_TRUNCATE: message truncated
[xyz-desktop:3800] *** MPI_ERRORS_ARE_FATAL: your MPI job will now abort

mpirun has exited due to process rank 3 with PID 3800 on
node xyz-desktop exiting improperly. There are two reasons this could occur:

mpirun has exited due to process rank 3 with PID 3800 on
node xyz-desktop exiting improperly. There are two reasons this could occur:

1. this process did not call "init" before exiting, but others in
the job did. This can cause a job to hang indefinitely while it waits
for all processes to call "init". By rule, if one process calls "init",
then ALL processes must call "init" prior to termination.

2. this process called "init", but exited without calling "finalize".
By rule, all processes that call "init" MUST call "finalize" prior to
exiting or it will be considered an "abnormal termination"

This may have caused other processes in the application to be
terminated by signals sent by mpirun (as reported here).
It has been compiled with "mpicc dateiname.c -lm" and executed with "mpirun -np 4 a.out", but I also varied the number of processes.

I also found out it always works when using only char[0] and not sending a string, and it happens far more often when using char[20] instead of char[6]. So I guess it has something to do with this?

I'd be very thankful if someone is an expert in MPI and has got an answer!