Source

mpi3-fortran / ompi / mpi / fortran / base / f08-interfaces.pl

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env perl
#
# Copyright (c) 2012 Cisco Systems, Inc.  All rights reserved.
# $COPYRIGHT$
# 
# Additional copyrights may follow
# 
# $HEADER$
#

# This script creates the mpi-f08-interfaces.F90 and
# pmpi-f08-interfaces.F90 files.

use strict;

use Data::Dumper;

#----------------------------------------------------------------------------

# Two command line args:
# - path to configure-fortran-output.h
# - path to output directory

my $config_output_file = $ARGV[0];
my $outdir = $ARGV[1];

die "Must specify path to configure-fortran-output as first CLI argument"
    if (! -r $config_output_file);
die "Must specify path to output directroy as second CLI argument"
    if (! -d $outdir);

#----------------------------------------------------------------------------

# Read values from configure

my $ignore_tkr_predecl;
my $ignore_tkr_type;
open(IN, $config_output_file) || 
    die "Can't open $config_output_file";
while (<IN>) {
    $ignore_tkr_predecl = $1
        if (m/^#define OMPI_FORTRAN_IGNORE_TKR_PREDECL (.+)$/);
    $ignore_tkr_type = $1
        if (m/^#define OMPI_FORTRAN_IGNORE_TKR_TYPE (.+)$/);
}
my $ignore_tkr = 1
    if ($ignore_tkr_predecl ne "!");

#----------------------------------------------------------------------------

# Write an output file only if a) the output file does not already
# exist, or b) it exists, but its contents are different than $str.

sub write_file {
    my ($filename_out, $str) = @_;

    my $need_write = 0;
    if (! -f $filename_out) {
        $need_write = 1;
    } else {
        open(FILE_IN, $filename_out) || die "Couldn't open $filename_out";
        my $tmp;
        $tmp .= $_
            while (<FILE_IN>);
        close(FILE_IN);
        if ($str ne $tmp) {
            $need_write = 1;
        }
    }
    
    if ($need_write) {
        open(FILE_OUT, ">$filename_out") || die "Couldn't open $filename_out";
        print FILE_OUT $str;
        close(FILE_OUT);
        print "created $filename_out\n";
    } else {
        print "$filename_out unchanged; not written\n";
    }
}

#----------------------------------------------------------------------------

print "creating Fortran header files (with common constants)...\n";

# Find the OMPI topdir.  It is likely the pwd.
my $topdir;
if (-r "ompi/fortran/base/mpi-f08-types.F90") {
    $topdir = ".";
} elsif (-r "fortran/base/mpi-f08-types.F90") {
    $topdir = "..";
} elsif (-r "base/mpi-f08-types.F90") {
    $topdir = "../..";
} elsif (-r "mpi-f08-types.F90") {
    $topdir = "../../..";
} else {
    print "Please run this script from the Open MPI topdir or topdir/include/mpi\n";
    print "Aborting.\n";
    exit(1);
}

#----------------------------------------------------------------------------

my $interfaces;

# Each entry in $interfaces must have all of the following fields
# defined:
#
# - params: comma-delimited string of all parameters.  Does NOT need to
#   include IERR.
#
# Additionally, an entry may have one or more of the following
# defined:
#
# - name: if defined, used instead of {$key}_f08
# - bind_c_name: if defined, used instead of {$key}_f08
# - function: if defined, is a function instead of a subroutine
# - no_ierr: if defined, the IERR arg will not be automatically added
#
# All of the follow can be either an array (if multiple values are
# needed) or a single string.  Arguments can be prefix-decorated with
# IN:, OUT:, INOUT:, ASYNC:, or NODIR:.  ASYNC arguments are assumed
# to be NODIR.  If no prefix is specified, IN: is assumed.
# 
# - mpi_f08_types: used for the "use mpi_f08_types, only : ..." line
# - mpi_f08_interfaces_callbacks: used for "use
#   mpi_f08_interfaces_callbacks ..." line
# - choice_buffers: names of all the choice buffers
# - integer_args: integer arguments.  IERR does NOT need to be listed here.
# - logical_args: logical arguments
# - aint_args: MPI_ADDRESS_KIND arguments
# - offset_args: MPI_OFFSET_KIND arguments
# - datatype_args: MPI_Datatype arguments
# - comm_args: MPI_Comm argument
# - errhandeler_args: MPI_Errhandler arguments
# - file_args: MPI_File arguments
# - group_args: MPI_Group arguments
# - info_args: MPI_Info arguments
# - message_args: MPI_Message arguments
# - op_args: MPI_Op arguments
# - request_args: MPI_Request arguments
# - win_args: MPI_Win arguments
# - status_args: MPI_Status arguments
# - procedure_args: OMPI_PROCEDURE arguments
# ---> need to list string args in here somehow

$interfaces->{MPI_Bsend} = {
    params => [ qw/buf count datatype dest tag comm/ ],
    mpi_f08_types => [ qw/MPI_Datatype MPI_Comm/ ],
    choice_buffers => "buf",
    integer_args => [ qw/count dest tag/ ],
    datatype_args => "datatype",
    comm_args => "comm",
};

$interfaces->{MPI_Bsend_init} = {
    params => [ qw/buf count datatype dest tag comm request/ ],
    mpi_f08_types => [ qw/MPI_Datatype MPI_Comm/ ],
    choice_buffers => "ASYNC:buf",
    integer_args => [ qw/count dest tag/ ],
    datatype_args => "datatype",
    comm_args => "comm",
    request_args => "OUT:request",
};

$interfaces->{MPI_Buffer_attach} = {
    params => [ qw/buffer size/ ],
    choice_buffers => "ASYNC:buffer",
    integer_args => "size",
};

$interfaces->{MPI_Buffer_detach} = {
    params => [ qw/buffer_addr size/ ],
    choice_buffers => "ASYNC:buffer_addr",
    integer_args => "OUT:size",
};

$interfaces->{MPI_Op_create} = {
    params => [ qw/user_fn commute op/ ],
    mpi_f08_types => "MPI_Op",
    mpi_f08_interfaces_callbacks => "MPI_User_function",
    procedure_args => "MPI_User_function NODIR:user_fn",
    logical_args => "commute",
    op_args => "OUT:op",
};

#----------------------------------------------------------------------------

# Fortran 08 interfaces files

my $header = '! -*- f90 -*-
! WARNING! THIS IS A GENERATED FILE!!
! ANY EDITS YOU PUT HERE WILL BE LOST!
! ==> Instead, edit topdir/ompi/fortran/base/f08-interfaces.pl

! Copyright (c) 2009-2012 Cisco Systems, Inc.  All rights reserved.
! Copyright (c) 2009-2012 Los Alamos National Security, LLC.
!                         All rights reserved.
!
! $COPYRIGHT$
!
! Additional copyrights may follow
!
! $HEADER$
!
! This file provides the interface specifications for the MPI Fortran
! API bindings.  It effectively maps between public names ("MPI_Init")
! and the name for tools ("MPI_Init_f08") and the back-end implementation
! name (e.g., "MPI_Init_f08").
!

#include "ompi/mpi/fortran/configure-fortran-output.h"

module mpi_f08_interfaces

';

my $footer = "end module mpi_f08_interfaces\n";


sub generate_type_param_line {
    my ($type, $params) = @_;

    # If we got no params, there's nothing to do
    return
        if (!defined($params));

    # Make it be in an array
    my @array;
    if (ref($params) eq "") {
        push(@array, $params);
    } else {
        @array = @{$params};
    }

    # Loop over the array, generating one line per param
    my $lines;
    foreach my $buf (@array) {
        $lines .= generate_param_line($type, $buf);
    }
    return $lines;
}

sub generate_param_line {
    my ($type, $buf) = @_;
    my $found = 0;

    $type = "    $type";
    if ($buf =~ s/ASYNC://) {
        $type .= " OMPI_ASYNCHRONOUS ";
        $found = 1;
    }
    if ($buf =~ s/OUT://) {
        $type .= ", INTENT(OUT)";
        $found = 1;
    }
    if ($buf =~ s/INOUT://) {
        $type .= ", INTENT(INOUT)";
        $found = 1;
    }
    if ($buf =~ s/NODIR://) {
        $found = 1;
    }
    if (!$found || $buf =~ s/IN://) {
        $type .= ", INTENT(IN)";
    }
    $type .= " :: $buf\n";
}

sub write_fortran_file {
    my ($header, $footer, $vals, $file) = @_;

    # Output each interface
    foreach my $key (sort(keys(%{$vals}))) {
        my $i = $vals->{$key};
        my $entity = (defined($i->{function})) ? "function" : "subroutine";

#        print Dumper($i);

        $header .= "interface  $key\n";
        my $name = $key . "_f08";
        $name = $i->{name}
            if (defined($i->{name}));
        $header .= "$entity $name(";

        my $num_params;
        if (ref($i->{params}) eq "ARRAY") {
            $header .= join(",", @{$i->{params}});
            $num_params = $#{$i->{params}} + 1;
        } else {
            $header .= $i->{params};
            $num_params = 1;
        }
        if (!defined($i->{no_ierr})) {
            $header .= ",ierr";
            ++$num_params;
        }
        $header .= " &
         ) OMPI_F08_INTERFACE_BIND_C(\"$name\")\n";

        # "use :: mpi_f08_types, only ..." line
        if (defined($i->{mpi_f08_types})) {
            $header .= "    use :: mpi_f08_types, only : ";
            if (ref($i->{mpi_f08_types}) eq "ARRAY") {
                $header .= join(", ", @{$i->{mpi_f08_types}}) . "\n";
            } else {
                $header .= "$i->{mpi_f08_types}\n";
            }
        }

        # "use :: mpi_f08_interfaces_callbacks, only ..." line
        if (defined($i->{mpi_f08_interfaces_callbacks})) {
            my @array;
            if (ref($i->{mpi_f08_interfaces_callbacks}) eq "") {
                push(@array, $i->{mpi_f08_interfaces_callbacks});
            } else {
                @array = @{$i->{mpi_f08_interfaces_callbacks}};
            }

            foreach my $t (@array) {
                $header .= "    use :: mpi_f08_interfaces_callbacks, only : $t\n";
            }
        }

        $header .= "    implicit none\n";

        # Output all choice buffers
        if (defined($i->{choice_buffers})) {
            my @array;
            if (ref($i->{choice_buffers}) eq "") {
                push(@array, $i->{choice_buffers});
            } else {
                @array = @{$i->{choice_buffers}};
            }
            
            foreach my $buf (@array) {
                my $type = "type(*), dimension(..)";
                my $bufname = $buf;
                $bufname =~ s/.+://;
                if ($ignore_tkr) {
                    $header .= "    $ignore_tkr_predecl $bufname\n";
                    $type = $ignore_tkr_type;
                }
                $header .= generate_param_line($type, $buf);
            }
        }

        # Output all other params
        $header .= 
            generate_type_param_line("LOGICAL", $i->{logical_args}) .
            generate_type_param_line("INTEGER", $i->{integer_args}) .
            generate_type_param_line("INTEGER (KIND=MPI_ADDRESS_KIND)", 
                                     $i->{aint_args}) .
            generate_type_param_line("INTEGER (KIND=MPI_OFFSET_KIND)", 
                                     $i->{offset_args}) .
            generate_type_param_line("TYPE(MPI_Datatype)", 
                                     $i->{datatype_args}) .
            generate_type_param_line("TYPE(MPI_Comm)", 
                                     $i->{comm_args}) .
            generate_type_param_line("TYPE(MPI_Errhandler)", 
                                     $i->{errhandler_args}) .
            generate_type_param_line("TYPE(MPI_File)", 
                                     $i->{file_args}) .
            generate_type_param_line("TYPE(MPI_Group)", 
                                     $i->{group_args}) .
            generate_type_param_line("TYPE(MPI_Info)", 
                                     $i->{info_args}) .
            generate_type_param_line("TYPE(MPI_Message)", 
                                     $i->{message_args}) .
            generate_type_param_line("TYPE(MPI_Op)", 
                                     $i->{op_args}) .
            generate_type_param_line("TYPE(MPI_Request)", 
                                     $i->{request_args}) .
            generate_type_param_line("TYPE(MPI_Win)", 
                                     $i->{win_args}) .
            generate_type_param_line("TYPE(MPI_Status)", 
                                     $i->{status_args});

        # Procedure arguments have to be handled separately
        if (defined($i->{procedure_args})) {
            # Make it be in an array
            my @array;
            if (ref($i->{procedure_args}) eq "") {
                push(@array, $i->{procedure_args});
            } else {
                @array = @{$i->{procedure_args}};
            }

            # For each of the args
            foreach my $proc (@array) {
                my ($type, $arg) = split(/\s+/, $proc);
                $header .= generate_param_line("OMPI_PROCEDURE($type)", $arg);
            }
        }

        $header .= generate_param_line("INTEGER", "OUT:ierr")
            if (!defined($i->{no_ierr}));

        # Done with this entity
        $header .= "end $entity  $name
end interface  $key\n\n";
    }
    $header .= $footer;

    write_file($file, $header);
}

write_fortran_file($header, $footer, $interfaces, "bogus.F90");
exit(0);

write_fortran_file($header, $footer, $interfaces,
                   "$topdir/ompi/include/mpi-f08-interfaces.F90");
write_fortran_file($header, $footer, $interfaces,
                   "$topdir/ompi/include/pmpi-f08-interfaces.F90");