Intel® Fortran Compiler 16.0 User and Reference Guide

Allocating Common Blocks

Use the [Q]dyncom option to dynamically allocate common blocks at run time.

This option designates a common block to be dynamic. The space for its data is allocated at run time rather than at compile time. On entry to each routine containing a declaration of the dynamic common block, a check is performed to see whether space for the common block has been allocated. If the dynamic common block is not yet allocated, space is allocated at the check time.

The following command-line example specifies the dynamic common option with the names of the common blocks to be allocated dynamically at run time:

ifort -dyncom "blk1,blk2,blk3" test.f (Linux OS and OS X)
ifort /Qdyncom"BLK1,BLK2,BLK3" test.f (Windows OS)

where BLK1, BLK2, and BLK3 are the names of the common blocks to be made dynamic.

Guidelines for Using the [Q]dyncom Option

The following are some limitations that you should be aware of when using the [Q]dyncom option:

Why Use a Dynamic Common Block?

A main reason for using dynamic common blocks is to enable you to control the common block allocation by supplying your own allocation routine. To use your own allocation routine, you should link it ahead of the Fortran run-time library. This routine must be written in the C language to generate the correct routine name.

The routine prototype is:

void _FTN_ALLOC(void **mem, int *size, char *name);

where

The default routine _FTN_ALLOC must be written in the C language to generate the correct routine name. To use your own allocation routine, you should link it ahead of the Fortran run-time library.

You can also use the Fortran run-time function FOR__SET_FTN_ALLOC to use your own allocation routine. This method is especially helpful when you are using shared libraries. If you choose this method, it overrides the default routine _FTN_ALLOC.

Note

For offload applications, routines FOR__SET_FTN_ALLOC and _FTN_ALLOC will work on native Intel® Many Integrated Core Architecture (Intel® MIC Architecture).

However, they will not work across the boundary between a host CPU and an Intel® MIC Architecture coprocessor. For example, if you use FOR__SET_FTN_ALLOC in the host-side code, it will work on COMMON allocations in other host-side code. The same is true if both are on the coprocessor side.

But if you change the allocation routine on the host, it won't be changed on the coprocessor and vice versa.

Allocating Memory to Dynamic Common Blocks

The run-time library routine, f90_dyncom, performs memory allocation. The compiler calls this routine at the beginning of each routine in a program that contains a dynamic common block. In turn, this library routine calls _FTN_ALLOC to allocate memory. By default, the compiler passes the size in bytes of the common block as declared in each routine to f90_dyncom, and then on to _FTN_ALLOC. The Fortran run-time library contains a default version of _FTN_ALLOC, which simply allocates the requested number of bytes and returns.

If you use the nonstandard extension having the common block of the same name declared with different sizes in different routines, you may get a run-time error depending on the order in which the routines containing the common block declarations are invoked.

You can now create your own routine to dynamically allocate common blocks, which is especially useful when you are sharing libraries. To use your own routine, you must specify the run-time function FOR__SET_FTN_ALLOC.

See Also