# Problem with MPI Fortran code

**URL:** <https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040>\
**Category:** Help\
**Created:** [December 24, 2023, 9:49am UTC](https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040 "2023-12-24T09:49:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AEROK25](https://avatars.discourse-cdn.com/v4/letter/a/5f9b8f/32.png) [@AEROK25](https://fortran-lang.discourse.group/u/AEROK25)\
**Post date:** [December 24, 2023, 9:49am UTC](https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040/1 "2023-12-24T09:49:02Z")

</div>

I’ve been working with this MPI FORTRAN code for over a week. This code generates OUTPUT files but not NUM\_OUT. I’m a beginner in programming and I’m trying to understand what the error is. I believe the MPI implementation is correct.

> **[PROGRAM PMPI use MPI !include 'mpif.h' IMPLICIT NONE !Variable D -...](https://pastebin.com/fjuTwGec)**
>
> Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.

---

<div class="post-metadata">

**Author:** ![wyphan](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wyphan/32/632_2.png) [@wyphan](https://fortran-lang.discourse.group/u/wyphan)\
**Post date:** [December 24, 2023, 7:05pm UTC](https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040/2 "2023-12-24T19:05:05Z")

</div>

You allocated `UPS` twice, and tried to access it out-of-bounds afterwards!

```auto
78 ALLOCATE(UPS(istart-2:iend+2, 1:2))
:
117 ALLOCATE(UPS(ISTART:IEND, 1))
:
131 UPS(IEND + 1, 1) = CMPLX(DATA_BR, DATA_BI, KIND=8)
137 UPS(ISTART - 1, 1) = CMPLX(DATA_BR, DATA_BI, KIND=8)
143 UPS(IEND + 1, 1) = CMPLX(DATA_CR, DATA_CI, KIND=8)
162 UPS(I, 2) = UPS(I, 1) - ((U * DT / DX) * (UPS(I, 1) - UPS(I - 1, 1)))
175 UPS(POINTS - 1, 2) = UPS(POINTS - 1, 1) - ((U * DT / DX) * (UPS(POINTS - 1, 1) - UPS(POINTS - 2, 1)))
193 UPS(ISTART:IEND, 1) = UPS(ISTART:IEND, 2)

```

I’m surprised the compiler didn’t catch this.

For the problematic output file, since `IEND` is calculated,

```auto
69 IEND = MIN((MY_RANK + 1) * PARTS, POINTS)

```

It might be helpful to print out `ISTART` and `IEND` for each MPI rank, to ensure that `IEND` is always bigger that `ISTART`,

```auto
PRINT *, "Rank ", MY_RANK, " ISTART=", ISTART, " IEND=", IEND

```

Otherwise, this loop won’t execute at all.

```auto
209 DO I = ISTART-1, IEND+1
210 WRITE(40, *) X(I), UPS(I, 1)
211 END DO

```

Also, a minor suggestion: instead of using a separate internal write for `PROC3`,

```auto
108 WRITE(PROC3, FMT='(I10)') MY_RANK
109 OUTFILE = "OUTPUT_LFS" // TRIM(ADJUSTL(PROC3)) // ".dat"
:
205 WRITE(PROC3, FMT='(I10)') MY_RANK
206 OUTFILE = "NUM_OUT_" // TRIM(ADJUSTL(PROC3)) // ".dat"

```

why not write it directly to `OUTFILE`?

```auto
WRITE(OUTFILE, '("OUTPUT_LFS", I0, ".dat")') MY_RANK
WRITE(OUTFILE, '("NUM_OUT_", I0, ".dat")') MY_RANK

```

Finally, while some compilers may be smart enough to automatically deallocate memory on `ALLOCATABLE` variables once it’s out of scope, it’s good practice to manually clean up after yourself so you don’t accidentally introduce memory leaks.

---

<div class="post-metadata">

**Author:** ![aerosayan](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@aerosayan](https://fortran-lang.discourse.group/u/aerosayan)\
**Post date:** [December 24, 2023, 10:30pm UTC](https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040/3 "2023-12-24T22:30:56Z")

</div>

> [@wyphan](#):
>
> Finally, while some compilers may be smart enough to automatically deallocate memory on `ALLOCATABLE` variables once it’s out of scope, it’s good practice to manually clean up after yourself so you don’t accidentally introduce memory leaks.

Allocatable arrays are always deallocated automatically at the end of scope though. So manually deallocating is probably not required.

---

<div class="post-metadata">

**Author:** ![wyphan](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/wyphan/32/632_2.png) [@wyphan](https://fortran-lang.discourse.group/u/wyphan)\
**Post date:** [December 27, 2023, 6:14pm UTC](https://fortran-lang.discourse.group/t/problem-with-mpi-fortran-code/7040/4 "2023-12-27T18:14:12Z")

</div>

@aerosayan There are certain edge cases tho, like with module variables and when using `SAVE`. In-depth discussion here:

> [@Best practice: Deallocating allocatable arrays explicitly vs implicitly](https://fortran-lang.discourse.group/t/best-practice-deallocating-allocatable-arrays-explicitly-vs-implicitly/3071):
>
> Allocatable errorsarrays are deallocated automatically once they are out of scope, i.e. at the end of the procedure they are declared. Should one still use explicit deallocation? (If you want to deallocate early then you have to do it explicitly of course.)
