OPALX (Object Oriented Parallel Accelerator Library for Exascal) master (dc2a29eed580)
OPALX
Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1//
2// This header file provides matrix and vector operations to replace boost::numeric::ublas.
3// Kokkos-friendly implementation with fixed-size matrices for GPU/CPU portability.
4//
5// Copyright (c) 2023, Paul Scherrer Institute, Villigen PSI, Switzerland
6// All rights reserved
7//
8// This file is part of OPAL.
9//
10// OPAL is free software: you can redistribute it and/or modify
11// it under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// You should have received a copy of the GNU General Public License
16// along with OPAL. If not, see <https://www.gnu.org/licenses/>.
17//
18
19#ifndef OPAL_MATRIX_HH
20#define OPAL_MATRIX_HH
21
22#include <Kokkos_Core.hpp>
23#include <cassert>
24#include <vector>
25#include "Ippl.h"
26
27// Kokkos-friendly fixed-size matrix structure
28template <int Rows, int Cols>
29struct matrix_t {
30 double m[Rows][Cols];
31
32 // Default constructor - initializes to identity if square, zero otherwise
33 KOKKOS_INLINE_FUNCTION
35 for (int i = 0; i < Rows; ++i) {
36 for (int j = 0; j < Cols; ++j) {
37 m[i][j] = 0.0;
38 }
39 }
40 // Set identity for square matrices
41 if (Rows == Cols) {
42 for (int i = 0; i < Rows; ++i) {
43 m[i][i] = 1.0;
44 }
45 }
46 }
47
48 // Constructor with initial value
49 KOKKOS_INLINE_FUNCTION
50 matrix_t(double init_value) {
51 for (int i = 0; i < Rows; ++i) {
52 for (int j = 0; j < Cols; ++j) {
53 m[i][j] = init_value;
54 }
55 }
56 }
57
58 // Copy constructor
59 KOKKOS_INLINE_FUNCTION
60 matrix_t(const matrix_t& right) {
61 for (int i = 0; i < Rows; ++i) {
62 for (int j = 0; j < Cols; ++j) {
63 m[i][j] = right(i, j);
64 }
65 }
66 }
67
68 // Assignment operator
69 KOKKOS_INLINE_FUNCTION
70 matrix_t& operator=(const matrix_t& right) {
71 if (this != &right) {
72 for (int i = 0; i < Rows; ++i) {
73 for (int j = 0; j < Cols; ++j) {
74 m[i][j] = right(i, j);
75 }
76 }
77 }
78 return *this;
79 }
80
81 // Operators
82 KOKKOS_INLINE_FUNCTION
83 double& operator()(int r, int c) { return m[r][c]; }
84
85 KOKKOS_INLINE_FUNCTION
86 double operator()(int r, int c) const { return m[r][c]; }
87
88 KOKKOS_INLINE_FUNCTION
89 int size1() const { return Rows; }
90
91 KOKKOS_INLINE_FUNCTION
92 int size2() const { return Cols; }
93};
94
95// Type aliases for common matrix sizes
98
99// Kokkos-friendly vector_t using ippl::Vector
100// Note: ippl::Vector uses unsigned int for dimension, so we use unsigned here too
101template <unsigned Size>
102using vector_t = ippl::Vector<double, Size>;
103
104// Kokkos-friendly matrix-vector product for fixed-size matrices
105template <int Rows, int Cols, class T>
106KOKKOS_INLINE_FUNCTION T prod_vector(const matrix_t<Rows, Cols>& rotation, const T& vect) {
107 T prodVector(0.0);
108
109 for (int i = 0; i < Rows; ++i) {
110 double val = 0.0;
111 for (int j = 0; j < Cols; ++j) {
112 val += rotation(i, j) * vect[j];
113 }
114 prodVector[i] = val;
115 }
116
117 return prodVector;
118}
119
120// Kokkos-friendly matrix^T-vector product for fixed-size matrices
121template <int Rows, int Cols, class T>
122KOKKOS_INLINE_FUNCTION T
123prod_vector_transpose(const matrix_t<Rows, Cols>& rotation, const T& vect) {
124 T prodVector(0.0);
125
126 for (int i = 0; i < Cols; ++i) {
127 double val = 0.0;
128 for (int j = 0; j < Rows; ++j) {
129 val += rotation(j, i) * vect[j];
130 }
131 prodVector[i] = val;
132 }
133
134 return prodVector;
135}
136
137// Kokkos-friendly matrix transpose for fixed-size matrices
138template <int Rows, int Cols>
139KOKKOS_INLINE_FUNCTION matrix_t<Cols, Rows> get_transpose(const matrix_t<Rows, Cols>& rotation) {
140 matrix_t<Cols, Rows> transpose;
141 for (int i = 0; i < Rows; ++i) {
142 for (int j = 0; j < Cols; ++j) {
143 transpose(j, i) = rotation(i, j);
144 }
145 }
146 return transpose;
147}
148
149// Kokkos-friendly matrix-matrix multiplication
150template <int Rows1, int Cols1, int Rows2, int Cols2>
151KOKKOS_INLINE_FUNCTION matrix_t<Rows1, Cols2> prod(
153 matrix_t<Rows1, Cols2> result(0.0);
154 for (int i = 0; i < Rows1; ++i) {
155 for (int j = 0; j < Cols2; ++j) {
156 double sum = 0.0;
157 for (int k = 0; k < Cols1; ++k) {
158 sum += a(i, k) * b(k, j);
159 }
160 result(i, j) = sum;
161 }
162 }
163 return result;
164}
165
166// Kokkos-friendly matrix-vector multiplication (matrix * vector)
167// Note: Casts int template parameters to unsigned for ippl::Vector compatibility
168template <int Rows, int Cols>
169KOKKOS_INLINE_FUNCTION ippl::Vector<double, static_cast<unsigned>(Rows)> prod_matrix_vector(
170 const matrix_t<Rows, Cols>& m, const ippl::Vector<double, static_cast<unsigned>(Cols)>& v) {
171 ippl::Vector<double, static_cast<unsigned>(Rows)> result(0.0);
172 for (int i = 0; i < Rows; ++i) {
173 double sum = 0.0;
174 for (int j = 0; j < Cols; ++j) {
175 sum += m(i, j) * v[j];
176 }
177 result[i] = sum;
178 }
179 return result;
180}
181
182#endif
KOKKOS_INLINE_FUNCTION matrix_t< Cols, Rows > get_transpose(const matrix_t< Rows, Cols > &rotation)
Definition Matrix.h:139
KOKKOS_INLINE_FUNCTION ippl::Vector< double, static_cast< unsigned >(Rows)> prod_matrix_vector(const matrix_t< Rows, Cols > &m, const ippl::Vector< double, static_cast< unsigned >(Cols)> &v)
Definition Matrix.h:169
ippl::Vector< double, Size > vector_t
Definition Matrix.h:102
KOKKOS_INLINE_FUNCTION T prod_vector(const matrix_t< Rows, Cols > &rotation, const T &vect)
Definition Matrix.h:106
KOKKOS_INLINE_FUNCTION T prod_vector_transpose(const matrix_t< Rows, Cols > &rotation, const T &vect)
Definition Matrix.h:123
KOKKOS_INLINE_FUNCTION matrix_t< Rows1, Cols2 > prod(const matrix_t< Rows1, Cols1 > &a, const matrix_t< Rows2, Cols2 > &b)
Definition Matrix.h:151
double T
Definition OPALTypes.h:8
double m[Rows][Cols]
Definition Matrix.h:30
KOKKOS_INLINE_FUNCTION double & operator()(int r, int c)
Definition Matrix.h:83
KOKKOS_INLINE_FUNCTION double operator()(int r, int c) const
Definition Matrix.h:86
KOKKOS_INLINE_FUNCTION matrix_t & operator=(const matrix_t &right)
Definition Matrix.h:70
KOKKOS_INLINE_FUNCTION matrix_t(const matrix_t &right)
Definition Matrix.h:60
KOKKOS_INLINE_FUNCTION int size2() const
Definition Matrix.h:92
KOKKOS_INLINE_FUNCTION int size1() const
Definition Matrix.h:89
KOKKOS_INLINE_FUNCTION matrix_t(double init_value)
Definition Matrix.h:50
KOKKOS_INLINE_FUNCTION matrix_t()
Definition Matrix.h:34