Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29030 Discussions

Use case of TLS index in Fortran

netphilou31
New Contributor III
442 Views

Hi,

I'am looking for some examples of use of TLS index in Fortran without OpenMP.

Is that possible?

Best regards,

0 Kudos
6 Replies
Steve_Lionel
Honored Contributor III
375 Views

I assume TLS here means Thread Local Storage, but otherwise I don't know what you're asking.

0 Kudos
jimdempseyatthecove
Honored Contributor III
341 Views

>>I'am looking for some examples of use of TLS index in Fortran without OpenMP.

You will have to be more specific.

Your query implies that your Fortran code is run in a multi-threaded environment.

However, while you state "in Fortran without OpenMP", you do not state how/where the additional threads are created/managed.

And, where and how the private variables are declared.

 

Should you have a Fortran library that is called from a multi-threaded main program (e.g. C++ using __declspec(thread) ...),

then have the calling thread pass the argument(s) by value or reference (to its TLS storage).

 

Thread private management is not guaranteed to be the same across different languages. You will have to be creative, and do not rely "it worked in one environment". 

If you have a collection of TLS variables declared in C/C++, and you want individual access in your called Fortran procedures, then I suggest you encapsulate those variables within a struct (that is marked for TLS), then pass the reference to the struct to your Fortran procedure(s). The Fortran code can use BIND(C) on its declaration of the type.

 

Jim Dempsey

0 Kudos
JohnNichols
Valued Contributor III
306 Views

https://github.com/mijacobs

 

and https://learn.microsoft.com/en-us/windows/win32/dlls/using-thread-local-storage-in-a-dynamic-link-library 

 

I would start with someone like this who publishes code about this sort of stuff, indirectly in C++. 

 

Sometimes as Jim indirectly notes, you are the first one down the valley (of death or Fortran failure).  Good luck here, before this Forum it was a lonely path, filled with orcs and trogs. 

 

Fortran is a great language but it is not the other languages, it is for speed and number crunching, it is like writing in Old English, the vocabulary is not modern English, so there is no word for gun in Old English, they call it a firestick.

0 Kudos
netphilou31
New Contributor III
267 Views

Hi all,

Thanks for your replies and sorry for not being more specific.

My goal was to be able to remove some common blocks variables initialized in a DllMain procedure of a dll without the need to make the corresponding common THREADPRIVATE through the use of OpenMP directives. But finally I was able to build something from what I found (and referenced by John) in Microsoft Learn website : Using Thread Local Storage in a Dynamic-Link Library - Win32 apps | Microsoft Learn

And to answer Jim's question, the treads are not created by a Fortran application, but by the caller which will be C#, C++ or Delphi application.

It compiled fine but now I need to test it in a multithread environement.

Brest regards,

0 Kudos
jimdempseyatthecove
Honored Contributor III
219 Views

Then I would suggest you place the C# and C++ thread private variables in one or more structs.

On the Fortran side, make module containing user defined types, BIND(C), that maps those structs appropriately.

in this module, use the OpenMP directive to declare threadprivate pointers to each of those structs.

Also create a subroutine that initializes the pointer upon a call from the C# or C++ program passing in its threadprivate pointer to its structure (use C_F_POINTER for conversion).

 

While you will need to compile with -openmp, the only place in the Fortran code that uses the !$OMP is this module.

You will need to USE this module wherever you access the pointers. (if you can place the threadprivate pointers and initializatoin code in a currently USE'd module, then the other source files need not be touched). You will need to compile those with -openmp else the code generation may not insert the TLS specific encoding.

 

Good luck,

 

Jim Dempsey

netphilou31
New Contributor III
179 Views

Hi Jim,

Thanks for your reply and advices. Since I am at the early stage of development of this feature, and even if I am familiar with BIND(C) and C_F_POINTER stuff, I am not sure to understand everything you said. However, for the time being I don't need to recover any pointer from the caller, I just need to store some global variables in a TLS index so I can recover them correctly. Furthermore, I would like, as far as possible, to avoid any OpenMP directive since I sometimes noticed a huge performance impact when using them.

Best regards,

0 Kudos
Reply