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

zero initialization of arrays

Pipa
Beginner
5,889 Views

Hi,

I want to know if there exist a compiler option which automatically initialize every array with zeros rather than garbage values.

I know i can do this while running the program by using:

a(:,:,:)=0

but for some reason it takes a lot of time.

Thanks.

0 Kudos
7 Replies
TimP
Honored Contributor III
5,889 Views
a = 0 could be faster. If you use standard Fortran compile-time initialization, or -Qzero, you could not expect better performance, except by the guarantee that it happens only once. Assuming you use SSE code, the directive
!dir$ vector nontemporal
should speed up initialization of arrays which are larger than cache. Evidently, you will want to check for vectorization.
0 Kudos
anthonyrichards
New Contributor III
5,889 Views

What about initialising an array of user-defined types? Do all elements of a type get initialised according to their basic type (REAL*8, INTEGER*4, CHARACTER, etc.)?

0 Kudos
Steven_L_Intel1
Employee
5,889 Views
There is no automatic initialization of arrays of any type. You can specify static initialization in the declaration, which will not take run-time (but may make the EXE larger.)

When making array references, use:

A = 0.

rather than:

A(:,:,:) = 0.

For reasons I don't quite understand, the compiler sometimes isn't able to tell that these are the same and you can get extra code for the second case. This is being looked at.
0 Kudos
Jugoslav_Dujic
Valued Contributor II
5,889 Views
UDT's can be initialized via default initialization, where you put initialization-expression for components within the definition of the TYPE itself:

TYPE T_MyType
INTEGER:: i = 0
REAL:: r = 0.
CHARACTER(5):: s=""
REAL:: f = 3.16
END TYPE T_MyType
They can also be initialized using UDT and array constructors, like:

TYPE(T_MyType):: MT(100) = &
(/ (T_MyType(0,0.,"",3.16), k=1,100) /)

There are subtle differences: the expression below is executed only once, while default initialization takes place whenever an object of given type is created, which includes allocation and dummy arguments with INTENT(OUT) attribute.

0 Kudos
Pipa
Beginner
5,889 Views

I think I didn't state my question clearly. I need to know if there is a compiler option which sets all the arrays to zero so that i dont need to write statements like :

a=0 or a(:)=0

in the code. It will save my time and space in coding.

0 Kudos
Paul_Curtis
Valued Contributor I
5,889 Views

In Windows, you can usean API to do the job very efficiently:

CALL ZeroMemory (LOC(your_item), SIZEOF(your_item))

This is immediately useful with UDTs as well.

A (personal, and wholly unsolicited) comment on the OP's desire to "save time and space in coding": it is IMHO very badpractice to write code with hidden, implicitdependenceon other entities beyond the scope of the code itself (ie, default behavior of a particular compiler). The converse, having explicit and obvious in-codeinitializations of arrays and UTDs, is good practice, portable, and easy to understand and debug.

0 Kudos
Pipa
Beginner
5,889 Views
Thankyou for answering my query. Also thanks for the advice.
0 Kudos
Reply