FORTRAN version of libff-mmap-semaphore.c

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/]). 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.

fortan_master.f90.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

1 Like

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.