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

Associate block performance

FlyingHermes
New Contributor I
366 Views

I was just wondering how does the compiler treat associate block when the associate-name "points" to a function results as in:

[fortran]  associate( Var1 => This%Fun1(a,b) )
    Var2        =       This%Fun2(c,d,Var1)
  end associate[/fortran]

My 2 questions are

  1. Does the compiler treats this by simply replacing the associated name (here "Var1") by the selector (here "This%Fun1(a,b)") inside the associated block ? If so, can associate blocks be seen as a sort of inlining for variable instead of procedures.
  2. Does associate blocks really  have no effect on performance ?

Thanks.

0 Kudos
3 Replies
jimdempseyatthecove
Honored Contributor III
366 Views

I think the real question to ask is with this code:

[fortran]
type(someType), pointer :: This
...
associate( Var1 => This%Fun1(a,b) )
...
This => That
Var2 = This%Fun2(c,d,Var1)
end associate
[/fortran]

What value for "This" does Var1 use? Prior "This" or subsequent (current) "This"?

Jim Dempsey

0 Kudos
Steven_L_Intel1
Employee
366 Views

ASSOCIATE is not like macro expansion. Probably the best way to think of it is like a pointer that points to the selector expression. There are some limitations as to what you can do inside the block. In the first case shown, the function is called when the block is entered and its result assigned to the newly created variable var1. Inside the block, var1 is used like a normal variable - there's no inlining going on.

It gets more interesting when you use something like:

associate (var1 => a%b(3))

where b is also a derived type. You can, inside the block, do something like var1%c.

In Jim's example, it uses the value of This at entry to the block.

As for performance impact, I would say it is minimal to nonexistent.

0 Kudos
FlyingHermes
New Contributor I
366 Views

Thanks for the answer.

0 Kudos
Reply