大型阵列导致OpenMP崩溃 [英] OpenMP Crashing with Large Arrays

查看:114
本文介绍了大型阵列导致OpenMP崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Fortran和OpenMP,但是当有大型数组时,当我尝试使用OpenMP并行化循环时,仍然遇到问题.例如,以下代码:

I'm using Fortran and OpenMP, but I keep encountering an issue when I try to parallelize loops using OpenMP when there are large arrays. For example, the following code:

PROGRAM main
IMPLICIT NONE   
INTEGER, PARAMETER :: NUMLOOPS = 300000
REAL(8) :: TESTMAT(NUMLOOPS)
INTEGER :: i,j

!$OMP PARALLEL SHARED(TESTMAT)
!$OMP DO
DO i=1,NUMLOOPS         
    TESTMAT(i) = i
END DO
!$OMP END DO
!$OMP END PARALLEL

write(*,*) SUM(TESTMAT)/(NUMLOOPS)

END PROGRAM main

使用此Makefile编译:

compiled using this Makefile:

.SUFFIXES: .f90

F90 = gfortran
FFLAGS_PFM = -ffree-form -ffree-line-length-none -fopenmp
LIB = -llapack
OBJ90 = main.o 

main: $(OBJ90)
    $(F90) $(FFLAGS_PFM) -o $@ $(LIB) $(OBJ90)

${OBJ90}: %.o: %.f90
    $(F90) $(FFLAGS_PFM) $(LIB) -c -o $@ $<

在Windows计算机上使用gfortran编译时崩溃.但是,如果我将NUMLOOPS值更改为小于260000左右的任何值,则程序运行正常.同样,大约1000x1000的矩阵会崩溃(实际上,任何大于500x500的矩阵都不起作用).因此,使用OpenMP时似乎允许最大数组大小?我还没有遇到过这种事情.我在多台Windows机器上尝试过,结果相同,但是都使用相同的配置,例如Windows 7和gfortran编译器.该代码始终可以毫无问题地进行编译,但是在运行时会崩溃.

Crashes when on a windows machine, compiled using gfortran. However, if I change the NUMLOOPS value to anything less than around 260000, the program runs just fine. Similarly, a matrix of about 1000x1000 would crash (anything above around 500x500, in fact, doesn't work). Thus, it seems like there is a maximum array size allowed when using OpenMP? I haven't come across anything of this kind though. I've tried on multiple windows machines, with the same result, however all use the same configuration, e.g. Windows 7 with gfortran compiler. The code always compiles without issue, but crashes when run.

推荐答案

在GNU Fortran中指定-fopenmp意味着-frecursive,这意味着所有局部变量(甚至大型数组)都是自动的(即在堆栈上分配).在Windows上,堆栈大小在PE可执行头文件中固定,并且必须在链接阶段指定,这与在Unix系统上可以通过限制机制动态控制的堆栈非常不同.

Specifying -fopenmp in GNU Fortran implies -frecursive which means that all local variables (even large arrays) are automatic (i.e. allocated on the stack). On Windows the stack size is fixed in the PE executable header file and has to be specified during link phase, very much unlike on Unix systems where it can be dynamically controlled by the limits mechanism.

要增加Windows可执行文件的堆栈大小,可以使用Microsoft的editbin.exe并通过以下命令行进行操作:

To increase the stack size of your Windows executable you can either use editbin.exe from Microsoft with a command line like:

editbin /STACK:<size> yourexe.exe

或向GCC提供以下选项:-Wl,--stack,<size in bytes>,其中<size in bytes>是所需的堆栈大小(以字节为单位).您应该将堆栈大小设置为至少足够大,以适合整个数组(即8*NUMLOOPS)以及局部变量和填充物.

or supply the following option to GCC: -Wl,--stack,<size in bytes>, where <size in bytes> is the desired stack size in bytes. You should set the stack size at least large enough to fit the whole array (that is 8*NUMLOOPS) and the local variables and stuff.

这篇关于大型阵列导致OpenMP崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