tensor-0.1.0
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
tensor_foldin.cc
1 // -*- mode: c++; fill-column: 80; c-basic-offset: 2; indent-tabs-mode: nil -*-
2 /*
3  Copyright (c) 2010 Juan Jose Garcia Ripoll
4 
5  Tensor is free software; you can redistribute it and/or modify it
6  under the terms of the GNU Library General Public License as published
7  by the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Library General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 #define TENSOR_LOAD_IMPL
21 #include <iostream>
22 #include <tensor/tensor.h>
23 #include <tensor/io.h>
24 #include <tensor/tensor_lapack.h>
25 #include "gemm.cc"
26 
27 namespace tensor {
28 
29  using namespace blas;
30 
31  template<typename elt_t>
32  void
33  do_foldin_into(Tensor<elt_t> &output,
34  const Tensor<elt_t> &a, int _ndx1, const Tensor<elt_t> &b, int _ndx2)
35  {
36  index i_len,j_len,k_len,l_len,m_len;
37  index rank, i;
38  const index ranka = a.rank();
39  const index rankb = b.rank();
40  index ndx1 = normalize_index(_ndx1, ranka);
41  index ndx2 = normalize_index(_ndx2, rankb);
42  Indices new_dims(ranka + rankb - 2);
43  /*
44  * Since we use row-major order, in which the first
45  * index varies faster, we nest the loops beginning with the last index,
46  * and the loop what does is
47  * c(k,i,j,m) = a(i,l,j) * b(k,l,m)
48  * where there is a sum over the repeated index "l". In the first part of
49  * the code we find out the size of the contracted (l_len,l_len) and
50  * uncontracted (new_dims, i_len,j_len,k_len,m_len) dimensions of the
51  * tensors.
52  */
53  for (i = 0, rank = 0, k_len=1; i < ndx2; i++) {
54  index di = b.dimension(i);
55  new_dims.at(rank++) = di;
56  k_len *= di;
57  }
58  l_len = b.dimension(i++);
59  for (i = 0, i_len=1; i < ndx1; i++) {
60  index di = a.dimension(i);
61  new_dims.at(rank++) = di;
62  i_len *= di;
63  }
64  if (l_len != a.dimension(i++)) {
65  std::cerr << "Unable to foldin() tensors with dimensions" << std::endl
66  << "\t" << a.dimensions() << " and "
67  << b.dimensions() << std::endl
68  << "\tbecause indices " << ndx1 << " and " << ndx2
69  << " have different sizes" << std::endl;
70  abort();
71  }
72  for (j_len = 1; i < ranka; i++) {
73  index di = a.dimension(i);
74  new_dims.at(rank++) = di;
75  j_len *= di;
76  }
77  for (m_len = 1, i = ndx2+1; i < rankb; i++) {
78  index di = b.dimension(i);
79  new_dims.at(rank++) = di;
80  m_len *= di;
81  }
82  /*
83  * Create the output tensor. Sometimes it is just a number.
84  */
85  if (rank == 0) {
86  rank = 1;
87  new_dims.at(0) = 1;
88  }
89  output = Tensor<elt_t>(new_dims);
90  if (output.size() == 0)
91  return;
92 
93  elt_t *pC = output.begin();
94  elt_t zero = number_zero<elt_t>();
95  elt_t one = number_one<elt_t>();
96  const elt_t *pA = a.begin();
97  const elt_t *pB = b.begin();
98  char op1 = 'N';
99  char op2 = 'T';
100  index il_len = i_len*l_len;
101  index kl_len = k_len*l_len;
102  index ki_len = k_len*i_len;
103  for (index m = 0; m < m_len; m++) {
104  for (index j = 0; j < j_len; j++) {
105  gemm(op1, op2, k_len, i_len, l_len, one,
106  pB + kl_len*m, k_len, pA + il_len*j, i_len,
107  zero, pC + ki_len*(j + j_len*m), k_len);
108  }
109  }
110  }
111 
112 } // namespace tensor