# Equivalent gfort and ifort compilation

**URL:** <https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092>\
**Category:** Help\
**Created:** [March 31, 2022, 8:30pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092 "2022-03-31T20:30:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![NC1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@NC1](https://fortran-lang.discourse.group/u/NC1)\
**Post date:** [March 31, 2022, 8:30pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/1 "2022-03-31T20:30:50Z")

</div>

I am trying to compile a code using gfortran instead of ifortran. The gfortran code runs much much slower than the ifortran code. It does not even use all threads from my computer. I have 40 cores and 80 threads. ifort uses all of them.I think it has to do with the ifort flag `/Qm64` but can’t be sure (using the /Qm32 flag on ifort is slower and does not use all the threads on openmp - not sure why).

This is the way that visual studio seems to be compiling my code:

> Compiling with Intel® Fortran Compiler Classic 2021.4.0 [Intel(R) 64]…  
> ifort /nologo /O2 /Qopenmp /module:“x64\Release\” /object:“x64\Release\” /Fd"x64\Release\vc160.pdb" /libs:dll /threads /c /Qlocation,link,“C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64” /Qm64 “D:\test\main1.f90”  
> Linking…  
> Link /OUT:“x64\Release\DebtDuration.exe” /INCREMENTAL:NO /NOLOGO /MANIFEST /MANIFESTFILE:“x64\Release\DebtDuration.exe.intermediate.manifest” /MANIFESTUAC:“level=‘asInvoker’ uiAccess=‘false’” /SUBSYSTEM:CONSOLE /STACK:999999999 /IMPLIB:“D:\test\x64\Release\DebtDuration.lib” -qm64 “x64\Release\splint.obj” “x64\Release\linspace.obj” “x64\Release\random\_normal.obj” “x64\Release\spline.obj” “x64\Release\rouwenhorst.obj” “x64\Release\bspline\_sub\_module.obj” “x64\Release\main1.obj”  
> Embedding manifest…

Here’s how I was trying to compile in gfortran:

> gfortran -c -O2 -m64 bspline\_sub\_module.f90  
> gfortran -w -ffree-form -ffree-line-length-0 -m64 -O2 -fopenmp main1.f90 random\_normal.f90 linspace.f90 bspline\_sub\_module.o rouwenhorst.f90 spline.f90 splint.f90  
> gfortran -fopenmp main1.o random\_normal.o linspace.o bspline\_sub\_module.o rouwenhorst.o spline.o splint.o

What am I doing wrong?

Here’s a video with what I mean in terms of differences in thread/core usage: [Dropbox - File Deleted - Simplify your life](https://www.dropbox.com/s/sc2ebrzckz9y0m1/Video_fortran.mp4?dl=0)

The first iteration of the ifort loop runs in 19.9 seconds with 100% CPU usage, the first iteration of the gfort loop runs in 38.8 seconds (almost double) with 50% CPU usage.  
If I compile my ifort code with \qm32 flag it gets similar performance to the gfortran.

---

<div class="post-metadata">

**Author:** ![MarDie](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mardie/32/54_2.png) [@MarDie](https://fortran-lang.discourse.group/u/MarDie)\
**Post date:** [March 31, 2022, 8:50pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/2 "2022-03-31T20:50:23Z")

</div>

If you want to run the code only on the machine where it is compiled, use `-march=native` for gfortran. On my system, the performance gain was impressive.

---

<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, 8:50pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/3 "2022-03-31T20:50:32Z")

</div>

Overall, in many cases from my experience, on Linux, ifort and gfortran have similar performance. ifort usually 10-20% faster, but the difference are not very big. However, it seems on windows, with the same flags, gfortran can be 7 times slower than its performance on Linux. Intel’s performance is consistent on windows and Linux.  
You may begin with the following flags.  
For ifort,

```auto
-O3 -xHost

```

For gfortran

```auto
-O3 -march=native

```

Both can be used with perhaps one single flag,  
For ifort,

```auto
-fast

```

For gfortran

```auto
-Ofast

```

See also,

> [@Optimization flags for ifort and gfortran](https://fortran-lang.discourse.group/t/optimization-flags-for-ifort-and-gfortran/2721/3):
>
> @Arjen: My understand was exactly the other way round. The gcc manual says: “Specifying -march=cpu-type implies -mtune=cpu-type" and "march=cpu-type allows GCC to generate code that may not run at all on processors other than the one indicated.” The Intel manual says for -xcode: Do not use code values to create binaries that will execute on a processor that is not compatible with the targeted processor.

> [@Is -Ofast in gfortran or -fast flag in Intel Fortran safe to use?](https://fortran-lang.discourse.group/t/is-ofast-in-gfortran-or-fast-flag-in-intel-fortran-safe-to-use/2755):
>
> Dear all, I just wonder, has anyone use -Ofast in gfortran or -fast flag in Intel Fortran? I sometimes find that some results with -fast are a little different from -O3. Is -Ofast or -fast safe to use? Thank you very much in advance!

---

<div class="post-metadata">

**Author:** ![NC1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@NC1](https://fortran-lang.discourse.group/u/NC1)\
**Post date:** [March 31, 2022, 10:46pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/4 "2022-03-31T22:46:55Z")

</div>

CRquantum, what puzzles me more is the fact that gfort does not make use of all the logical processors available on my computer (see video). It is very puzzling to me.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [March 31, 2022, 11:31pm UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/5 "2022-03-31T23:31:09Z")

</div>

> [@NC1](#):
>
> gfort does not make use of all the logical processors available

If I had to take a guess I would say that it’s probably because of different default variables between the two compilers.

There are some very experienced gfortran devs here that might be able to shed some light into this.

As for the difference in per thread performance, I would expect it to be noticeable. Intel’s MKL is an absolutely amazing library that can significantly speed up calculations.

Although I think you can still link to MKL whilst using gfortran

---

<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:** [April 1, 2022, 12:31am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/6 "2022-04-01T00:31:43Z")

</div>

I am not gfortran expert. Other people’s opinion may be more useful.  
But it looks like you used openMP. The speed difference seems is simply caused by the fact that as you said, gfortran only uses half of the threads as ifort did.  
If in openMP you explicitly specify the number of threads you want to use, does that help gfortran to make full use of all the threads? But again, I am not expert in gfortran, other people may give you much better answer and solve the puzzle.

By the way, the `-m64` flag in gfortran may be not necessary.  
`-O3 -march=native` may be enough to begin with, in many cases.

Welcome to the Discourse by the way! 🙂

---

<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:** [April 1, 2022, 12:39am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/7 "2022-04-01T00:39:34Z")

</div>

Thank you @gnikit for mentioning intel MKL.

Uhm, may I ask, by performance boost using intel MKL, do you mean using the function’s provided by Intel MKL could give a performance boost or something?  
The only reason I did not dig too much into MKL is that, I feel if I use a lot of MKL exclusive function/subroutines, then my code will perhaps be intel Fortran exclusive. So it will not compile on both gfortran and ifort, therefore may not be generic enough.

Or like, do you mean, like, some same lapack subroutines, like you know dgesv, dgeev, or something, by linking MKL there will be performance boost than not linking MKL? Just curious. Again, I guess I am neither expert in ifort or gfortran, just curious. Thanks!

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [April 1, 2022, 8:31am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/8 "2022-04-01T08:31:45Z")

</div>

I think MKL does both. They definitely provided optimised versions of libraries like BLAS and LAPACK, etc. but they also seem to have proprietary modules too see the [docs](https://www.intel.com/content/www/us/en/develop/documentation/onemkl-developer-reference-fortran/top.html).

As for Fortran intrinsics like `sum` or `mult` I am not certain whether or not they use MKL in the background.

I have only used MKL passively by providing it at configuration time to external libraries like PETSc and Zoltan

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [April 1, 2022, 10:52am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/9 "2022-04-01T10:52:32Z")

</div>

We’d need to learn more about how OpenMP/multi-threading is being used, e.g. is dynamic scheduling being used.

A few other comments:

- I don’t see much purpose in using the [`-m64`](https://www.intel.com/content/www/us/en/develop/documentation/fortran-compiler-oneapi-dev-guide-and-reference/top/compiler-reference/compiler-options/compiler-option-details/code-generation-options/m32-m64-qm32-qm64.html) flag unless you’re cross compiling or for some other odd-reason.
- As others have said, try using `-march=native` with gfortran.
- How are you setting the number of OpenMP threads?

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [April 1, 2022, 11:41am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/10 "2022-04-01T11:41:17Z")

</div>

> [@ivanpribec](#):
>
> How are you setting the number of OpenMP threads?

I think this is the most important bit. Also, @NC1 are you using dynamic teams? If so try and disable them

```fortran
call omp_set_dynamic(.false.)

```

---

<div class="post-metadata">

**Author:** ![NC1](https://avatars.discourse-cdn.com/v4/letter/n/ee7513/32.png) [@NC1](https://fortran-lang.discourse.group/u/NC1)\
**Post date:** [April 7, 2022, 12:21am UTC](https://fortran-lang.discourse.group/t/equivalent-gfort-and-ifort-compilation/3092/11 "2022-04-07T00:21:15Z")

</div>

I am just doing this: call omp\_set\_num\_threads(80). But it does not really matter whether I include this line or not. `call omp_set_dynamic(.false.)` did not make the gfort code any different as well.
