-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse.cpp
More file actions
executable file
·170 lines (147 loc) · 3.12 KB
/
Copy pathsparse.cpp
File metadata and controls
executable file
·170 lines (147 loc) · 3.12 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
ver.03.03
file sparse.cpp
*/
#include "sparse.h"
using std::vector;
const double Sparse_wiz::tolerance=1.0E-14;
Sparse_wiz::Sparse_wiz(bool symmetric):
N(0),
ia(NULL),
ja(NULL),
iap1(NULL),
jap1(NULL),
values(NULL),
isInitialized(false),
vja(NULL),
Sparse(symmetric)
{}
Sparse_wiz::~Sparse_wiz(void){
if(values)
delete[] values;
if(ia)
delete[] ia;
if(ja)
delete[] ja;
if(vja)
delete[] vja;
if(iap1)
delete[] iap1;
if(jap1)
delete[] jap1;
}
void Sparse_wiz::initialize(int N){
if(isInitialized)
return;
isInitialized=true;
this->N=N;
ia=new int[N+1];
vja=new vector<int>[N];
for(int i=0;i<N;i++){
ia[i]=i;
vja[i].push_back(i);
}
ia[N]=N;
}
#ifdef __INTEL_COMPILER
void Sparse_wiz::solve_mkl(double* rhs,double* solution){
if(!symmetric){
for(unsigned row=0;row<N;row++)
for(unsigned j=ia[row];j<ia[row+1];j++){
unsigned col=ja[j];
if(row<col)
values[getSparseIndex(col,row)]=values[j];
}
}
pardiso(rhs,solution,N,iap1,jap1,values);
}
#endif
void Sparse_wiz::add(int row,int col){
if(row==col){
return;
}else if(vja[row].front()>col){
//vja[row].push_front(col);
vja[row].insert(vja[row].begin(),col);
}else{
vector<int>::iterator pja=vja[row].end();
for(;pja!=vja[row].begin();){
pja--;
if(*pja==col)
return;
if(*pja<col)
break;
}
vja[row].insert(++pja,col);
}
//fixme: this can be made faster by using frequency occurance
for(int i=row+1;i<N+1;i++)
ia[i]++;
if(!symmetric && row<col)
add(col,row);
}
void Sparse_wiz::add(int row,int col,double value){
SPARSEINDEX si=getSparseIndex(row,col);
#ifdef _DEBUG
if(si==-1){
cout << "error in Sparse_wiz::add(value)" << endl;
cout << row << " " << col << endl;
return;
}
#endif
values[si]+=value;
}
double& Sparse_wiz::operator()(int row,int col){
SPARSEINDEX si=getSparseIndex(row,col);
return values[si];
}
void Sparse_wiz::allocate(void){
vja_size=0;
for(int i=0;i<N;i++)
vja_size+=vja[i].size();
ja=new int[vja_size];
values=new double[vja_size];
ia[N]=vja_size;
int cnt=0;
for(int i=0;i<N;i++)
for(vector<int>::iterator pja=vja[i].begin();pja!=vja[i].end();pja++)
ja[cnt++]=*pja;
for(unsigned int i=0;i<vja_size;i++)
values[i]=0.0;
iap1=new int[N+1];
for(int i=0;i<N+1;i++)
iap1[i]=ia[i]+1;
jap1=new int[vja_size];
for(int i=0;i<vja_size;i++)
jap1[i]=ja[i]+1;
}
void Sparse_wiz::zeroOut(void){
for(unsigned int i=0;i<vja_size;i++)
values[i]=0.0;
}
void Sparse_wiz::print(const char filename[]){
using std::ofstream;
using std::endl;
ofstream fout(filename);
for(MATRIXINDEX i=0;i<N;i++){
for(MATRIXINDEX j=0;j<N;j++){
SPARSEINDEX si=getSparseIndex(i,j);
if(si==-1){
fout << 0 << ",";
}else{
fout << values[si] << ",";
}
}
fout << endl;
}
}
int Sparse_wiz::getSparseIndex(MATRIXINDEX row,MATRIXINDEX col){
int cnt=ia[row];
for(vector<int>::iterator pja=vja[row].begin();pja!=vja[row].end();pja++){
if(*pja==col)
return cnt;
if(*pja>col)
return -1;
cnt++;
}
return -1;
}