Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
pugs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
code
pugs
Commits
0a24f6ae
Commit
0a24f6ae
authored
Aug 1, 2019
by
Stéphane Del Pino
Browse files
Options
Downloads
Patches
Plain Diff
Add a first simple version of conjugate gradient
No preconditionner is used ATM
parent
261dffcb
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!26
Feature/linear systems
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/algebra/PCG.hpp
+94
-0
94 additions, 0 deletions
src/algebra/PCG.hpp
src/algebra/Vector.hpp
+18
-1
18 additions, 1 deletion
src/algebra/Vector.hpp
src/main.cpp
+10
-2
10 additions, 2 deletions
src/main.cpp
with
122 additions
and
3 deletions
src/algebra/PCG.hpp
0 → 100644
+
94
−
0
View file @
0a24f6ae
#ifndef PCG_HPP
#define PCG_HPP
struct
PCG
{
template
<
typename
VectorType
,
typename
MatrixType
,
typename
PreconditionerType
>
PCG
(
const
VectorType
&
f
,
const
MatrixType
&
A
,
[[
maybe_unused
]]
const
PreconditionerType
&
C
,
VectorType
&
x
,
const
size_t
maxiter
,
const
double
epsilon
=
1e-6
)
{
std
::
cout
<<
"- conjugate gradient
\n
"
;
std
::
cout
<<
" epsilon = "
<<
epsilon
<<
'\n'
;
std
::
cout
<<
" maximum number of iterations: "
<<
maxiter
<<
'\n'
;
VectorType
h
{
f
.
size
()};
VectorType
b
=
copy
(
f
);
if
(
true
)
{
h
=
A
*
x
;
h
-=
f
;
std
::
cout
<<
"- initial *real* residu : "
<<
(
h
,
h
)
<<
'\n'
;
}
VectorType
g
{
b
.
size
()};
VectorType
cg
=
copy
(
b
);
double
gcg
=
0
;
double
gcg0
=
1
;
double
relativeEpsilon
=
epsilon
;
for
(
size_t
i
=
1
;
i
<=
maxiter
;
++
i
)
{
if
(
i
==
1
)
{
h
=
A
*
x
;
cg
-=
h
;
g
=
copy
(
cg
);
// C.computes(cg, g);
gcg
=
(
g
,
cg
);
h
=
copy
(
g
);
}
b
=
A
*
h
;
double
hAh
=
(
h
,
b
);
if
(
hAh
==
0
)
{
hAh
=
1.
;
}
double
ro
=
gcg
/
hAh
;
cg
-=
ro
*
b
;
// VectorType b2 = b;
// C.computes(b2, b);
x
+=
ro
*
h
;
g
-=
ro
*
b
;
double
gamma
=
gcg
;
gcg
=
(
g
,
cg
);
if
((
i
==
1
)
&&
(
gcg
!=
0
))
{
relativeEpsilon
=
epsilon
*
gcg
;
gcg0
=
gcg
;
std
::
cout
<<
" initial residu: "
<<
gcg
<<
'\n'
;
}
std
::
cout
<<
" - iteration "
<<
std
::
setw
(
6
)
<<
i
<<
"
\t
residu: "
<<
gcg
/
gcg0
;
std
::
cout
<<
"
\t
absolute: "
<<
gcg
<<
'\n'
;
if
(
gcg
<
relativeEpsilon
)
{
break
;
}
gamma
=
gcg
/
gamma
;
h
*=
gamma
;
h
+=
g
;
}
if
(
gcg
>
relativeEpsilon
)
{
std
::
cout
<<
" conjugate gradient: *NOT CONVERGED*
\n
"
;
std
::
cout
<<
" - epsilon: "
<<
epsilon
<<
'\n'
;
std
::
cout
<<
" - relative residu : "
<<
gcg
/
gcg0
<<
'\n'
;
std
::
cout
<<
" - absolute residu : "
<<
gcg
<<
'\n'
;
}
}
};
#endif // PCG_HPP
This diff is collapsed.
Click to expand it.
src/algebra/Vector.hpp
+
18
−
1
View file @
0a24f6ae
...
...
@@ -20,6 +20,16 @@ class Vector
static_assert
(
std
::
is_same_v
<
typename
decltype
(
m_values
)
::
index_type
,
index_type
>
);
public:
friend
PUGS_INLINE
Vector
copy
(
const
Vector
&
x
)
{
auto
values
=
copy
(
x
.
m_values
);
Vector
x_copy
{
0
};
x_copy
.
m_values
=
values
;
return
x_copy
;
}
friend
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
const
Vector
<
DataType
>&
x
)
{
...
...
@@ -29,6 +39,12 @@ class Vector
return
os
;
}
friend
Vector
operator
*
(
const
DataType
&
a
,
const
Vector
&
x
)
{
Vector
y
=
copy
(
x
);
return
y
*=
a
;
}
PUGS_INLINE
DataType
operator
,(
const
Vector
&
y
)
...
...
@@ -37,7 +53,7 @@ class Vector
// Does not use parallel_for to preserve sum order
for
(
index_type
i
=
0
;
i
<
this
->
size
();
++
i
)
{
sum
+=
m_values
[
i
]
+
y
.
m_values
[
i
];
sum
+=
m_values
[
i
]
*
y
.
m_values
[
i
];
}
return
sum
;
...
...
@@ -102,6 +118,7 @@ class Vector
operator
=
(
const
DataType
&
value
)
noexcept
{
m_values
.
fill
(
value
);
return
*
this
;
}
template
<
typename
DataType2
>
...
...
This diff is collapsed.
Click to expand it.
src/main.cpp
+
10
−
2
View file @
0a24f6ae
...
...
@@ -25,6 +25,7 @@
#include
<SynchronizerManager.hpp>
#include
<CRSMatrix.hpp>
#include
<PCG.hpp>
#include
<iostream>
...
...
@@ -44,9 +45,10 @@ main(int argc, char* argv[])
FlexibleMatrix
F
(
matrix_size
);
for
(
size_t
i
=
0
;
i
<
matrix_size
;
++
i
)
{
F
(
i
,
i
)
=
2
;
F
(
i
,
i
)
=
4
;
if
(
i
+
1
<
matrix_size
)
{
F
(
i
,
i
+
1
)
=
1
;
F
(
i
,
i
+
1
)
=
-
1
;
F
(
i
+
1
,
i
)
=
-
1
;
}
}
std
::
cout
<<
F
<<
'\n'
;
...
...
@@ -64,6 +66,12 @@ main(int argc, char* argv[])
Vector
<
double
>
Ax
=
A
*
x
;
std
::
cout
<<
"Ax=
\n
"
<<
Ax
<<
'\n'
;
Vector
<
double
>
u
{
x
.
size
()};
u
=
0
;
PCG
{
Ax
,
A
,
A
,
u
,
9
,
1e-4
};
std
::
cout
<<
"u="
<<
u
<<
'\n'
;
}
Kokkos
::
finalize
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment