| 1 |
# (C) Copyright 2018- ECMWF. |
| 2 |
# This software is licensed under the terms of the Apache Licence Version 2.0 |
| 3 |
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 4 |
# In applying this licence, ECMWF does not waive the privileges and immunities |
| 5 |
# granted to it by virtue of its status as an intergovernmental organisation |
| 6 |
# nor does it submit to any jurisdiction. |
| 7 |
|
| 8 |
import pytest |
| 9 |
import numpy as np |
| 10 |
|
| 11 |
from loki import Subroutine, Module, cgen, cppgen, cudagen, FindNodes |
| 12 |
from loki.jit_build import jit_compile, jit_compile_and_run, jit_compile_lib, clean_test, Builder, Obj |
| 13 |
import loki.expression.symbols as sym |
| 14 |
from loki.frontend import available_frontends |
| 15 |
from loki import ir |
| 16 |
|
| 17 |
from loki.transformations.transpile import ( |
| 18 |
FortranCTransformation, FortranISOCWrapperTransformation |
| 19 |
) |
| 20 |
|
| 21 |
# pylint: disable=too-many-lines |
| 22 |
|
| 23 |
|
| 24 |
def wrapperpath(path, module_or_routine): |
| 25 |
""" |
| 26 |
Utility that generates the ``<name>_fc.F90`` path for Fortran wrappers |
| 27 |
""" |
| 28 |
name = f'{module_or_routine.name}_fc' |
| 29 |
return (path/name.lower()).with_suffix('.F90') |
| 30 |
|
| 31 |
|
| 32 |
def cpath(path, module_or_routine, suffix='.c'): |
| 33 |
""" |
| 34 |
Utility that generates the ``<name>_c.h`` path for Fortran wrappers |
| 35 |
""" |
| 36 |
name = f'{module_or_routine.name}_c' |
| 37 |
return (path/name.lower()).with_suffix(suffix) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.fixture(scope='function', name='builder') |
| 41 |
def fixture_builder(tmp_path): |
| 42 |
yield Builder(source_dirs=tmp_path, build_dir=tmp_path) |
| 43 |
Obj.clear_cache() |
| 44 |
|
| 45 |
|
| 46 |
def test_transpile_unsupported_lang(): |
| 47 |
""" |
| 48 |
A simple test for testing failure/exception for unsupported |
| 49 |
language(s). |
| 50 |
""" |
| 51 |
with pytest.raises(ValueError): |
| 52 |
FortranCTransformation(language='not-supported') |
| 53 |
|
| 54 |
|
| 55 |
@pytest.mark.parametrize('case_sensitive', (False, True)) |
| 56 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 57 |
@pytest.mark.parametrize('language', ('c', 'cuda')) |
| 58 |
def test_transpile_case_sensitivity(tmp_path, frontend, case_sensitive, language): |
| 59 |
""" |
| 60 |
A simple test for testing lowering the case and case-sensitivity |
| 61 |
for specific symbols. |
| 62 |
""" |
| 63 |
|
| 64 |
fcode = """ |
| 65 |
subroutine transpile_case_sensitivity(a) |
| 66 |
integer, intent(in) :: a |
| 67 |
|
| 68 |
end subroutine transpile_case_sensitivity |
| 69 |
""" |
| 70 |
def convert_case(_str, case_sensitive): |
| 71 |
return _str.lower() if not case_sensitive else _str |
| 72 |
|
| 73 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 74 |
|
| 75 |
var_thread_idx = sym.Variable(name="threadIdx", case_sensitive=case_sensitive) |
| 76 |
var_x = sym.Variable(name="x", parent=var_thread_idx, case_sensitive=case_sensitive) |
| 77 |
assignment = ir.Assignment(lhs=routine.variable_map['a'], rhs=var_x) |
| 78 |
routine.arguments=routine.arguments + (routine.arguments[0].clone(name='sOmE_vAr', case_sensitive=case_sensitive), |
| 79 |
sym.Variable(name="oTher_VaR", case_sensitive=case_sensitive, type=routine.arguments[0].type.clone())) |
| 80 |
|
| 81 |
call = ir.CallStatement(sym.Variable(name='somE_cALl', case_sensitive=case_sensitive), |
| 82 |
arguments=(routine.variable_map['a'],)) |
| 83 |
inline_call = sym.InlineCall(function=sym.Variable(name='somE_InlINeCaLl', case_sensitive=case_sensitive), |
| 84 |
parameters=(sym.IntLiteral(1),)) |
| 85 |
inline_call_assignment = ir.Assignment(lhs=routine.variable_map['a'], rhs=inline_call) |
| 86 |
routine.body = (routine.body, assignment, call, inline_call_assignment) |
| 87 |
|
| 88 |
f2c = FortranCTransformation(language=language) |
| 89 |
f2c.apply(source=routine, path=tmp_path) |
| 90 |
ccode = cpath(tmp_path, routine).read_text().replace(' ', '').replace('\n', ' ').replace('\r', '').replace('\t', '') |
| 91 |
assert convert_case('transpile_case_sensitivity_c(inta,intsOmE_vAr,intoTher_VaR)', case_sensitive) in ccode |
| 92 |
assert convert_case('a=threadIdx%x;', case_sensitive) in ccode |
| 93 |
assert convert_case('somE_cALl(a);', case_sensitive) in ccode |
| 94 |
assert convert_case('a=somE_InlINeCaLl(1);', case_sensitive) in ccode |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 98 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 99 |
def test_transpile_simple_loops(tmp_path, builder, frontend, use_c_ptr): |
| 100 |
""" |
| 101 |
A simple test routine to test C transpilation of loops |
| 102 |
""" |
| 103 |
|
| 104 |
fcode = """ |
| 105 |
subroutine simple_loops(n, m, scalar, vector, tensor) |
| 106 |
use iso_fortran_env, only: real64 |
| 107 |
implicit none |
| 108 |
integer, intent(in) :: n, m |
| 109 |
real(kind=real64), intent(inout) :: scalar |
| 110 |
real(kind=real64), intent(inout) :: vector(n), tensor(n, m) |
| 111 |
|
| 112 |
integer :: i, j |
| 113 |
|
| 114 |
! For testing, the operation is: |
| 115 |
do i=1, n |
| 116 |
vector(i) = vector(i) + tensor(i, 1) + 1.0 |
| 117 |
end do |
| 118 |
|
| 119 |
do j=1, m |
| 120 |
do i=1, n |
| 121 |
tensor(i, j) = 10.* j + i |
| 122 |
end do |
| 123 |
end do |
| 124 |
end subroutine simple_loops |
| 125 |
""" |
| 126 |
|
| 127 |
# Generate reference code, compile run and verify |
| 128 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 129 |
filepath = tmp_path/(f'simple_loops{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 130 |
n, m = 3, 4 |
| 131 |
scalar = 2.0 |
| 132 |
vector = np.zeros(shape=(n,), order='F') + 3. |
| 133 |
tensor = np.zeros(shape=(n, m), order='F') + 4. |
| 134 |
jit_compile_and_run(routine, n, m, scalar, vector, tensor, filepath=filepath) |
| 135 |
|
| 136 |
assert np.all(vector == 8.) |
| 137 |
assert np.all(tensor == [[11., 21., 31., 41.], |
| 138 |
[12., 22., 32., 42.], |
| 139 |
[13., 23., 33., 43.]]) |
| 140 |
|
| 141 |
# Generate and test the transpiled C kernel |
| 142 |
f2c = FortranCTransformation() |
| 143 |
f2c.apply(source=routine, path=tmp_path) |
| 144 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 145 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 146 |
|
| 147 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 148 |
c_kernel = jit_compile_lib( |
| 149 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 150 |
path=tmp_path, name=libname, builder=builder |
| 151 |
) |
| 152 |
fc_function = c_kernel.simple_loops_fc_mod.simple_loops_fc |
| 153 |
|
| 154 |
# check the generated F2C wrapper |
| 155 |
with open(wrapperpath(tmp_path, routine), 'r') as f2c_f: |
| 156 |
f2c_str = f2c_f.read().upper().replace(' ', '') |
| 157 |
if use_c_ptr: |
| 158 |
assert f2c_str.count('TARGET') == 2 |
| 159 |
assert f2c_str.count('C_LOC') == 3 |
| 160 |
assert 'VECTOR(:)' in f2c_str |
| 161 |
assert 'TENSOR(:,:)' in f2c_str |
| 162 |
else: |
| 163 |
assert f2c_str.count('TARGET') == 0 |
| 164 |
assert f2c_str.count('C_LOC') == 0 |
| 165 |
assert 'VECTOR(N)' in f2c_str |
| 166 |
assert 'TENSOR(N,M)' in f2c_str |
| 167 |
|
| 168 |
n, m = 3, 4 |
| 169 |
scalar = 2.0 |
| 170 |
vector = np.zeros(shape=(n,), order='F') + 3. |
| 171 |
tensor = np.zeros(shape=(n, m), order='F') + 4. |
| 172 |
fc_function(n, m, scalar, vector, tensor) |
| 173 |
|
| 174 |
assert np.all(vector == 8.) |
| 175 |
assert np.all(tensor == [[11., 21., 31., 41.], |
| 176 |
[12., 22., 32., 42.], |
| 177 |
[13., 23., 33., 43.]]) |
| 178 |
|
| 179 |
|
| 180 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 181 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 182 |
def test_transpile_arguments(tmp_path, builder, frontend, use_c_ptr): |
| 183 |
""" |
| 184 |
A test the correct exchange of arguments with varying intents |
| 185 |
""" |
| 186 |
|
| 187 |
fcode = """ |
| 188 |
subroutine transpile_arguments(n, array, array_io, a, b, c, a_io, b_io, c_io) |
| 189 |
use iso_fortran_env, only: real32, real64 |
| 190 |
implicit none |
| 191 |
|
| 192 |
integer, intent(in) :: n |
| 193 |
real(kind=real64), intent(inout) :: array(n) |
| 194 |
real(kind=real64), intent(out) :: array_io(n) |
| 195 |
|
| 196 |
integer, intent(out) :: a |
| 197 |
real(kind=real32), intent(out) :: b |
| 198 |
real(kind=real64), intent(out) :: c |
| 199 |
integer, intent(inout) :: a_io |
| 200 |
real(kind=real32), intent(inout) :: b_io |
| 201 |
real(kind=real64), intent(inout) :: c_io |
| 202 |
|
| 203 |
integer :: i |
| 204 |
|
| 205 |
do i=1, n |
| 206 |
array(i) = 3. |
| 207 |
array_io(i) = array_io(i) + 3. |
| 208 |
end do |
| 209 |
|
| 210 |
a = 2**3 |
| 211 |
b = 3.2_real32 |
| 212 |
c = 4.1_real64 |
| 213 |
|
| 214 |
a_io = a_io + 2 |
| 215 |
b_io = b_io + real(3.2, kind=real32) |
| 216 |
c_io = c_io + 4.1 |
| 217 |
end subroutine transpile_arguments |
| 218 |
""" |
| 219 |
|
| 220 |
# Test the reference solution |
| 221 |
n = 3 |
| 222 |
array = np.zeros(shape=(n,), order='F') |
| 223 |
array_io = np.zeros(shape=(n,), order='F') + 3. |
| 224 |
# To do scalar inout we allocate data in single-element arrays |
| 225 |
a_io = np.array(1) |
| 226 |
b_io = np.array(2.) |
| 227 |
c_io = np.array(3.) |
| 228 |
|
| 229 |
# Generate reference code, compile run and verify |
| 230 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 231 |
filepath = tmp_path/(f'transpile_arguments{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 232 |
a, b, c = jit_compile_and_run(routine, n, array, array_io, a_io, b_io, c_io, filepath=filepath) |
| 233 |
|
| 234 |
assert np.all(array == 3.) and array.size == n |
| 235 |
assert np.all(array_io == 6.) |
| 236 |
assert a_io == 3. and np.isclose(b_io, 5.2) and np.isclose(c_io, 7.1) |
| 237 |
assert a == 8 and np.isclose(b, 3.2) and np.isclose(c, 4.1) |
| 238 |
|
| 239 |
# Generate and test the transpiled C kernel |
| 240 |
f2c = FortranCTransformation() |
| 241 |
f2c.apply(source=routine, path=tmp_path) |
| 242 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 243 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 244 |
|
| 245 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 246 |
c_kernel = jit_compile_lib( |
| 247 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 248 |
path=tmp_path, name=libname, builder=builder |
| 249 |
) |
| 250 |
fc_function = c_kernel.transpile_arguments_fc_mod.transpile_arguments_fc |
| 251 |
|
| 252 |
# check the generated F2C wrapper |
| 253 |
with open(wrapperpath(tmp_path, routine), 'r') as f2c_f: |
| 254 |
f2c_str = f2c_f.read().upper().replace(' ', '') |
| 255 |
if use_c_ptr: |
| 256 |
assert f2c_str.count('TARGET') == 2 |
| 257 |
assert f2c_str.count('C_LOC') == 3 |
| 258 |
assert 'ARRAY(:)' in f2c_str |
| 259 |
assert 'ARRAY_IO(:)' in f2c_str |
| 260 |
else: |
| 261 |
assert f2c_str.count('TARGET') == 0 |
| 262 |
assert f2c_str.count('C_LOC') == 0 |
| 263 |
assert 'ARRAY(N)' in f2c_str |
| 264 |
assert 'ARRAY_IO(N)' in f2c_str |
| 265 |
|
| 266 |
array = np.zeros(shape=(n,), order='F') |
| 267 |
array_io = np.zeros(shape=(n,), order='F') + 3. |
| 268 |
a_io = np.array(1) |
| 269 |
b_io = np.array(2.) |
| 270 |
c_io = np.array(3.) |
| 271 |
a, b, c = fc_function(n, array, array_io, a_io, b_io, c_io) |
| 272 |
|
| 273 |
assert np.all(array == 3.) and array.size == n |
| 274 |
assert np.all(array_io == 6.) |
| 275 |
assert a_io == 3. and np.isclose(b_io, 5.2) and np.isclose(c_io, 7.1) |
| 276 |
assert a == 8 and np.isclose(b, 3.2) and np.isclose(c, 4.1) |
| 277 |
|
| 278 |
|
| 279 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 280 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 281 |
def test_transpile_derived_type(tmp_path, builder, frontend, use_c_ptr): |
| 282 |
""" |
| 283 |
Tests handling and type-conversion of various argument types |
| 284 |
""" |
| 285 |
|
| 286 |
fcode_type = """ |
| 287 |
module transpile_type_mod |
| 288 |
use iso_fortran_env, only: real32, real64 |
| 289 |
implicit none |
| 290 |
|
| 291 |
type my_struct |
| 292 |
integer :: a |
| 293 |
real(kind=real32) :: b |
| 294 |
real(kind=real64) :: c |
| 295 |
end type my_struct |
| 296 |
end module transpile_type_mod |
| 297 |
""".strip() |
| 298 |
|
| 299 |
fcode_routine = """ |
| 300 |
subroutine transp_der_type(a_struct) |
| 301 |
use transpile_type_mod, only: my_struct |
| 302 |
implicit none |
| 303 |
type(my_struct), intent(inout) :: a_struct |
| 304 |
|
| 305 |
a_struct%a = a_struct%a + 4 |
| 306 |
a_struct%b = a_struct%b + 5. |
| 307 |
a_struct%c = a_struct%c + 6. |
| 308 |
end subroutine transp_der_type |
| 309 |
""".strip() |
| 310 |
|
| 311 |
module = Module.from_source(fcode_type, frontend=frontend, xmods=[tmp_path]) |
| 312 |
routine = Subroutine.from_source(fcode_routine, definitions=module, frontend=frontend, xmods=[tmp_path]) |
| 313 |
refname = f'ref_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 314 |
reference = jit_compile_lib([module, routine], path=tmp_path, name=refname, builder=builder) |
| 315 |
|
| 316 |
# Test the reference solution |
| 317 |
a_struct = reference.transpile_type_mod.my_struct() |
| 318 |
a_struct.a = 4 |
| 319 |
a_struct.b = 5. |
| 320 |
a_struct.c = 6. |
| 321 |
reference.transp_der_type(a_struct) |
| 322 |
assert a_struct.a == 8 |
| 323 |
assert a_struct.b == 10. |
| 324 |
assert a_struct.c == 12. |
| 325 |
|
| 326 |
# Translate the header module to expose parameters |
| 327 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 328 |
f2cwrap.apply(source=module, path=tmp_path, role='header') |
| 329 |
|
| 330 |
# Create transformation object and apply |
| 331 |
f2c = FortranCTransformation() |
| 332 |
f2c.apply(source=routine, path=tmp_path, role='kernel') |
| 333 |
f2cwrap.apply(source=routine, path=tmp_path, role='kernel') |
| 334 |
|
| 335 |
# Build and wrap the cross-compiled library |
| 336 |
sources = [module, wrapperpath(tmp_path, routine), cpath(tmp_path, routine)] |
| 337 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 338 |
c_kernel = jit_compile_lib(sources=sources, path=tmp_path, name=libname, builder=builder) |
| 339 |
|
| 340 |
a_struct = c_kernel.transpile_type_mod.my_struct() |
| 341 |
a_struct.a = 4 |
| 342 |
a_struct.b = 5. |
| 343 |
a_struct.c = 6. |
| 344 |
function = c_kernel.transp_der_type_fc_mod.transp_der_type_fc |
| 345 |
function(a_struct) |
| 346 |
assert a_struct.a == 8 |
| 347 |
assert a_struct.b == 10. |
| 348 |
assert a_struct.c == 12. |
| 349 |
|
| 350 |
|
| 351 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 352 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 353 |
def test_transpile_associates(tmp_path, builder, frontend, use_c_ptr): |
| 354 |
""" |
| 355 |
Tests C-transpilation of associate statements |
| 356 |
""" |
| 357 |
|
| 358 |
fcode_type = """ |
| 359 |
module assoc_type_mod |
| 360 |
use iso_fortran_env, only: real32, real64 |
| 361 |
implicit none |
| 362 |
|
| 363 |
type my_struct |
| 364 |
integer :: a |
| 365 |
real(kind=real32) :: b |
| 366 |
real(kind=real64) :: c |
| 367 |
end type my_struct |
| 368 |
end module assoc_type_mod |
| 369 |
""".strip() |
| 370 |
|
| 371 |
fcode_routine = """ |
| 372 |
subroutine transp_assoc(a_struct) |
| 373 |
use assoc_type_mod, only: my_struct |
| 374 |
implicit none |
| 375 |
type(my_struct), intent(inout) :: a_struct |
| 376 |
|
| 377 |
associate(a_struct_a=>a_struct%a, a_struct_b=>a_struct%b,& |
| 378 |
& a_struct_c=>a_struct%c) |
| 379 |
a_struct%a = a_struct_a + 4. |
| 380 |
a_struct_b = a_struct%b + 5. |
| 381 |
a_struct_c = a_struct_a + a_struct%b + a_struct_c |
| 382 |
end associate |
| 383 |
end subroutine transp_assoc |
| 384 |
""".strip() |
| 385 |
|
| 386 |
module = Module.from_source(fcode_type, frontend=frontend, xmods=[tmp_path]) |
| 387 |
routine = Subroutine.from_source(fcode_routine, definitions=module, frontend=frontend, xmods=[tmp_path]) |
| 388 |
refname = f'ref_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 389 |
reference = jit_compile_lib([module, routine], path=tmp_path, name=refname, builder=builder) |
| 390 |
|
| 391 |
# Test the reference solution |
| 392 |
a_struct = reference.assoc_type_mod.my_struct() |
| 393 |
a_struct.a = 4 |
| 394 |
a_struct.b = 5. |
| 395 |
a_struct.c = 6. |
| 396 |
reference.transp_assoc(a_struct) |
| 397 |
assert a_struct.a == 8 |
| 398 |
assert a_struct.b == 10. |
| 399 |
assert a_struct.c == 24. |
| 400 |
|
| 401 |
# Translate the header module to expose parameters |
| 402 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 403 |
f2cwrap.apply(source=module, path=tmp_path, role='header') |
| 404 |
|
| 405 |
# Create transformation object and apply |
| 406 |
f2c = FortranCTransformation() |
| 407 |
f2c.apply(source=routine, path=tmp_path, role='kernel') |
| 408 |
f2cwrap.apply(source=routine, path=tmp_path, role='kernel') |
| 409 |
|
| 410 |
# Build and wrap the cross-compiled library |
| 411 |
sources = [module, wrapperpath(tmp_path, routine), cpath(tmp_path, routine)] |
| 412 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 413 |
c_kernel = jit_compile_lib(sources=sources, path=tmp_path, name=libname, builder=builder) |
| 414 |
|
| 415 |
a_struct = c_kernel.assoc_type_mod.my_struct() |
| 416 |
a_struct.a = 4 |
| 417 |
a_struct.b = 5. |
| 418 |
a_struct.c = 6. |
| 419 |
function = c_kernel.transp_assoc_fc_mod.transp_assoc_fc |
| 420 |
function(a_struct) |
| 421 |
assert a_struct.a == 8 |
| 422 |
assert a_struct.b == 10. |
| 423 |
assert a_struct.c == 24. |
| 424 |
|
| 425 |
|
| 426 |
@pytest.mark.skip(reason='More thought needed on how to test structs-of-arrays') |
| 427 |
def test_transpile_derived_type_array(): |
| 428 |
""" |
| 429 |
Tests handling of multi-dimensional arrays and pointers. |
| 430 |
|
| 431 |
a_struct%scalar = 3. |
| 432 |
a_struct%vector(i) = a_struct%scalar + 2. |
| 433 |
a_struct%matrix(j,i) = a_struct%vector(i) + 1. |
| 434 |
|
| 435 |
! subroutine transpile_derived_type_array(a_struct) |
| 436 |
! use transpile_type, only: array_struct |
| 437 |
! implicit none |
| 438 |
! ! real(kind=real64) :: vector(:) |
| 439 |
! ! real(kind=real64) :: matrix(:,:) |
| 440 |
! type(array_struct), intent(inout) :: a_struct |
| 441 |
! integer :: i, j |
| 442 |
|
| 443 |
! a_struct%scalar = 3. |
| 444 |
! do i=1, 3 |
| 445 |
! a_struct%vector(i) = a_struct%scalar + 2. |
| 446 |
! end do |
| 447 |
! do i=1, 3 |
| 448 |
! do j=1, 3 |
| 449 |
! a_struct%matrix(j,i) = a_struct%vector(i) + 1. |
| 450 |
! end do |
| 451 |
! end do |
| 452 |
|
| 453 |
! end subroutine transpile_derived_type_array |
| 454 |
""" |
| 455 |
|
| 456 |
|
| 457 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 458 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 459 |
def test_transpile_module_variables(tmp_path, builder, frontend, use_c_ptr): |
| 460 |
""" |
| 461 |
Tests the use of imported module variables (via getter routines in C) |
| 462 |
""" |
| 463 |
|
| 464 |
fcode_type = """ |
| 465 |
module mod_var_type_mod |
| 466 |
use iso_fortran_env, only: real32, real64 |
| 467 |
implicit none |
| 468 |
|
| 469 |
save |
| 470 |
|
| 471 |
integer :: PARAM1 |
| 472 |
real(kind=real32) :: param2 |
| 473 |
real(kind=real64) :: param3 |
| 474 |
end module mod_var_type_mod |
| 475 |
""".strip() |
| 476 |
|
| 477 |
fcode_routine = """ |
| 478 |
subroutine transp_mod_var(a, b, c) |
| 479 |
use iso_fortran_env, only: real32, real64 |
| 480 |
use mod_var_type_mod, only: PARAM1, param2, param3 |
| 481 |
|
| 482 |
integer, intent(out) :: a |
| 483 |
real(kind=real32), intent(out) :: b |
| 484 |
real(kind=real64), intent(out) :: c |
| 485 |
|
| 486 |
a = 1 + PARAM1 ! Ensure downcasing is done right |
| 487 |
b = 1. + param2 |
| 488 |
c = 1. + param3 |
| 489 |
end subroutine transp_mod_var |
| 490 |
""".strip() |
| 491 |
|
| 492 |
module = Module.from_source(fcode_type, frontend=frontend, xmods=[tmp_path]) |
| 493 |
routine = Subroutine.from_source(fcode_routine, definitions=module, frontend=frontend, xmods=[tmp_path]) |
| 494 |
refname = f'ref_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 495 |
reference = jit_compile_lib([module, routine], path=tmp_path, name=refname, builder=builder) |
| 496 |
|
| 497 |
reference.mod_var_type_mod.param1 = 2 |
| 498 |
reference.mod_var_type_mod.param2 = 4. |
| 499 |
reference.mod_var_type_mod.param3 = 3. |
| 500 |
a, b, c = reference.transp_mod_var() |
| 501 |
assert a == 3 and b == 5. and c == 4. |
| 502 |
|
| 503 |
# Translate the header module to expose parameters |
| 504 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 505 |
f2cwrap.apply(source=module, path=tmp_path, role='header') |
| 506 |
|
| 507 |
# Create transformation object and apply |
| 508 |
f2c = FortranCTransformation() |
| 509 |
f2c.apply(source=routine, path=tmp_path, role='kernel') |
| 510 |
f2cwrap.apply(source=routine, path=tmp_path, role='kernel') |
| 511 |
|
| 512 |
# Build and wrap the cross-compiled library |
| 513 |
sources = [ |
| 514 |
module, wrapperpath(tmp_path, module), |
| 515 |
wrapperpath(tmp_path, routine), cpath(tmp_path, routine) |
| 516 |
] |
| 517 |
wrap = [tmp_path/'mod_var_type_mod.f90', wrapperpath(tmp_path, routine).name] |
| 518 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 519 |
c_kernel = jit_compile_lib(sources=sources, wrap=wrap, path=tmp_path, name=libname, builder=builder) |
| 520 |
|
| 521 |
c_kernel.mod_var_type_mod.param1 = 2 |
| 522 |
c_kernel.mod_var_type_mod.param2 = 4. |
| 523 |
c_kernel.mod_var_type_mod.param3 = 3. |
| 524 |
a, b, c = c_kernel.transp_mod_var_fc_mod.transp_mod_var_fc() |
| 525 |
assert a == 3 and b == 5. and c == 4. |
| 526 |
|
| 527 |
|
| 528 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 529 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 530 |
def test_transpile_vectorization(tmp_path, builder, frontend, use_c_ptr): |
| 531 |
""" |
| 532 |
Tests vector-notation conversion and local multi-dimensional arrays. |
| 533 |
""" |
| 534 |
|
| 535 |
fcode = """ |
| 536 |
subroutine transp_vect(n, m, scalar, v1, v2) |
| 537 |
use iso_fortran_env, only: real64 |
| 538 |
implicit none |
| 539 |
integer, intent(in) :: n, m |
| 540 |
real(kind=real64), intent(inout) :: scalar |
| 541 |
real(kind=real64), intent(inout) :: v1(n), v2(n) |
| 542 |
|
| 543 |
real(kind=real64) :: matrix(n, m) |
| 544 |
|
| 545 |
integer :: i |
| 546 |
|
| 547 |
v1(:) = scalar + 1.0 |
| 548 |
matrix(:, :) = scalar + 2. |
| 549 |
v2(:) = matrix(:, 2) |
| 550 |
v2(1) = 1. |
| 551 |
end subroutine transp_vect |
| 552 |
""" |
| 553 |
|
| 554 |
# Generate reference code, compile run and verify |
| 555 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 556 |
filepath = tmp_path/(f'transp_vect{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 557 |
n, m = 3, 4 |
| 558 |
scalar = 2.0 |
| 559 |
v1 = np.zeros(shape=(n,), order='F') |
| 560 |
v2 = np.zeros(shape=(n,), order='F') |
| 561 |
jit_compile_and_run(routine, n, m, scalar, v1, v2, filepath=filepath) |
| 562 |
|
| 563 |
assert np.all(v1 == 3.) |
| 564 |
assert v2[0] == 1. and np.all(v2[1:] == 4.) |
| 565 |
|
| 566 |
# Generate and test the transpiled C kernel |
| 567 |
f2c = FortranCTransformation() |
| 568 |
f2c.apply(source=routine, path=tmp_path) |
| 569 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 570 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 571 |
|
| 572 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 573 |
c_kernel = jit_compile_lib( |
| 574 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 575 |
path=tmp_path, name=libname, builder=builder |
| 576 |
) |
| 577 |
fc_function = c_kernel.transp_vect_fc_mod.transp_vect_fc |
| 578 |
|
| 579 |
# Test the trnapiled C kernel |
| 580 |
n, m = 3, 4 |
| 581 |
scalar = 2.0 |
| 582 |
v1 = np.zeros(shape=(n,), order='F') |
| 583 |
v2 = np.zeros(shape=(n,), order='F') |
| 584 |
fc_function(n, m, scalar, v1, v2) |
| 585 |
|
| 586 |
assert np.all(v1 == 3.) |
| 587 |
assert v2[0] == 1. and np.all(v2[1:] == 4.) |
| 588 |
|
| 589 |
|
| 590 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 591 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 592 |
def test_transpile_intrinsics(tmp_path, builder, frontend, use_c_ptr): |
| 593 |
""" |
| 594 |
A simple test routine to test supported intrinsic functions |
| 595 |
""" |
| 596 |
|
| 597 |
fcode = """ |
| 598 |
subroutine transpile_intrinsics(v1, v2, v3, v4, vmin, vmax, vabs, vmin_nested, vmax_nested) |
| 599 |
! Test supported intrinsic functions |
| 600 |
use iso_fortran_env, only: real64 |
| 601 |
real(kind=real64), intent(in) :: v1, v2, v3, v4 |
| 602 |
real(kind=real64), intent(out) :: vmin, vmax, vabs, vmin_nested, vmax_nested |
| 603 |
|
| 604 |
vmin = min(v1, v2) |
| 605 |
vmax = max(v1, v2) |
| 606 |
vabs = abs(v1 - v2) |
| 607 |
vmin_nested = min(min(v1, v2), min(v3, v4)) |
| 608 |
vmax_nested = max(max(v1, v2), max(v3, v4)) |
| 609 |
end subroutine transpile_intrinsics |
| 610 |
""" |
| 611 |
|
| 612 |
# Generate reference code, compile run and verify |
| 613 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 614 |
filepath = tmp_path/(f'transpile_intrinsics{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 615 |
# Test the reference solution |
| 616 |
v1, v2, v3, v4 = 2., 4., 1., 5. |
| 617 |
vmin, vmax, vabs, vmin_nested, vmax_nested = jit_compile_and_run( |
| 618 |
routine, v1, v2, v3, v4, filepath=filepath |
| 619 |
) |
| 620 |
assert vmin == 2. and vmax == 4. and vabs == 2. |
| 621 |
assert vmin_nested == 1. and vmax_nested == 5. |
| 622 |
|
| 623 |
# Generate and test the transpiled C kernel |
| 624 |
f2c = FortranCTransformation() |
| 625 |
f2c.apply(source=routine, path=tmp_path) |
| 626 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 627 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 628 |
|
| 629 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 630 |
c_kernel = jit_compile_lib( |
| 631 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 632 |
path=tmp_path, name=libname, builder=builder |
| 633 |
) |
| 634 |
fc_function = c_kernel.transpile_intrinsics_fc_mod.transpile_intrinsics_fc |
| 635 |
|
| 636 |
vmin, vmax, vabs, vmin_nested, vmax_nested = fc_function(v1, v2, v3, v4) |
| 637 |
assert vmin == 2. and vmax == 4. and vabs == 2. |
| 638 |
assert vmin_nested == 1. and vmax_nested == 5. |
| 639 |
|
| 640 |
|
| 641 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 642 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 643 |
def test_transpile_loop_indices(tmp_path, builder, frontend, use_c_ptr): |
| 644 |
""" |
| 645 |
Test to ensure loop indexing translates correctly |
| 646 |
""" |
| 647 |
|
| 648 |
fcode = """ |
| 649 |
subroutine transp_loop_ind(n, idx, mask1, mask2, mask3) |
| 650 |
! Test to ensure loop indexing translates correctly |
| 651 |
use iso_fortran_env, only: real64 |
| 652 |
integer, intent(in) :: n, idx |
| 653 |
integer, intent(inout) :: mask1(n), mask2(n) |
| 654 |
real(kind=real64), intent(inout) :: mask3(n) |
| 655 |
|
| 656 |
integer :: i |
| 657 |
|
| 658 |
do i=1, n |
| 659 |
if (i < idx) then |
| 660 |
mask1(i) = 1 |
| 661 |
end if |
| 662 |
|
| 663 |
if (i == idx) then |
| 664 |
mask1(i) = 2 |
| 665 |
end if |
| 666 |
|
| 667 |
mask2(i) = i |
| 668 |
end do |
| 669 |
mask3(n) = 3.0 |
| 670 |
end subroutine transp_loop_ind |
| 671 |
""" |
| 672 |
|
| 673 |
# Generate reference code, compile run and verify |
| 674 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 675 |
filepath = tmp_path/(f'transp_loop_ind{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 676 |
# Test the reference solution |
| 677 |
n = 6 |
| 678 |
cidx, fidx = 3, 4 |
| 679 |
mask1 = np.zeros(shape=(n,), order='F', dtype=np.int32) |
| 680 |
mask2 = np.zeros(shape=(n,), order='F', dtype=np.int32) |
| 681 |
mask3 = np.zeros(shape=(n,), order='F', dtype=np.float64) |
| 682 |
|
| 683 |
jit_compile_and_run(routine, n=n, idx=fidx, mask1=mask1, mask2=mask2, mask3=mask3, filepath=filepath) |
| 684 |
assert np.all(mask1[:cidx-1] == 1) |
| 685 |
assert mask1[cidx] == 2 |
| 686 |
assert np.all(mask1[cidx+1:] == 0) |
| 687 |
assert np.all(mask2 == np.arange(n, dtype=np.int32) + 1) |
| 688 |
assert np.all(mask3[:-1] == 0.) |
| 689 |
assert mask3[-1] == 3. |
| 690 |
|
| 691 |
# Generate and test the transpiled C kernel |
| 692 |
f2c = FortranCTransformation() |
| 693 |
f2c.apply(source=routine, path=tmp_path) |
| 694 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 695 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 696 |
|
| 697 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 698 |
c_kernel = jit_compile_lib( |
| 699 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 700 |
path=tmp_path, name=libname, builder=builder |
| 701 |
) |
| 702 |
fc_function = c_kernel.transp_loop_ind_fc_mod.transp_loop_ind_fc |
| 703 |
|
| 704 |
mask1 = np.zeros(shape=(n,), order='F', dtype=np.int32) |
| 705 |
mask2 = np.zeros(shape=(n,), order='F', dtype=np.int32) |
| 706 |
mask3 = np.zeros(shape=(n,), order='F', dtype=np.float64) |
| 707 |
fc_function(n=n, idx=fidx, mask1=mask1, mask2=mask2, mask3=mask3) |
| 708 |
assert np.all(mask1[:cidx-1] == 1) |
| 709 |
assert mask1[cidx] == 2 |
| 710 |
assert np.all(mask1[cidx+1:] == 0) |
| 711 |
assert np.all(mask2 == np.arange(n, dtype=np.int32) + 1) |
| 712 |
assert np.all(mask3[:-1] == 0.) |
| 713 |
assert mask3[-1] == 3. |
| 714 |
|
| 715 |
|
| 716 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 717 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 718 |
def test_transpile_logical_statements(tmp_path, builder, frontend, use_c_ptr): |
| 719 |
""" |
| 720 |
A simple test routine to test logical statements |
| 721 |
""" |
| 722 |
|
| 723 |
fcode = """ |
| 724 |
subroutine logical_stmts(v1, v2, v_xor, v_xnor, v_nand, v_neqv, v_val) |
| 725 |
logical, intent(in) :: v1, v2 |
| 726 |
logical, intent(out) :: v_xor, v_nand, v_xnor, v_neqv, v_val(2) |
| 727 |
|
| 728 |
v_xor = (v1 .and. .not. v2) .or. (.not. v1 .and. v2) |
| 729 |
v_xnor = v1 .eqv. v2 |
| 730 |
v_nand = .not. (v1 .and. v2) |
| 731 |
v_neqv = v1 .neqv. v2 |
| 732 |
v_val(1) = .true. |
| 733 |
v_val(2) = .false. |
| 734 |
|
| 735 |
end subroutine logical_stmts |
| 736 |
""" |
| 737 |
|
| 738 |
# Generate reference code, compile run and verify |
| 739 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 740 |
filepath = tmp_path/(f'logical_stmts{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 741 |
function = jit_compile(routine, filepath=filepath, objname='logical_stmts') |
| 742 |
|
| 743 |
# Test the reference solution |
| 744 |
for v1 in range(2): |
| 745 |
for v2 in range(2): |
| 746 |
v_val = np.zeros(shape=(2,), order='F', dtype=np.int32) |
| 747 |
v_xor, v_xnor, v_nand, v_neqv = function(v1, v2, v_val) |
| 748 |
assert v_xor == (v1 and not v2) or (not v1 and v2) |
| 749 |
assert v_xnor == (v1 and v2) or not (v1 or v2) |
| 750 |
assert v_nand == (not (v1 and v2)) |
| 751 |
assert v_neqv == ((not (v1 and v2)) and (v1 or v2)) |
| 752 |
assert v_val[0] and not v_val[1] |
| 753 |
|
| 754 |
# Generate and test the transpiled C kernel |
| 755 |
f2c = FortranCTransformation() |
| 756 |
f2c.apply(source=routine, path=tmp_path) |
| 757 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 758 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 759 |
|
| 760 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 761 |
c_kernel = jit_compile_lib( |
| 762 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 763 |
path=tmp_path, name=libname, builder=builder |
| 764 |
) |
| 765 |
fc_function = c_kernel.logical_stmts_fc_mod.logical_stmts_fc |
| 766 |
|
| 767 |
for v1 in range(2): |
| 768 |
for v2 in range(2): |
| 769 |
v_val = np.zeros(shape=(2,), order='F', dtype=np.int32) |
| 770 |
v_xor, v_xnor, v_nand, v_neqv = fc_function(v1, v2, v_val) |
| 771 |
assert v_xor == (v1 and not v2) or (not v1 and v2) |
| 772 |
assert v_xnor == (v1 and v2) or not (v1 or v2) |
| 773 |
assert v_nand == (not (v1 and v2)) |
| 774 |
assert v_neqv == ((not (v1 and v2)) and (v1 or v2)) |
| 775 |
assert v_val[0] and not v_val[1] |
| 776 |
|
| 777 |
|
| 778 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 779 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 780 |
def test_transpile_multibody_conditionals(tmp_path, builder, frontend, use_c_ptr): |
| 781 |
""" |
| 782 |
Test correct transformation of multi-body conditionals. |
| 783 |
""" |
| 784 |
fcode = """ |
| 785 |
subroutine multibody_cond(in1, out1, out2) |
| 786 |
integer, intent(in) :: in1 |
| 787 |
integer, intent(out) :: out1, out2 |
| 788 |
|
| 789 |
if (in1 > 5) then |
| 790 |
out1 = 5 |
| 791 |
else |
| 792 |
out1 = 1 |
| 793 |
end if |
| 794 |
|
| 795 |
if (in1 < 0) then |
| 796 |
out2 = 0 |
| 797 |
else if (in1 > 5) then |
| 798 |
out2 = 6 |
| 799 |
out2 = out2 - 1 |
| 800 |
else if (3 < in1 .and. in1 <= 5) then |
| 801 |
out2 = 4 |
| 802 |
else |
| 803 |
out2 = in1 |
| 804 |
end if |
| 805 |
end subroutine multibody_cond |
| 806 |
""" |
| 807 |
# Generate reference code, compile run and verify |
| 808 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 809 |
filepath = tmp_path/(f'multibody_cond{"_c_ptr" if use_c_ptr else ""}_{frontend}.f90') |
| 810 |
function = jit_compile(routine, filepath=filepath, objname='multibody_cond') |
| 811 |
|
| 812 |
out1, out2 = function(5) |
| 813 |
assert out1 == 1 and out2 == 4 |
| 814 |
|
| 815 |
out1, out2 = function(2) |
| 816 |
assert out1 == 1 and out2 == 2 |
| 817 |
|
| 818 |
out1, out2 = function(-1) |
| 819 |
assert out1 == 1 and out2 == 0 |
| 820 |
|
| 821 |
out1, out2 = function(10) |
| 822 |
assert out1 == 5 and out2 == 5 |
| 823 |
|
| 824 |
clean_test(filepath) |
| 825 |
|
| 826 |
# Generate and test the transpiled C kernel |
| 827 |
f2c = FortranCTransformation() |
| 828 |
f2c.apply(source=routine, path=tmp_path) |
| 829 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 830 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 831 |
|
| 832 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 833 |
c_kernel = jit_compile_lib( |
| 834 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 835 |
path=tmp_path, name=libname, builder=builder |
| 836 |
) |
| 837 |
fc_function = c_kernel.multibody_cond_fc_mod.multibody_cond_fc |
| 838 |
|
| 839 |
out1, out2 = fc_function(5) |
| 840 |
assert out1 == 1 and out2 == 4 |
| 841 |
|
| 842 |
out1, out2 = fc_function(2) |
| 843 |
assert out1 == 1 and out2 == 2 |
| 844 |
|
| 845 |
out1, out2 = fc_function(-1) |
| 846 |
assert out1 == 1 and out2 == 0 |
| 847 |
|
| 848 |
out1, out2 = fc_function(10) |
| 849 |
assert out1 == 5 and out2 == 5 |
| 850 |
|
| 851 |
|
| 852 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 853 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 854 |
def test_transpile_inline_elemental_functions(tmp_path, builder, frontend, use_c_ptr): |
| 855 |
""" |
| 856 |
Test correct inlining of elemental functions in C transpilation. |
| 857 |
""" |
| 858 |
fcode_module = """ |
| 859 |
module multiply_mod_c |
| 860 |
use iso_fortran_env, only: real64 |
| 861 |
implicit none |
| 862 |
contains |
| 863 |
|
| 864 |
elemental function multiply(a, b) |
| 865 |
real(kind=real64) :: multiply |
| 866 |
real(kind=real64), intent(in) :: a, b |
| 867 |
|
| 868 |
multiply = a * b |
| 869 |
end function multiply |
| 870 |
end module multiply_mod_c |
| 871 |
""" |
| 872 |
|
| 873 |
fcode = """ |
| 874 |
subroutine inline_elemental(v1, v2, v3) |
| 875 |
use iso_fortran_env, only: real64 |
| 876 |
use multiply_mod_c, only: multiply |
| 877 |
real(kind=real64), intent(in) :: v1 |
| 878 |
real(kind=real64), intent(out) :: v2, v3 |
| 879 |
|
| 880 |
v2 = multiply(v1, 6._real64) |
| 881 |
v3 = 600. + multiply(6._real64, 11._real64) |
| 882 |
end subroutine inline_elemental |
| 883 |
""" |
| 884 |
# Generate reference code, compile run and verify |
| 885 |
module = Module.from_source(fcode_module, frontend=frontend, xmods=[tmp_path]) |
| 886 |
routine = Subroutine.from_source(fcode, frontend=frontend, xmods=[tmp_path]) |
| 887 |
refname = f'ref_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 888 |
reference = jit_compile_lib([module, routine], path=tmp_path, name=refname, builder=builder) |
| 889 |
|
| 890 |
v2, v3 = reference.inline_elemental(11.) |
| 891 |
assert v2 == 66. |
| 892 |
assert v3 == 666. |
| 893 |
|
| 894 |
(tmp_path/f'{module.name}.f90').unlink() |
| 895 |
(tmp_path/f'{routine.name}.f90').unlink() |
| 896 |
|
| 897 |
# Now transpile with supplied elementals but without module |
| 898 |
routine = Subroutine.from_source(fcode, definitions=module, frontend=frontend, xmods=[tmp_path]) |
| 899 |
|
| 900 |
f2c = FortranCTransformation(inline_elementals=True) |
| 901 |
f2c.apply(source=routine, path=tmp_path) |
| 902 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 903 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 904 |
|
| 905 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 906 |
c_kernel = jit_compile_lib( |
| 907 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 908 |
path=tmp_path, name=libname, builder=builder |
| 909 |
) |
| 910 |
fc_mod = c_kernel.inline_elemental_fc_mod |
| 911 |
|
| 912 |
v2, v3 = fc_mod.inline_elemental_fc(11.) |
| 913 |
assert v2 == 66. |
| 914 |
assert v3 == 666. |
| 915 |
|
| 916 |
|
| 917 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 918 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 919 |
def test_transpile_inline_elementals_recursive(tmp_path, builder, frontend, use_c_ptr): |
| 920 |
""" |
| 921 |
Test correct inlining of nested elemental functions. |
| 922 |
""" |
| 923 |
fcode_module = """ |
| 924 |
module multiply_plus_one_mod |
| 925 |
use iso_fortran_env, only: real64 |
| 926 |
implicit none |
| 927 |
contains |
| 928 |
|
| 929 |
elemental function multiply(a, b) |
| 930 |
real(kind=real64) :: multiply |
| 931 |
real(kind=real64), intent(in) :: a, b |
| 932 |
|
| 933 |
multiply = a * b |
| 934 |
end function multiply |
| 935 |
|
| 936 |
elemental function plus_one(a) |
| 937 |
real(kind=real64) :: plus_one |
| 938 |
real(kind=real64), intent(in) :: a |
| 939 |
|
| 940 |
plus_one = a + 1._real64 |
| 941 |
end function plus_one |
| 942 |
|
| 943 |
elemental function multiply_plus_one(a, b) |
| 944 |
real(kind=real64) :: multiply_plus_one |
| 945 |
real(kind=real64), intent(in) :: a, b |
| 946 |
|
| 947 |
multiply_plus_one = multiply(plus_one(a), b) |
| 948 |
end function multiply_plus_one |
| 949 |
end module multiply_plus_one_mod |
| 950 |
""" |
| 951 |
|
| 952 |
fcode = """ |
| 953 |
subroutine inline_elementals_rec(v1, v2, v3) |
| 954 |
use iso_fortran_env, only: real64 |
| 955 |
use multiply_plus_one_mod, only: multiply_plus_one |
| 956 |
real(kind=real64), intent(in) :: v1 |
| 957 |
real(kind=real64), intent(out) :: v2, v3 |
| 958 |
|
| 959 |
v2 = multiply_plus_one(v1, 6._real64) |
| 960 |
v3 = 600. + multiply_plus_one(5._real64, 11._real64) |
| 961 |
end subroutine inline_elementals_rec |
| 962 |
""" |
| 963 |
# Generate reference code, compile run and verify |
| 964 |
module = Module.from_source(fcode_module, frontend=frontend, xmods=[tmp_path]) |
| 965 |
routine = Subroutine.from_source(fcode, frontend=frontend, xmods=[tmp_path]) |
| 966 |
refname = f'ref_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 967 |
reference = jit_compile_lib([module, routine], path=tmp_path, name=refname, builder=builder) |
| 968 |
|
| 969 |
v2, v3 = reference.inline_elementals_rec(10.) |
| 970 |
assert v2 == 66. |
| 971 |
assert v3 == 666. |
| 972 |
|
| 973 |
(tmp_path/f'{module.name}.f90').unlink() |
| 974 |
(tmp_path/f'{routine.name}.f90').unlink() |
| 975 |
|
| 976 |
# Now transpile with supplied elementals but without module |
| 977 |
routine = Subroutine.from_source(fcode, definitions=module, frontend=frontend, xmods=[tmp_path]) |
| 978 |
|
| 979 |
f2c = FortranCTransformation(inline_elementals=True) |
| 980 |
f2c.apply(source=routine, path=tmp_path) |
| 981 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 982 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 983 |
|
| 984 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 985 |
c_kernel = jit_compile_lib( |
| 986 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 987 |
path=tmp_path, name=libname, builder=builder |
| 988 |
) |
| 989 |
fc_mod = c_kernel.inline_elementals_rec_fc_mod |
| 990 |
|
| 991 |
v2, v3 = fc_mod.inline_elementals_rec_fc(10.) |
| 992 |
assert v2 == 66. |
| 993 |
assert v3 == 666. |
| 994 |
|
| 995 |
|
| 996 |
@pytest.mark.parametrize('use_c_ptr', (False, True)) |
| 997 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 998 |
def test_transpile_expressions(tmp_path, builder, frontend, use_c_ptr): |
| 999 |
""" |
| 1000 |
A simple test to verify expression parenthesis and resolution |
| 1001 |
of minus sign |
| 1002 |
""" |
| 1003 |
|
| 1004 |
fcode = """ |
| 1005 |
subroutine transpile_expressions(n, scalar, vector) |
| 1006 |
use iso_fortran_env, only: real64 |
| 1007 |
implicit none |
| 1008 |
integer, intent(in) :: n |
| 1009 |
real(kind=real64), intent(in) :: scalar |
| 1010 |
real(kind=real64), intent(inout) :: vector(n) |
| 1011 |
|
| 1012 |
integer :: i |
| 1013 |
|
| 1014 |
vector(1) = scalar |
| 1015 |
do i=2, n |
| 1016 |
vector(i) = vector(i-1) - (-scalar) |
| 1017 |
end do |
| 1018 |
end subroutine transpile_expressions |
| 1019 |
""" |
| 1020 |
|
| 1021 |
# Generate reference code, compile run and verify |
| 1022 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1023 |
filepath = tmp_path/f'{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend!s}.f90' |
| 1024 |
n = 10 |
| 1025 |
scalar = 2.0 |
| 1026 |
vector = np.zeros(shape=(n,), order='F') |
| 1027 |
jit_compile_and_run(routine, n, scalar, vector, filepath=filepath) |
| 1028 |
|
| 1029 |
assert np.all(vector == [i * scalar for i in range(1, n+1)]) |
| 1030 |
|
| 1031 |
# Generate and test the transpiled C kernel |
| 1032 |
f2c = FortranCTransformation() |
| 1033 |
f2c.apply(source=routine, path=tmp_path) |
| 1034 |
f2cwrap = FortranISOCWrapperTransformation(use_c_ptr=use_c_ptr) |
| 1035 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 1036 |
|
| 1037 |
libname = f'fc_{routine.name}{"_c_ptr" if use_c_ptr else ""}_{frontend}' |
| 1038 |
c_kernel = jit_compile_lib( |
| 1039 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 1040 |
path=tmp_path, name=libname, builder=builder |
| 1041 |
) |
| 1042 |
fc_function = c_kernel.transpile_expressions_fc_mod.transpile_expressions_fc |
| 1043 |
|
| 1044 |
# Make sure minus signs are represented correctly in the C code |
| 1045 |
ccode = cpath(tmp_path, routine).read_text() |
| 1046 |
# double minus due to index shift to 0 |
| 1047 |
assert 'vector[i - 1 - 1]' in ccode or 'vector[-1 + i - 1]' in ccode |
| 1048 |
assert 'vector[i - 1]' in ccode |
| 1049 |
assert '-scalar' in ccode # scalar with negative sign |
| 1050 |
|
| 1051 |
n = 10 |
| 1052 |
scalar = 2.0 |
| 1053 |
vector = np.zeros(shape=(n,), order='F') |
| 1054 |
fc_function(n, scalar, vector) |
| 1055 |
|
| 1056 |
assert np.all(vector == [i * scalar for i in range(1, n+1)]) |
| 1057 |
|
| 1058 |
|
| 1059 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1060 |
@pytest.mark.parametrize('language', ('c', 'cuda')) |
| 1061 |
@pytest.mark.parametrize('chevron', (False, True)) |
| 1062 |
def test_transpile_call(tmp_path, frontend, language, chevron): |
| 1063 |
fcode_module = """ |
| 1064 |
module transpile_call_kernel_mod |
| 1065 |
implicit none |
| 1066 |
contains |
| 1067 |
|
| 1068 |
subroutine transpile_call_kernel(a, b, c, arr1, arr2, len) |
| 1069 |
integer, intent(inout) :: a, c |
| 1070 |
integer, intent(in) :: b |
| 1071 |
integer, intent(in) :: len |
| 1072 |
integer, intent(inout) :: arr1(len, len) |
| 1073 |
integer, intent(in) :: arr2(len, len) |
| 1074 |
a = b |
| 1075 |
c = b |
| 1076 |
end subroutine transpile_call_kernel |
| 1077 |
end module transpile_call_kernel_mod |
| 1078 |
""" |
| 1079 |
|
| 1080 |
fcode = """ |
| 1081 |
subroutine transpile_call_driver(a) |
| 1082 |
use transpile_call_kernel_mod, only: transpile_call_kernel |
| 1083 |
integer, intent(inout) :: a |
| 1084 |
integer, parameter :: len = 5 |
| 1085 |
integer :: arr1(len, len) |
| 1086 |
integer :: arr2(len, len) |
| 1087 |
integer :: b |
| 1088 |
b = 2 * len |
| 1089 |
call transpile_call_kernel(a, b, arr2(1, 1), arr1, arr2, len) |
| 1090 |
end subroutine transpile_call_driver |
| 1091 |
""" |
| 1092 |
module = Module.from_source(fcode_module, frontend=frontend, xmods=[tmp_path]) |
| 1093 |
routine = Subroutine.from_source(fcode, frontend=frontend, definitions=module, xmods=[tmp_path]) |
| 1094 |
if chevron: |
| 1095 |
calls = FindNodes(ir.CallStatement).visit(routine.body) |
| 1096 |
calls[0]._update(chevron=(sym.IntLiteral(1), sym.IntLiteral(1))) |
| 1097 |
f2c = FortranCTransformation(language=language) |
| 1098 |
f2c.apply(source=module.subroutine_map['transpile_call_kernel'], path=tmp_path, role='kernel') |
| 1099 |
ccode_kernel = cpath(tmp_path, module.routines[0]).read_text().replace(' ', '').replace('\n', '') |
| 1100 |
f2c.apply(source=routine, path=tmp_path, role='kernel') |
| 1101 |
ccode_driver = cpath(tmp_path, routine).read_text().replace(' ', '').replace('\n', '') |
| 1102 |
|
| 1103 |
assert "int*a,intb,int*c" in ccode_kernel |
| 1104 |
# check for applied Dereference |
| 1105 |
assert "(*a)=b;" in ccode_kernel |
| 1106 |
assert "(*c)=b;" in ccode_kernel |
| 1107 |
# check for applied 'const' and 'restrict'/'__restrict__' |
| 1108 |
if language == 'cuda': |
| 1109 |
assert 'int*a,intb,int*c,int*__restrict__arr1,constint*__restrict__arr2' in ccode_kernel |
| 1110 |
else: |
| 1111 |
assert 'int*a,intb,int*c,int*restrictarr1,int*restrictarr2' in ccode_kernel |
| 1112 |
# check for applied Reference and chevron |
| 1113 |
if chevron and language == 'cuda': |
| 1114 |
assert "transpile_call_kernel<<<1,1>>>((&a),b,(&arr2[" in ccode_driver |
| 1115 |
else: |
| 1116 |
assert "transpile_call_kernel((&a),b,(&arr2[" in ccode_driver |
| 1117 |
|
| 1118 |
|
| 1119 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1120 |
@pytest.mark.parametrize('codegen', (cgen, cppgen, cudagen)) |
| 1121 |
@pytest.mark.parametrize('header', (False, True)) |
| 1122 |
@pytest.mark.parametrize('guards', (False, True)) |
| 1123 |
@pytest.mark.parametrize('extern', (False, True)) |
| 1124 |
@pytest.mark.parametrize('guard_name', (None, 'random_guard_name')) |
| 1125 |
def test_transpile_simple_routine(tmp_path, frontend, codegen, guards, guard_name, |
| 1126 |
header, extern): |
| 1127 |
""" |
| 1128 |
Test correct transpilation of functions in C transpilation with a focus |
| 1129 |
on code-gen options. |
| 1130 |
""" |
| 1131 |
|
| 1132 |
fcode = """ |
| 1133 |
subroutine add(a, b, result) |
| 1134 |
real, intent(in) :: a, b |
| 1135 |
real, intent(out) :: result |
| 1136 |
|
| 1137 |
result = a + b |
| 1138 |
end subroutine add |
| 1139 |
""".strip() |
| 1140 |
|
| 1141 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1142 |
f2c = FortranCTransformation() |
| 1143 |
f2c.apply(source=routine, path=tmp_path) |
| 1144 |
|
| 1145 |
c_routine = codegen(routine, guards=guards, guard_name=guard_name, |
| 1146 |
header=header, extern=extern) |
| 1147 |
if extern and codegen in (cppgen, cudagen): |
| 1148 |
assert 'extern "C"' in c_routine |
| 1149 |
else: |
| 1150 |
assert 'extern "C"' not in c_routine |
| 1151 |
if header: |
| 1152 |
assert 'void add(double a, double b, double result);' in c_routine |
| 1153 |
else: |
| 1154 |
assert 'void add(double a, double b, double result) {' in c_routine |
| 1155 |
if guards: |
| 1156 |
if guard_name is None: |
| 1157 |
assert '#ifndef ADD_H' in c_routine |
| 1158 |
assert '#define ADD_H' in c_routine |
| 1159 |
assert '#endif' in c_routine |
| 1160 |
else: |
| 1161 |
assert '#ifndef random_guard_name' in c_routine |
| 1162 |
assert '#define random_guard_name' in c_routine |
| 1163 |
assert '#endif' in c_routine |
| 1164 |
else: |
| 1165 |
assert '#ifndef' not in c_routine |
| 1166 |
assert '#define' not in c_routine |
| 1167 |
assert '#endif' not in c_routine |
| 1168 |
|
| 1169 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1170 |
@pytest.mark.parametrize('codegen', (cgen, cppgen, cudagen)) |
| 1171 |
def test_transpile_routine_with_interface(tmp_path, frontend, codegen): |
| 1172 |
""" |
| 1173 |
Test transpilation of 'INTERFACE's. |
| 1174 |
""" |
| 1175 |
|
| 1176 |
fcode = """ |
| 1177 |
subroutine some_routine_with_interf(a, b, result) |
| 1178 |
INTERFACE |
| 1179 |
SUBROUTINE KERNEL(a, b, c) |
| 1180 |
INTEGER, INTENT(INOUT) :: a, b, c |
| 1181 |
END SUBROUTINE KERNEL |
| 1182 |
END INTERFACE |
| 1183 |
|
| 1184 |
real, intent(in) :: a, b |
| 1185 |
real, intent(out) :: result |
| 1186 |
|
| 1187 |
result = a + b |
| 1188 |
end subroutine some_routine_with_interf |
| 1189 |
""".strip() |
| 1190 |
|
| 1191 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1192 |
f2c = FortranCTransformation() |
| 1193 |
f2c.apply(source=routine, path=tmp_path) |
| 1194 |
c_routine = codegen(routine).lower() |
| 1195 |
assert 'interface' not in c_routine |
| 1196 |
assert 'kernel' not in c_routine |
| 1197 |
|
| 1198 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1199 |
@pytest.mark.parametrize('f_type', ['integer', 'real']) |
| 1200 |
@pytest.mark.parametrize('codegen', (cgen, cppgen, cudagen)) |
| 1201 |
def test_transpile_inline_functions(tmp_path, frontend, f_type, codegen): |
| 1202 |
""" |
| 1203 |
Test correct transpilation of functions in C transpilation. |
| 1204 |
""" |
| 1205 |
|
| 1206 |
fcode = f""" |
| 1207 |
function add(a, b) |
| 1208 |
{f_type} :: add |
| 1209 |
{f_type}, intent(in) :: a, b |
| 1210 |
|
| 1211 |
add = a + b |
| 1212 |
end function add |
| 1213 |
""".format(f_type) |
| 1214 |
|
| 1215 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1216 |
f2c = FortranCTransformation() |
| 1217 |
f2c.apply(source=routine, path=tmp_path) |
| 1218 |
|
| 1219 |
f_type_map = {'integer': 'int', 'real': 'double'} |
| 1220 |
c_routine = codegen(routine) |
| 1221 |
assert 'return add;' in c_routine |
| 1222 |
assert f'{f_type_map[f_type]} add(' in c_routine |
| 1223 |
|
| 1224 |
|
| 1225 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1226 |
@pytest.mark.parametrize('f_type', ['integer', 'real']) |
| 1227 |
@pytest.mark.parametrize('codegen', (cgen, cppgen, cudagen)) |
| 1228 |
def test_transpile_inline_functions_return(tmp_path, frontend, f_type, codegen): |
| 1229 |
""" |
| 1230 |
Test correct transpilation of functions in C transpilation. |
| 1231 |
""" |
| 1232 |
|
| 1233 |
fcode = f""" |
| 1234 |
function add(a, b) result(res) |
| 1235 |
{f_type} :: res |
| 1236 |
{f_type}, intent(in) :: a, b |
| 1237 |
|
| 1238 |
res = a + b |
| 1239 |
end function add |
| 1240 |
""".format(f_type) |
| 1241 |
|
| 1242 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1243 |
f2c = FortranCTransformation() |
| 1244 |
f2c.apply(source=routine, path=tmp_path) |
| 1245 |
|
| 1246 |
f_type_map = {'integer': 'int', 'real': 'double'} |
| 1247 |
c_routine = codegen(routine) |
| 1248 |
assert 'return res;' in c_routine |
| 1249 |
assert f'{f_type_map[f_type]} add(' in c_routine |
| 1250 |
|
| 1251 |
|
| 1252 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1253 |
@pytest.mark.parametrize('codegen', (cgen, cppgen, cudagen)) |
| 1254 |
def test_transpile_multiconditional_simple(tmp_path, builder, frontend, codegen): |
| 1255 |
""" |
| 1256 |
A simple test to verify multiconditionals/select case statements. |
| 1257 |
""" |
| 1258 |
|
| 1259 |
fcode = """ |
| 1260 |
subroutine multi_cond_simple(in, out) |
| 1261 |
implicit none |
| 1262 |
integer, intent(in) :: in |
| 1263 |
integer, intent(inout) :: out |
| 1264 |
|
| 1265 |
select case (in) |
| 1266 |
case (1) |
| 1267 |
out = 10 |
| 1268 |
case (2) |
| 1269 |
out = 20 |
| 1270 |
case default |
| 1271 |
out = 100 |
| 1272 |
end select |
| 1273 |
|
| 1274 |
end subroutine multi_cond_simple |
| 1275 |
""".strip() |
| 1276 |
|
| 1277 |
# for testing purposes |
| 1278 |
in_var = 0 |
| 1279 |
test_vals = [0, 1, 2, 5] |
| 1280 |
expected_results = [100, 10, 20, 100] |
| 1281 |
out_var = np.array(0) |
| 1282 |
|
| 1283 |
# compile original Fortran version |
| 1284 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1285 |
filepath = tmp_path/f'{routine.name}_{frontend!s}.f90' |
| 1286 |
function = jit_compile(routine, filepath=filepath, objname=routine.name) |
| 1287 |
# test Fortran version |
| 1288 |
for i, val in enumerate(test_vals): |
| 1289 |
in_var = val |
| 1290 |
function(in_var, out_var) |
| 1291 |
assert out_var == expected_results[i] |
| 1292 |
|
| 1293 |
# apply F2C trafo |
| 1294 |
f2c = FortranCTransformation() |
| 1295 |
f2c.apply(source=routine, path=tmp_path) |
| 1296 |
f2cwrap = FortranISOCWrapperTransformation() |
| 1297 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 1298 |
|
| 1299 |
# check whether 'switch' statement is within C code |
| 1300 |
assert 'switch' in codegen(routine) |
| 1301 |
|
| 1302 |
# compile C version |
| 1303 |
libname = f'fc_{routine.name}_{frontend}' |
| 1304 |
c_kernel = jit_compile_lib( |
| 1305 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 1306 |
path=tmp_path, name=libname, builder=builder |
| 1307 |
) |
| 1308 |
fc_function = c_kernel.multi_cond_simple_fc_mod.multi_cond_simple_fc |
| 1309 |
# test C version |
| 1310 |
for i, val in enumerate(test_vals): |
| 1311 |
in_var = val |
| 1312 |
fc_function(in_var, out_var) |
| 1313 |
assert out_var == expected_results[i] |
| 1314 |
|
| 1315 |
|
| 1316 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1317 |
def test_transpile_multiconditional(tmp_path, builder, frontend): |
| 1318 |
""" |
| 1319 |
A test to verify multiconditionals/select case statements. |
| 1320 |
""" |
| 1321 |
|
| 1322 |
fcode = """ |
| 1323 |
subroutine multi_cond(in, out) |
| 1324 |
implicit none |
| 1325 |
integer, intent(in) :: in |
| 1326 |
integer, intent(inout) :: out |
| 1327 |
|
| 1328 |
select case (in) |
| 1329 |
case (:5) |
| 1330 |
out = 10 |
| 1331 |
case (6, 7, 10:15) |
| 1332 |
out = 15 |
| 1333 |
case (8) |
| 1334 |
out = 12 |
| 1335 |
case (20:30) |
| 1336 |
out = 20 |
| 1337 |
case default |
| 1338 |
out = 100 |
| 1339 |
end select |
| 1340 |
|
| 1341 |
end subroutine multi_cond |
| 1342 |
""".strip() |
| 1343 |
|
| 1344 |
# for testing purposes |
| 1345 |
in_var = 0 |
| 1346 |
# [(<input>, <expected result>), (<input 2>, <expected result 2>), ...] |
| 1347 |
test_results = [(0, 10), (1, 10), (5, 10), (6, 15), (10, 15), (11, 15), |
| 1348 |
(15, 15), (8, 12), (20, 20), (21, 20), (29, 20), (50, 100)] |
| 1349 |
out_var = np.array(0) |
| 1350 |
|
| 1351 |
# compile original Fortran version |
| 1352 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1353 |
filepath = tmp_path/f'{routine.name}_{frontend!s}.f90' |
| 1354 |
function = jit_compile(routine, filepath=filepath, objname=routine.name) |
| 1355 |
# test Fortran version |
| 1356 |
for val in test_results: |
| 1357 |
in_var = val[0] |
| 1358 |
function(in_var, out_var) |
| 1359 |
assert out_var == val[1] |
| 1360 |
|
| 1361 |
# apply F2C trafo |
| 1362 |
f2c = FortranCTransformation() |
| 1363 |
f2c.apply(source=routine, path=tmp_path) |
| 1364 |
f2cwrap = FortranISOCWrapperTransformation() |
| 1365 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 1366 |
|
| 1367 |
# check whether 'switch' statement is within C code |
| 1368 |
assert 'switch' in cgen(routine) |
| 1369 |
|
| 1370 |
# compile C version |
| 1371 |
libname = f'fc_{routine.name}_{frontend}' |
| 1372 |
c_kernel = jit_compile_lib( |
| 1373 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 1374 |
path=tmp_path, name=libname, builder=builder |
| 1375 |
) |
| 1376 |
fc_function = c_kernel.multi_cond_fc_mod.multi_cond_fc |
| 1377 |
# test C version |
| 1378 |
for val in test_results: |
| 1379 |
in_var = val[0] |
| 1380 |
fc_function(in_var, out_var) |
| 1381 |
assert out_var == val[1] |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1386 |
@pytest.mark.parametrize('dtype', ('integer', 'real',)) |
| 1387 |
@pytest.mark.parametrize('add_float', (False, True)) |
| 1388 |
def test_transpile_special_functions(tmp_path, builder, frontend, dtype, add_float): |
| 1389 |
""" |
| 1390 |
A simple test to verify multiconditionals/select case statements. |
| 1391 |
""" |
| 1392 |
if dtype == 'real': |
| 1393 |
decl_type = f'{dtype}(kind=real64)' |
| 1394 |
kind = '._real64' |
| 1395 |
else: |
| 1396 |
decl_type = dtype |
| 1397 |
kind = '' |
| 1398 |
|
| 1399 |
fcode = f""" |
| 1400 |
subroutine transpile_special_functions(in, out) |
| 1401 |
use iso_fortran_env, only: real64 |
| 1402 |
implicit none |
| 1403 |
{decl_type}, intent(in) :: in |
| 1404 |
{decl_type}, intent(inout) :: out |
| 1405 |
if (mod(in{'+ 2._real64' if add_float else ''}, 2{kind}{'+ 0._real64' if add_float else ''}) .eq. 0) then |
| 1406 |
out = 42{kind} |
| 1407 |
else |
| 1408 |
out = 11{kind} |
| 1409 |
endif |
| 1410 |
end subroutine transpile_special_functions |
| 1411 |
""".strip() |
| 1412 |
|
| 1413 |
def init_var(dtype, val=0): |
| 1414 |
if dtype == 'real': |
| 1415 |
return np.array(np.float64(val)) |
| 1416 |
return np.array(np.int_(val)) |
| 1417 |
|
| 1418 |
# for testing purposes |
| 1419 |
in_var = init_var(dtype) |
| 1420 |
test_vals = [2, 10, 5, 3] |
| 1421 |
expected_results = [42, 42, 11, 11] |
| 1422 |
out_var = init_var(dtype) |
| 1423 |
|
| 1424 |
# compile original Fortran version |
| 1425 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1426 |
filepath = tmp_path/f'{routine.name}_{frontend!s}.f90' |
| 1427 |
function = jit_compile(routine, filepath=filepath, objname=routine.name) |
| 1428 |
# test Fortran version |
| 1429 |
for i, val in enumerate(test_vals): |
| 1430 |
in_var = val |
| 1431 |
function(in_var, out_var) |
| 1432 |
assert out_var == expected_results[i] |
| 1433 |
|
| 1434 |
clean_test(filepath) |
| 1435 |
|
| 1436 |
# apply F2C trafo |
| 1437 |
f2c = FortranCTransformation() |
| 1438 |
f2c.apply(source=routine, path=tmp_path) |
| 1439 |
f2cwrap = FortranISOCWrapperTransformation() |
| 1440 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 1441 |
|
| 1442 |
# check whether correct modulo was inserted |
| 1443 |
ccode = cpath(tmp_path, routine).read_text() |
| 1444 |
if dtype == 'integer' and not add_float: |
| 1445 |
assert '%' in ccode |
| 1446 |
if dtype == 'real' or add_float: |
| 1447 |
assert 'fmod' in ccode |
| 1448 |
|
| 1449 |
# compile C version |
| 1450 |
libname = f'fc_{routine.name}_{frontend}' |
| 1451 |
c_kernel = jit_compile_lib( |
| 1452 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine)], |
| 1453 |
path=tmp_path, name=libname, builder=builder |
| 1454 |
) |
| 1455 |
fc_function = c_kernel.transpile_special_functions_fc_mod.transpile_special_functions_fc |
| 1456 |
# test C version |
| 1457 |
for i, val in enumerate(test_vals): |
| 1458 |
in_var = val |
| 1459 |
fc_function(in_var, out_var) |
| 1460 |
assert int(out_var) == expected_results[i] |
| 1461 |
|
| 1462 |
|
| 1463 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1464 |
def test_transpile_interface_to_module(tmp_path, frontend): |
| 1465 |
driver_fcode = """ |
| 1466 |
SUBROUTINE driver_interface_to_module(a, b, c) |
| 1467 |
IMPLICIT NONE |
| 1468 |
INTERFACE |
| 1469 |
SUBROUTINE KERNEL(a, b, c) |
| 1470 |
INTEGER, INTENT(INOUT) :: a, b, c |
| 1471 |
END SUBROUTINE KERNEL |
| 1472 |
END INTERFACE |
| 1473 |
INTERFACE |
| 1474 |
SUBROUTINE KERNEL2(a, b) |
| 1475 |
INTEGER, INTENT(INOUT) :: a, b |
| 1476 |
END SUBROUTINE KERNEL2 |
| 1477 |
END INTERFACE |
| 1478 |
INTERFACE |
| 1479 |
SUBROUTINE KERNEL3(a) |
| 1480 |
INTEGER, INTENT(INOUT) :: a |
| 1481 |
END SUBROUTINE KERNEL3 |
| 1482 |
END INTERFACE |
| 1483 |
|
| 1484 |
INTEGER, INTENT(INOUT) :: a, b, c |
| 1485 |
|
| 1486 |
CALL kernel(a, b ,c) |
| 1487 |
CALL kernel2(a, b) |
| 1488 |
END SUBROUTINE driver_interface_to_module |
| 1489 |
""".strip() |
| 1490 |
|
| 1491 |
routine = Subroutine.from_source(driver_fcode, frontend=frontend) |
| 1492 |
|
| 1493 |
interfaces = FindNodes(ir.Interface).visit(routine.spec) |
| 1494 |
imports = FindNodes(ir.Import).visit(routine.spec) |
| 1495 |
assert len(interfaces) == 3 |
| 1496 |
assert not imports |
| 1497 |
|
| 1498 |
f2c = FortranCTransformation() |
| 1499 |
f2c.apply(source=routine, path=tmp_path, targets=('kernel',), role='driver') |
| 1500 |
|
| 1501 |
assert len(routine.interfaces) == 2 |
| 1502 |
imports = routine.imports |
| 1503 |
assert len(imports) == 1 |
| 1504 |
assert imports[0].module.upper() == 'KERNEL_FC_MOD' |
| 1505 |
assert imports[0].symbols == ('KERNEL_FC',) |
| 1506 |
|
| 1507 |
|
| 1508 |
@pytest.mark.parametrize('frontend', available_frontends()) |
| 1509 |
@pytest.mark.parametrize('language', ['c', 'cpp']) |
| 1510 |
def test_transpile_optional_args(tmp_path, builder, frontend, language): |
| 1511 |
""" |
| 1512 |
A simple test to verify multiconditionals/select case statements. |
| 1513 |
""" |
| 1514 |
|
| 1515 |
fcode = """ |
| 1516 |
subroutine transpile_optional_args(in, out, out2, opt_flag) |
| 1517 |
implicit none |
| 1518 |
integer, intent(in) :: in |
| 1519 |
integer, intent(inout) :: out |
| 1520 |
integer, intent(out), optional :: out2 |
| 1521 |
logical, intent(in), optional :: opt_flag |
| 1522 |
|
| 1523 |
out = in |
| 1524 |
if (present(out2)) then |
| 1525 |
out2 = 2*in |
| 1526 |
if (present(opt_flag)) then |
| 1527 |
if (opt_flag) then |
| 1528 |
out = 2* out2 |
| 1529 |
else |
| 1530 |
out = 4* out2 |
| 1531 |
endif |
| 1532 |
else |
| 1533 |
out = out2 |
| 1534 |
endif |
| 1535 |
endif |
| 1536 |
if (.not. present(out2) .and. present(opt_flag)) then |
| 1537 |
if (opt_flag) then |
| 1538 |
out = in + 1 |
| 1539 |
else |
| 1540 |
out = in + 2 |
| 1541 |
endif |
| 1542 |
endif |
| 1543 |
|
| 1544 |
end subroutine transpile_optional_args |
| 1545 |
""".strip() |
| 1546 |
|
| 1547 |
def init_out_vars(): |
| 1548 |
return np.array(0), np.array(0) |
| 1549 |
|
| 1550 |
# for testing purposes |
| 1551 |
in_var = 10 |
| 1552 |
|
| 1553 |
# compile and test original Fortran version |
| 1554 |
routine = Subroutine.from_source(fcode, frontend=frontend) |
| 1555 |
filepath = tmp_path/f'{routine.name}_{frontend!s}.f90' |
| 1556 |
function = jit_compile(routine, filepath=filepath, objname=routine.name) |
| 1557 |
out_var, out_var2 = init_out_vars() |
| 1558 |
function(in_var, out_var) |
| 1559 |
assert out_var == 10 and out_var2 == 0 |
| 1560 |
out_var, out_var2 = init_out_vars() |
| 1561 |
function(in_var, out_var, out_var2) |
| 1562 |
assert out_var == 20 and out_var2 == 20 |
| 1563 |
opt_flag = 1 |
| 1564 |
out_var, out_var2 = init_out_vars() |
| 1565 |
function(in_var, out_var, opt_flag=opt_flag) |
| 1566 |
assert out_var == 11 and out_var2 == 0 |
| 1567 |
opt_flag = 0 |
| 1568 |
out_var, out_var2 = init_out_vars() |
| 1569 |
function(in_var, out_var, opt_flag=opt_flag) |
| 1570 |
assert out_var == 12 and out_var2 == 0 |
| 1571 |
opt_flag = 1 |
| 1572 |
out_var, out_var2 = init_out_vars() |
| 1573 |
function(in_var, out_var, out_var2, opt_flag) |
| 1574 |
assert out_var == 40 and out_var2 == 20 |
| 1575 |
opt_flag = 0 |
| 1576 |
out_var, out_var2 = init_out_vars() |
| 1577 |
function(in_var, out_var, out_var2, opt_flag) |
| 1578 |
assert out_var == 80 and out_var2 == 20 |
| 1579 |
|
| 1580 |
clean_test(filepath) |
| 1581 |
|
| 1582 |
# transpile |
| 1583 |
f2c = FortranCTransformation(language=language) |
| 1584 |
f2c.apply(source=routine, path=tmp_path) |
| 1585 |
f2cwrap = FortranISOCWrapperTransformation(language=language) |
| 1586 |
f2cwrap.apply(source=routine, path=tmp_path) |
| 1587 |
|
| 1588 |
# compile and testC/C++ version |
| 1589 |
libname = f'fc_{routine.name}_{language}_{frontend}' |
| 1590 |
c_kernel = jit_compile_lib( |
| 1591 |
[wrapperpath(tmp_path, routine), cpath(tmp_path, routine, suffix=f'.{language}')], |
| 1592 |
path=tmp_path, name=libname, builder=builder |
| 1593 |
) |
| 1594 |
fc_function = c_kernel.transpile_optional_args_fc_mod.transpile_optional_args_fc |
| 1595 |
if language != 'c': |
| 1596 |
out_var, out_var2 = init_out_vars() |
| 1597 |
fc_function(in_var, out_var) |
| 1598 |
assert out_var == 10 and out_var2 == 0 |
| 1599 |
opt_flag = 1 |
| 1600 |
out_var, out_var2 = init_out_vars() |
| 1601 |
fc_function(in_var, out_var, opt_flag=opt_flag) |
| 1602 |
assert out_var == 11 and out_var2 == 0 |
| 1603 |
opt_flag = 0 |
| 1604 |
out_var, out_var2 = init_out_vars() |
| 1605 |
fc_function(in_var, out_var, opt_flag=opt_flag) |
| 1606 |
assert out_var == 12 and out_var2 == 0 |
| 1607 |
opt_flag = 1 |
| 1608 |
out_var, out_var2 = init_out_vars() |
| 1609 |
fc_function(in_var, out_var, out_var2, opt_flag) |
| 1610 |
assert out_var == 40 and out_var2 == 20 |
| 1611 |
opt_flag = 0 |
| 1612 |
out_var, out_var2 = init_out_vars() |
| 1613 |
fc_function(in_var, out_var, out_var2, opt_flag) |
| 1614 |
assert out_var == 80 and out_var2 == 20 |