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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
|
#:mute
#! Project version number
#:set PROJECT_VERSION = "{}.{}.{}".format(PROJECT_VERSION_MAJOR, PROJECT_VERSION_MINOR, PROJECT_VERSION_PATCH)
#! Support for C_BOOL logical
#:if not defined("WITH_CBOOL")
#:set WITH_CBOOL = False
#:endif
#! Support for quadruple precision floating point numbers
#:if not defined("WITH_QP")
#:set WITH_QP = False
#:endif
#! Support for extended double precision floating point numbers
#:if not defined("WITH_XDP")
#:set WITH_XDP = False
#:endif
#! Support for linear algebra with 64-bit integer sizes
#:if not defined("WITH_ILP64")
#:set WITH_ILP64 = False
#:endif
#! Real kinds to be considered during templating
#:set REAL_KINDS = ["sp", "dp"]
#:if WITH_XDP
#:set REAL_KINDS = REAL_KINDS + ["xdp"]
#:endif
#:if WITH_QP
#:set REAL_KINDS = REAL_KINDS + ["qp"]
#:endif
#! BLAS/LAPACK initials for each real kind
#:set REAL_INIT = ["s", "d"]
#:if WITH_XDP
#:set REAL_INIT = REAL_INIT + ["x"]
#:endif
#:if WITH_QP
#:set REAL_INIT = REAL_INIT + ["q"]
#:endif
#! Real types to be considered during templating
#:set REAL_TYPES = ["real({})".format(k) for k in REAL_KINDS]
#:set REAL_SUFFIX = REAL_KINDS
#! Real CPPS to be considered during templating
#:set REAL_CPPS = ["" for k in REAL_KINDS]
#! Collected (kind, type) tuples for real types
#:set REAL_KINDS_TYPES = list(zip(REAL_KINDS, REAL_TYPES, REAL_INIT, REAL_CPPS))
#! Complex kinds to be considered during templating
#:set CMPLX_KINDS = ["sp", "dp"]
#:if WITH_XDP
#:set CMPLX_KINDS = CMPLX_KINDS + ["xdp"]
#:endif
#:if WITH_QP
#:set CMPLX_KINDS = CMPLX_KINDS + ["qp"]
#:endif
#! BLAS/LAPACK initials for each complex kind
#:set CMPLX_INIT = ["c", "z"]
#:if WITH_XDP
#:set CMPLX_INIT = CMPLX_INIT + ["y"]
#:endif
#:if WITH_QP
#:set CMPLX_INIT = CMPLX_INIT + ["w"]
#:endif
#! BLAS/LAPACK complex->real kind initial conversion
#! Converts a BLAS/LAPACK complex kind initial to a real kind initial
#!
#! Args:
#! ci (character): Complex kind initial in ["c","z","y","w"]
#!
#! Returns:
#! Real kind initial in ["s","d","x","q"] or an empty string on invalid input
#!
#:def c2ri(cmplx)
$:"s" if cmplx=="c" else "d" if cmplx=="z" else "x" if cmplx=="y" else "q" if cmplx=="w" else "ERROR"
#:enddef
#! BLAS/LAPACK/Linear Algebra Integer Kinds
#:set LINALG_INT_KINDS = ["ilp"]
#:set LINALG_INT_SUFFIX = [""]
#:if WITH_ILP64
#:set LINALG_INT_KINDS = LINALG_INT_KINDS+["ilp64"]
#:set LINALG_INT_SUFFIX = LINALG_INT_SUFFIX+["_I64"]
#:endif
#:set LINALG_INT_TYPES = ["integer({})".format(k) for k in LINALG_INT_KINDS]
#:set LINALG_INT_KINDS_TYPES = list(zip(LINALG_INT_KINDS, LINALG_INT_TYPES, LINALG_INT_SUFFIX))
#! Complex types to be considered during templating
#:set CMPLX_TYPES = ["complex({})".format(k) for k in CMPLX_KINDS]
#:set CMPLX_SUFFIX = ["c{}".format(k) for k in CMPLX_KINDS]
#! Collected (kind, type, initial) tuples for complex types
#:set CMPLX_KINDS_TYPES = list(zip(CMPLX_KINDS, CMPLX_TYPES, CMPLX_INIT))
#! Integer kinds to be considered during templating
#:set INT_KINDS = ["int8", "int16", "int32", "int64"]
#! Integer types to be considered during templating
#:set INT_TYPES = ["integer({})".format(k) for k in INT_KINDS]
#! Integer abbreviations to be considered during templating
#:set INT_INIT = ["" for k in INT_KINDS]
#! Integer CPPs to be considered during templating
#:set INT_CPPS = ["" for k in INT_KINDS]
#! Collected (kind, type) tuples for integer types
#:set INT_KINDS_TYPES = list(zip(INT_KINDS, INT_TYPES, INT_INIT, INT_CPPS))
#! Logical kinds to be considered during templating
#:set LOG_KINDS = ["lk"]
#:if WITH_CBOOL
#:set LOG_KINDS = LOG_KINDS + ["c_bool"]
#:endif
#! Logical types to be considered during templating
#:set LOG_TYPES = ["logical({})".format(k) for k in LOG_KINDS]
#! Collected (kind, type) tuples for logical types
#:set LOG_KINDS_TYPES = list(zip(LOG_KINDS, LOG_TYPES))
#! Derived type string_type
#:set STRING_KINDS = ["string_type"]
#! String types to be considered during templating
#:set STRING_TYPES = ["type({})".format(k) for k in STRING_KINDS]
#! String abbreviations to be considered during templating
#:set STRING_INIT = ["" for k in STRING_KINDS]
#! String CPPs to be considered during templating
#:set STRING_CPPS = ["" for k in STRING_KINDS]
#! Collected (kind, type) tuples for string derived types
#:set STRING_KINDS_TYPES = list(zip(STRING_KINDS, STRING_TYPES))
#! Derived type bitsets
#:set BITSETS_KINDS = ["bitset_64", "bitset_large"]
#! Bitset types to be considered during templating
#:set BITSETS_TYPES = ["type({})".format(k) for k in BITSETS_KINDS]
#! Bitset abbreviations directive to be considered during templating
#:set BITSETS_INIT = ["" for k in BITSETS_KINDS]
#! Bitset CPP directive to be considered during templating
#:set BITSETS_CPPS = ["STDLIB_BITSETS" for k in BITSETS_KINDS]
#! Collected (kind, type) tuples for bitset types
#:set BITSETS_KINDS_TYPES = list(zip(BITSETS_KINDS, BITSETS_TYPES, BITSETS_INIT, BITSETS_CPPS))
#! Sparse types to be considered during templating
#:set SPARSE_KINDS = ["COO", "CSR", "CSC", "ELL"]
#! Whether Fortran 90 compatible code should be generated
#:set VERSION90 = defined('VERSION90')
#! Ranks to be generated when templates are created
#:if not defined('MAXRANK')
#:if VERSION90
#:set MAXRANK = 7
#:else
#:set MAXRANK = 15
#:endif
#:endif
#! Generates an array rank suffix.
#!
#! Args:
#! rank (int): Rank of the variable
#!
#! Returns:
#! Array rank suffix string (e.g. (:,:) if rank = 2)
#!
#:def ranksuffix(rank)
#{if rank > 0}#(${":" + ",:" * (rank - 1)}$)#{endif}#
#:enddef
#! Generates an empty array rank suffix.
#!
#! Args:
#! rank (int): Rank of the variable
#!
#! Returns:
#! Empty array rank suffix string (e.g. (0,0) if rank = 2)
#!
#:def emptyranksuffix(rank)
#{if rank > 0}#(${"0" + ",0" * (rank - 1)}$)#{endif}#
#:enddef
#! Generates an array rank suffix with a fixed integer size for all dimensions.
#!
#! Args:
#! rank (int): Rank of the variable
#! size (int): Size along each dimension
#!
#! Returns:
#! Array rank suffix string
#! E.g.,
#! fixedranksuffix(3,4)
#! -> (4,4,4)
#!
#:def fixedranksuffix(rank,size)
#{if rank > 0}#(${str(size) + (","+str(size)) * (rank - 1)}$)#{endif}#
#:enddef
#! Joins stripped lines with given character string
#!
#! Args:
#! txt (str): Text to process
#! joinstr (str): String to use as connector
#! prefix (str): String to add as prefix before the joined text
#! suffix (str): String to add as suffix after the joined text
#!
#! Returns:
#! Lines stripped and joined with the given string.
#!
#:def join_lines(txt, joinstr, prefix="", suffix="")
${prefix + joinstr.join([line.strip() for line in txt.split("\n")]) + suffix}$
#:enddef
#! Brace enclosed, comma separated Fortran expressions for a reduced shape.
#!
#! Rank of the original variable will be reduced by one. The routine generates
#! for each dimension a Fortan expression using merge(), which calculates the
#! size of the array for that dimension.
#!
#! Args:
#! varname (str): Name of the variable to be used as origin
#! origrank (int): Rank of the original variable
#! idim (int): Index of the reduced dimension
#!
#! Returns:
#! Shape expression enclosed in braces, so that it can be used as suffix to
#! define array shapes in declarations.
#!
#:def reduced_shape(varname, origrank, idim)
#:assert origrank > 0
#:if origrank > 1
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, origrank)
merge(size(${varname}$, ${i}$), size(${varname}$, ${i + 1}$), mask=${i}$<${idim}$)
#:endfor
#:endcall
#:endif
#:enddef
#! Generates a routine name from a generic name, rank, type and kind
#!
#! Args:
#! gname (str): Generic name
#! rank (integer): Rank if exist
#! type (str): Type of the input
#! kind (str): kind of inputs variable
#! suffix (str): other identifier (could be used for output type/kind)
#!
#! Returns:
#! A string with a new name
#!
#:def rname(gname, rank, type, kind, suffix='')
$:"{0}_{1}_{2}{3}_{2}{3}".format(gname, rank, type[0], kind) if suffix == '' else "{0}_{1}_{2}{3}_{4}".format(gname, rank, type[0], kind, suffix)
#:enddef
#! Generates an array rank suffix for subarrays reducing the dimension
#!
#! Args:
#! rank (int): Rank of the original variable
#! selectors (array): Dimension and name of the variable(s)
#!
#! Returns:
#! Array rank suffix string enclosed in braces
#!
#! E.g.,
#! select_subarray(5 , [(4, 'i'), (5, 'j')])
#! -> (:, :, :, i, j)
#!
#:def select_subarray(rank, selectors)
#:assert rank > 0
#:set seldict = dict(selectors)
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, rank + 1)
$:seldict.get(i, ":")
#:endfor
#:endcall
#:enddef
#!
#! Generates an array rank suffix for subarrays along a dimension
#!
#! Args:
#! varname (str): Name of the variable to be used as origin
#! rank (int): Rank of the original variable
#! dim (int): Dimension of the variable
#!
#! Returns:
#! Array rank suffix string enclosed in braces
#!
#! E.g.,
#! select_subvector('j', 5, 2)
#! -> (j1, :, j3, j4, j5)
#!
#! Used, e.g., in
#! stdlib_stats_median.fypp
#!
#:def select_subvector(varname, rank, idim)
#:assert rank > 0
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, idim)
${varname}$${i}$
#:endfor
:
#:for i in range(idim + 1, rank + 1)
${varname}$${i}$
#:endfor
#:endcall
#:enddef
#!
#! Generates an array rank suffix for arrays
#!
#! Args:
#! varname (str): Name of the variable to be used as origin
#! rank (int): Rank of the original array variable
#! idim (int): Dimension of the variable dropped
#!
#! Returns:
#! Array rank suffix string enclosed in braces
#!
#! E.g.,
#! reduce_subvector('j', 5, 2)
#! -> (j1, j3, j4, j5)
#!
#! Used, e.g., in
#! stdlib_stats_median.fypp
#!
#:def reduce_subvector(varname, rank, idim)
#:assert rank > 0
#:if rank > 1
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, idim)
${varname}$${i}$
#:endfor
#:for i in range(idim + 1, rank + 1)
${varname}$${i}$
#:endfor
#:endcall
#:endif
#:enddef
#!
#! Generates a list of loop variables
#!
#! Args:
#! varname(str): Name of the variable to be used as prefix
#! n (int): Number of loop variables to be created
#! offset (int): Optional index offset
#!
#! Returns:
#! Variable definition string
#!
#! E.g.,
#! loop_variables('j', 5)
#! -> "j1, j2, j3, j4, j5
#!
#:def loop_variables(varname, n, offset=0)
#:assert n > 0
#:call join_lines(joinstr=", ")
#:for i in range(1, n + 1)
${varname}$${i+offset}$
#:endfor
#:endcall
#:enddef
#!
#! Generates a list of loop variables from an array
#!
#! Args:
#! varname(str): Name of the array variable to be used as prefix
#! n (int): Number of loop variables to be created
#! offset (int): Optional index offset
#!
#! Returns:
#! Variable definition string
#!
#! E.g.,
#! loop_array_variables('j', 5)
#! -> "j(1), j(2), j(3), j(4), j(5)
#!
#! loop_array_variables('j', 5, 2)
#! -> "j(3), j(4), j(5), j(6), j(7)
#!
#:def loop_array_variables(varname, n, offset=0)
#:assert n > 0
#:call join_lines(joinstr=", ")
#:for i in range(1, n + 1)
${varname}$(${i+offset}$)
#:endfor
#:endcall
#:enddef
#! Generates an array shape specifier from an N-D array size
#!
#! Args:
#! name (str): Name of the original variable
#! rank (int): Rank of the original variable
#! offset(int): optional offset of the dimension loop (default = 0)
#!
#! Returns:
#! Array rank suffix string enclosed in braces
#!
#! E.g.,
#! shape_from_array_size('mat', 5)}$
#! -> (size(mat,1),size(mat,2),size(mat,3),size(mat,4),size(mat,5))
#! shape_from_array_size('mat', 5, 2)}$
#! -> (size(mat,3),size(mat,4),size(mat,5),size(mat,6),size(mat,7))
#!
#:def shape_from_array_size(name, rank, offset=0)
#:assert rank > 0
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, rank + 1)
size(${name}$,${i+offset}$)
#:endfor
#:endcall
#:enddef
#! Generates an array shape specifier from an N-D array of sizes
#!
#! Args:
#! name (str): Name of the original variable
#! rank (int): Rank of the original variable
#! offset(int): optional offset of the dimension loop (default = 0)
#!
#! Returns:
#! Array rank suffix string enclosed in braces
#!
#! E.g.,
#! shape_from_array_data('mat', 5)}$
#! -> (1:mat(1),1:mat(2),1:mat(3),1:mat(4),1:mat(5))
#! shape_from_array_data('mat', 5, 2)}$
#! -> (1:mat(3),1:mat(4),1:mat(5),1:mat(6),1:mat(7))
#!
#:def shape_from_array_data(name, rank, offset=0)
#:assert rank > 0
#:call join_lines(joinstr=", ", prefix="(", suffix=")")
#:for i in range(1, rank + 1)
1:${name}$(${i+offset}$)
#:endfor
#:endcall
#:enddef
#!
#! Start a sequence of loop with indexed variables over an N-D array
#!
#! Args:
#! varname (str): Name of the variable to be used as prefix
#! matname (str): Name of the variable to be used as array
#! n (int): Number of nested loops to be created (1=innermost; n=outermost)
#! dim_offset (int): Optional dimension offset (1st loop is over dimension 1+dim_offset)
#! intent (str): Optional indentation. Default: 8 spaces
#!
#!
#:def loop_variables_start(varname, matname, n, dim_offset=0, indent=" "*8)
#:assert n > 0
#:for i in range(1, n + 1)
${indent}$do ${varname}$${n+1+dim_offset-i}$ = lbound(${matname}$, ${n+1+dim_offset-i}$), ubound(${matname}$, ${n+1+dim_offset-i}$)
#:endfor
#:enddef
#:def loop_variables_end(n, indent=" "*8)
#:assert n > 0
#:call join_lines(joinstr="; ",prefix=indent)
#:for i in range(1, n + 1)
enddo
#:endfor
#:endcall
#:enddef
#!
#! Encapsulate code into CPP pre-processing directives #ifdef and #endif
#!
#! Args:
#! code (str): Code to be encapsulated
#! cpp_var (str): CPP variable
#!
#:def generate_cpp(code, cpp_var)
#:if cpp_var != ""
#if ${cpp_var}$
#:endif
$:code
#:if cpp_var != ""
#endif
#:endif
#:enddef generate_cpp
#:endmute
|