Home > Mobile >  MPI Scatter error on communicator MPI_COMM_WORLD
MPI Scatter error on communicator MPI_COMM_WORLD

Time:11-24

The following code fails during runtime because of the MPI Scatter Error which I am not able to fix. When following documentation and other similar error pages, I didn't see any issue. Please help. I am using openmpi/4.0.5-gcc.

#include <stdio.h>
#include <stdlib.h>
#include "mpi.h"

#define UPPER_LIMIT 4
#define master 0
int main(int argc, char *argv[])
{
  int *data;
  int process_id, total_process, temp_result;
  int i, tag, final_result;
  
  MPI_Init(&argc, &argv);
  MPI_Comm_rank(MPI_COMM_WORLD, &process_id);
  MPI_Comm_size(MPI_COMM_WORLD, &total_process);
  MPI_Status status;

  if ( total_process > UPPER_LIMIT )
  {
        if ( process_id == 0 )
          printf("max allowed processes limit [%d] exceeded.\n", UPPER_LIMIT);
        exit(0);
  }

  final_result = 0;
  
  for ( i = 0; i < total_process; i  ){
      data[i] = (int)i;
  }
    int j;
    for(j = 0; j < total_process; j  ) {
        printf("%d ", data[j]);
        if(j==total_process-1)
            printf("*** %d\n", process_id);
    }
    
    MPI_Scatter(data, total_process, MPI_INT, &temp_result, 1, MPI_INT, 0, MPI_COMM_WORLD);
    if(process_id!=master){
        temp_result = temp_result/process_id;
        MPI_Reduce(&temp_result, &final_result, 1, MPI_INT, MPI_SUM, 1, MPI_COMM_WORLD);  
    }
    
    if(final_result>0){
        tag = process_id;
        MPI_Send(&final_result, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);
    }
    
    if(process_id==master){
        MPI_Recv(&final_result, 1, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &status);
    }
    
  MPI_Finalize();

  return 0;
}

Error Log

0 1 2 3 *** 0
0 1 2 3 *** 1
0 1 2 3 *** 2
0 1 2 3 *** 3
 *** An error occurred in MPI_Scatter
 *** reported by process [1855324161,0]
 *** on communicator MPI_COMM_WORLD
 *** MPI_ERR_TRUNCATE: message truncated
 *** MPI_ERRORS_ARE_FATAL (processes in this communicator will now abort,
 ***    and potentially your MPI job)

MPI_Scatter Documentation

MPI_Scatter Sends data from one process to all other processes in a communicator

Synopsis int MPI_Scatter(const void *sendbuf, int sendcount, MPI_Datatype sendtype, void *recvbuf, int recvcount, MPI_Datatype recvtype, int root, MPI_Comm comm)

Input Parameters

sendbuf - address of send buffer (choice, significant only at root)

sendcount - number of elements sent to each process (integer, significant only at root)

sendtype - data type of send buffer elements (significant only at root) (handle)

recvcount - number of elements in receive buffer (integer)

recvtype - data type of receive buffer elements (handle)

root - rank of sending process (integer)

comm - communicator (handle)

Output Parameters

recvbuf - address of receive buffer (choice)


Update

After updating the MPI_Scatter send count to 1, The above-mentioned error goes away but the program stays ideal and it didn't print anything that was placed after the MPI_Scatter line.

MPI_Scatter(data, 1, MPI_INT, &temp_result, 1, MPI_INT, 0, MPI_COMM_WORLD);

CodePudding user response:

You quote the relevant line: "sendcount - number of elements sent to each process (integer". So if you send 1 element to each process, you need to set the sendcount to 1, not total_process.

  • Related