root/ompi/mpi/fortran/base/fint_2_int.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*
   2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
   3  *                         University Research and Technology
   4  *                         Corporation.  All rights reserved.
   5  * Copyright (c) 2004-2009 The University of Tennessee and The University
   6  *                         of Tennessee Research Foundation.  All rights
   7  *                         reserved.
   8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
   9  *                         University of Stuttgart.  All rights reserved.
  10  * Copyright (c) 2004-2005 The Regents of the University of California.
  11  *                         All rights reserved.
  12  * Copyright (c) 2011-2012 Cisco Systems, Inc.  All rights reserved.
  13  * Copyright (c) 2012      Oracle and/or its affiliates.  All rights reserved.
  14  * Copyright (c) 2014-2019 Research Organization for Information Science
  15  *                         and Technology (RIST).  All rights reserved.
  16  * $COPYRIGHT$
  17  *
  18  * Additional copyrights may follow
  19  *
  20  * $HEADER$
  21  */
  22 
  23 #ifndef OMPI_FORTRAN_BASE_FINT_2_INT_H
  24 #define OMPI_FORTRAN_BASE_FINT_2_INT_H
  25 
  26 #include "ompi_config.h"
  27 
  28 #include <stdlib.h>
  29 
  30 /*
  31  * Define MACROS to take account of different size of MPI_Fint from int
  32  */
  33 
  34 #if OMPI_SIZEOF_FORTRAN_INTEGER == SIZEOF_INT
  35   #define OMPI_ARRAY_NAME_DECL(a)
  36   #define OMPI_2_DIM_ARRAY_NAME_DECL(a, dim2)
  37   #define OMPI_SINGLE_NAME_DECL(a)
  38   #define OMPI_ARRAY_NAME_CONVERT(a) a
  39   #define OMPI_SINGLE_NAME_CONVERT(a) a
  40   #define OMPI_INT_2_FINT(a) a
  41   #define OMPI_FINT_2_INT(a) a
  42   #define OMPI_PFINT_2_PINT(a) a
  43   #define OMPI_ARRAY_FINT_2_INT_ALLOC(in, n)
  44   #define OMPI_ARRAY_FINT_2_INT(in, n)
  45   #define OMPI_2_DIM_ARRAY_FINT_2_INT(in, n, dim2)
  46   #define OMPI_ARRAY_FINT_2_INT_CLEANUP(in)
  47   #define OMPI_SINGLE_FINT_2_INT(in)
  48   #define OMPI_SINGLE_INT_2_FINT(in)
  49   #define OMPI_ARRAY_INT_2_FINT(in, n)
  50 
  51 #elif OMPI_SIZEOF_FORTRAN_INTEGER > SIZEOF_INT
  52   #define OMPI_ARRAY_NAME_DECL(a) int *c_##a
  53   #define OMPI_2_DIM_ARRAY_NAME_DECL(a, dim2) int (*c_##a)[dim2], dim2_index
  54   #define OMPI_SINGLE_NAME_DECL(a) int c_##a
  55   #define OMPI_ARRAY_NAME_CONVERT(a) c_##a
  56   #define OMPI_SINGLE_NAME_CONVERT(a) &c_##a
  57   #define OMPI_INT_2_FINT(a) a
  58   #define OMPI_FINT_2_INT(a) (int) (a)
  59   #define OMPI_PFINT_2_PINT(a) (int *) (a)
  60 
  61   /* This is for OUT parameters. Does only alloc */
  62   #define OMPI_ARRAY_FINT_2_INT_ALLOC(in, n) \
  63     OMPI_ARRAY_NAME_CONVERT(in) = malloc(n * sizeof(int))
  64 
  65   /* This is for IN/IN-OUT parameters. Does alloc and assignment */
  66   #define OMPI_ARRAY_FINT_2_INT(in, n) \
  67     do { \
  68       int converted_n = (int)(n); \
  69       OMPI_ARRAY_NAME_CONVERT(in) = malloc(converted_n * sizeof(int)); \
  70       while(--converted_n >= 0) { \
  71         OMPI_ARRAY_NAME_CONVERT(in)[converted_n] = (int) in[converted_n]; \
  72       } \
  73     } while (0)
  74 
  75   /* This is for 2-dim arrays */
  76   #define OMPI_2_DIM_ARRAY_FINT_2_INT(in, n, dim2) \
  77     do { \
  78       int converted_n = (int)(n); \
  79       OMPI_ARRAY_NAME_CONVERT(in) = (int (*)[dim2]) malloc(converted_n * sizeof(*OMPI_ARRAY_NAME_CONVERT(in))); \
  80       while(--converted_n >= 0) { \
  81         for(dim2_index = 0; dim2_index < dim2; ++dim2_index) { \
  82           OMPI_ARRAY_NAME_CONVERT(in)[converted_n][dim2_index] = (int)in[converted_n][dim2_index]; \
  83         } \
  84       } \
  85     } while (0)
  86 
  87   /* This is for IN parameters. Does only free */
  88   #define OMPI_ARRAY_FINT_2_INT_CLEANUP(in) \
  89     free(OMPI_ARRAY_NAME_CONVERT(in))
  90 
  91   /* This is for single IN parameter */
  92   #define OMPI_SINGLE_FINT_2_INT(in) \
  93     OMPI_ARRAY_NAME_CONVERT(in) = (int) *(in)
  94 
  95   /* This is for single OUT parameter */
  96   #define OMPI_SINGLE_INT_2_FINT(in) \
  97     *(in) = OMPI_ARRAY_NAME_CONVERT(in)
  98 
  99   /* This is for OUT/IN-OUT parametes. Does back assignment and free */
 100   #define OMPI_ARRAY_INT_2_FINT(in, n) \
 101     do { \
 102       int converted_n = (int)(n); \
 103       while(--converted_n >= 0) { \
 104         in[converted_n] = OMPI_ARRAY_NAME_CONVERT(in)[converted_n]; \
 105       } \
 106       free(OMPI_ARRAY_NAME_CONVERT(in)); \
 107     } while (0)
 108 #else /* int > MPI_Fint  */
 109   #define OMPI_ARRAY_NAME_DECL(a) int *c_##a
 110   #define OMPI_2_DIM_ARRAY_NAME_DECL(a, dim2) int (*c_##a)[dim2], dim2_index
 111   #define OMPI_SINGLE_NAME_DECL(a) int c_##a
 112   #define OMPI_ARRAY_NAME_CONVERT(a) c_##a
 113   #define OMPI_SINGLE_NAME_CONVERT(a) &c_##a
 114   #define OMPI_INT_2_FINT(a) (MPI_Fint)(a)
 115   #define OMPI_FINT_2_INT(a) (a)
 116   #define OMPI_PFINT_2_PINT(a) a
 117 
 118   /* This is for OUT parameters. Does only alloc */
 119   #define OMPI_ARRAY_FINT_2_INT_ALLOC(in, n) \
 120     OMPI_ARRAY_NAME_CONVERT(in) = malloc(n * sizeof(int))
 121 
 122   #define OMPI_ARRAY_FINT_2_INT(in, n) \
 123     do { \
 124       int converted_n = (int)(n); \
 125       OMPI_ARRAY_NAME_CONVERT(in) = malloc(converted_n * sizeof(int)); \
 126       while(--converted_n >= 0) { \
 127         OMPI_ARRAY_NAME_CONVERT(in)[converted_n] = in[converted_n]; \
 128       } \
 129     } while (0)
 130 
 131   #define OMPI_2_DIM_ARRAY_FINT_2_INT(in, n, dim2) \
 132     do { \
 133       int converted_n = (int)(n); \
 134       OMPI_ARRAY_NAME_CONVERT(in) = (int (*)[dim2]) malloc(converted_n * sizeof(*OMPI_ARRAY_NAME_CONVERT(in))); \
 135       while(--converted_n >= 0) { \
 136         for(dim2_index = 0; dim2_index < dim2; ++dim2_index) { \
 137           OMPI_ARRAY_NAME_CONVERT(in)[converted_n][dim2_index] = in[converted_n][dim2_index]; \
 138         } \
 139       } \
 140     } while (0)
 141 
 142   #define OMPI_ARRAY_FINT_2_INT_CLEANUP(in) \
 143     free(OMPI_ARRAY_NAME_CONVERT(in))
 144 
 145   #define OMPI_SINGLE_FINT_2_INT(in) \
 146      OMPI_ARRAY_NAME_CONVERT(in) = *(in)
 147 
 148   #define OMPI_SINGLE_INT_2_FINT(in) \
 149     *in = (MPI_Fint) OMPI_ARRAY_NAME_CONVERT(in)
 150 
 151   #define OMPI_ARRAY_INT_2_FINT(in, n) \
 152     do { \
 153       int converted_n = (int)(n); \
 154       while(--converted_n >= 0) { \
 155         in[converted_n] = OMPI_ARRAY_NAME_CONVERT(in)[converted_n]; \
 156       } \
 157       free(OMPI_ARRAY_NAME_CONVERT(in)); \
 158     } while (0)
 159 
 160 #endif
 161 
 162 /*
 163  * Define MACROS to take account of different size of logical from int
 164  *
 165  * There used to be an in-place option for the below conversions of
 166  * logical arrays.  So if mpi_cart_create(..., periods, ...) took an
 167  * input array of Fortran logicals, it would walk the array converting
 168  * the elements to C-logical values, then at the end it would restore
 169  * the values back to Fortran logicals.
 170  *
 171  * The problem with that is periods is an INPUT argument and some
 172  * Fortran compilers even put it in read-only memory because of that.
 173  * So writing to it wasn't generally okay, even though we were restoring it
 174  * before returning.
 175  *
 176  * The in-place option is hence only valid if no conversion is ever needed
 177  * (e.g. Fortran logical and C int have the same size *and** Fortran logical
 178  * .TRUE. value is 1 in C.
 179  */
 180 
 181 #if (OMPI_SIZEOF_FORTRAN_LOGICAL == SIZEOF_INT) && (OMPI_FORTRAN_VALUE_TRUE == 1)
 182 #  define OMPI_LOGICAL_NAME_DECL(in)               /* Not needed for int==logical */
 183 #  define OMPI_LOGICAL_NAME_CONVERT(in)        in  /* Not needed for int==logical */
 184 #  define OMPI_LOGICAL_SINGLE_NAME_CONVERT(in) in /* Not needed for int==logical */
 185 #  define OMPI_LOGICAL_ARRAY_NAME_DECL(in)         /* Not needed for int==logical */
 186 #  define OMPI_LOGICAL_ARRAY_NAME_CONVERT(in)  in  /* Not needed for int==logical */
 187 #  define OMPI_ARRAY_LOGICAL_2_INT_ALLOC(in,n)     /* Not needed for int==logical */
 188 #  define OMPI_ARRAY_LOGICAL_2_INT_CLEANUP(in)     /* Not needed for int==logical */
 189 
 190 #  define OMPI_FORTRAN_MUST_CONVERT_LOGICAL_2_INT    0
 191 #  define OMPI_LOGICAL_2_INT(a) a
 192 #  define OMPI_INT_2_LOGICAL(a) a
 193 #  define OMPI_ARRAY_LOGICAL_2_INT(in, n)
 194 #  define OMPI_ARRAY_INT_2_LOGICAL(in, n)
 195 #  define OMPI_SINGLE_INT_2_LOGICAL(a)            /* Single-OUT variable -- Not needed for int==logical, true=1 */
 196 #else
 197 /*
 198  * For anything other than Fortran-logical == C-int or some .TRUE. is not 1 in C, we have to convert
 199  */
 200 #  define OMPI_FORTRAN_MUST_CONVERT_LOGICAL_2_INT    1
 201 #  define OMPI_LOGICAL_NAME_DECL(in)           int c_##in
 202 #  define OMPI_LOGICAL_NAME_CONVERT(in)        c_##in
 203 #  define OMPI_LOGICAL_SINGLE_NAME_CONVERT(in) &c_##in
 204 #  define OMPI_LOGICAL_ARRAY_NAME_DECL(in)     int * c_##in
 205 #  define OMPI_LOGICAL_ARRAY_NAME_CONVERT(in)  c_##in
 206 #  define OMPI_ARRAY_LOGICAL_2_INT_ALLOC(in,n) \
 207       OMPI_LOGICAL_ARRAY_NAME_CONVERT(in) = malloc(n * sizeof(int))
 208 #  define OMPI_ARRAY_LOGICAL_2_INT_CLEANUP(in) \
 209       free(OMPI_LOGICAL_ARRAY_NAME_CONVERT(in))
 210 
 211 #  if OMPI_FORTRAN_VALUE_TRUE == 1
 212 #    define OMPI_LOGICAL_2_INT(a) (int)a
 213 #    define OMPI_INT_2_LOGICAL(a) (ompi_fortran_logical_t)a
 214 #    define OMPI_SINGLE_INT_2_LOGICAL(a) *a=(OMPI_INT_2_LOGICAL(OMPI_LOGICAL_NAME_CONVERT(a)))
 215 #  else
 216 #    define OMPI_LOGICAL_2_INT(a) ((a)==0? 0 : 1)
 217 #    define OMPI_INT_2_LOGICAL(a) ((a)==0? 0 : OMPI_FORTRAN_VALUE_TRUE)
 218 #    define OMPI_SINGLE_INT_2_LOGICAL(a) *a=(OMPI_INT_2_LOGICAL(OMPI_LOGICAL_NAME_CONVERT(a)))
 219 #  endif
 220 #  define OMPI_ARRAY_LOGICAL_2_INT(in, n) do { \
 221        int converted_n = (int)(n); \
 222        OMPI_ARRAY_LOGICAL_2_INT_ALLOC(in, converted_n + 1); \
 223        while (--converted_n >= 0) { \
 224          OMPI_LOGICAL_ARRAY_NAME_CONVERT(in)[converted_n]=OMPI_LOGICAL_2_INT(in[converted_n]); \
 225        } \
 226      } while (0)
 227 #  define OMPI_ARRAY_INT_2_LOGICAL(in, n) do { \
 228        int converted_n = (int)(n); \
 229        while (--converted_n >= 0) { \
 230          in[converted_n]=OMPI_INT_2_LOGICAL(OMPI_LOGICAL_ARRAY_NAME_CONVERT(in)[converted_n]); \
 231        } \
 232        OMPI_ARRAY_LOGICAL_2_INT_CLEANUP(in); \
 233      }  while (0)
 234 #endif /* OMPI_SIZEOF_FORTRAN_LOGICAL && OMPI_FORTRAN_VALUE_TRUE */
 235 
 236 
 237 #endif /* OMPI_FORTRAN_BASE_FINT_2_INT_H */

/* [<][>][^][v][top][bottom][index][help] */