Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > Fortran templat...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 61 Topic 8140 of 8908
Post > Topic >>

Fortran templates

by relaxmike <michael.baudin@[EMAIL PROTECTED] > Apr 15, 2008 at 07:16 AM

Hi all fortran gurus !

I would like to discuss the current methods to manage
templates in fortran, following the thread

http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/f055f8=
70774d98a0/110fa05197d7b709?lnk=3Dgst&q=3Dtemplates+fortran#110fa05197d7b709=


which was very interesting, but showed no details on
how to pratically manage the source code.
This feature is very well-known by C++ developers as "templates".

One example of the problems solved by C++ templates is to
have a sorting source code which is able to manage for any
data type, including integers, reals, or even abstract data
types.

I currently know 3 ways of dealing with templates in fortran,
even if none of them is included in any fortran norm
(and none of them is detailed in a fortran book, to my knowledge) :
- pre-processing macros,
- clever use of the "include" statement,
- m4 macros.

Let's begin with the pre-processing macros.
Suppose that the file "sorting_template.f90" contains a template
sorting module, parametrized by the _QSORT_TYPE macro.
The following is an example of parametrized argument declaration :

  subroutine qsort ( array, compare )
    _QSORT_TYPE, dimension(:) :: array

Here "_QSORT_TYPE" may be an integer, a real or any fortran
derived type.
This name has been chosen because a fortran variable cannot
begin with an underscore so that no confusion can occur
between a preprocessing macro and a variable name.
Before using the template, one must instanciate it,
which is done with :

  #define _QSORT_TYPE type(SORT_DATA)
  #include "sorting_template.f90"

The "instanciation" is based, for example, on the following
SORT_DATA derived type, which may be defined in the
module "m_testsorting.f90" :

    type SORT_DATA
       integer key
       integer index
    end type SORT_DATA

Even if this derived type is quite simple, practical uses of this
method may include more complex data types.
The module "m_testsorting" has to be pre-processed before
being used. After pre-processing, the previous line has
been transformed to :

  subroutine qsort ( array, compare )
    type(SORT_DATA), dimension(:) :: array

With this method, generic source code templates can be
configured at will and allow to generate specific
sorting algorithms for whatever type of data.
This method can lead to more complex templates, with
no limit in the number of macros, that is, no limit
in the number of abstract data types in the template.

The main drawback is that the debugging process is not
possible interactively. This is because the source code
is generated at compile-time and the "template" does
not correspond to the post-processed one anymore.
One solution is to use manually the pre-processor to
generate the pre-processed template and to debug on that
later file.

Two other methods exist to manage to do generic programming
in fortran : the "include" statement and m4 macros.

The "include" fortran statement can be used so that the
target source code contains the definitions of an
abstract data type used in the template.
This method is used by Arjen Markus in the Flibs
library, for example in the linked list abstract
data structure :

http://flibs.sourceforge.net/linked_list.html

Here we suppose that the file "sorting_template.f90"
contains one sorting subroutine, which arguments
are defined like this :

subroutine qsort_array( array, compare )
  type(SORT_DATA), dimension(:) :: array

The trick is to use the include fortran statement
to put that template source code into a context in
which the abstract data type is defined.
The following source code defines a module, the derived
type SORT_DATA and then include the template source code.

module m_testsorting
  type SORT_DATA
     integer key
     integer index
  end type SORT_DATA
contains
  include "sorting_template.f90"
end module m_testsorting

The module m_testsorting is now providing the derived type
SORT_DATA and the methods to manage it, which are included
in the template. With a little more work, it is even possible
to make so that the final derived type has a name which
corresponds more to the one implemented by the abstraction,
as shown by Arjen Markus on the linkedlist example.

module MYDATA_MODULE
type MYDATA
    character(len=3D20) :: string
end type MYDATA
end module
module MYDATA_LISTS
    use MYDATA_MODULE, LIST_DATA =3D> MYDATA
    include "linkedlist.f90"
end module MYDATA_LISTS

The main advantage of the "include" method is that
it uses only fortran statements so that the debugging
process is possible interactively. Moreover, it is very
elegant.

Another method is to use m4 macros to generate source code
at compile-time. Gnu m4 is an implementation of the traditional
Unix macro processor. This method is used by Toby White the
in the Fox library :

http://uszla.me.uk/space/software/FoX/

The template source code is defined in files which have the .m4.
extension and the corresponding source code is defined in the
=2EF90 file. The source code may be difficult to maintain, but the
method is extremely powerful, thanks to the m4 features.
For example, one can use a m4 "for" loop to generate
several subroutine based on one template subroutine.
But, as for the pre-processing method, the interactive
debugging of the .m4 templates is not possible :
instead, the processed .f90 generated files are debugged easily
and directly.

Are there other well-known methods ?
Is there a definitive drawback for one of these methods so
that another way should be chosen?

All comments will be appreciated.

Best regards,

Micha=EBl Baudin
 




 61 Posts in Topic:
