Advanced Fortran 90: Callbacks with the Transfer Function Justine Dune, 6 months ago 0 6 min read 550 This post is not specific to the Mac, but there are probably many MR readers that have to use Fortran in some aspect of their work, so it certainly can’t hurt. Fortran on a Mac. First let me say I’m not a big Fortran user but any blog posts about Fortran always seem to be very popular, and I do get asked regularly about how to compile Fortran applications. Here are a series of mini-book tutorials that are aimed at beginners and experts Fortran Learn Books.
This post is not specific to the Mac, but there are probably many MR readers that have to use Fortran in some aspect of their work, so it certainly can’t hurt…
Callbacks for Mac
A few weeks ago I was lamenting the difficulty of implementing decent callback functionality in Fortran 90. A callback is common place in C programming; it basically allows you to customize the behavior of a subroutine. Take this simple example:
The point of this rather obscure example is that the main program can call the
MakeSoundTenTimes
function passing a function pointer. (If you are not familiar with C’s cryptic function pointer syntax, don’t worry about it: it is not important.) The loop in MakeSoundTenTimes
can call the function passed in, effectively customizing its behavior. It’s a bit like rewriting the inside of the MakeSoundTenTimes
each time: the function passed determines the results.You can actually do this in Fortran 90. It looks like this:
Callbacks with Arbitrary Arguments
This works fine, but problems start to arise if you need to pass some data to the callback. In C, it is conventional to include a void pointer, which can point to any type of data. But Fortran 90 doesn’t have void pointers, so how do you pass arbitrary data to the callback? To see how you can do this, let’s again begin with an example from C which passes data via a void pointer:
This is quite similar to the original example, but now the callbacks take a
void*
argument, as does the IncrementTenTimes
function that calls back. The callback functions know what type of data is passed to them, so they cast the void pointer to the appropriate type, increment, and print. The IncrementTenTimes
function, on the other hand, does not know anything about what the data represents. It is just a generic pointer that gets passed on to a callback function. This is actually a strength of the callback pattern — the behavior of the calling-back function and the callback function are largely decoupled, facilitating code reuse.For a long time, I didn’t think you could have this sort of callback in Fortran 90, but I recently realized that you can. It involves a relatively obscure intrinsic function called
transfer
. If you ask the majority of Fortran programmers what transfer
does, chances are you will get a blank look or a rather vague answer. What transfer actually is is a means of casting data from one type to another.So what would the above C example look like in Fortran 90 with the
transfer
function. Here it is:Now I’m the first to admit this is a little more verbose than the C version, and that’s putting it mildly, but it is possible to do, and could be a very handy tool in certain instances.
So how does it work? Let’s take a
transfer
function call from the main program, and dissect that:What this does is copy the bytes of the variable
r
, returning it with the type of data
. The second argument to the transfer
function is there purely to tell the function what type of data it should return. The data returned by transfer
is then stored in the variable data
, which is an array of characters.If the second argument to
transfer
is an array, no matter how long, it will figure out how big the return array needs to be to fully accommodate the type passed in as first argument. That’s pretty nifty, and you can see that we use that to determine how big our data array needs to be to be able to contain the real
or integer
variable being passed. In particular, this linedoes the conversion, but only uses the result to send to the
size
intrinsic to determine how long the data
array should be. The data
array is then allocated (malloc
ed for our C friends) and transfer
is called again, this time with the result actually getting stored in data
.One final thing before signing off: we have used an array of
character(len=1)
to store generic data here, but you can use any array type you like. transfer
will figure out from the type you are using how many elements the array needs to have. I have chosen to use an array of characters, because a character usually corresponds to one byte, and that way I won’t be wasting any memory. But you could also use an array of integers, for example, and then your data granularity would be something like 4-bytes, depending on the operating system and compiler.Fortran is often underestimated as a programming language, but there is plenty in there for scientific developers. Some aspects of the language, like
transfer
, can seem a bit abstract to begin with, and their potential not fully realized. Hopefully this little tutorial has shown you that transfer
is useful, and encourages you to seek out other uses yourself. (Hint: You can also build data containers for generic types, like dynamic arrays and dictionaries, using transfer
.)Publisher's Description
Simply run the command:. HPC distributes gfortran as a tar-file. Therefore each file contained in the tar-file should be removed. The easiest way to do this is probably to run the command:. This will remove all files but not directories, which may contain other files that are not to be removed.
To remove empty directories only, then run the command:. The R Project distribution is a. To list the files that were installed from this package, one needs to locate the 'receipt', which is a.
On my system Mac OS X To find the file, run a command such as:. Once the appropriate file has been located, a list of files that have been installed can be found by running the command:.
How to install gfortran on Mac OS X – skipperkongen
Fortran 90 Install
Therefore, similarly to removing the HPC distribution, the files can be removed by running commands such as:. Does anyone have suggestions as far as what I need to download starting from the very beginning? Get started by installing homebrew. Homebrew is a package manager - it lets you quickly and easily download and install tools, libraries, and applications that are usually pre-compiled and always ready to work with your Mac.
Downloads - NAG Fortran Compiler Versions
Fortran 90 Pdf
Then you can invoke the command gfortran to compile your code. I've been using gcc in a mac and I never had a problem. You could try Simply Fortran for macOS. It includes a compiler and a development environment. On first run, it should trigger installation of some Apple development tools you might need.
Mac Os Fortran
The GNU Fortran See More News NOTE: Users will need at least version 3. With the release of Simply Fortran version 3.
Microsoft Fortran 90
Download Version 3.