Ensign
Low-rank framework for multi-core CPUs and GPUs
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <generic/common.hpp>
9#include <generic/storage.hpp>
10#include <generic/kernels.hpp>
11#include <generic/index.hpp>
12#include <generic/utility.hpp>
13
14namespace Ensign {
15
17namespace Matrix {
18
19/* Set matrix to zero
20*/
21template<class T>
23
24/* Set matrix to identity
25*/
26template<class T>
28
29/* Set vector or matrix to a constant value
30*/
31template<class T, size_t d>
32void set_const(multi_array<T,d>& a, T alpha);
33
34
35/* Multiply every row of a matrix with a vector (i.e. diag(w)a)
36*/
37template<class T>
39
40/* Transpose a square matrix inplace (intended for small matrices)
41*/
42template<class T>
44
45
47struct blas_ops {
48
49 blas_ops(bool gpu = false);
51
52 /* Matrix multiplication (computes c = a b)
53 *
54 * The number of rows in matrix c can be smaller than the number of rows in matrix a. In this
55 * case only a partial result is computed.
56 */
57 template<class T>
58 void matmul(const multi_array<T,2>& a, const multi_array<T,2>& b, multi_array<T,2>& c) const;
59
60 /* Matrix multiplication with first matrix transposed (computes c = a^T b)
61 */
62 template<class T>
64
65 /* Matrix multiplication with second matrix transposed (computes c = a b^T)
66 */
67 template<class T>
69
70 /* Matrix multiplication with both matrices transposed (computes c = a^T b^T)
71 */
72 template<class T>
74
75 /* Matrix vector multiplication (c = a b)
76*/
77 template<class T>
78 void matvec(const multi_array<T,2>& a, const multi_array<T,1>& b, multi_array<T,1>& c) const;
79
80 /* Matrix vector multiplication with matrix transposed (c = a^T b)
81 */
82 template<class T>
84
85 /* Transpose a matrix
86 */
87 template<class T>
88 void transpose(const multi_array<T,2>& in, multi_array<T,2>& out) const;
89
90
91 bool gpu;
92 #ifdef __CUDA__
93 cublasHandle_t handle;
94 cublasHandle_t handle_devres; // cuBLAS routines return scalar results on device
95 cusolverDnHandle_t handle_cusolver;
96 #endif
97};
98
99/* Helper class to perform diagonalization of a symmetric matrix (also known as the Schur decomposition)
100 Factorizes the matrix CC as follows
101 CC = TT*diag(diag_r)*TT^T
102*/
105
107
108private:
109 #ifdef __MKL__
110 MKL_INT lwork;
111 #else
112 int lwork;
113 #endif
114};
115
116/* Computes the singular value decomposition of the matrix input
117*/
118template<class T>
119void svd(const multi_array<T,2>& input, multi_array<T,2>& U, multi_array<T,2>& V, multi_array<T,1>& sigma_diag, const blas_ops& blas);
120
121template<class T>
123
124
125/* Linear solver based on a LU decomposition.
126*/
127template<class T>
128struct lu_solver {
130
131 lu_solver(Index n) : A({n, n}), ipiv(n) {
132 }
133
134 void lu();
135 void solve(multi_array<T,1>& xb); // solves Ax=b, lu needs to be called first
136
137private:
138 #ifdef __MKL__
139 vector<MKL_INT> ipiv;
140 #else
141 vector<int> ipiv;
142 #endif
143};
144
145
146} // namespace Matrix
147
148} // namespace Ensign
Provides shared types, platform headers, and basic definitions.
ptrdiff_t Index
Definition common.hpp:83
Converts between vector indices and combined linear indices.
Declares CUDA kernels for elementwise operations and numerical updates.
void set_identity(multi_array< T, 2 > &a)
Set matrix to identity.
void set_zero(multi_array< T, 2 > &a)
Set matrix to zero.
void set_const(multi_array< T, d > &a, T alpha)
Set vector or matrix to a constant value.
void transpose_inplace(multi_array< T, 2 > &a)
Transpose a square matrix inplace (intended for small matrices)
void svd(const multi_array< T, 2 > &input, multi_array< T, 2 > &U, multi_array< T, 2 > &V, multi_array< T, 1 > &sigma_diag, const blas_ops &blas)
Computes the singular value decomposition of the matrix input.
void qr(multi_array< T, 2 > &Q, multi_array< T, 2 > &R)
void ptw_mult_row(const multi_array< T, 2 > &a, const multi_array< T, 1 > &w, multi_array< T, 2 > &out)
Multiply every row of a matrix with a vector (i.e.
Core namespace for Ensign's numerical algorithms and data structures.
Definition common.hpp:96
Provides contiguous multidimensional arrays for CPU and GPU storage.
Provides matrix and vector operations with optional GPU execution.
Definition matrix.hpp:47
void matmul(const multi_array< T, 2 > &a, const multi_array< T, 2 > &b, multi_array< T, 2 > &c) const
Matrix multiplication (computes c = a b)
void matmul_transb(const multi_array< T, 2 > &a, const multi_array< T, 2 > &b, multi_array< T, 2 > &c) const
Matrix multiplication with second matrix transposed (computes c = a b^T)
void matvec_trans(const multi_array< T, 2 > &a, const multi_array< T, 1 > &b, multi_array< T, 1 > &c) const
Matrix vector multiplication with matrix transposed (c = a^T b)
blas_ops(bool gpu=false)
void matmul_transa(const multi_array< T, 2 > &a, const multi_array< T, 2 > &b, multi_array< T, 2 > &c) const
Matrix multiplication with first matrix transposed (computes c = a^T b)
void matvec(const multi_array< T, 2 > &a, const multi_array< T, 1 > &b, multi_array< T, 1 > &c) const
Matrix vector multiplication (c = a b)
void matmul_transab(const multi_array< T, 2 > &a, const multi_array< T, 2 > &b, multi_array< T, 2 > &c) const
Matrix multiplication with both matrices transposed (computes c = a^T b^T)
void transpose(const multi_array< T, 2 > &in, multi_array< T, 2 > &out) const
Transpose a matrix.
Helper class to perform diagonalization of a symmetric matrix (also known as the Schur decomposition)...
Definition matrix.hpp:103
void operator()(const multi_array< double, 2 > &CC, multi_array< double, 2 > &TT, multi_array< double, 1 > &diag_r)
Linear solver based on a LU decomposition.
Definition matrix.hpp:128
void solve(multi_array< T, 1 > &xb)
multi_array< T, 2 > A
Definition matrix.hpp:129
Dense d-dimensional array stored contiguously in column-major order (i.e.
Definition storage.hpp:24
Provides numerical helpers and utilities for arrays and indices.