-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from itpplasma/issue227_allocatable
Fix #227: Allow allocatable derived types as function outputs
- Loading branch information
Showing
5 changed files
with
129 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FC = gfortran | ||
FCFLAGS = -fPIC | ||
PYTHON = python | ||
|
||
%.o : %.f90 | ||
${FC} ${FCFLAGS} -c $< -o $@ | ||
|
||
all: alloc_output.o | ||
f90wrap -m itest -P alloc_output.f90 -v | ||
f2py-f90wrap --build-dir . -c -m _itest f90wrap_alloc_output.f90 alloc_output.o | ||
|
||
test: all | ||
$(PYTHON) run.py | ||
|
||
clean: | ||
rm -f *.o f90wrap*.f90 *.so *.mod | ||
rm -rf src.*/ | ||
rm -rf itest/ | ||
-rm -rf src.*/ .f2py_f2cmap .libs/ __pycache__/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
include ../make.meson.inc | ||
|
||
NAME := itest | ||
|
||
test: build | ||
$(PYTHON) run.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module alloc_output | ||
implicit none | ||
|
||
type :: alloc_output_type | ||
real :: a | ||
end type alloc_output_type | ||
|
||
contains | ||
|
||
! This should be used by the wrapper generator | ||
function alloc_output_type_func(val) result(out) | ||
real, intent(in) :: val | ||
type(alloc_output_type), allocatable :: out | ||
allocate(out) | ||
out%a = val | ||
end function alloc_output_type_func | ||
|
||
|
||
! This should be discarded by the wrapper generator | ||
function alloc_output_intrinsic_func(val) result(out) | ||
real, intent(in) :: val | ||
real, allocatable :: out | ||
allocate(out) | ||
out = val | ||
end function alloc_output_intrinsic_func | ||
|
||
|
||
! This should be discarded by the wrapper generator | ||
function alloc_output_array_func(val) result(out) | ||
real, intent(in) :: val(:) | ||
real, allocatable :: out(:) | ||
allocate(out(size(val))) | ||
out(:) = val | ||
end function alloc_output_array_func | ||
|
||
|
||
subroutine noalloc_output_subroutine(val, out) | ||
real, intent(in) :: val | ||
type(alloc_output_type), intent(inout) :: out | ||
out%a = val | ||
end subroutine noalloc_output_subroutine | ||
|
||
end module alloc_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#!/usr/bin/env python | ||
import os | ||
import gc | ||
import tracemalloc | ||
|
||
import itest | ||
|
||
|
||
def main(): | ||
test_type_output_is_wrapped() | ||
test_intrinsic_output_is_not_wrapped() | ||
test_array_output_is_not_wrapped() | ||
test_type_output_wrapper() | ||
test_memory_leak() | ||
|
||
|
||
def test_type_output_is_wrapped(): | ||
assert hasattr(itest.alloc_output, 'alloc_output_type_func') | ||
|
||
|
||
def test_intrinsic_output_is_not_wrapped(): | ||
assert (not hasattr(itest.alloc_output, 'alloc_output_intrinsic_func')) | ||
|
||
|
||
def test_array_output_is_not_wrapped(): | ||
assert (not hasattr(itest.alloc_output, 'alloc_output_array_func')) | ||
|
||
|
||
VAL = 10.0 | ||
TOL = 1e-13 | ||
|
||
|
||
def test_type_output_wrapper(): | ||
t = itest.alloc_output.alloc_output_type_func(VAL) | ||
assert(abs(t.a - VAL) < TOL) | ||
|
||
|
||
def test_memory_leak(): | ||
gc.collect() | ||
t = [] | ||
tracemalloc.start() | ||
start_snapshot = tracemalloc.take_snapshot() | ||
for i in range(2048): | ||
t.append(itest.alloc_output.alloc_output_type_func(VAL)) | ||
del t | ||
gc.collect() | ||
end_snapshot = tracemalloc.take_snapshot() | ||
tracemalloc.stop() | ||
stats = end_snapshot.compare_to(start_snapshot, 'lineno') | ||
assert sum(stat.size_diff for stat in stats) < 1024 | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters