1 // 2 // BAGEL - Brilliantly Advanced General Electronic Structure Library 3 // Filename: btas_interface.h 4 // Copyright (C) 2013 Toru Shiozaki 5 // 6 // Author: Toru Shiozaki <shiozaki@northwestern.edu> 7 // Maintainer: Shiozaki group 8 // 9 // This file is part of the BAGEL package. 10 // 11 // This program is free software: you can redistribute it and/or modify 12 // it under the terms of the GNU General Public License as published by 13 // the Free Software Foundation, either version 3 of the License, or 14 // (at your option) any later version. 15 // 16 // This program is distributed in the hope that it will be useful, 17 // but WITHOUT ANY WARRANTY; without even the implied warranty of 18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 // GNU General Public License for more details. 20 // 21 // You should have received a copy of the GNU General Public License 22 // along with this program. If not, see <http://www.gnu.org/licenses/>. 23 // 24 25 #ifndef __SRC_MATH_BTAS_INTERFACE_H 26 #define __SRC_MATH_BTAS_INTERFACE_H 27 28 #include <bagel_config.h> 29 #define _CONTRACT_OPT_BAGEL 30 #define _HAS_CBLAS 31 #ifdef HAVE_MKL_H 32 #define _HAS_INTEL_MKL 33 #endif 34 35 #include <iostream> 36 #include <iomanip> 37 #include <complex> 38 #include <btas/btas.h> 39 #include <btas/tensor.h> 40 #include <btas/tensor_func.h> 41 #include <src/util/math/btas_varray.h> 42 #include <src/util/math/preallocarray.h> 43 44 namespace btas { 45 // int N is not nessesary, but leave it so that we can switch to fixed-rank tensors in the future 46 template<int N> 47 using CRange = RangeNd<CblasColMajor>; 48 49 template<typename T> 50 using Tensor1 = Tensor<T, CRange<1>, bagel::varray<T>>; 51 template<typename T> 52 using Tensor2 = Tensor<T, CRange<2>, bagel::varray<T>>; 53 template<typename T> 54 using Tensor3 = Tensor<T, CRange<3>, bagel::varray<T>>; 55 template<typename T> 56 using Tensor4 = Tensor<T, CRange<4>, bagel::varray<T>>; 57 58 template<typename T> 59 using TensorView1 = TensorView<T, CRange<1>, bagel::varray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 60 template<typename T> 61 using TensorView2 = TensorView<T, CRange<2>, bagel::varray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 62 template<typename T> 63 using TensorView3 = TensorView<T, CRange<3>, bagel::varray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 64 template<typename T> 65 using TensorView4 = TensorView<T, CRange<4>, bagel::varray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 66 67 template<typename T, int N> 68 using TensorN = Tensor<T, CRange<N>, bagel::varray<T>>; 69 template<typename T, int N> 70 using TensorViewN = TensorView<T, CRange<N>, bagel::varray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 71 72 // Tensors on preallocated memory 73 template<typename T> 74 using PTensor1 = Tensor<T, CRange<1>, bagel::PreAllocArray<T>>; 75 template<typename T> 76 using PTensor2 = Tensor<T, CRange<2>, bagel::PreAllocArray<T>>; 77 template<typename T> 78 using PTensor3 = Tensor<T, CRange<3>, bagel::PreAllocArray<T>>; 79 template<typename T> 80 using PTensor4 = Tensor<T, CRange<4>, bagel::PreAllocArray<T>>; 81 82 template<typename T> 83 using PTensorView1 = TensorView<T, CRange<1>, bagel::PreAllocArray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 84 template<typename T> 85 using PTensorView2 = TensorView<T, CRange<2>, bagel::PreAllocArray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 86 template<typename T> 87 using PTensorView3 = TensorView<T, CRange<3>, bagel::PreAllocArray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 88 template<typename T> 89 using PTensorView4 = TensorView<T, CRange<4>, bagel::PreAllocArray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 90 91 template<typename T, int N> 92 using PTensorN = Tensor<T, CRange<N>, bagel::PreAllocArray<T>>; 93 template<typename T, int N> 94 using PTensorViewN = TensorView<T, CRange<N>, bagel::PreAllocArray<T>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 95 96 // print functions for Tensor2 97 template <typename T, class = typename std::enable_if<btas::is_boxtensor<T>::value>::type> 98 void print(const T& t, std::string name = "", const typename T::size_type size = 10) { 99 assert(t.rank() == 2 && t.range().ordinal().contiguous()); 100 if (!name.empty()) std::cout << "++++ " + name + " ++++" << std::endl; 101 constexpr const int width = std::is_same<typename std::remove_cv<typename T::value_type>::type, double>::value ? 12 : 30; 102 constexpr const int prec = std::is_same<typename std::remove_cv<typename T::value_type>::type, double>::value ? 9 : 8; 103 for (int i = 0; i != std::min(size, t.extent(0)); ++i) { 104 for (int j = 0; j != std::min(size, t.extent(1)); ++j) { 105 std::cout << std::fixed << std::setw(width) << std::setprecision(prec) << *(t.begin()+i+t.extent(0)*j) << " "; 106 } 107 std::cout << std::endl; 108 } 109 } 110 111 } 112 113 extern template class btas::Tensor <double,btas::RangeNd<CblasColMajor>,bagel::varray<double>>; 114 extern template class btas::TensorView<double,btas::RangeNd<CblasColMajor>,bagel::varray<double>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 115 extern template class btas::Tensor <std::complex<double>,btas::RangeNd<CblasColMajor>,bagel::varray<std::complex<double>>>; 116 extern template class btas::TensorView<std::complex<double>,btas::RangeNd<CblasColMajor>,bagel::varray<std::complex<double>>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 117 118 extern template class btas::Tensor <double,btas::RangeNd<CblasColMajor>,bagel::PreAllocArray<double>>; 119 extern template class btas::TensorView<double,btas::RangeNd<CblasColMajor>,bagel::PreAllocArray<double>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 120 extern template class btas::Tensor <std::complex<double>,btas::RangeNd<CblasColMajor>,bagel::PreAllocArray<std::complex<double>>>; 121 extern template class btas::TensorView<std::complex<double>,btas::RangeNd<CblasColMajor>,bagel::PreAllocArray<std::complex<double>>,btas::TensorViewPolicy<btas::TensorViewPolicy_RuntimeConst>>; 122 123 #include <src/util/archive.h> 124 BOOST_CLASS_EXPORT_KEY(btas::Tensor1<double>) 125 BOOST_CLASS_EXPORT_KEY(btas::Tensor1<std::complex<double>>) 126 127 #endif 128