- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am wondering if using RECURSIVE subroutines is a good practice. I have the following code:
RECURSIVE SUBROUTINE MY_RECURSIVE_SUB(A,ALPHA,ALPHA_TARGET,D_ALPHA_MAX,FLAG)
USE MODULE_WHERE_T_COMPLICATED_STRUCTURE_IS_DEFINED
IMPLICIT NONE
TYPE(T_COMPLICATED_STRUCTURE),INTENT(INOUT) :: A
LOGICAL,INTENT(IN) ::FLAG
REAL(KIND=8),INTENT(INOUT) :: ALPHA,ALPHA_TARGET,D_ALPHA_MAX
...
IF (FLAG) THEN
IF (ABS(ALPHA-ALPHA_TARGET)>D_ALPHA_MAX) THEN
CALL MY_RECURSIVE_SUB(A,ALPHA,ALPHA_TARGET,D_ALPHA_MAX,.FALSE.)
ALPHA_TARGET = ALPHA
ELSE
... do some work with A ...
ENDIF
ELSE
... do some workwith A ...
ENDIF
END SUBROUTINE MY_RECURSIVE_SUB
A is a very large and complicated derived type structure which contains allocatable arrays, other derived type variables, etc. When the subroutine recursively calls itself,is a copy of Amade? Is there a speed penalty associated with such a coding approach?
Thanks,
Olivier
RECURSIVE SUBROUTINE MY_RECURSIVE_SUB(A,ALPHA,ALPHA_TARGET,D_ALPHA_MAX,FLAG)
USE MODULE_WHERE_T_COMPLICATED_STRUCTURE_IS_DEFINED
IMPLICIT NONE
TYPE(T_COMPLICATED_STRUCTURE),INTENT(INOUT) :: A
LOGICAL,INTENT(IN) ::FLAG
REAL(KIND=8),INTENT(INOUT) :: ALPHA,ALPHA_TARGET,D_ALPHA_MAX
...
IF (FLAG) THEN
IF (ABS(ALPHA-ALPHA_TARGET)>D_ALPHA_MAX) THEN
CALL MY_RECURSIVE_SUB(A,ALPHA,ALPHA_TARGET,D_ALPHA_MAX,.FALSE.)
ALPHA_TARGET = ALPHA
ELSE
... do some work with A ...
ENDIF
ELSE
... do some workwith A ...
ENDIF
END SUBROUTINE MY_RECURSIVE_SUB
A is a very large and complicated derived type structure which contains allocatable arrays, other derived type variables, etc. When the subroutine recursively calls itself,is a copy of Amade? Is there a speed penalty associated with such a coding approach?
Thanks,
Olivier
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It certainly appears that all these data must be replicated on stack for each recursive call. I don't see that a generalization could be made about the performance, even if I assume that your alternative is an array of those "complicated structures." If there is no reason for each instance to have its own copy of those arrays, it seems you could get better cache locality by sharing a single copy, if only one thread is involved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Tim18, I had the gut feeling it would be a no-go. I'll restructure my code to avoid this need for recursivity.
Olivier
Olivier
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Unless I'm misreading this, no copies are made. You are simply passing along the dummy arguments to a routine whose explicit interface is known, so it would just pass the same item by reference. RECURSIVE allows local variables to be distinct, but I don't see any in this excerpt. It would also ensure correct behavior for any locally created descriptors or pointers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Oh, I see. Yes, it definitely makes sense that the local variables are duplicated.The dummy arguments do not need to (which is what I was concerned about).
Thanks Steve for clarifying this.
Olivier
Thanks Steve for clarifying this.
Olivier

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