Forum Jump

Select Group :
Select Forum :
Sorted By :
Sort Order :
From The :
 
Thread Tools  Search this thread 
opmkl
Total Points:
2,115
Status Points:
1,615
Brown Belt
November 4, 2009 9:23 AM PST
Is there a penalty (in terms of speed) associated with the use of RECURSIVE subroutines?
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 work with 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 A made? Is there a speed penalty associated with such a coding approach?

Thanks,

Olivier
tim18
Total Points:
68,987
Status Points:
68,987
Black Belt
November 4, 2009 9:54 AM PST
Rate
 
#1
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.


opmkl
Total Points:
2,115
Status Points:
1,615
Brown Belt
November 4, 2009 10:57 AM PST
Rate
 
#2 Reply to #1
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


Steve Lionel (Intel)
Total Points:
115,365
Status Points:
115,365
Black Belt
November 4, 2009 10:59 AM PST
Rate
 
#3

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.



opmkl
Total Points:
2,115
Status Points:
1,615
Brown Belt
November 4, 2009 11:40 AM PST
Rate
 
#4 Reply to #3
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




Intel Software Network Forums Statistics

8491 users have contributed to 31629 threads and 100770 posts to date.
In the past 24 hours, we have 28 new thread(s) 120 new posts(s), and 168 new user(s).

In the past 3 days, the most popular thread for everyone has been Implicite multithreading ??? The most posts were made to Crash when loading skeleton The post with the most views is Dear Steve, excuse me for a d

Please welcome our newest member shadowwolf99