Replacing Common blocks and using LAPACK on windows

Hi,
I am trying to port an old F77 code to modern style. What is the get around for COMMON blocks ?

Declare all common data in a separate module and use that module wherever you require the data.

2 Likes

Note that in the past, programmers would play memory-saving tricks with COMMON blocks, such as declaring the COMMON block in different ways in different routines - a COMMON block simply makes a piece of memory available and it is up to you to partition that into variables or arrrays. If that is the case, then you may have to carefully disentangle the use to a get well-behaved data structure.

As @mfurquan indicates, you can start by putting the COMMON block into a module and use the module. That way you guarantee that all COMMON blocks have the same partitioning. If that works well, you can change the variables in the COMMON blocks into ordinary variables in the module.

One other thing to keep in mind: you may need to convert a BLOCK DATA block if that is indeed used.

Here is a trivial example for the first step:

module common
    real :: x, y, z
    common /mycommon/ x, y, z(10)
end module common

then use the module “common” in stead of the COMMON block

Much better to write

module common
real :: x,y,z(10)
end module common

and eliminate the COMMON statement altogether. Note that the COMMON statement is an obsolescent feature in Fortran, and its use is discouraged.

Thanks @Arjen, @billlong .
@Arjen I understand what you meant by keeping common block in a separate module - to get a well behaved data structure. What @mfurquan and @billlong suggested is identical I think.
Moreover can you tell me where can I get BLAS and Lapack libraries on windows ?
I think discussion is actively going on whether to include them in stdlib. I think the earliest we do, the best it is. Right now linear algebra section in stdlib is looking so empty. We can incrementally add user friendly interfaces later. Once we add that Fortran will be in some sense as powerful as Matlab (atleast for linear algebra). @certik what do you say ?

I have used COMMON blocks in the past via include files - that way you could guarantee that they were the same in all program units. But you can actually do something like this:

subroutine suba
integer i
common /mycomm/ i(200)
end subroutine suba
subroutine subb
real r
complex c
common /mycomm/ r(10), c(15000)
end subroutine subb

(Note the different lengths and different types)

This is because a COMMON block merely reserves a (named) block of memory. If your program does something like that, then you will have to unravel that. When I wrote my reply, I thought there would be a bit of benefit keeping the COMMON structure as a first step (to bring out the differences in the instances of the COMMON blocks, for instance), but that is not the case. Do the changes as suggested (and use IMPLICIT NONE, of course) and the compiler will complain when necessary.

1 Like

Even if we add modern interfaces to Lapack in stdlib (which we should!), I would imagine stdlib would just depend on Lapack as an fpm package.

So the first step should be to make Lapack and Blas fpm packages and ensure they install on Windows easily. The same with OpenBLAS. Then you can use them right away.

2 Likes

That looks like a “fun” project :slight_smile: One of the things that will require some thought is the installation on Windows. Where would you put the files? I may be completely wrong, but I do not know of a convention for installing libraries (not programs) on Windows.

FWIW, I found this LAPACK for Windows
I don’t understand anything about the build systems. Please see if this can be incorporated in fpm.

There is little that fpm can do - the site provides you with prebuilt libraries and you can simply include them in your build procedure. fpm is about building the libraries themselves, among other things.

I did find this three-years old post: linux - Where to "install" thirdparty libraries & header files to on Windows? - Stack Overflow

From that I conclude that the question is one without a definitive answer

Thanks @Arjen . For now I will be happy with Cygwin.

See the thread Gfortran compiled LAPACK libraries for Windows, especially the last post.

Can the moderators create a new thread and transfer a few posts, since this is not about Replacing Common Blocks?

1 Like

Agree.

For any reader interested in the specific issue with COMMON or the general one of updating old programs (always a noble pursuit in my opinion; the supposed adage if it ain't broke, don't fix it popularized by a questionable official in a US Administration of even further questionable merit during the difficult late 1970s is mostly without merit and insufficient thought and insight into human endeavor in my experience), consider almost any of the books with “modern Fortran” in its title. They all tend to have well-thought out sections on the topic. Examples:

1 Like

I had some experience in the past building LAPACK for Windows using MinGW. It leverages CMake to build the library. I think I can start work on integrating this into fpm.

1 Like