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

passing information to functions

ffmda
Beginner
677 Views

I'm new to the Fortran 90/95 syntax. I often do function minimizations, and I want touse IMSL routines like UVMIF, which minimizes a function F(x). However, I need F(x) to have access to the values of a lot of parameters in addition to the value of X.

In my Fortan 77 code, I would solve this using a common block that both the Program calling UVMIF and the function F had access to. What's the best way to do this in Fortran 90/95?

Thanks

0 Kudos
3 Replies
Steven_L_Intel1
Employee
677 Views

Module variables are generally the replacement for COMMON, though any sort of global state is often frowned upon. Ideally, there would be a way to pass some sort of arbirtary context to the routine, but IMSL doesn't allow for that, so global state it is.

0 Kudos
forall
Beginner
677 Views

Steve,

When you say ideally, do you mean this is possible in Fortran (passing arbitrary context to a routine)? This type of problem (of efficiently and 'elegantly' passing data to procedures) crops up frequently enough for me, but I havent yet worked out a decent general solution (not just in IMSL but in general even if i have complete control over the code).

I am also curious why global states are often frowned upon. Any chance you could elaborate on that?

thanks in advance,

dmitri

0 Kudos
Steven_L_Intel1
Employee
677 Views

Sure, it's possible in Fortran if you design for it. The interfaces I am familiar with on various platforms usually have an address-sized user context value which you pass in to the routine and it passes it to your callback routine. That value can be anything you want - a number, the address of a data structure, etc. You can deal with this in Fortran in various ways - if you have a variable you want to pass, use loc(variable) and have the callback routine accept a variable of that type by reference.

IMSL doesn't provide for this sort of thing, so my mention of it was meant to be educational.

Global state makes it difficult to parallelize code and also prevents use of recursion. It also can make it easy to introduce errors into the code, either at initial implementation or down the road. We've had a project for the last year or so to remove as much global state from the compiler as possible with the goal of multithreading large parts of the compilation process.

A bit of thought in advance can save you lots of trouble down the road. On the other hand, if the global data is read-only, that's generally harmless. Use module variables to avoid the problems that mismatched COMMON declarations can cause. (It also helps you identify what parts of code can access the variables and what can't.)

0 Kudos
Reply