Fortran zero array dimension for multi-dimensional arrays

Hello programming colleagues,
you might have seen the post on Open MP flag changing the results
even for the non-OpenMP code.

Now I am trying to track down all the issues in this code, I find that the code owners (“developers”) use the array dimension as a form of conditional.

e.g : their typical usage:

     parameter (ni=11,nj=12,nk=34,np=0,ns=13)
     real a(ni,nj,nk,np,ns)

  do ip = 1, np

C    work on P stuff in "a"

  end do

Some versions have NP as a real useful value. In this case the loop is not done when np = 0 (or so they intend).

I do not like this kind of programming but it would take a lot of effort to rip it out or program around it so I have to allow for it. However, what I would like to know is how the PGI compiler is dealing with the actual array?
Especially as NP is not the outer dimension.


Dr. Skids

Hi Dr. Skids,

This is an interesting problem that I don’t know the answer to. Let me talk with our compiler engineers and get back to you next week.

  • Mat

Hi Dr. Skids,

Since an arrays size is the product of all it’s extent, if any given extent is zero, then the entire array is treated as a zero-size array and all operations on the array are treated as a nop.

Hope this helps,

Hi Mat,
thanks for getting back with this answer. Does this apply to static array dimensions as shown in my example. These researchers are using this code for production runs and have been for many years. Surely these zero-sized arrays would then manifest as some sort of out-of-bound error? I understand “nop” means null operation but what is that in Fortran procedural terms?

I am now wondering if those subroutines are ever passed-through.
I also will have trouble using valgrind as the XT system requires static execs but valgrind expects to use shared libs. (I am still researching this on a simple single socket AMD quad core suse 11 system before burning a Cray budget).
NOTE the test case will not run on less than 8 cores (~24GB RAM).

Dr. Skids

Hi Dr. Skids,

Sorry if I’m off a bit here. I’ve never used zero-sized arrays myself and frankly don’t see the point of them. But the standard allows them so there we are.

My understanding is that if the user had written

ARR = scalar

where ARR is a zero-sized array, this operation becomes a nop.

If the user has

do i=1,n
do j=1,m
do k=1,nn
do ii=1,mm
do jj=1,nnn
ARR(i,j,h,ii,jj) = scalar

where the “mm” is zero, again this would be ok since the “ii” do loop would never execute.

However, if ARR was executed with a sub-script, then this behavior undefined and would most likely cause a segmentation violation.

I’m not sure I’ve answered your question, but if not we can iterate over it until we both understand.

  • Mat