Using source= and mold= for multiple arrays

According to Modern Fortran Explained, it seems valid (from F2008) to use source= and mold= for multiple arrays in one allocate() statement, e.g.,

program main
    implicit none
    integer :: s(3)
    integer, allocatable :: a(:), b(:), c(:), d(:), e(:), f(:)

    s = [1,2,3]

    allocate( a, b, source= s )
    print *, "a = ", a, " b = ", b

    allocate( c, d, mold= s )
    print *, "c = ", c, " d = ", d

    allocate( e(3), f(2), source= 7 )
    print *, "e = ", e, " f = ", f
end

which gives

 a =  1         2        3    b =  1        2        3
 c =  0   268435456      0    d =  0   268435456     0
 e =  7         7        7    f =  7        7

and they seem convenient for allocating multiple arrays at once (e.g. source=zeros(2,3) with zeros() some helper function). But, my concern is whether it is well supported by many compilers already. A few years ago, I remember that the above syntax was not supported by some compiler (which I asked as Q/A on the net). But they work nicely with gfortran-10 if I test now. So, I am wondering if it is now probably “okay” to use them for coding (in terms of compiler support)…?

1 Like

The code compiles and runs with Intel, giving similar results, but flang says

(base) bingy@DESKTOP-O9ELSIT:/mnt/c/fortran/test$ flang --version
clang version 7.0.1 
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
(base) bingy@DESKTOP-O9ELSIT:/mnt/c/fortran/test$ flang xmold.f90
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 8)
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 8)
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 11)
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 11)
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 14)
F90-S-0155-With SOURCE or MOLD specifications, only one item can be allocated  (xmold.f90: 14)
  0 inform,   0 warnings,   6 severes, 0 fatal for main
1 Like

I believe the most recent Intel Fortran compiler 2021 supports Fortran 2018 in its entirety. NAG compiler also supports Fortran 2018, I believe, though not 100% sure. However, the issue frequently is that supercomputers and servers have some older versions of compilers installed, so you will have to either install the latest versions locally by yourself, or avoid the latest features of the language, or fence the usage of the latest features with FPP preprocessor directives.

3 Likes

This should work with Classic Flang. The feature came in June 2019.

2 Likes

The code also compiles and runs with the Cray/HPE compiler CCE.

ftn test.f90
./a.out
a = 1, 2, 3 b = 1, 2, 3
c = 30 d = 30
e = 37 f = 27

Note that the code is not standard conforming as the the arrays C and D are not defined at the point where they are printed.

1 Like

It also compiles and runs well with nvfortran 20.11-0:

$ ./a.out
a =             1            2            3  b =             1            2 
            3
 c =             0            0            0  d =             0            0 
            0
 e =             7            7            7  f =             7            7
1 Like

@Beliavsky @kargl @shahmoradi @kiranchandramohan @billlong @pcosta

Thanks very much for quick response and trying various compilers! I think the support status is now very good because 5 compilers support it (gfortran/intel/nvfortran for free, NAG for commercial, and Cray for supercomputer). This is more than sufficient so I think I will use it from now on :slight_smile:

Note that the code is not standard conforming as the the arrays C and D are not defined at the point where they are printed.

Yes, exactly… I think I should have filled some value (after using mold=) or just printed size() etc.

1 Like

Should Fortran allow both SOURCE and MOLD to be provided in an ALLOCATE statement, with the restriction that SOURCE be a scalar? Then one could write

allocate (a,mold=b,source=0)

to avoid having uninitialized data in a. But instead you can write

allocate (a,source=0*b)

so my suggested syntax is unnecessary.

1 Like

Just to clarify, I did check and it worked with classic flang as well.

$ flang test1.f90
$ ./a.out

 a =             1            2            3  b =             1            2 
            3
 c =             0            0            0  d =             0            0 
            0
 e =             7            7            7  f =             7            7
1 Like

Yeah, I guess that is one possibility (although it might be another idea to introduce a different keyword like allocate(a, shape=b, value=0) or allocate(a, shape_from=b, value=0) because I did not understand the meaning of the word “mold” unless I look into online English dictionary… XD)

@kiranchandramohan
Thanks again for checking this. So the supporting compilers are now 6 :slight_smile: (I thought “nvfortran” is a kind of superset of “classic flang”, so I counted them to be the same)