FORTRAN-写入大小为4的无效

时间:2013-12-25 14:10:34

标签: fortran mpi valgrind

我使用MPI编写了一个Fortran代码,因为我是valgrind的新手,我无法找到问题所在。运行检查时出现此错误:

==3040== Invalid write of size 4
==3040==    at 0x804BFE0: MAIN__ (NS.f90:174)
==3040==    by 0x804E190: main (NS.f90:321)
==3040==  Address 0x5746f20 is 0 bytes after a block of size 8 alloc'd
==3040==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3040==    by 0x804C5F3: MAIN__ (NS.f90:198)
==3040==    by 0x804E190: main (NS.f90:321)
==3040== 
==3041== Invalid write of size 4
==3041==    at 0x804BFE0: MAIN__ (NS.f90:174)
==3040== Invalid write of size 4
==3040==    at 0x804C036: MAIN__ (NS.f90:175)
==3040==    by 0x804E190: main (NS.f90:321)
==3041==    by 0x804E190: main (NS.f90:321)
==3041==  Address 0x5748bbc is 0 bytes after a block of size 12 alloc'd
==3041==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3040==  Address 0x5746f58 is 0 bytes after a block of size 8 alloc'd
==3040==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3040==    by 0x804C729: MAIN__ (NS.f90:198)
==3040==    by 0x804E190: main (NS.f90:321)
==3040== 
==3041==    by 0x804C5F3: MAIN__ (NS.f90:198)
==3041==    by 0x804E190: main (NS.f90:321)
==3041== 
==3040== Invalid write of size 4
==3040==    at 0x804C087: MAIN__ (NS.f90:176)
==3040==    by 0x804E190: main (NS.f90:321)
==3040==  Address 0x5746f90 is 0 bytes after a block of size 8 alloc'd
==3040==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3040==    by 0x804C862: MAIN__ (NS.f90:198)
==3040==    by 0x804E190: main (NS.f90:321)
==3040== 
==3041== Invalid write of size 4
==3041==    at 0x804C036: MAIN__ (NS.f90:175)
==3041==    by 0x804E190: main (NS.f90:321)
==3041==  Address 0x5748bfc is 0 bytes after a block of size 12 alloc'd
==3041==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3041==    by 0x804C729: MAIN__ (NS.f90:198)
==3041==    by 0x804E190: main (NS.f90:321)
==3041== 
==3040== Invalid write of size 4
==3040==    at 0x804C1C5: MAIN__ (NS.f90:178)
==3040==    by 0x804E190: main (NS.f90:321)
==3040==  Address 0x5746fc8 is 0 bytes after a block of size 8 alloc'd
==3040==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3040==    by 0x804C99B: MAIN__ (NS.f90:198)
==3040==    by 0x804E190: main (NS.f90:321)
==3040== 
==3041== Invalid write of size 4
==3041==    at 0x804C087: MAIN__ (NS.f90:176)
==3041==    by 0x804E190: main (NS.f90:321)
==3041==  Address 0x5748c3c is 0 bytes after a block of size 12 alloc'd
==3041==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==3041==    by 0x804C862: MAIN__ (NS.f90:198)
==3041==    by 0x804E190: main (NS.f90:321)
==3041== 
==3040== 
==3041== Invalid write of size 4
==3041==    at 0x804C1C5: MAIN__ (NS.f90:178)
==3041==    by 0x804E190: main (NS.f90:321)
==3041==  Address 0x5748c7c is 0 bytes after a block of size 12 alloc'd
==3041==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

第174到178行包含这段代码:

A(I) = -0.5 * (0.5 * (1.0 + EPX) * CX(I-1, J) + DX)
B(I) = (1 + DX + 0.5 * EPX * CX(I, J))
C(I) = 0.5 * (0.5 * (1 - EPX) * CX(I+1, J) - DX)
R(I) = 0.5 * (0.5 * (1 + EPY) * CY(I, J-1) + DY) * OMEGA(I, J-1) + (1 - DY - 0.5 * EPY * CY(I, J)) * OMEGA(I, J) + 0.5 * (-0.5 * (1 - EPY) * CY(I, J+1) + DY) * OMEGA(I, J+1)

有人能告诉我这里有什么问题吗?我还得到其他错误:

==3041== Invalid read of size 4
==3041==    at 0x804C271: MAIN__ (NS.f90:183)
==3041==    by 0x804E190: main (NS.f90:321)
==3041==  Address 0x5748c40 is 4 bytes after a block of size 12 alloc'd
==3041==    at 0x402BE68: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

第183行引用:

R(N) = R(N) - C(N) * OMEGA(N+1, J)

如果有人能告诉我问题所在,我将不胜感激。

以下是代码块:

A = 0.0
B = 0.0
C = 0.0
R = 0.0
X = 0.0
DO J = 1, M
DO I = 1, N 
IF (U(I, J) > 0.0) THEN
EPX = 1.0
ELSE 
EPX = -1.0
ENDIF
IF (V(I, J) > 0.0) THEN
EPY = 1.0
ELSE 
EPY = -1.0
ENDIF
A(I) = -0.5 * (0.5 * (1.0 + EPX) * CX(I-1, J) + DX)
B(I) = (1 + DX + 0.5 * EPX * CX(I, J))
C(I) = 0.5 * (0.5 * (1 - EPX) * CX(I+1, J) - DX)
R(I) = 0.5 * (0.5 * (1 + EPY) * CY(I, J-1) + DY) * OMEGA(I, J-1) + (1 - DY - 0.5 * EPY * CY(I, J)) * OMEGA(I, J) + 0.5 * (-0.5 * (1 - EPY) * CY(I, J+1) + DY) * OMEGA(I, J+1)
ENDDO
R(1) = R(1) - A(1) * OMEGA(0, J)
R(N) = R(N) - C(N) * OMEGA(N+1, J)
ENDDO

1 个答案:

答案 0 :(得分:2)

您很可能遇到索引错误并且溢出了数组边界。这是一个常见错误,编译器可以协助调试。对于gfortran使用-fcheck=all,对于Intel us -check all。其他编译器也有类似的功能。

之后,您必须确保您的数组具有适当的尺寸,并且您正在运行具有正确的上限和下限值的循环。

在这种情况下,我们无法说明错误的位置,因为您没有显示变量的声明。