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

Module vs. Common block data: initialize with deafult values

joey_hylton
Beginner
486 Views

(I repost this because my previous post is in other people's thread. BM please help me delete the old one, thanks)

I have a common block data as follow:

BLOCK DATA
INTEGER*4IREST,ISTR,IS1,IS2,NPC,NDER,LOTEM,IPR
REAL*8 EPS,SC2MIN
COMMON /LCONTR/IREST,ISTR,IS1,IS2,EPS,SC2MIN,
& NPC,NDER,LOTEM,IPR
DATAirest/-1/,istr/100/,is1/0/,is2/0/,eps/1D-9/,
& npc/1/,nder/0/,lotem/0/,ipr/0/,sc2min/-1D0/
END

I want to transfer all common blocks to Module, and create a module as:

MODULE LCONTR
INTEGER*4 IREST,ISTR,IS1,IS2, NPC, NDERIREST
REAL*8 EPS,SC2MIN
END

But I have questions about this. In the common block, the variables are initialized with deafult values, but how can I initilize the variabes in module? Because my code will use common block data many times, in my understanding, it will set all the variables as the deaults values when it start tousethem (the program will change the values during the running, and initialize themat every beginning of the loop). But with module, how can I do the same thing?

Thanks in advance!

0 Kudos
8 Replies
Steven_L_Intel1
Employee
486 Views
You use initialization clauses. For example:

MODULE LCONTR
INTEGER, PARAMETER :: K_INT = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: K_DBL = SELECTED_REAL_KIND(15,300)
INTEGER(K_INT) :: IREST = -1
INTEGER(K_INT) :: ISTR = 100
INTEGER(K_INT) :: IS1 = 0
INTEGER(K_INT) :: IS2 = 0
INTEGER(K_INT) :: NPC = 1
INTEGER(K_INT) :: NDERIREST = 123
REAL(K_DBL) :: EPS = 1.0D-9
REAL(K_DBL) :: SC2MIN = -1.D0
END

I have also taken the liberty to replace your use of the extensions INTEGER*4 and REAL*8 with the recommended style using standard-conforming and portable features of the language.

The key to the initialization question is to use the double-colon form of declaration and the initialization clause with an equals sign.

0 Kudos
joey_hylton
Beginner
486 Views

Thanks, steve.

So, in my code, when I declear variables, should I also use the standard-conforming and portable features as

USE LCONTR, ONLY: K_INT, KDBL

Implicit none

INTEGER(K_INT) I,J,K

REAL(K_DBL) E,F,G

0 Kudos
Steven_L_Intel1
Employee
486 Views
That's the recommended style. What a lot of people do is create a module that has only the declarations of the various kind constants and then use that module everywhere, but it's up to you.

If you do this, rather than have constants such as 1.0D0, you would use 1.0_K_DBL.
0 Kudos
Steven_L_Intel1
Employee
486 Views
Oh, and rather than use SELECTED_INT_KIND and SELECTED_REAL_KIND, you could use:

KIND(0)

and

KIND(0.0D0)

This gives you that compiler's kind value for default integer and for double precision. If you don't have specific requirements for number of digts, etc., this may be a simpler alternative.
0 Kudos
TimP
Honored Contributor III
486 Views
When you are logged in with the account which created a post, you should see a delete option which will work until someone adds a response to a thread. Even then, you could use the edit button to clean out your post and note that you moved it.
Technically, your code relies on extensions to Fortran, so is not absolutely required to operate in a portable way, even where compilers accept it.
I believe I have seen posted articles on recommended style for doing what I think you want. A good textbook also should show reasonably sane methods. Since an initializer implies SAVE, you could set up the module like
integer :: irest = -1
...
or you could USE the module in the main program and assign values prior to use elsewhere.
0 Kudos
onkelhotte
New Contributor II
486 Views

MADsblionel:

INTEGER, PARAMETER :: K_INT = SELECTED_INT_KIND(9)
INTEGER, PARAMETER :: K_DBL = SELECTED_REAL_KIND(15,300)

I have also taken the liberty to replace your use of the extensions INTEGER*4 and REAL*8 with the recommended style using standard-conforming and portable features of the language.

Hi Steve,
why is that kind of declarationbetter? I used integer*4 because I knew that this would handle numbersfrom -2147483648 to 2147483647. Is that different on other platforms or other compiler?

Markus

0 Kudos
g_f_thomas
Beginner
486 Views

You're on a forum dedicated to runninga decidedly nonportable compiler IVF on Windows. I you want portable use Mingw. gfortran is now part of GCC. As far as *4, *8, and *16 goes, they work across ia32, Intel64, and IA64.

Gerry

0 Kudos
Steven_L_Intel1
Employee
486 Views
The *4 type of declaration is non-standard. I usually recommend using a standard-conforming syntax where available. While that syntax is widely accepted, it may not mean the same thing everywhere. You're better off to use SELECTED_INT_KIND to determine the kind number your compiler uses for a particular integer range.
0 Kudos
Reply