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)…?
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
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.
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
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.
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 (I thought “nvfortran” is a kind of superset of “classic flang”, so I counted them to be the same)