MPI中的分段错误

时间:2017-12-10 00:31:55

标签: segmentation-fault mpi

请问,请帮我解决一些问题。我有分段错误 - 无效的内存引用。程序的结果总是不同的。程序必须在矩阵内部找到最大矩阵。有一些进程,每个进程都找到max_sum。然后他们将结果发送到主进程。它选择它们的最大值,然后具有最大值的过程应该将坐标发送到所有进程。我认为直到这些方面才有效:

call MPI_Barrier(MPI_COMM_WORLD, mpiErr)
call MPI_Bcast(j, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
write(*,*)"j in process ",mpiRank," is equal to", j
call MPI_Barrier(MPI_COMM_WORLD, mpiErr)    
call MPI_Bcast(x1, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
call MPI_Bcast(x2, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
call MPI_Bcast(y1, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
call MPI_Bcast(y2, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);

以下是完整的代码:

module Homework
implicit none
include "mpif.h"

contains
    subroutine FindMaxCoordinates(A, x1, y1, x2, y2)
    implicit none
include "mpif.h"
    integer :: mpisize, mpiErr,rank,comm,j,i,i1
integer(4):: mpiRank
    real(8),  dimension(:,:) :: A
    integer, intent(out) :: x1, y1, x2, y2
    integer(4) :: n, L, R, Up, Down, m, tmp
    real(8), allocatable :: current_column(:), B(:,:)
    real(8) :: current_sum, max_sum
    logical :: transpos 
    real(8),allocatable,dimension(:)::max_sumar
integer(4), allocatable, dimension(:):: X_1, X_2, Y_1, Y_2, coords
integer(4), dimension(MPI_STATUS_SIZE) :: status


    m = size(A, dim=1) 
    n = size(A, dim=2) 
    transpos = .FALSE.
allocate(coords(4))


    if (m < n) then 
        transpos = .TRUE.   
        B = transpose(A)
        m = size(B, dim=1) 
        n = size(B, dim=2) 
    else
        B = A     
        endif

    allocate(current_column(m))


    max_sum=-huge(0d0)
    x1=1
    y1=1
    x2=1
    y2=1

call mpi_init(mpiErr)
call mpi_comm_size(MPI_COMM_WORLD, mpisize, mpiErr)
    call mpi_comm_rank(MPI_COMM_WORLD, mpiRank, mpiErr)
rank=mpiRank+1
do  L=rank,n,mpisize
    current_column = B(:, L) 
          do R=L,n

                 if (R > L) then 
                    current_column = current_column + B(:, R)
                 endif

         call FindMaxInArray(current_column, current_sum, Up, Down) 
                 if (current_sum > max_sum) then
                    max_sum = current_sum
                    x1=Up
                    x2=Down
                    y1=L
                    y2=R
                 endif 
      enddo
enddo  

if (mpiRank/=0) then    
    do i1=1,mpisize
      call mpi_send(max_sum,1,MPI_REAL8,0,5,MPI_COMM_WORLD,mpiErr)
    enddo
endif 

call MPI_Barrier(MPI_COMM_WORLD, mpiErr)
if(mpiRank==0)then
    allocate(max_sumar(mpisize))
    max_sumar(1)=max_sum
    do i=1,mpisize-1
       call mpi_recv(max_sum,1,MPI_REAL8,i,MPI_ANY_TAG,MPI_COMM_WORLD,status,mpiErr)
       max_sumar(i+1)=max_sum
    enddo



    max_sum=maxval(max_sumar)
    do j=1,mpisize
        if(max_sumar(j)==max_sum) then
            exit
        endif
    enddo
    deallocate(max_sumar) 
endif

call MPI_Barrier(MPI_COMM_WORLD, mpiErr)
 call MPI_Bcast(j, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
call MPI_Barrier(MPI_COMM_WORLD, mpiErr)
write(*,*)"j in process ",mpiRank," is equal to", j
 call MPI_Barrier(MPI_COMM_WORLD, mpiErr)   
 call MPI_Bcast(x1, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
 call MPI_Bcast(x2, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
 call MPI_Bcast(y1, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);
 call MPI_Bcast(y2, 1, MPI_INTEGER, j-1, MPI_COMM_WORLD);

deallocate(current_column)

     if (transpos) then  
        tmp = x1
        x1 = y1
        y1 = tmp

       tmp = y2
        y2 = x2
        x2 = tmp
     endif

    call MPI_FINALIZE(mpiErr)
    end subroutine


    subroutine FindMaxInArray(a, Sum, Up, Down)
        real(8), intent(in), dimension(:) :: a
        integer(4), intent(out) :: Up, Down
        real(8), intent(out) :: Sum
        real(8) :: cur_sum
        integer(4) :: minus_pos, i

        Sum = a(1)
        Up = 1
        Down = 1
        cur_sum = 0
        minus_pos = 0



        do i=1, size(a)
            cur_sum = cur_sum + a(i)
            if (cur_sum > Sum) then
                Sum = cur_sum
                Up = minus_pos + 1
                 Down = i
            endif

            if (cur_sum < 0) then
                cur_sum = 0
                minus_pos = i
            endif

        enddo


    end subroutine FindMaxInArray

结束模块家庭作业

0 个答案:

没有答案