Skip to content

Commit 02517b5

Browse files
committed
Update demo for torch_tensor_to_array having been dropped
1 parent 2af81d2 commit 02517b5

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed

examples/0_Tensor/tensor_manipulation.f90

+15-18
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ program tensor_manipulation
33
! Import the FTorch procedures that are used in this worked example
44
use ftorch, only: assignment(=), operator(+), torch_kCPU, torch_kFloat32, torch_tensor, &
55
torch_tensor_delete, torch_tensor_empty, torch_tensor_from_array, &
6-
torch_tensor_ones, torch_tensor_print, torch_tensor_to_array
6+
torch_tensor_ones, torch_tensor_print
77

88
use, intrinsic :: iso_c_binding, only: c_int64_t
99

@@ -24,10 +24,7 @@ program tensor_manipulation
2424

2525
! Variables for constructing tensors with torch_tensor_from_array
2626
integer, parameter :: tensor_layout(ndims) = [1, 2]
27-
real(wp), dimension(2,3), target :: in_data
28-
29-
! Array for extracting an array from a tensor
30-
real(wp), dimension(:,:), pointer :: out_data
27+
real(wp), dimension(2,3), target :: in_data, out_data
3128

3229
! Create a tensor of ones
3330
! -----------------------
@@ -37,6 +34,7 @@ program tensor_manipulation
3734

3835
! Print the contents of the tensor
3936
! --------------------------------
37+
! This will show the tensor data as well as its device type, data type, and shape.
4038
write(*,*) "Contents of first input tensor:"
4139
call torch_tensor_print(a)
4240

@@ -51,30 +49,29 @@ program tensor_manipulation
5149
write(*,*) "Contents of second input tensor:"
5250
write(*,*) in_data
5351

52+
! Extract data from the tensor as a Fortran array
53+
! -----------------------------------------------
54+
! This requires some setup in advance. Create a tensor based off the Fortran array that you want
55+
! to extract data into in the same way as above. There's no need to assign values to the array.
56+
call torch_tensor_from_array(c, out_data, tensor_layout, torch_kCPU)
57+
5458
! Perform arithmetic on the tensors using the overloaded addition operator
5559
! ------------------------------------------------------------------------
56-
! It's important that the tensor used for the sum has been constructed. It's sufficient to use
57-
! torch_tensor_empty, which leaves its values unset. Note that it's required to import the
58-
! overloaded assignment and addition operators for this to work correctly.
59-
call torch_tensor_empty(c, ndims, tensor_shape, torch_kFloat32, torch_kCPU)
60+
! Note that if the output tensor hasn't been constructed as above then it will be automatically
61+
! constructed using `torch_tensor_empty` but it won't be possible to extract its data into an
62+
! array.
6063
c = a + b
61-
62-
! Extract data from the tensor as a Fortran array
63-
! -----------------------------------------------
64-
! Note that the torch_tensor_to_array subroutine will allocate the output array to the
65-
! appropriate size if it hasn't already been allocated.
66-
call torch_tensor_to_array(c, out_data, shape(in_data))
6764
write(*,*) "Output:"
6865
write(*,*) out_data
6966

7067
! Clean up
7168
! --------
72-
! It's good practice to free the memory associated with the tensors after use. We should also
73-
! nullify any pointers, such as those required by torch_tensor_to_array.
69+
! It's good practice to free the memory associated with the tensors after use. However, with
70+
! recent versions of FTorch calling `torch_tensor_delete` is optional because it has been set up
71+
! to be called automatically when the tensor goes out of scope.
7472
call torch_tensor_delete(a)
7573
call torch_tensor_delete(b)
7674
call torch_tensor_delete(c)
77-
nullify(out_data)
7875

7976
write(*,*) "Tensor manipulation example ran successfully"
8077

0 commit comments

Comments
 (0)