Tpetra parallel linear algebra  Version of the Day
Tpetra_Details_mklGemm.cpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos: Node API and Parallel Node Kernels
6 // Copyright (2008) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ************************************************************************
41 //@HEADER
42 */
43 
45 #include "KokkosKernels_config.h"
46 #ifdef HAVE_KOKKOSKERNELS_MKL
47 # include <mkl.h>
48 #endif // HAVE_KOKKOSKERNELS_MKL
49 #include <sstream>
50 #include <stdexcept>
51 
52 namespace Tpetra {
53 namespace Details {
54 namespace Blas {
55 namespace Mkl {
56 namespace Impl {
57 
58 #ifdef HAVE_KOKKOSKERNELS_MKL
59 namespace { // (anonymous)
60 
61 CBLAS_TRANSPOSE
62 transCharToMklEnum (const char trans)
63 {
64  if (trans == 'T' || trans == 't') {
65  return CblasTrans;
66  }
67  else if (trans == 'C' || trans == 'c' ||
68  trans == 'H' || trans == 'h') {
69  return CblasConjTrans;
70  }
71  else {
72  return CblasNoTrans;
73  }
74 }
75 
76 } // namespace (anonymous)
77 #endif // HAVE_KOKKOSKERNELS_MKL
78 
79 void
80 cgemm (const char transA,
81  const char transB,
82  const int m,
83  const int n,
84  const int k,
85  const ::Kokkos::complex<float>& alpha,
86  const ::Kokkos::complex<float> A[],
87  const int lda,
88  const ::Kokkos::complex<float> B[],
89  const int ldb,
90  const ::Kokkos::complex<float>& beta,
91  ::Kokkos::complex<float> C[],
92  const int ldc)
93 {
94 #ifdef HAVE_KOKKOSKERNELS_MKL
95  const CBLAS_TRANSPOSE etransA = transCharToMklEnum (transA);
96  const CBLAS_TRANSPOSE etransB = transCharToMklEnum (transB);
97  // MKL's complex interface, unlike real interface, takes alpha and
98  // beta by pointer.
99  ::cblas_cgemm (CblasColMajor, etransA, etransB,
100  m, n, k,
101  &alpha, A, lda,
102  B, ldb,
103  &beta, C, ldc);
104 #else // NOT HAVE_KOKKOSKERNELS_MKL
105  throw std::runtime_error ("You must enable MKL in your Trilinos build in "
106  "order to invoke MKL functions in Tpetra.");
107 #endif // NOT HAVE_KOKKOSKERNELS_MKL
108 }
109 
110 void
111 dgemm (const char transA,
112  const char transB,
113  const int m,
114  const int n,
115  const int k,
116  const double alpha,
117  const double A[],
118  const int lda,
119  const double B[],
120  const int ldb,
121  const double beta,
122  double C[],
123  const int ldc)
124 {
125 #ifdef HAVE_KOKKOSKERNELS_MKL
126  const CBLAS_TRANSPOSE etransA = transCharToMklEnum (transA);
127  const CBLAS_TRANSPOSE etransB = transCharToMklEnum (transB);
128  ::cblas_dgemm (CblasColMajor, etransA, etransB,
129  m, n, k,
130  alpha, A, lda,
131  B, ldb,
132  beta, C, ldc);
133 #else // NOT HAVE_KOKKOSKERNELS_MKL
134  throw std::runtime_error ("You must enable MKL in your Trilinos build in "
135  "order to invoke MKL functions in Tpetra.");
136 #endif // NOT HAVE_KOKKOSKERNELS_MKL
137 }
138 
139 void
140 sgemm (const char transA,
141  const char transB,
142  const int m,
143  const int n,
144  const int k,
145  const float alpha,
146  const float A[],
147  const int lda,
148  const float B[],
149  const int ldb,
150  const float beta,
151  float C[],
152  const int ldc)
153 {
154 #ifdef HAVE_KOKKOSKERNELS_MKL
155  const CBLAS_TRANSPOSE etransA = transCharToMklEnum (transA);
156  const CBLAS_TRANSPOSE etransB = transCharToMklEnum (transB);
157  ::cblas_sgemm (CblasColMajor, etransA, etransB,
158  m, n, k,
159  alpha, A, lda,
160  B, ldb,
161  beta, C, ldc);
162 #else // NOT HAVE_KOKKOSKERNELS_MKL
163  throw std::runtime_error ("You must enable MKL in your Trilinos build in "
164  "order to invoke MKL functions in Tpetra.");
165 #endif // NOT HAVE_KOKKOSKERNELS_MKL
166 }
167 
168 void
169 zgemm (const char transA,
170  const char transB,
171  const int m,
172  const int n,
173  const int k,
174  const ::Kokkos::complex<double>& alpha,
175  const ::Kokkos::complex<double> A[],
176  const int lda,
177  const ::Kokkos::complex<double> B[],
178  const int ldb,
179  const ::Kokkos::complex<double>& beta,
180  ::Kokkos::complex<double> C[],
181  const int ldc)
182 {
183 #ifdef HAVE_KOKKOSKERNELS_MKL
184  const CBLAS_TRANSPOSE etransA = transCharToMklEnum (transA);
185  const CBLAS_TRANSPOSE etransB = transCharToMklEnum (transB);
186  // MKL's complex interface, unlike real interface, takes alpha and
187  // beta by pointer.
188  ::cblas_zgemm (CblasColMajor, etransA, etransB,
189  m, n, k,
190  &alpha, A, lda,
191  B, ldb,
192  &beta, C, ldc);
193 #else // NOT HAVE_KOKKOSKERNELS_MKL
194  throw std::runtime_error ("You must enable MKL in your Trilinos build in "
195  "order to invoke MKL functions in Tpetra.");
196 #endif // NOT HAVE_KOKKOSKERNELS_MKL
197 }
198 
199 } // namespace Impl
200 } // namespace Mkl
201 } // namespace Blas
202 } // namespace Details
203 } // namespace Tpetra
Namespace Tpetra contains the class and methods constituting the Tpetra library.
Implementation details of Tpetra.
Implementation detail of Tpetra::MultiVector.