Fortran templates
relaxmike <michael.bau  2008-04-15 07:16:41 
Re: Fortran templates
Arjen Markus <arjen.ma  2008-04-15 07:30:30 
Re: Fortran templates
"Gerry Ford" &l  2008-04-16 01:10:31 
Re: Fortran templates
Bil Kleb <Bil.Kleb@[EM  2008-04-15 12:33:46 
Re: Fortran templates
Terence <tbwright@[EMA  2008-04-15 16:46:12 
Re: Fortran templates
Damian <damian@[EMAIL   2008-04-15 22:25:33 
Re: Fortran templates
Arjen Markus <arjen.ma  2008-04-15 23:50:46 
Re: Fortran templates
Arjen Markus <arjen.ma  2008-04-15 23:57:17 
Re: Fortran templates
Arjen Markus <arjen.ma  2008-04-15 23:59:23 
Re: Fortran templates
relaxmike <michael.bau  2008-04-16 01:15:40 
Re: Fortran templates
Damian <damian@[EMAIL   2008-04-16 07:23:32 
Re: Fortran templates
Damian <damian@[EMAIL   2008-04-16 07:25:45 
Re: Fortran templates
relaxmike <michael.bau  2008-04-16 08:14:52 
Re: Fortran templates
gunnar <bjorkmang@[EMA  2008-04-16 10:36:41 
Re: Fortran templates
GaryScott <garylscott@  2008-04-16 13:39:46 
Re: Fortran templates
relaxmike <michael.bau  2008-04-17 04:58:16 
Re: Fortran templates
Gary Scott <garylscott  2008-04-17 07:27:21 
Re: Fortran templates
nospam@[EMAIL PROTECTED]   2008-04-17 09:29:54 
Re: Fortran templates
relaxmike <michael.bau  2008-04-17 08:18:58 
Re: Fortran templates
relaxmike <michael.bau  2008-04-18 03:07:30 
Re: Fortran templates
nospam@[EMAIL PROTECTED]   2008-04-18 08:13:22 
Re: Fortran templates
relaxmike <michael.bau  2008-04-18 05:51:03 
Re: Fortran templates
"James Van Buskirk&q  2008-04-18 12:31:00 
Re: Fortran templates
Charles Coldwell <cold  2008-04-20 19:10:02 
Re: Fortran templates
relaxmike <michael.bau  2008-04-21 05:27:30 
Re: Fortran templates
nospam@[EMAIL PROTECTED]   2008-04-21 07:52:18 
Re: Fortran templates
"James Van Buskirk&q  2008-04-21 11:00:38 
Re: Fortran templates
glen herrmannsfeldt <g  2008-04-21 12:01:41 
Re: Fortran templates
Craig Powers <enigma@[  2008-04-21 16:16:07 
Re: Fortran templates
"James Van Buskirk&q  2008-04-21 10:43:05 
Re: Fortran templates
relaxmike <michael.bau  2008-04-22 05:08:34 
Re: Fortran templates
glen herrmannsfeldt <g  2008-04-22 10:42:12 
Re: Fortran templates
Charles Coldwell <cold  2008-04-23 10:06:43 
Re: Fortran templates
Ken.Fairfield@[EMAIL PROT  2008-04-23 09:03:50 
Re: Fortran templates
Charles Coldwell <cold  2008-04-23 21:21:28 
Re: Fortran templates
glen herrmannsfeldt <g  2008-04-23 13:56:00 
Re: Fortran templates
Charles Coldwell <cold  2008-04-23 22:43:29 
Re: Fortran templates
Tobias Burnus <burnus@  2008-04-23 10:22:13 
Re: Fortran templates
relaxmike <michael.bau  2008-04-24 01:01:37 
Re: Fortran templates
lindahl@[EMAIL PROTECTED]  2008-04-24 01:14:24 
Re: Fortran templates
Charles Coldwell <cold  2008-04-24 10:36:44 
Re: Fortran templates
"James Van Buskirk&q  2008-04-24 11:33:13 
Re: Fortran templates
"James Van Buskirk&q  2008-04-24 16:19:33 
Re: Fortran templates
Charles Coldwell <cold  2008-04-25 00:12:36 
Re: Fortran templates
relaxmike <michael.bau  2008-04-24 04:28:01 
Re: Fortran templates
Jim Xia <jimxia@[EMAIL  2008-04-30 08:07:37 
Re: Fortran templates
Damian <damian@[EMAIL   2008-04-30 17:54:17 
Re: Fortran templates
Damian <damian@[EMAIL   2008-05-01 21:55:50 
Re: Fortran templates
"Ron Ford" <  2008-05-02 00:17:32 
Re: Fortran templates
Jim Xia <jimxia@[EMAIL  2008-05-02 08:16:26 
Re: Fortran templates
Craig Powers <enigma@[  2008-05-02 13:47:58 
Re: Fortran templates
"James Van Buskirk&q  2008-05-02 15:33:31 
Re: Fortran templates
"James Van Buskirk&q  2008-05-02 17:56:49 
Re: Fortran templates
Tobias Burnus <burnus@  2008-05-03 05:38:33 
Re: Fortran templates
Jim Xia <jimxia@[EMAIL  2008-05-06 21:42:11 
Re: Fortran templates
=?ISO-8859-1?Q?B=E1lint_A  2008-05-07 01:36:25 
Re: Fortran templates
nospam@[EMAIL PROTECTED]   2008-05-07 07:33:40 
Re: Fortran templates
"James Van Buskirk&q  2008-05-07 08:54:35 
Re: Fortran templates
Jim Xia <jimxia@[EMAIL  2008-05-07 08:21:49 
Re: Fortran templates
Jim Xia <jimxia@[EMAIL  2008-05-07 08:35:19 
Re: Fortran templates
Terence <tbwright@[EMA  2008-05-07 17:24:40 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Nov 19 8:22:01 CST 2008.