Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Modules, Dynamic Array Allocation

Ernie_P_1
Beginner
696 Views
Hi,

I'm in the process of eliminating all common blocks in my program. The reason for this is many arrays have been created and given the maximum size they could possibly need when I run this program. Instead I wanted to read the size they'd need from a text file and dynamically allocate the arrays. Should moving from common blocks to dynamically allocated arrays make performance better?

Here is an example of what I'm doing:

SUBROUTINE SUB1
COMMON/TEST1/ARRAY1(99),ARRAY2(99),ARRAY3(99)
...
END SUBROUTINE SUB1

REPLACED WITH:

MODULE TESTMOD1
REAL, ALLOCATABLE :: ARRAY1(:),ARRAY2(:),ARRAY3(:)
...
END MODULE TESTMOD1


SUBROUTINE ALLOCATE_ARRAYS()
USE TESTMOD1

ALLOCATE(ARRAY1(SIZE))
ALLOCATE(ARRAY2(SIZE))
ALLOCATE(ARRAY3(SIZE))
END SUBROUTINE ALLOCATE_ARRAYS

PROGRAM TESTPROGRAM
USE TESTMOD1

!CODE TO READ ARRAY SIZE
...
CALL ALLOCATE_ARRAYS()

STOP
END


If my goal is to increase readability/supportability of the code, and to increase performance, is this the best way to eliminate common blocks?

I tried grouping the allocate statements in one line like:
ALLOCATE(ARRAY1(SIZE), ARRAY2(SIZE), ARRAY3(SIZE)...........)
I did this with maybe a hundred arrays. This led me to an error that I had no virtual memory free.
Do I really need a separate ALLOCATE for each array?

Thanks,
EP
0 Kudos
4 Replies
Steven_L_Intel1
Employee
696 Views
I'd say you have the right approach. As a matter of style, I would use separate ALLOCATE statements as that allows you to use STAT= on each one to detect errors. Otherwise, there is no functional difference.
0 Kudos
roddur
Beginner
696 Views
Quoting - Ernie P.
If my goal is to increase readability/supportability of the code, and to increase performance,


As far as increase is performance is concerned, i once did the similar job and got a huge performance uplift. this is generally true if your array size is variable: so, it compiler will not allocate unnecessary memory.(this is how i can explain this...i really dont know the inner working).



0 Kudos
Ernie_P_1
Beginner
696 Views
I'd say you have the right approach. As a matter of style, I would use separate ALLOCATE statements as that allows you to use STAT= on each one to detect errors. Otherwise, there is no functional difference.

Thanks for the replies.

A couple other questions:

If I include USE, ONLY to only allow specific variables to be visible to a subroutine, would that increase performance over not including ONLY and making every variable available? Or is ONLY more to keep one from altering a variable they didn't intend to?

Main main goal is increasing performance,more specifically, execution time. Is there a discussion of general ideas/topics related to increasing performance that you're aware of that could help me out? I know there is a lot I could do with compiler settings - I was hoping to be lazy and find similar discussions that people have already had.

Thanks
EP
0 Kudos
TimP
Honored Contributor III
696 Views
Quoting - Ernie P.

If I include USE, ONLY to only allow specific variables to be visible to a subroutine, would that increase performance over not including ONLY and making every variable available?
No, the ONLY has effect at compile time. Locally unused variables won't affect performance.
The performance effects of USE vs COMMON are complicated, so simple generalizations are likely to be wrong.
0 Kudos
Reply