@@ -3,7 +3,7 @@ program tensor_manipulation
3
3
! Import the FTorch procedures that are used in this worked example
4
4
use ftorch, only: assignment (= ), operator (+ ), torch_kCPU, torch_kFloat32, torch_tensor, &
5
5
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
7
7
8
8
use , intrinsic :: iso_c_binding, only: c_int64_t
9
9
@@ -24,10 +24,7 @@ program tensor_manipulation
24
24
25
25
! Variables for constructing tensors with torch_tensor_from_array
26
26
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
31
28
32
29
! Create a tensor of ones
33
30
! -----------------------
@@ -37,6 +34,7 @@ program tensor_manipulation
37
34
38
35
! Print the contents of the tensor
39
36
! --------------------------------
37
+ ! This will show the tensor data as well as its device type, data type, and shape.
40
38
write (* ,* ) " Contents of first input tensor:"
41
39
call torch_tensor_print(a)
42
40
@@ -51,30 +49,29 @@ program tensor_manipulation
51
49
write (* ,* ) " Contents of second input tensor:"
52
50
write (* ,* ) in_data
53
51
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
+
54
58
! Perform arithmetic on the tensors using the overloaded addition operator
55
59
! ------------------------------------------------------------------------
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.
60
63
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))
67
64
write (* ,* ) " Output:"
68
65
write (* ,* ) out_data
69
66
70
67
! Clean up
71
68
! --------
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.
74
72
call torch_tensor_delete(a)
75
73
call torch_tensor_delete(b)
76
74
call torch_tensor_delete(c)
77
- nullify(out_data)
78
75
79
76
write (* ,* ) " Tensor manipulation example ran successfully"
80
77
0 commit comments