Skip to content

Commit 9361566

Browse files
authored
Provide information about tensor finalizer (#522)
* FAQ about warnings * Provide some additional information about tensor finalizer * Provide link to Fortran-lang learn section on OO-Fortran * Reference common warnings section for finalizer warning * Refer to f08/0011 for technical details * Text specific to structure finalizer
1 parent d2eadd6 commit 9361566

2 files changed

Lines changed: 67 additions & 6 deletions

File tree

pages/usage/tensor.md

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,25 @@ the other methods (simply drop the preceding `torch_tensor_`).
8888
#### Deallocation
8989

9090
We provide a subroutine for deallocating the memory associated with a
91-
[[ftorch_tensor(module):torch_tensor(type)]] object:
92-
[[ftorch_tensor(module):torch_tensor_delete(subroutine)]].
93-
An interface is provided such that this can also be applied to arrays of tensors.
94-
Calling this subroutine manually is optional as it is called as a destructor
95-
when a [[ftorch_tensor(module):torch_tensor(type)]] goes out of scope.
96-
91+
[[ftorch(module):torch_tensor(type)]] object:
92+
[[ftorch(module):torch_tensor_delete(subroutine)]]. An interface
93+
[[ftorch(module):torch_delete(interface)]] is provided such that this can also
94+
be applied to arrays of tensors.
95+
96+
Manually deallocating a tensor that you declared and constructed in your code is
97+
actually optional. If the tensor was declared in a subroutine then
98+
[[ftorch(module):torch_tensor_delete(subroutine)]] will get called as the
99+
finalizer of [[ftorch(module):torch_tensor(type)]] when it goes out of scope. If
100+
the tensor was declared in a program then the finalizer won't get called, but
101+
this is not considered to be an issue, in the same way that in Fortran it
102+
doesn't matter if allocated arrays aren't deallocated at the end of the program.
103+
Note that if you are building FTorch with gfortran and the 2008 standard then
104+
you may see a warning related to finalizers - see the
105+
[common warnings entry](|page|/usage/troubleshooting.html#finalizer-with-fortran-2008)
106+
for more information.
107+
108+
See the [Fortran-lang page on object-oriented Fortran](https://fortran-lang.org/learn/oop_features_in_fortran/object_based_programming_techniques/#finalization-and-conclusions)
109+
for further details about finalization.
97110

98111
#### Manipulation
99112

pages/usage/troubleshooting.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ If you are experiencing problems building or using FTorch please see below for g
1010
- [Common Errors](#common-errors)
1111
- [No specific subroutine](#no-specific-subroutine)
1212
- [Segmentation faults](#segmentation-faults)
13+
- [Common warnings](#common-warnings)
1314

1415

1516
### Usage
@@ -97,3 +98,50 @@ the overloaded assignment operator should be triggered. As such, if you aren't
9798
using the bare `use ftorch` import then you should ensure you specify
9899
`use ftorch, only: assignment(=)` (as well as any other module members you
99100
require). See the [tensor documentation](|page|/usage/tensor.html) for more details.
101+
102+
103+
### Common warnings
104+
105+
#### Structure constructor finalizer with Fortran 2008
106+
107+
If you are building FTorch with gfortran and are specifying the Fortran 2008
108+
standard (e.g., with the compiler flag `-std=f2008` or by default) then you may
109+
get compiler warnings of the form:
110+
```
111+
Warning: The structure constructor at (1) has been finalized. This feature was removed by f08/0011. Use -std=f2018 or -std=gnu to eliminate the finalization.
112+
```
113+
These warn that the structure finalizer of the
114+
[[ftorch(module):torch_tensor(type)]] derived type is triggered when a tensor
115+
goes out of scope, despite the fact that this feature was removed from the 2008
116+
standard. That is, the [[ftorch(module):torch_tensor_delete(subroutine)]]
117+
subroutine is called so that the associated memory is automatically freed.
118+
Firstly, this is the behaviour that we want so we should not be too concerned.
119+
Secondly, structure finalizers are not used anywhere in FTorch, so we believe
120+
this warning to be errorneous. Use of the structure constructor for the
121+
`torch_tensor` type would be something like
122+
```fortran
123+
program
124+
use, intrinsic :: iso_c_binding, only: c_null_ptr
125+
use ftorch
126+
implicit none
127+
type(torch_tensor) :: tensor
128+
129+
tensor = torch_tensor(c_null_ptr)
130+
end program
131+
```
132+
While this code would compile successfully, the warning mentioned above would be
133+
raised.
134+
135+
@warning
136+
The code snippet above is **not** the intended way to create a tensor. The
137+
intended way is to use the provided API procedures such as
138+
[[ftorch(module):torch_tensor_from_array(interface)]] or
139+
[[ftorch(module):torch_tensor_ones(subroutine)]]. The code snippet above is
140+
only intended to illustrate the use of the structure constructor and the
141+
associated warning.
142+
@endwarning
143+
144+
See the [tensor documentation](|page|/usage/tensor.html#deallocation) for more
145+
details on the memory management of tensors and the use of the finalizer. For
146+
technical details on `f08/0011`, we refer to
147+
[https://wg5-fortran.org/N2001-N2050/N2006.txt](https://wg5-fortran.org/N2001-N2050/N2006.txt).

0 commit comments

Comments
 (0)