fml  0.1-0
Fused Matrix Library
dot.hh
1 // This file is part of fml which is released under the Boost Software
2 // License, Version 1.0. See accompanying file LICENSE or copy at
3 // https://www.boost.org/LICENSE_1_0.txt
4 
5 #ifndef FML_GPU_LINALG_DOT_H
6 #define FML_GPU_LINALG_DOT_H
7 #pragma once
8 
9 
10 #include <stdexcept>
11 
12 #include "../../_internals/linalgutils.hh"
13 
14 #include "../arch/arch.hh"
15 
16 #include "../gpumat.hh"
17 
18 #include "internals/err.hh"
19 
20 
21 namespace fml
22 {
23 namespace linalg
24 {
38  template <typename REAL>
39  REAL dot(const gpuvec<REAL> &x, const gpuvec<REAL> &y)
40  {
41  err::check_card(x, y);
42  const len_t len = std::min(x.size(), y.size());
43 
44  len_t m, n, k;
45  fml::linalgutils::matmult_params(true, false, len, 1, len, 1, &m, &n, &k);
46 
47  REAL d;
48  gpuscalar<REAL> d_device(x.get_card());
49  gpublas_status_t check = gpublas::gemm(x.get_card()->blas_handle(),
50  GPUBLAS_OP_T, GPUBLAS_OP_N, m, n, k, (REAL)1, x.data_ptr(), len,
51  y.data_ptr(), len, (REAL)0, d_device.data_ptr(), 1);
52  gpublas::err::check_ret(check, "gemm");
53 
54  d_device.get_val(&d);
55 
56  return d;
57  }
58 
59 
60 
62  template <typename REAL>
63  REAL dot(const gpuvec<REAL> &x)
64  {
65  return dot(x, x);
66  }
67 }
68 }
69 
70 
71 #endif
fml::univec::data_ptr
T * data_ptr()
Pointer to the internal array.
Definition: univec.hh:28
fml::gpuvec
Vector class for data held on a single GPU.
Definition: gpuvec.hh:32
fml
Core namespace.
Definition: dimops.hh:10
fml::univec::size
len_t size() const
Number of elements in the vector.
Definition: univec.hh:26
fml::gpuscalar
Definition: gpuscalar.hh:16
fml::linalg::dot
REAL dot(const cpuvec< REAL > &x, const cpuvec< REAL > &y)
Computes the dot product of two vectors, i.e. the sum of the product of the elements.
Definition: dot.hh:31