1 #ifndef _COMPADRE_LINEAR_ALGEBRA_DEFINITIONS_HPP_
2 #define _COMPADRE_LINEAR_ALGEBRA_DEFINITIONS_HPP_
7 namespace GMLS_LinearAlgebra {
15 for (
int i=0; i<columns; ++i) {
17 for (
int j=0; j<i; ++j) {
18 double M_data_entry_i_j = 0;
19 teamMember.team_barrier();
21 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(teamMember,rows), [=] (
const int k,
double &entry_val) {
23 double val_i = weighted_P(k, i);
24 double val_j = weighted_P(k, j);
25 entry_val += val_i*val_j;
26 }, M_data_entry_i_j );
28 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
29 M_data(i,j) = M_data_entry_i_j;
30 M_data(j,i) = M_data_entry_i_j;
32 teamMember.team_barrier();
35 double M_data_entry_i_j = 0;
36 teamMember.team_barrier();
38 Kokkos::parallel_reduce(Kokkos::TeamThreadRange(teamMember,rows), [=] (
const int k,
double &entry_val) {
40 double val = weighted_P(k, i);
42 }, M_data_entry_i_j );
44 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
45 M_data(i,i) = M_data_entry_i_j;
47 teamMember.team_barrier();
49 teamMember.team_barrier();
54 KOKKOS_INLINE_FUNCTION
57 Kokkos::single(Kokkos::PerTeam(teamMember), [&] () {
59 double maxRange = 100;
63 double eigenvalue_relative_tolerance = 1e-6;
66 double v[3] = {1,1,1};
67 double v_old[3] = {v[0], v[1], v[2]};
72 while (error > eigenvalue_relative_tolerance) {
75 v[0] = PtP(0,0)*tmp1 + PtP(0,1)*v[1];
76 if (dimensions>2) v[0] += PtP(0,2)*v[2];
79 v[1] = PtP(1,0)*tmp1 + PtP(1,1)*tmp2;
80 if (dimensions>2) v[1] += PtP(1,2)*v[2];
83 v[2] = PtP(2,0)*tmp1 + PtP(2,1)*tmp2 + PtP(2,2)*v[2];
85 norm_v = v[0]*v[0] + v[1]*v[1];
86 if (dimensions>2) norm_v += v[2]*v[2];
87 norm_v = std::sqrt(norm_v);
91 if (dimensions>2) v[2] = v[2] / norm_v;
93 error = (v[0]-v_old[0])*(v[0]-v_old[0]) + (v[1]-v_old[1])*(v[1]-v_old[1]);
94 if (dimensions>2) error += (v[2]-v_old[2])*(v[2]-v_old[2]);
95 error = std::sqrt(error);
101 if (dimensions>2) v_old[2] = v[2];
110 for (
int i=0; i<2; ++i) {
115 V(1,0) = 1.0; V(1,1) = 1.0;
116 dot_product = V(0,0)*V(1,0) + V(0,1)*V(1,1);
117 V(1,0) -= dot_product*V(0,0);
118 V(1,1) -= dot_product*V(0,1);
120 norm = std::sqrt(V(1,0)*V(1,0) + V(1,1)*V(1,1));
126 for (
int i=0; i<3; ++i) {
128 for (
int j=0; j<3; ++j) {
129 PtP(i,j) -= norm_v*v[i]*v[j];
134 v[0] = rand_gen.drand(maxRange); v[1] = rand_gen.drand(maxRange); v[2] = rand_gen.drand(maxRange);
135 v_old[0] = v[0]; v_old[1] = v[1]; v_old[2] =v[2];
136 while (error > eigenvalue_relative_tolerance) {
139 v[0] = PtP(0,0)*tmp1 + PtP(0,1)*v[1] + PtP(0,2)*v[2];
142 v[1] = PtP(1,0)*tmp1 + PtP(1,1)*tmp2 + PtP(1,2)*v[2];
144 v[2] = PtP(2,0)*tmp1 + PtP(2,1)*tmp2 + PtP(2,2)*v[2];
146 norm_v = std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
148 v[0] = v[0] / norm_v;
149 v[1] = v[1] / norm_v;
150 v[2] = v[2] / norm_v;
152 error = std::sqrt((v[0]-v_old[0])*(v[0]-v_old[0]) + (v[1]-v_old[1])*(v[1]-v_old[1]) + (v[2]-v_old[2])*(v[2]-v_old[2])) / norm_v;
159 for (
int i=0; i<3; ++i) {
164 dot_product = V(0,0)*V(1,0) + V(0,1)*V(1,1) + V(0,2)*V(1,2);
166 V(1,0) -= dot_product*V(0,0);
167 V(1,1) -= dot_product*V(0,1);
168 V(1,2) -= dot_product*V(0,2);
170 norm = std::sqrt(V(1,0)*V(1,0) + V(1,1)*V(1,1) + V(1,2)*V(1,2));
176 V(2,0) = V(0,1)*V(1,2) - V(1,1)*V(0,2);
177 V(2,1) = V(1,0)*V(0,2) - V(0,0)*V(1,2);
178 V(2,2) = V(0,0)*V(1,1) - V(1,0)*V(0,1);
181 norm = std::sqrt(V(2,0)*V(2,0) + V(2,1)*V(2,1) + V(2,2)*V(2,2));
188 random_number_pool.free_state(rand_gen);
pool_type::generator_type generator_type
team_policy::member_type member_type
KOKKOS_INLINE_FUNCTION void largestTwoEigenvectorsThreeByThreeSymmetric(const member_type &teamMember, scratch_matrix_right_type V, scratch_matrix_right_type PtP, const int dimensions, pool_type &random_number_pool)
Calculates two eigenvectors corresponding to two dominant eigenvalues.
Kokkos::View< double **, layout_right, Kokkos::MemoryTraits< Kokkos::Unmanaged > > scratch_matrix_right_type
Kokkos::Random_XorShift64_Pool pool_type
KOKKOS_INLINE_FUNCTION void createM(const member_type &teamMember, scratch_matrix_right_type M_data, scratch_matrix_right_type weighted_P, const int columns, const int rows)
Creates a matrix M=A^T*A from a matrix A.