# FORTRAN version of libff-mmap-semaphore.c

**URL:** <https://community.freefem.org/t/fortran-version-of-libff-mmap-semaphore-c/124>\
**Category:** Feature Request\
**Created:** [September 3, 2019, 8:36am UTC](https://community.freefem.org/t/fortran-version-of-libff-mmap-semaphore-c/124 "2019-09-03T08:36:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gfnallo](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@gfnallo](https://community.freefem.org/u/gfnallo)\
**Post date:** [September 3, 2019, 8:36am UTC](https://community.freefem.org/t/fortran-version-of-libff-mmap-semaphore-c/124/1 "2019-09-03T08:36:26Z")

</div>

Dear FreeFem++ developers,  
thanks first of all for the great work.

I am interested in coupling a (large) Fortran code with a FreeFem++ application. I have been considering several options. As mentioned in the topic title, option 3) is the one I would like to draw your attention to:

1. Data exchange by writing on files (rough, slow and unstable but it requires very little modification to both the Fortran code and the FreeFem++ application) - this is the one I have currently implemented. Nevertheless, loss of synchronization between the two codes and data loss are frequent, so I am not satisfied;
2. Coupling of the two codes by means of an external coupling framework (I have been considering OpenPALM [[https://www.cerfacs.fr/globc/PALM\_WEB/](https://www.cerfacs.fr/globc/PALM_WEB/)]). This is probably the most flexible and robust way, but it is not completely straightforward to implement, and after many attempts I gave up -I am still considering the option of contacting the cerfacs developing team, though…- ;
3. Using the mmap/semaphore feature you mention at paragraph 3.7.3 of the manual. This would be very stable and immediately applicable.

The issue with point 3) is that the current version of the library is written in C. It would be great if you could provide a Fortran equivalent for libff-mmap-semaphore.c and libff-mmap-semaphore.h. Do you think it would be possible to do that?

Thanks in advance,  
best regards

G. F.

---

<div class="post-metadata">

**Author:** ![frederichecht](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/frederichecht/32/15_2.png) [@frederichecht](https://community.freefem.org/u/frederichecht)\
**Post date:** [September 3, 2019, 2:06pm UTC](https://community.freefem.org/t/fortran-version-of-libff-mmap-semaphore-c/124/2 "2019-09-03T14:06:37Z")

</div>

[fortan\_master.f90.zip](https://community.freefem.org/uploads/short-url/pAyLdEEeaGA6DLBstS9CqAYnzBJ.zip) (1.0 KB)  
and example in fortran:  
to compile with the freefem++ source : on ~/ff/ff-git/

ifort fortan\_master.f90 -I~/ff/ff-git/plugin/seq/ ~/ff/ff-git/plugin/seq/libff-mmap-semaphore.o -o fortan\_master

brochet:couplage-code-with-ff++ hecht$ ./fortan\_master  
len 1024 size 1024  
ffmmap\_write\_ 1 0.000000 8  
– FreeFem++ v4.400001 (Ven 30 aoû 2019 14:21:56 CEST - git v4.4-1-2-g05f06665)  
Load: lg\_fem lg\_mesh lg\_mesh3 eigenvalue  
sizestack + 1024 =1808 ( 784 )

len 1024 size 1024  
ff status = 1  
ffmmap\_write\_ 4622382067542392832 11.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 11 1  
\*\* ff 11 0.195774  
ffmmap\_read\_ 4596221518108646428 0.195774 16  
iter 1 0.195773892045254  
ffmmap\_write\_ 4622945017495814144 12.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 12 1  
\*\* ff 12 0.232987  
ffmmap\_read\_ 4597562265627791501 0.232987 16  
iter 2 0.232987111194351  
ffmmap\_write\_ 4623507967449235456 13.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 13 1  
\*\* ff 13 0.273436  
ffmmap\_read\_ 4598597409716504116 0.273436 16  
iter 3 0.273436262443371  
ffmmap\_write\_ 4624070917402656768 14.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 14 1  
\*\* ff 14 0.317121  
ffmmap\_read\_ 4599384370216871847 0.317121 16  
iter 4 0.317121345792311  
ffmmap\_write\_ 4624633867356078080 15.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 15 1  
\*\* ff 15 0.364042  
ffmmap\_read\_ 4600229624087637234 0.364042 16  
iter 5 0.364042361241174  
ffmmap\_write\_ 4625196817309499392 16.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 16 1  
\*\* ff 16 0.414199  
ffmmap\_read\_ 4601133171328800200 0.414199 16  
iter 6 0.414199308789958  
ffmmap\_write\_ 4625478292286210048 17.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 17 1  
\*\* ff 17 0.467592  
ffmmap\_read\_ 4602095011940360802 0.467592 16  
iter 7 0.467592188438664  
ffmmap\_write\_ 4625759767262920704 18.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 18 1  
\*\* ff 18 0.524221  
ffmmap\_read\_ 4602896982547482941 0.524221 16  
iter 8 0.524221000187289  
ffmmap\_write\_ 4626041242239631360 19.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 19 1  
\*\* ff 19 0.584086  
ffmmap\_read\_ 4603436196223660859 0.584086 16  
iter 9 0.584085744035839  
ffmmap\_write\_ 4626322717216342016 20.000000 0  
bb ffsem\_post  
bb ffsem\_wait  
After wait … ff 20 1  
\*\* ff 20 0.647186  
ffmmap\_read\_ 4604004556585037569 0.647186 16  
iter 10 0.647186419984308  
ffmmap\_write\_ 0 0.000000 8  
Fin Master  
After wait … ff 20 0  
Fin FreeFem++  
Fin Freefem dans master

---

<div class="post-metadata">

**Author:** ![gfnallo](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@gfnallo](https://community.freefem.org/u/gfnallo)\
**Post date:** [September 4, 2019, 6:16am UTC](https://community.freefem.org/t/fortran-version-of-libff-mmap-semaphore-c/124/3 "2019-09-04T06:16:45Z")

</div>

Dear prof. Hecht,  
thanks a lot for the very prompt reply and for your explanation.

Indeed, once compiled, the functions contained in the library libff-mmap-semaphore.c (thus becoming libff-mmap-semaphore.o) can be easily called from a FORTRAN code such as the one you have kindly provided. It is sufficient to dynamically link the library when compiling the FORTRAN code.

This perfectly solved my problem, thanks again!

G. F.
