Loading...
Searching...
No Matches
torch2Eigen.C
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ██╗████████╗██╗ ██╗ █████╗ ██████╗ █████╗ ███████╗██╗ ██╗
3 ██║╚══██╔══╝██║ ██║██╔══██╗██╔════╝██╔══██╗ ██╔════╝██║ ██║
4 ██║ ██║ ███████║███████║██║ ███████║█████╗█████╗ ██║ ██║
5 ██║ ██║ ██╔══██║██╔══██║██║ ██╔══██║╚════╝██╔══╝ ╚██╗ ██╔╝
6 ██║ ██║ ██║ ██║██║ ██║╚██████╗██║ ██║ ██║ ╚████╔╝
7 ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═══╝
8
9 * In real Time Highly Advanced Computational Applications for Finite Volumes
10 * Copyright (C) 2017 by the ITHACA-FV authors
11-------------------------------------------------------------------------------
12
13 License
14 This file is part of ITHACA-FV
15
16 ITHACA-FV is free software: you can redistribute it and/or modify
17 it under the terms of the GNU Lesser General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 ITHACA-FV is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU Lesser General Public License for more details.
25
26 You should have received a copy of the GNU Lesser General Public License
27 along with ITHACA-FV. If not, see <http://www.gnu.org/licenses/>.
28
29\*---------------------------------------------------------------------------*/
30#include "torch2Eigen.H"
31using namespace ITHACAtorch::torch2Eigen;
32
33namespace ITHACAtorch
34{
35namespace torch2Eigen
36{
37
38template<class type>
40 Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> _eigenMatrix)
41{
42 Eigen::MatrixXf eigenMatrix = _eigenMatrix.template cast <float> ();
43 int rows = eigenMatrix.rows();
44 int cols = eigenMatrix.cols();
45
46 if (!eigenMatrix.IsRowMajor)
47 {
48 eigenMatrix.transposeInPlace();
49 }
50
51 return torch::from_blob(eigenMatrix.data(), {rows, cols}).clone();
52}
53
54template<class type>
55Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic> torchTensor2eigenMatrix(
56 torch::Tensor& torchTensor)
57{
58 std::string error_message("The provided tensor has " + std::to_string(
59 torchTensor.dim()) + " dimensions and cannot be casted in a Matrix.");
60 M_Assert(torchTensor.dim() <= 2, error_message.c_str());
61 M_Assert(torchTensor.dim() != 0, "The provided tensor has 0 dimension");
62 int rows;
63 int cols;
64 int nElem = torchTensor.size(0);
65
66 for (int i = 1; i < torchTensor.dim(); i++)
67 {
68 nElem *= torchTensor.size(i);
69 }
70
71 if (torchTensor.dim() == 1)
72 {
73 rows = torchTensor.size(0);
74 cols = 1;
75 }
76 else
77 {
78 rows = torchTensor.size(0);
79 cols = torchTensor.size(1);
80 }
81
82 typedef Eigen::Matrix<type, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
83 MatrixXf_rm;
84 type* data_p = torchTensor.data_ptr<type>();
85 std::vector<type> raw(nElem);
86
87 for (int i = 0; i < nElem; i++)
88 {
89 type d(*(data_p + i));
90 type a = static_cast <type>(d);
91 raw[i] = a;
92 }
93
94 Eigen::Map<MatrixXf_rm> eigenMatrix(&raw[0], rows,
95 cols);
96 return eigenMatrix;
97}
98
99template Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic>
100torchTensor2eigenMatrix<int>(torch::Tensor& torchTensor);
101
102template Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>
103torchTensor2eigenMatrix<double>(torch::Tensor& torchTensor);
104
105template Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic>
106torchTensor2eigenMatrix<float>(torch::Tensor& torchTensor);
107
108template torch::Tensor eigenMatrix2torchTensor<float>(
109 Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic> eigenMatrix);
110
111template torch::Tensor eigenMatrix2torchTensor<double>(
112 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> eigenMatrix);
113
114template torch::Tensor eigenMatrix2torchTensor<int>(
115 Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> eigenMatrix);
116
117
118
119}
120
121}
#define M_Assert(Expr, Msg)
template Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > torchTensor2eigenMatrix< float >(torch::Tensor &torchTensor)
template torch::Tensor eigenMatrix2torchTensor< int >(Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > eigenMatrix)
template Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > torchTensor2eigenMatrix< int >(torch::Tensor &torchTensor)
Eigen::Matrix< type, Eigen::Dynamic, Eigen::Dynamic > torchTensor2eigenMatrix(torch::Tensor &torchTensor)
Convert a torch tensor to an eigen Matrix.
Definition torch2Eigen.C:55
torch::Tensor eigenMatrix2torchTensor(Eigen::Matrix< type, Eigen::Dynamic, Eigen::Dynamic > _eigenMatrix)
Convert an eigen Matrix to a torch tensor.
Definition torch2Eigen.C:39
template Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > torchTensor2eigenMatrix< double >(torch::Tensor &torchTensor)
template torch::Tensor eigenMatrix2torchTensor< double >(Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > eigenMatrix)
template torch::Tensor eigenMatrix2torchTensor< float >(Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic > eigenMatrix)
label i
Definition pEqn.H:46
Header file of the torch2Eigen namespace.