# When is CONTIGUOUS useful?

**URL:** <https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082>\
**Category:** Uncategorized\
**Created:** [March 31, 2022, 5:44am UTC](https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082 "2022-03-31T05:44:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [March 31, 2022, 5:44am UTC](https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082/1 "2022-03-31T05:44:58Z")

</div>

Dear all,

Quick question, when I am using Intel OneAPI’s Intel Advisor, I remember there was a time I saw a performance hints, saying that using F2008’s `CONTIGUOUS` property. So I checked it,

> **[CONTIGUOUS](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference/top/language-reference/a-to-z-reference/c-to-d/contiguous.html)**
>
> Developer guide and reference for users of the Intel® Fortran Compiler Classic and Intel® Fortran Compiler

Intel’s example,

```auto
REAL, CONTIGUOUS, DIMENSION(:,:) :: A
REAL, POINTER, CONTIGUOUS :: MY_POINTER(:)

```

It is perhaps to emphasis the assumed-shape array is `CONTIGUOUS` so it make the compiler easier to do vectorization or some optimization.

Just curious, does anyone use `CONTIGUOUS` and get some performance gains?  
In what scenario `CONTIGUOUS` can be useful?

Thanks much in advance!

---

<div class="post-metadata">

**Author:** ![Pap](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/pap/32/1305_2.png) [@Pap](https://fortran-lang.discourse.group/u/Pap)\
**Post date:** [March 31, 2022, 7:56am UTC](https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082/2 "2022-03-31T07:56:54Z")

</div>

It has its uses, although not very often, at least not for me. One case that `contiguous` might be useful is when you have to deal with C interoperability. Say, you call a C function from within Fortran code that allocates memory and returns a pointer to that memory block. The corresponding Fortran pointer should be something like  
`integer(kind=c_signed_char), dimension(:), pointer, contiguous :: data`  
I can recall a few cases that this didn’t work without the `contiguous` attribute.  
Another example, actually taken from a program I wrote some time ago: Assume you have a C subroutine that renders an image using an array of RGB triplets for each pixel. Now, another subroutine, written in Fortran, has to read the data from an image file. We do not know in advance how big the image is, so we have to use an assumed-shape array to store the data; that array must be contiguous, otherwise the rendering subroutine will render garbage.

---

<div class="post-metadata">

**Author:** ![alozada](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/alozada/32/298_2.png) [@alozada](https://fortran-lang.discourse.group/u/alozada)\
**Post date:** [March 31, 2022, 7:58am UTC](https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082/3 "2022-03-31T07:58:07Z")

</div>

This thread could be useful

> [@Fortran pointers, and aliasing](https://fortran-lang.discourse.group/t/fortran-pointers-and-aliasing/1804):
>
> In C, the restrict keyword allows a programmer to make a promise to the compiler, saying approximately “I promise that this pointer doesn’t overlap with memory referred to by other pointers that are passed to this function”. That’s the default state in Fortran, for assumed-shape arrays, for example. But what bout when actually using pointers in Fortran? If the programmer knows that there is no aliasing, is there a way to communicate that to the compiler? Or is it unnecessary, since only targets …

---

<div class="post-metadata">

**Author:** ![Arjen](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Arjen](https://fortran-lang.discourse.group/u/Arjen)\
**Post date:** [March 31, 2022, 3:02pm UTC](https://fortran-lang.discourse.group/t/when-is-contiguous-useful/3082/4 "2022-03-31T15:02:35Z")

</div>

The programmer is her/himself responsible to make sure that is the case. Unlike in C where the compiler must take into account that things might overlap, the Fortran compiler can simply assume that to be the case. I guess the volatile attribute might help if you know that is not going to be the case?
