-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSolve.cpp
85 lines (71 loc) · 2.32 KB
/
Solve.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Solve.h"
void gaussSeidel_host( float* x, BandedMatrix const& A, float const* b, size_t iterations )
{
int i,jband;
float tmp;
float const* a = A.a;
int const* bands = A.bands;
int* jbandOff = new int[A.nbands];
int midBand = A.nbands/2;
// Pre-compute column index offsets.
for( jband = 0; jband < A.nbands; ++jband )
jbandOff[jband] = jband*A.apitch;
for( ; iterations > 0; --iterations )
{
for( i = 0; i < A.rows; ++i )
{
tmp = 0.f;
for( jband = 0; jband < midBand; ++jband )
tmp += a[i+jbandOff[jband]] * x[i+bands[jband]];
// Skip the 0-band (bands[jband]==0)
for( ++jband; jband < A.nbands; ++jband )
tmp += a[i+jbandOff[jband]] * x[i+bands[jband]];
tmp = b[i] - tmp;
x[i] = tmp/a[i+jbandOff[midBand]]; // x[i] = tmp/a_ii
}
}
delete[] jbandOff;
}
void jacobi_host( float* x, BandedMatrix const& A, float const* b, size_t iterations, int xpad, float omega )
{
int i,jband;
float tmp;
float omega1 = 1.f-omega;
float const* a = A.a;
int const* bands = A.bands;
int* jbandOff = new int[A.nbands];
int midBand = A.nbands/2;
// WARNING: this needs padding so that xx[i+bands[jband]] is always ok.
float* xx = new float[A.rows + 2*xpad];
xx += xpad;
//float* xx = new float[A.rows + 4000];
//xx += 2000;
float* swapper;
// Pre-compute column index offsets.
for( jband = 0; jband < A.nbands; ++jband )
jbandOff[jband] = jband*A.apitch;
for( ; iterations > 0; --iterations )
{
for( i = 0; i < A.rows; ++i )
{
tmp = 0.f;
for( jband = 0; jband < midBand; ++jband )
tmp += a[i+jbandOff[jband]] * x[i+bands[jband]];
// Skip the 0-band (bands[jband]==0)
for( ++jband; jband < A.nbands; ++jband )
tmp += a[i+jbandOff[jband]] * x[i+bands[jband]];
tmp = b[i] - tmp;
xx[i] = omega*tmp/a[i+jbandOff[midBand]] + omega1*x[i];
}
// Swap xx and x.
swapper = x;
x = xx;
xx = swapper;
}
// WARNING: because we swap x and xx at each iteration, we may lose the
// result of the very last iteration on returning.
//xx -= 2000;
xx -= xpad;
delete[] xx;
delete[] jbandOff;
}