/usr/include/trilinos/Kokkos_MV_GEMM.hpp is in libtrilinos-tpetra-dev 12.4.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | /*
//@HEADER
// ************************************************************************
//
// Kokkos: Node API and Parallel Node Kernels
// Copyright (2008) Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER
*/
#ifndef KOKKOS_MV_GEMM_HPP
#define KOKKOS_MV_GEMM_HPP
// Note this code lives only temporarily in TpetraCore. As soon as
// GEMM kernels exist in the TpetraKernels subpackage, and thus a
// depnedency on Teuchos can be eliminated, the code will move to
// TpetraKernels.
#include<Teuchos_BLAS.hpp>
#ifdef KOKKOS_HAVE_CUDA
#include<cublas.h>
#endif
namespace Teuchos {
// mfh 11 Nov 2014: The DeviceGEMM specializations below need to be
// able to use Teuchos::BLAS::{GEMM, GEMV}. We provide just enough
// of a specialization for Kokkos::complex<{float, double}> to make
// DeviceGEMM work. They just defer to BLAS<int,
// std::complex<{float, double}> > via reinterpret_cast. Please
// feel free to expand these specializations if you need to.
template<>
class BLAS<int, ::Kokkos::complex<float> > {
public:
typedef float mag_type;
typedef ::Kokkos::complex<float> val_type;
typedef std::complex<float> impl_type;
BLAS () {}
BLAS (const BLAS<int, val_type>&) {}
virtual ~BLAS () {}
// void ROTG (val_type* da, val_type* db, mag_type* c, val_type* s) const;
// void ROT (const int n, val_type* dx, const int incx, val_type* dy, const int incy, RealType* c, val_type* s) const;
// RealType ASUM (const int n, const val_type* x, const int incx) const;
//void AXPY (const int n, const val_type alpha, const val_type* x, const int incx, val_type* y, const int incy) const;
//void COPY (const int n, const val_type* x, const int incx, val_type* y, const int incy) const;
//val_type DOT(const int n, const val_type* x, const int incx, const val_type* y, const int incy) const;
//RealType NRM2(const int n, const val_type* x, const int incx) const;
//void SCAL(const int n, const val_type alpha, val_type* x, const int incx) const;
//int IAMAX(const int n, const val_type* x, const int incx) const;
void
GEMV (ETransp trans, const int m, const int n, const val_type alpha,
const val_type* A, const int lda, const val_type* x, const int incx,
const val_type beta, val_type* y, const int incy) const
{
BLAS<int, impl_type> blas;
blas.GEMV (trans, m, n, static_cast<impl_type> (alpha),
reinterpret_cast<const impl_type*> (A), lda,
reinterpret_cast<const impl_type*> (x), incx,
static_cast<impl_type> (beta),
reinterpret_cast<impl_type*> (y), incy);
}
//void TRMV(EUplo uplo, ETransp trans, EDiag diag, const int n, const val_type* A, const int lda, val_type* x, const int incx) const;
//void GER(const int m, const int n, const val_type alpha, const val_type* x, const int incx, const val_type* y, const int incy, val_type* A, const int lda) const;
void
GEMM (ETransp transa, ETransp transb, const int m, const int n, const int k,
const val_type alpha, const val_type* A, const int lda,
const val_type* B, const int ldb, const val_type beta, val_type* C,
const int ldc) const
{
BLAS<int, impl_type> blas;
blas.GEMM (transa, transb, m, n, k,
static_cast<impl_type> (alpha),
reinterpret_cast<const impl_type*> (A), lda,
reinterpret_cast<const impl_type*> (B), ldb,
static_cast<impl_type> (beta),
reinterpret_cast<impl_type*> (C), ldc);
}
//void SYMM(ESide side, EUplo uplo, const int m, const int n, const val_type alpha, const val_type* A, const int lda, const val_type *B, const int ldb, const val_type beta, val_type *C, const int ldc) const;
//void SYRK(EUplo uplo, ETransp trans, const int n, const int k, const val_type alpha, const val_type* A, const int lda, const val_type beta, val_type* C, const int ldc) const;
//void TRMM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const int m, const int n, const val_type alpha, const val_type* A, const int lda, val_type* B, const int ldb) const;
//void TRSM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const int m, const int n, const val_type alpha, const val_type* A, const int lda, val_type* B, const int ldb) const;
};
template<>
class BLAS<int, ::Kokkos::complex<double> > {
public:
typedef double mag_type;
typedef ::Kokkos::complex<double> val_type;
typedef std::complex<double> impl_type;
BLAS () {}
BLAS (const BLAS<int, val_type>&) {}
virtual ~BLAS () {}
// void ROTG (val_type* da, val_type* db, mag_type* c, val_type* s) const;
// void ROT (const int n, val_type* dx, const int incx, val_type* dy, const int incy, RealType* c, val_type* s) const;
// RealType ASUM (const int n, const val_type* x, const int incx) const;
//void AXPY (const int n, const val_type alpha, const val_type* x, const int incx, val_type* y, const int incy) const;
//void COPY (const int n, const val_type* x, const int incx, val_type* y, const int incy) const;
//val_type DOT(const int n, const val_type* x, const int incx, const val_type* y, const int incy) const;
//RealType NRM2(const int n, const val_type* x, const int incx) const;
//void SCAL(const int n, const val_type alpha, val_type* x, const int incx) const;
//int IAMAX(const int n, const val_type* x, const int incx) const;
void
GEMV (ETransp trans, const int m, const int n, const val_type alpha,
const val_type* A, const int lda, const val_type* x, const int incx,
const val_type beta, val_type* y, const int incy) const
{
BLAS<int, impl_type> blas;
blas.GEMV (trans, m, n, static_cast<impl_type> (alpha),
reinterpret_cast<const impl_type*> (A), lda,
reinterpret_cast<const impl_type*> (x), incx,
static_cast<impl_type> (beta),
reinterpret_cast<impl_type*> (y), incy);
}
//void TRMV(EUplo uplo, ETransp trans, EDiag diag, const int n, const val_type* A, const int lda, val_type* x, const int incx) const;
//void GER(const int m, const int n, const val_type alpha, const val_type* x, const int incx, const val_type* y, const int incy, val_type* A, const int lda) const;
void
GEMM (ETransp transa, ETransp transb, const int m, const int n, const int k,
const val_type alpha, const val_type* A, const int lda,
const val_type* B, const int ldb, const val_type beta, val_type* C,
const int ldc) const
{
BLAS<int, impl_type> blas;
blas.GEMM (transa, transb, m, n, k,
static_cast<impl_type> (alpha),
reinterpret_cast<const impl_type*> (A), lda,
reinterpret_cast<const impl_type*> (B), ldb,
static_cast<impl_type> (beta),
reinterpret_cast<impl_type*> (C), ldc);
}
//void SYMM(ESide side, EUplo uplo, const int m, const int n, const val_type alpha, const val_type* A, const int lda, const val_type *B, const int ldb, const val_type beta, val_type *C, const int ldc) const;
//void SYRK(EUplo uplo, ETransp trans, const int n, const int k, const val_type alpha, const val_type* A, const int lda, const val_type beta, val_type* C, const int ldc) const;
//void TRMM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const int m, const int n, const val_type alpha, const val_type* A, const int lda, val_type* B, const int ldb) const;
//void TRSM(ESide side, EUplo uplo, ETransp transa, EDiag diag, const int m, const int n, const val_type alpha, const val_type* A, const int lda, val_type* B, const int ldb) const;
};
} // namespace Teuchos
namespace Kokkos {
namespace Impl {
template<class ViewType>
size_t getStride2DView (ViewType A) {
size_t stride[8];
A.stride (stride);
return A.dimension_1 () > 1 ? stride[1] : A.dimension_0 ();
}
}
/// \struct DeviceGEMM
/// \brief Class that provides GEMM for a particular Kokkos Device.
/// \tparam Scalar The type of the entries in the matrices.
/// \tparam DeviceType The Kokkos Device type.
///
/// GEMM refers to the BLAS' dense matrix-matrix multiply routine.
template <typename Scalar, typename DeviceType>
struct DeviceGEMM {
public:
static void
GEMM (const Teuchos::ETransp transA,
const Teuchos::ETransp transB,
const Scalar alpha,
View<const Scalar**, LayoutLeft, DeviceType> A,
View<const Scalar**, LayoutLeft, DeviceType> B,
const Scalar beta,
View<Scalar**, LayoutLeft, DeviceType> C)
{
Teuchos::BLAS<int,Scalar> blas;
const int m = static_cast<int> (C.dimension_0 ()),
n = static_cast<int> (C.dimension_1 ()),
k = (transA == Teuchos::NO_TRANS ? A.dimension_1 () : A.dimension_0 ()),
lda = static_cast<int> (Impl::getStride2DView (A)),
ldb = static_cast<int> (Impl::getStride2DView (B)),
ldc = static_cast<int> (Impl::getStride2DView (C));
// For some BLAS implementations (e.g., MKL), GEMM when B has
// one column may be signficantly less efficient than GEMV.
if (n == 1 && transB == Teuchos::NO_TRANS) {
blas.GEMV (transA, A.dimension_0 (), A.dimension_1 (), alpha,
A.ptr_on_device(), lda,
B.ptr_on_device(), static_cast<int> (1),
beta, C.ptr_on_device(), static_cast<int> (1));
}
else {
blas.GEMM (transA, transB, m, n, k, alpha,
A.ptr_on_device(), lda,
B.ptr_on_device(), ldb,
beta, C.ptr_on_device(), ldc);
}
}
};
// template <typename Scalar>
// struct DeviceGEMM<Scalar,Serial> {
// public:
// static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha,
// View<const Scalar**,LayoutLeft,Serial> A, View<const Scalar**,LayoutLeft,Serial> B,
// Scalar beta, View<Scalar**,Serial> C) {
// Teuchos::BLAS<int,Scalar> blas;
// const int m = static_cast<int>(C.dimension_0()),
// n = static_cast<int>(C.dimension_1()),
// k = (transA == Teuchos::NO_TRANS ? A.dimension_1() : A.dimension_0()),
// lda = static_cast<int>(Impl::getStride2DView(A)),
// ldb = static_cast<int>(Impl::getStride2DView(B)),
// ldc = static_cast<int>(Impl::getStride2DView(C));
// // For some BLAS implementations (i.e. MKL), GEMM when B has one column
// // is signficantly less efficient
// if (n == 1 && transB == Teuchos::NO_TRANS)
// blas.GEMV(transA, A.dimension_0(), A.dimension_1(), alpha, A.ptr_on_device(), lda, B.ptr_on_device(), static_cast<int>(1), beta, C.ptr_on_device(), static_cast<int>(1));
// else
// blas.GEMM(transA, transB, m, n, k, alpha, A.ptr_on_device(), lda, B.ptr_on_device(), ldb, beta, C.ptr_on_device(), ldc);
// }
// };
// #ifdef KOKKOS_HAVE_PTHREAD
// template <typename Scalar>
// struct DeviceGEMM<Scalar,Threads> {
// public:
// static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha,
// View<const Scalar**,LayoutLeft,Threads> A, View<const Scalar**,LayoutLeft,Threads> B,
// Scalar beta, View<Scalar**,LayoutLeft,Threads> C) {
// Teuchos::BLAS<int,Scalar> blas;
// const int m = static_cast<int>(C.dimension_0()),
// n = static_cast<int>(C.dimension_1()),
// k = (transA == Teuchos::NO_TRANS ? A.dimension_1() : A.dimension_0()),
// lda = static_cast<int>(Impl::getStride2DView(A)),
// ldb = static_cast<int>(Impl::getStride2DView(B)),
// ldc = static_cast<int>(Impl::getStride2DView(C));
// blas.GEMM(transA, transB, m, n, k, alpha, A.ptr_on_device(), lda, B.ptr_on_device(), ldb, beta, C.ptr_on_device(), ldc);
// }
// };
// #endif
// #ifdef KOKKOS_HAVE_OPENMP
// template <typename Scalar>
// struct DeviceGEMM<Scalar,OpenMP> {
// public:
// static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha,
// View<const Scalar**,LayoutLeft,OpenMP> A, View<const Scalar**,LayoutLeft,OpenMP> B,
// Scalar beta, View<Scalar**,LayoutLeft,OpenMP> C) {
// Teuchos::BLAS<int,Scalar> blas;
// const int m = static_cast<int>(C.dimension_0()),
// n = static_cast<int>(C.dimension_1()),
// k = (transA == Teuchos::NO_TRANS ? A.dimension_1() : A.dimension_0()),
// lda = static_cast<int>(Impl::getStride2DView(A)),
// ldb = static_cast<int>(Impl::getStride2DView(B)),
// ldc = static_cast<int>(Impl::getStride2DView(C));
// blas.GEMM(transA, transB, m, n, k, alpha, A.ptr_on_device(), lda, B.ptr_on_device(), ldb, beta, C.ptr_on_device(), ldc);
// }
// };
// #endif
#ifdef KOKKOS_HAVE_CUDA
template <typename Scalar>
struct DeviceGEMM<Scalar,Cuda> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, Scalar alpha,
View<const Scalar**,LayoutLeft,Cuda> A, View<const Scalar**,LayoutLeft,Cuda> B,
Scalar beta, View<Scalar**,LayoutLeft,Cuda> C) {
TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "DeviceGEMM: Kokkos::Cuda has no support for GEMM operations over Scalar=" << Teuchos::typeName(alpha) << ".");
}
};
template <>
struct DeviceGEMM<float,Cuda> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, float alpha,
View<const float**,LayoutLeft,Cuda> A, View<const float**,LayoutLeft,Cuda> B,
float beta, View<float**,LayoutLeft,Cuda> C) {
const int m = static_cast<int>(C.dimension_0()),
n = static_cast<int>(C.dimension_1()),
k = (transA == Teuchos::NO_TRANS ? A.dimension_1() : A.dimension_0()),
lda = static_cast<int>(Impl::getStride2DView(A)),
ldb = static_cast<int>(Impl::getStride2DView(B)),
ldc = static_cast<int>(Impl::getStride2DView(C));
const char char_transA = (transA == Teuchos::NO_TRANS ? 'N' : 'T'),
char_transB = (transB == Teuchos::NO_TRANS ? 'N' : 'T');
cublasSgemm(char_transA, char_transB, m, n, k, alpha, A.ptr_on_device(), lda, B.ptr_on_device(), ldb, beta, C.ptr_on_device(), ldc);
#ifdef HAVE_KOKKOS_DEBUG
cublasStatus info = cublasGetError();
TEUCHOS_TEST_FOR_EXCEPTION( info != CUBLAS_STATUS_SUCCESS, std::runtime_error, "cublasSgemm failed with status " << info << "." );
#endif
}
};
template <>
struct DeviceGEMM<double,Cuda> {
public:
static void GEMM(Teuchos::ETransp transA, Teuchos::ETransp transB, double alpha,
View<const double**,LayoutLeft,Cuda> A, View<const double**,LayoutLeft,Cuda> B,
double beta, View<double**,LayoutLeft,Cuda> C) {
const int m = static_cast<int>(C.dimension_0()),
n = static_cast<int>(C.dimension_1()),
k = (transA == Teuchos::NO_TRANS ? A.dimension_1() : A.dimension_0()),
lda = static_cast<int>(Impl::getStride2DView(A)),
ldb = static_cast<int>(Impl::getStride2DView(B)),
ldc = static_cast<int>(Impl::getStride2DView(C));
const char char_transA = (transA == Teuchos::NO_TRANS ? 'N' : 'T'),
char_transB = (transB == Teuchos::NO_TRANS ? 'N' : 'T');
cublasDgemm(char_transA, char_transB, m, n, k, alpha, A.ptr_on_device(), lda, B.ptr_on_device(), ldb, beta, C.ptr_on_device(), ldc);
#ifdef HAVE_KOKKOS_DEBUG
cublasStatus info = cublasGetError();
TEUCHOS_TEST_FOR_EXCEPTION( info != CUBLAS_STATUS_SUCCESS, std::runtime_error, "cublasDgemm failed with status " << info << "." );
#endif
}
};
#endif
}
#endif // KOKKOS_MV_GEMM_HPP
|