3

我已经上下搜索寻找有关此问题的帮助,尝试了各种解决方案等,但似乎无法追踪问题。我正在尝试并行化一个 do 循环,其中包含对 NLopt 库中优化例程的调用(对于 NLOpt,请参见:http ://ab-initio.mit.edu/wiki/index.php/NLopt )。这是我正在尝试做的一个玩具示例(真正的问题要复杂得多,额外调用更新参数的子例程calfun):

program tester

implicit none

! Include optimization package
include 'nlopt.f'

integer :: i,t,obs,J,MAXI=1000
real(kind=8) :: TOL = 0.0000001

! Optimization variables
real(kind=8), allocatable :: lb(:), ub(:), args(:,:)
integer(kind=8), allocatable :: opt(:)
integer, allocatable :: ires(:),val(:)

print*, 'How many times would you like to evaluate the optimization problem?'
read*, obs
print*, 'What is the size of the input vector?'
read*, J

allocate(lb(J))
allocate(ub(J))
allocate(args(J,obs))
allocate(opt(obs))
allocate(ires(obs))
allocate(val(obs))

do i=1,J
    lb(i) = -5
    ub(i) = 5
end do

!$OMP PARALLEL DO SHARED(lb,ub,args,opt,ires,val) PRIVATE(t,i,J,TOL,MAXI) 

do t=1,obs

    ! Call optimization routine
    call nlo_create(opt(t),NLOPT_LN_BOBYQA,J)
    ! Set Bounds
    call nlo_set_lower_bounds(ires(t), opt(t), lb(1:J))
    call nlo_set_upper_bounds(ires(t), opt(t), ub(1:J))
    call nlo_set_max_objective(ires(t), opt(t), calfun, 0)
    ! Set initial values
    do i=1,J
        args(i,t) = 0
    end do
    ! Set tolerance and stopping criteria
    call nlo_set_xtol_abs1(ires(t), opt(t), TOL)
    call nlo_set_maxeval(ires(t), opt(t), MAXI)
    ! Call optimizer
    call nlo_optimize( ires(t), opt(t), args(1:J,t), val(t) )
    call nlo_destroy( opt(t) )
end do
!$OMP END PARALLEL DO

! Write argmax to working directory
open(unit=2, file="out.txt") 
write(2,*) args
close(2)

contains

! Function to be optimized
subroutine calfun(val, dims, args, grad, need_grad, f_data)

integer :: dims, need_grad
real(kind=8) :: val, args(dims), grad(dims)
real(kind=8) :: f_data
real(kind=8) :: sq_in =0
real(kind=8) :: lin_in = 1

! Example function has the form -2*(Sum(x(i)^2)) + Prod x(i)
if (need_grad == 0) then
    do i=1,dims
        sq_in = -2*args(i)**2 + sq_in
        lin_in = args(i)*lin_in
    end do
    val = sq_in + lin_in
end if

end subroutine calfun
end program tester

我已经按照本文档进行了故障排除:

https://software.intel.com/en-us/articles/determining-root-cause-of-sigsegv-or-sigbus-errors

我还尝试在这篇文章之后重置堆栈大小:

为什么此 openmp 代码中发生分段错误?

上面的编译和运行正确使用gfortran如下:

$ gfortran -I/usr/local/include/ tester.f90 /usr/local/lib/libnlopt.a 
$ ./a.out

当我添加 OpenMP 标志时,它也会编译:

$ gfortran -fopenmp -I/usr/local/include/ tester.f90 /usr/local/lib/libnlopt.a 

但是,即使在设置后(在 Mac OSX 上运行),我也会收到分段错误

$ ulimit -s 65532

使用回溯进行编译只会显示选项卡错误和 NLOpt 包中未使用的虚拟参数。我不知道如何进行,真的需要并行化这个操作。我是否需要手动进入 NLOpt 例程并使用threadprivate?我似乎找不到关于此的好的文档。欣赏任何见解...

(PS:这是我的第一篇 Stackexchange 帖子。这些年来我一直是一个狂热的读者。对我放轻松!!谢谢!)

4

0 回答 0