fml  0.1-0
Fused Matrix Library
blas.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_PAR_CPU_LINALG_BLAS_H
6 #define FML_PAR_CPU_LINALG_BLAS_H
7 #pragma once
8 
9 
10 #include "../../../cpu/linalg/linalg_blas.hh"
11 
12 #include "../parmat.hh"
13 
14 
15 namespace fml
16 {
17 namespace linalg
18 {
36  template <typename REAL>
37  void matmult(const parmat_cpu<REAL> &x, const cpumat<REAL> &y,
38  parmat_cpu<REAL> &ret)
39  {
40  linalg::matmult(false, false, (REAL)1.0, x.data_obj(), y, ret.data_obj());
41  }
42 
44  template <typename REAL>
46  {
47  parmat_cpu<REAL> ret(x.get_comm(), x.nrows(), x.ncols(), x.nrows_before());
48  matmult(x, y, ret);
49  }
50 
52  template <typename REAL>
53  void matmult(const parmat_cpu<REAL> &x, const parmat_cpu<REAL> &y,
54  cpumat<REAL> &ret)
55  {
56  linalg::matmult(true, false, (REAL)1.0, x.data_obj(), y.data_obj(), ret);
57  x.get_comm().allreduce(ret.nrows()*ret.ncols(), ret.data_ptr());
58  }
59 
61  template <typename REAL>
63  {
64  cpumat<REAL> ret(x.ncols(), y.ncols());
65  matmult(x, y, ret);
66  }
67 
68 
69 
87  template <typename REAL>
88  void crossprod(const REAL alpha, const parmat_cpu<REAL> &x, cpumat<REAL> &ret)
89  {
90  const len_t n = x.ncols();
91  if (n != ret.nrows() || n != ret.ncols())
92  ret.resize(n, n);
93 
94  linalg::crossprod(alpha, x.data_obj(), ret);
95 
96  comm r = x.get_comm();
97  r.allreduce(n*n, ret.data_ptr());
98  }
99 
101  template <typename REAL>
102  cpumat<REAL> crossprod(const REAL alpha, const parmat_cpu<REAL> &x)
103  {
104  const len_t n = x.ncols();
105  cpumat<REAL> ret(n, n);
106 
107  crossprod(alpha, x, ret);
108  return ret;
109  }
110 }
111 }
112 
113 
114 #endif
fml::cpumat
Matrix class for data held on a single CPU.
Definition: cpumat.hh:36
fml::parmat_cpu
Definition: parmat.hh:21
fml::linalg::crossprod
void crossprod(const REAL alpha, const cpumat< REAL > &x, cpumat< REAL > &ret)
Computes lower triangle of alpha*x^T*x.
Definition: crossprod.hh:37
fml::comm::allreduce
void allreduce(int n, T *data, MPI_Op op=MPI_SUM) const
Sum reduce operation across all processes in the MPI communicator.
Definition: comm.hh:357
fml::unimat::nrows
len_t nrows() const
Number of rows.
Definition: unimat.hh:36
fml::cpumat::resize
void resize(len_t nrows, len_t ncols)
Resize the internal object storage.
Definition: cpumat.hh:233
fml::unimat::ncols
len_t ncols() const
Number of columns.
Definition: unimat.hh:38
fml::comm
MPI communicator data and helpers.
Definition: comm.hh:24
fml::unimat::data_ptr
REAL * data_ptr()
Pointer to the internal array.
Definition: unimat.hh:40
fml
Core namespace.
Definition: dimops.hh:10
fml::linalg::matmult
void matmult(const bool transx, const bool transy, const REAL alpha, const cpumat< REAL > &x, const cpumat< REAL > &y, cpumat< REAL > &ret)
Computes ret = alpha*op(x)*op(y) where op(A) is A or A^T.
Definition: matmult.hh:43