我上下搜索寻找这个问题的一些帮助,尝试了各种解决方案等,但似乎无法追踪问题。我尝试并行化包含对NLopt库中的优化例程的调用的do循环(对于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
我也尝试在这篇帖子后重置堆栈大小:
Why Segmentation fault is happening in this openmp code?
以上编译并使用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
设置内容?我似乎无法找到关于此的良好文档。欣赏任何见解...
(P.S。:这是我的第一篇Stackexchange帖子。多年来,我一直是个狂热的读者。对我这么简单!谢谢!)