- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have got a problem in allocating arrays in subroutines and transferring data from them to the program to use them in other subroutines. I would very much appreciate your comments.
The following code is simplified example for this purpose.
PROGRAM TESTPROG
IMPLICIT NONE
INTEGER, ALLOCATABLE :: A(:)
INTEGER I, N
N = 10
CALL TESTSUB(A,N)
DO 2I = 1, N
2 PRINT*, A(I)
END PROGRAM TESTPROG
SUBROUTINE TESTSUB(A,N)
INTEGER, ALLOCATABLE, INTENT(INOUT) :: A(:)
INTEGER, INTENT(INOUT) :: N
INTEGER I
ALLOCATE (A(N))
DO 5I = 1, N
5 A(I) = I
RETURN
END SUBROUTINE TESTSUB
The code can be compiled with Visual Fortran Compiler but the following error occurs when it runs:
forrtl: severe (157): Program Exception - access violation
I don't know what's wrong, but could solve it putting the procedure in a module as bellow:
MODULE TESTMODULE
IMPLICIT NONE
INTEGER, ALLOCATABLE :: A(:)
CONTAINS
SUBROUTINE TESTSUB(N)
IMPLICIT NONE
INTEGER, INTENT(INOUT) :: N
INTEGER I
ALLOCATE(A(N))
DO 12I = 1, N
12 A(I) = I
RETURN
END SUBROUTINE TESTSUB
END MODULE TESTMODULE
PROGRAM TESTPROG
USE TESTMODULE
IMPLICIT NONE
INTEGER I, N
N = 10
CALL TESTSUB(N)
DO 15I = 1, N
15 PRINT *, A(I)
END PROGRAM TESTPROG
It works, but I am not happy with the solution as I prefer to write a program first and all subroutines after that, not possible in this way. I also don't know if there is a better solution as the program seems to run slowly.
Kind regards
Hamid
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The time spent on allocate and deallocate may be similar to the time spent on filling the array, up to around N==100 for stack allocation, and larger for heap, so you can expect measurable performance reduction unless the allocation is larger.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The time spent on allocate and deallocate may be similar to the time spent on filling the array, up to around N==100 for stack allocation, and larger for heap, so you can expect measurable performance reduction unless the allocation is larger.
Thank you very much for your comment. As you said I moved TESTSUB out of the module and added USE TESTMODULE to the program. But I didn't move the allocation as it is more convenient to be in my subroutine. The program is now in the following form and it works.
If you see something suspicious in the code please let me know.
Many thanks
Hamid
MODULE TESTMODULE
IMPLICIT NONE
INTEGER, ALLOCATABLE :: A(:)
END MODULE TESTMODULE
PROGRAM TESTPROG
USE TESTMODULE
IMPLICIT NONE
INTEGER I, N
N = 10
CALL TESTSUB(N)
DO 15I = 1, N
15 PRINT *, A(I)
END PROGRAM TESTPROG
SUBROUTINE TESTSUB(N)
USE TESTMODULE
IMPLICIT NONE
INTEGER, INTENT(INOUT) :: N
INTEGER I
ALLOCATE(A(N))
DO 12I = 1, N
12 A(I) = I
RETURN
END SUBROUTINE TESTSUB

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page