# Automatic arrays and intrinsic array operations: to use or not to use?

**URL:** <https://fortran-lang.discourse.group/t/automatic-arrays-and-intrinsic-array-operations-to-use-or-not-to-use/4070>\
**Category:** Help\
**Created:** [July 25, 2022, 4:42pm UTC](https://fortran-lang.discourse.group/t/automatic-arrays-and-intrinsic-array-operations-to-use-or-not-to-use/4070 "2022-07-25T16:42:23Z")\
**Posts on this page:** 1\
**Showing post:** 26

<div class="post-metadata">

**Author:** ![zaikunzhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/zaikunzhang/32/785_2.png) [@zaikunzhang](https://fortran-lang.discourse.group/u/zaikunzhang)\
**Post date:** [July 28, 2022, 2:49pm UTC](https://fortran-lang.discourse.group/t/automatic-arrays-and-intrinsic-array-operations-to-use-or-not-to-use/4070/26 "2022-07-28T14:49:48Z")

</div>

> [@RonShepard](#):
>
> Does this happen because the result of MATMUL() is stored in a temporary array on the stack before being copied to the result B? Or is it because internally MATMUL is generating temporary arrays?

I am not sure. This is what I obtained by `valgrin ./a.out`:

```auto
 Initialize
 Loop
 Succeed
 MATMUL
==324261== Warning: client switching stacks? SP change: 0x1ffeffb4e0 --> 0x1ffd176ce0
==324261== to suppress, use: --max-stackframe=32000000 or greater
==324261== Invalid write of size 8
==324261== at 0x4050E9: MAIN__ (test_matmul.f90:47)
==324261== Address 0x1ffd176ce0 is on thread 1's stack
==324261== 
==324261== Warning: ignored attempt to set SIGKILL handler in sigaction();
==324261== the SIGKILL signal is uncatchable
==324261== Warning: ignored attempt to set SIGSTOP handler in sigaction();
==324261== the SIGSTOP signal is uncatchable
==324261== Warning: ignored attempt to set SIGRT32 handler in sigaction();
==324261== the SIGRT32 signal is used internally by Valgrind
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source             
a.out 000000000040601A Unknown Unknown Unknown
libpthread-2.31.s 00000000049AF420 Unknown Unknown Unknown
a.out 00000000004050E9 Unknown Unknown Unknown
a.out 0000000000403822 Unknown Unknown Unknown
libc-2.31.so 00000000049E2083 __libc_start_main Unknown Unknown
a.out 000000000040372E Unknown Unknown Unknown

```

and by `gdb ./a.out`:

```auto
Using host libthread_db library "/usr/lib/x86_64-linux-gnu/libthread_db.so.1".
 Initialize
 Loop
 Succeed
 MATMUL

Program received signal SIGSEGV, Segmentation fault.
0x00000000004050e9 in test_matmul () at test_matmul.f90:47
47	B = matmul(A, A)
(gdb) bt
#0 0x00000000004050e9 in test_matmul () at test_matmul.f90:47
#1 0x0000000000403822 in main ()
#2 0x00007ffff7c85083 in __libc_start_main (main=0x4037f0 <main>, argc=1, argv=0x7fffffff9968, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffff9958)
    at ../csu/libc-start.c:308
#3 0x000000000040372e in _start ()
(gdb) 

```

It seems to me that the segfault occurred when writing the result of `matmul` to `B`, but I am not sure.  
(BTW, why doesn’t `ifort` print the call stack of the error even when `-g` is specified?)

> [@RonShepard](#):
>
> If it is the former, then maybe the general solution is to propose to the language committee that a subroutine interface should be added to the language for matrix-matrix products. A simplified version of the BLAS xGEMM() subroutine would be a good starting point. This would also address the problems associated with unnecessary temporary arrays for operations like MATMUL(TRANSPOSE(A),B).

This sounds truly interesting. It is something I hope for. **How could we make it happen?**

---

_[View the full topic](https://fortran-lang.discourse.group/t/automatic-arrays-and-intrinsic-array-operations-to-use-or-not-to-use/4070)._
