Is there an intrinsic function for initializing arrays to zero in Fortran?
Asked Answered
O

2

6

Is there a way to set an array (vector, or matrix, or even a scalar) to zero yet in Fortran? 2003/2008 seem to be taking Fortran to a very modern level, and I have to wonder if they have included an easy way to set array values to zero without having to do

do i = 1,X

        do j = 1,Y

            A(i,j) = 0

        enddo

enddo

Where X is the number of rows and Y is the number of columns in a 2-d matrix. This could be generalized to as many dimensions as you like, but the principle is the same.

In Matlab this is easily done with the zeros function, i.e.,

A = zeros(X,Y)

Modern Fortran seems to be incorporating a lot of the things people like about Matlab and other languages into it's repertoire, so I am just curious if they have an intrinsic for this simple and basic task yet.

Or maybe in modern Fortran it isn't necessary to clear out any previously stored values in the memory by initializing arrays?

I guess a shorter way would be to just go

real, dimension(X,Y)        :: A

A = A*0.0 ! Set all elements to zero by multiplying matrix by scalar value 0

But the question about an intrinsic still stands.

Okun answered 16/4, 2017 at 2:5 Comment(6)
I think A = 0.0 will do it.Playboy
@rici, you are correct. That is a very simple solution to something I have been doing overkill.Okun
You've found the right answer, but please note your "multiply by zero" method is not correct - it is never correct to use an uninitialised variable in an expressionAgapanthus
Also, it can be ensured via compiler options. The -finit-local-zero option instructs the compiler to initialize local INTEGER, REAL, and COMPLEX variables to zero for gfortran.Orva
Please don't do this, an assumption like that makes your code non-standard conformingAgapanthus
Yeap, my previous comment clearly advice a bad practice, it exist, but not recommended to use, see also. I wonder if there is any other good enough reason to use that option apart from debugging.Orva
P
15

It turns out the answer is quite simple.

A = 0.0 

or just

A = 0

will set all elements in your array to 0.0000...

(Moved from the question.)

Philina answered 16/4, 2017 at 2:5 Comment(8)
Does this work for all versions or Fortran, or is it a feature that was added in newer versions?Lenes
@Lenes Since Fortran 90.Mathia
@VladimirF is it possible to do this? real, dimension(X,Y) :: A = 0?Crossbred
Does this work for any real? i.e. could I do: real, dimension(X,Y) :: A = 273.15 ?Burtis
real(8), dimension (NL, ML) :: D = 0.0D0Franciscka
@Franciscka Not really necessary. Just real(8), dimension (NL, ML) :: D = 0. Also, using the magic number 8 for the kind is ugly and not portable and not guaranteed to be the same as double precision. If you want a kind 8 literal, use 0.0_8, not 0.0d0.Mathia
@HermanToothrot Yes, it is.Mathia
@Burtis Yes, you can.Mathia
C
0

In the original question the example overlooked the way that FORTRAN arrays are laid out in memory.

A(1,1), A(2,1), A(3,1), ... A(1,2), A(2,2), ...

The sample loop will not exploit locality of memory references and can't easily be vectorized on modern equipment. Each successive array reference has a 'stride' which is ideally the size of one array entry. I think you will also get pipeline stalls every time the CPU has to wait for memory to be loaded. You may also see an increase in cache misses. Memory alignment is best left for another day.

Modern processors employ multiple strategies to convert electrical impulses to heat (doing useful work on the side). This was true in 2017 and is still true in 2024.

Casia answered 8/6 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.