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

Does // String Concatenation do any dynamic allocation

c_armstrong
Beginner
1,243 Views

Hi

I am trying to write my Fortran is a high performance style. My strategy for doing this is to avoid anything dynamic (ie that has to be decided at runtime) in order to give the compiler the best chance of optimising things.

So I am happy to use modern features like Derived Types, but would avoid EXTENDS as this would then involve dynamic type binding, limiting the foresight of the compiler.

In this vein I also want to avoid dynamic memory allocation. 

I am wondering what // and TRIM in particular actually do. Does the compiler create its own string of length equal to the length of all the Strings concatenated with // or is a String allocated dynamically at runtime? Also does TRIM allocate a new String dynamically or just work with the original string and a substring index equal to the last non blank?

Please feel free to correct false assumptions in the above points. Also I am using the "Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference" which although excellent in terms of specifying inputs and outputs, doesn't give many hints about underlying implications or what is actually going on? Is there another document with this kind of information for each intrinsic function and operator?

Thank you for your help. 

 

 

 

 

0 Kudos
1 Solution
Steve_Lionel
Honored Contributor III
1,221 Views

It depends on the context. By default, if a temporary value is needed it will be created on the stack. Assuming you're not using deferred-length allocatable variables, assigning a concatenation doesn't need a temp at all.  My experiments with TRIM don't show it creating a temp.  The heap-arrays option tells the compiler to dynamically allocate any temps it needs.

View solution in original post

0 Kudos
5 Replies
Steve_Lionel
Honored Contributor III
1,222 Views

It depends on the context. By default, if a temporary value is needed it will be created on the stack. Assuming you're not using deferred-length allocatable variables, assigning a concatenation doesn't need a temp at all.  My experiments with TRIM don't show it creating a temp.  The heap-arrays option tells the compiler to dynamically allocate any temps it needs.

0 Kudos
c_armstrong
Beginner
1,187 Views

Thank you. So from the fact you did experiments, the best way to answer those sort of questions is to experiment i.e. there isn't a standard reference on such issues? 

0 Kudos
Steve_Lionel
Honored Contributor III
1,167 Views

There is not - the Fortran standard says what the results should be, but not how it is achieved. Also, the answer to how a compiler does something can change over time.

My usual advice is to write code that is readable and does not complicate things in the pursuit of micro-optimization. The compiler optimizes straightforward code the easiest. You can run the program through a performance analyzer, such as Intel VTune, to see where time is being spent if you feel performance is not where you want. The hot spots are usually not where you think they are.

If you really want to make sure you're not using dynamic allocation, generate a link map and search the list of symbols for the string "alloc". I think this will catch them all.

0 Kudos
c_armstrong
Beginner
1,143 Views

Thanks. I'll make a mental note of the link map trick. Do expert level Fortran programmers check the assembly code much? My first job was Fortran but then went down the Java etc route for readability, maintainability reasons. Diving back into Fortran as got a use case where microoptimisation is the name of the game!

0 Kudos
Steve_Lionel
Honored Contributor III
1,124 Views

No, few check assembly code except for trivial examples. I do it in cases such as your question as it's the easiest way to see what's actually happening - assuming that one can read and understand the assembly! I used to write assembler code decades ago, would not even consider doing so today.

Reply