# OpenMP stochastic simulation

**URL:** <https://fortran-lang.discourse.group/t/openmp-stochastic-simulation/8606>\
**Category:** Uncategorized\
**Created:** [September 16, 2024, 4:20pm UTC](https://fortran-lang.discourse.group/t/openmp-stochastic-simulation/8606 "2024-09-16T16:20:00Z")\
**Posts on this page:** 1\
**Showing post:** 27

<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:** [September 18, 2024, 1:02pm UTC](https://fortran-lang.discourse.group/t/openmp-stochastic-simulation/8606/27 "2024-09-18T13:02:55Z")

</div>

On my PC (Ryzen5700X(8-core, max-freq set to 3.4GHz) + Ubuntu22), the timing with gfortran-12 -O3 -march=native -fopenmp is like this:

```auto
1-thread : stdout -> 11.6 sec (time command -> 8.50user 4.55system)
2-thread : stdout -> 6.8 sec (time command -> 8.57user 4.58system)
4-thread : stdout -> 4.2 sec (time command -> 8.63user 4.54system)
8-thread : stdout -> 3.0 sec (time command -> 8.72user 4.64system)

```

If I replace `random_number()` with `u = 0.5d0`, the result is:

```auto
1-thread : stdout -> 6.9 sec (time command -> 3.74user 4.57system)
2-thread : stdout -> 4.4 sec (time command -> 3.83user 4.58system)
4-thread : stdout -> 3.1 sec (time command -> 3.81user 4.64system)
8-thread : stdout -> 2.4 sec (time command -> 3.85user 4.84system)

```

For serial runs, my PC is ~ 2 times slower than your laptop (= Core i7 12800H, max 4.8 GHz?, the memory (DDR4 vs DDR5?) may also be different):  
[https://www.cpubenchmark.net/cpu.php?cpu=Intel+Core+i7-12800H&id=4778](https://www.cpubenchmark.net/cpu.php?cpu=Intel+Core+i7-12800H&id=4778)

But with threading the program becomes faster to some extent, although the scaling is not very good (probably because memory bound? (as mentioned above))

These pages may also be related:

> [@Learning coarrays, collective subroutines and other parallel features of Modern Fortran](https://fortran-lang.discourse.group/t/learning-coarrays-collective-subroutines-and-other-parallel-features-of-modern-fortran/1146/18):
>
> The ifort RANDOM\_NUMBER is known to use an exclusive lock in threaded applications, reducing performance. It might not be the best choice if you’re comparing performance.

> **[MSVC mutex is slower than you might expect](https://stoyannk.wordpress.com/2016/04/30/msvc-mutex-is-slower-than-you-might-expect/)**
>
> TLDR; The 2015 MSVC C++ runtime’s std::mutex (and potentially other) implementation is significantly slower than the equivalent code written by hand. The reason is that the runtime is built w…

Though this is not about ifort + Windows, similar things might be happening…? (But it also seems (to me) that the speed-down is too large for the above laptop.)

---

_[View the full topic](https://fortran-lang.discourse.group/t/openmp-stochastic-simulation/8606)._
