我有一个使用 MPI 进行并行工作的现有 Fortran 代码。我有兴趣添加一些 PETSc 求解器(特别是 KSP),但是当包含相关的 .h 或 .h90 文件(petsc、petscsys、petscksp 等)时,我遇到了与共享相同名称的变量的问题MPI 的那些。
IE:
error #6405: The same named entity from different modules and/or program units cannot be referenced. [MPI_DOUBLE_PRECISION]
error #6405: The same named entity from different modules and/or program units cannot be referenced. [MPI_SUM]
error #6405: The same named entity from different modules and/or program units cannot be referenced. [MPI_COMM_WORLD]
and so on.
(使用 ics/composer_xe_2011_sp1.6.233 和 ics/impi/4.0.3.008 和 petsc 3.6.0,也尝试过旧的 petsc 版本 3.5.4)
所有这些在 MPI 和 PETSc 中都同样定义——有没有办法解决这个冲突并同时使用两者?
我会指出,我不想用 PETSc 调用替换 MPI 调用,因为代码应该有一个独立于 PETSc 运行的选项。
至于最少的代码,清理庞大的代码显然是一个问题,所以我做了以下简单的例子,其中包括相关部分:
program mpitest
implicit none
use mpi
! Try any of the following:
!!!#include "petsc.h"
!!!#include "petsc.h90"
!!!#include "petscsys.h"
! etc'
integer :: ierr, error
integer :: ni=256, nj=192, nk=256
integer :: i,j,k
real, allocatable :: phi(:,:,:)
integer :: mp_rank, mp_size
real :: sum_phi,max_div,max_div_final,sum_div_final,sum_div
call mpi_init(ierr)
call mpi_comm_rank(mpi_comm_world,mp_rank,ierr)
call mpi_comm_size(mpi_comm_world,mp_size,ierr)
allocate(phi(nj,nk,0:ni/mp_size+1))
sum_phi = 0.0
do i=1,ni/mp_size
do k=1,nk
do j=1,nj
sum_phi = sum_phi + phi(j,k,i)
enddo
enddo
enddo
sum_phi = sum_phi / real(ni/mp_size*nk*nj)
call mpi_allreduce(sum_div,sum_div_final,1,mpi_double_precision,mpi_sum, &
mpi_comm_world,ierr)
call mpi_allreduce(max_div,max_div_final,1,mpi_double_precision,mpi_max, &
mpi_comm_world,ierr)
call mpi_finalize(error)
deallocate(phi)
WRITE(*,*) 'Done'
end program mpitest
这在包含 PETSc 标头时直接发生,并且在删除包含时消失。