#include
#include
#include
int main(int argc,char **argv)
{
int N;
int *array;
int i=0;
int max_mpi=0,min_mpi;
int maxx=0,minn=99999999;
int rank,size;
MPI_Status status;
MPI_Comm comm=MPI_COMM_WORLD;
MPI_Init(&argc,&argv);
MPI_Comm_size(comm,&size);
MPI_Comm_rank(comm,&rank);
if(rank==0)
{
printf("please input the N :\n");
scanf("%d",&N);
array=(int*)malloc(sizeof(int)*N);
for(i=0;i<N;i++)
{
array[i]=0;
array[i]=rand()%100000;
}
for(i=1;i<size;i++)
{
MPI_Send(&N,1,MPI_INT,i,97,comm);
MPI_Send(&array,N,MPI_INT,i,97,comm);
}
for(i=1;i<size;i++)
{
MPI_Recv(&maxx,1,MPI_INT,i,96,comm,&status);
MPI_Recv(&minn,1,MPI_INT,i,96,comm,&status);
if(max_mpi<maxx)
max_mpi=maxx;
if(min_mpi>minn)
min_mpi=minn;
}
printf("❤❤❤❤❤❤❤❤❤❤❤❤❤\n");
printf("The maximum value of the array is(by t he mpi): %d \n",max_mpi);
printf("The minimum value of the array is(by the mpi): %d \n",min_mpi);
printf("❤❤❤❤❤❤❤❤❤❤❤❤❤\n");
}
else
{
int local_N;
MPI_Recv(&local_N,1,MPI_INT,0,97,comm,&status);
int *local_array;
local_array=(int*)malloc(sizeof(int)*local_N);
MPI_Recv(&local_array,N,MPI_INT,0,97,comm,&status);
int beg,end;
beg=(rank-1)*(local_N/(size-1));
end=rank*(local_N/(size-1));
for(i=beg;i<=end;i++)
{
if(maxx<local_array[i])
maxx=local_array[i];
if(minn>local_array[i])
minn=local_array[i];
}
printf("in process [%d] maxx is (%d),minn is (%d), begin from %d end in %d\n", rank, maxx,minn, beg,end);
MPI_Send(&maxx,1,MPI_INT,0,96,comm);
MPI_Send(&minn,1,MPI_INT,0,96,comm);
}
MPI_Finalize();
}
错误提示如下:
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7fff11568950, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7fff11568920) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7fff44eee630, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7fff44eee600) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffd14d7a450, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffd14d7a420) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffc241a4300, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffc241a42d0) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffeb1d9ca00, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffeb1d9c9d0) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
❤❤❤❤❤❤❤100
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffff9b335b0, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffff9b33580) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
❤❤❤❤❤❤❤100
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7fff60aa3650, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7fff60aa3620) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
❤❤❤❤❤❤❤100
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffcd6bed240, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffcd6bed210) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
❤❤❤❤❤❤❤100
Fatal error in PMPI_Recv: Message truncated, error stack:
PMPI_Recv(171)....................: MPI_Recv(buf=0x7ffff3192220, count=0, MPI_INT, src=0, tag=97, MPI_COMM_WORLD, status=0x7ffff31921f0) failed
MPIDI_CH3U_Receive_data_found(131): Message from rank 0 and tag 97 truncated; 400 bytes received but buffer size is 0
please input the N :
100
= BAD TERMINATION OF ONE OF YOUR APPLICATION PROCESSES
= PID 24244 RUNNING AT localhost.localdomain
= EXIT CODE: 11
= CLEANING UP REMAINING PROCESSES
= YOU CAN IGNORE THE BELOW CLEANUP MESSAGES
YOUR APPLICATION TERMINATED WITH THE EXIT STRING: Segmentation fault (signal 11)
This typically refers to a problem with your application.
Please see the FAQ page for debugging suggestions