# Using source= and mold= for multiple arrays

**URL:** <https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945>\
**Category:** Uncategorized\
**Created:** [April 2, 2021, 10:34pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945 "2021-04-02T22:34:25Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 2, 2021, 10:34pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/1 "2021-04-02T22:34:25Z")

</div>

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

```auto
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

```auto
 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)…?

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 2, 2021, 10:47pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/2 "2021-04-02T22:47:02Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [April 3, 2021, 2:49am UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/4 "2021-04-03T02:49:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kiranchandramohan](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kiranchandramohan](https://fortran-lang.discourse.group/u/kiranchandramohan)\
**Post date:** [April 3, 2021, 8:37am UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/5 "2021-04-03T08:37:58Z")

</div>

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

> <https://github.com/flang-compiler/flang/commit/1e57dcc156ea6c04eb493291664bcc433c90a150>

---

<div class="post-metadata">

**Author:** ![billlong](https://avatars.discourse-cdn.com/v4/letter/b/71e660/32.png) [@billlong](https://fortran-lang.discourse.group/u/billlong)\
**Post date:** [April 3, 2021, 1:51pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/6 "2021-04-03T13:51:32Z")

</div>

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 = 3_0 d = 3_0  
> e = 3_7 f = 2_7

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.

---

<div class="post-metadata">

**Author:** ![pcosta](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pcosta/32/5504_2.png) [@pcosta](https://fortran-lang.discourse.group/u/pcosta)\
**Post date:** [April 3, 2021, 3:34pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/7 "2021-04-03T15:34:06Z")

</div>

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

```auto
$ ./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

```

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 4, 2021, 12:30pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/8 "2021-04-04T12:30:45Z")

</div>

@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 🙂

> 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.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [April 4, 2021, 12:50pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/9 "2021-04-04T12:50:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kiranchandramohan](https://avatars.discourse-cdn.com/v4/letter/k/a8b319/32.png) [@kiranchandramohan](https://fortran-lang.discourse.group/u/kiranchandramohan)\
**Post date:** [April 4, 2021, 1:36pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/10 "2021-04-04T13:36:27Z")

</div>

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

```auto
$ 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

```

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 4, 2021, 9:55pm UTC](https://fortran-lang.discourse.group/t/using-source-and-mold-for-multiple-arrays/945/11 "2021-04-04T21:55:08Z")

</div>

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)
