Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102737593
main.cpp
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sun, Feb 23, 16:23
Size
2 KB
Mime Type
text/x-c
Expires
Tue, Feb 25, 16:23 (1 d, 14 h)
Engine
blob
Format
Raw Data
Handle
24409993
Attached To
R6658 PHPC CG Project
main.cpp
View Options
#include <iostream>
#include <string>
#include <random>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
#include "cg.h"
int
main
(
int
argc
,
char
**
argv
){
MPI
::
Init
(
argc
,
argv
);
clock_t
begin
=
clock
();
int
rank
;
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
rank
);
string
filename
=
argv
[
1
];
string
outFilename
=
argv
[
2
];
string
line
;
ifstream
myfile
(
filename
);
bool
firstLine
=
0
;
int
rows
=
0
;
int
cols
=
0
;
int
nonZeroEntries
=
0
;
int
nzeCounter
=
0
;
int
X_size
;
vector
<
double
>
X_data
;
if
(
myfile
.
is_open
())
{
while
(
getline
(
myfile
,
line
)
)
{
if
(
line
.
find
(
"%"
)
==
std
::
string
::
npos
){
if
(
firstLine
==
0
){
firstLine
=
1
;
istringstream
iss
(
line
);
vector
<
string
>
tokens
{
istream_iterator
<
string
>
{
iss
},
istream_iterator
<
string
>
{}};
rows
=
stoi
(
tokens
[
0
]);
cols
=
stoi
(
tokens
[
1
]);
nonZeroEntries
=
stoi
(
tokens
[
2
]);
if
(
rows
!=
cols
){
cout
<<
"Not a square matrix
\n
"
;
return
0
;
}
X_size
=
rows
*
cols
;
vector
<
double
>
v
(
X_size
);
X_data
=
v
;
}
else
{
nzeCounter
++
;
istringstream
iss
(
line
);
vector
<
string
>
tokens
{
istream_iterator
<
string
>
{
iss
},
istream_iterator
<
string
>
{}};
int
rowNum
=
stoi
(
tokens
[
0
])
-
1
;
int
colNum
=
stoi
(
tokens
[
1
])
-
1
;
double
value
=
stod
(
tokens
[
2
]);
X_data
[
rowNum
*
cols
+
colNum
]
=
value
;
X_data
[
colNum
*
cols
+
rowNum
]
=
value
;
}
}
}
myfile
.
close
();
}
else
cout
<<
"Unable to open file
\n
"
;
if
(
nzeCounter
!=
nonZeroEntries
){
cout
<<
"Wrong number of entries in file "
<<
filename
<<
"
\n
"
;
}
int
maxRandomValue
=
2
;
vector
<
double
>
Y
=
operation
::
randomVector
(
rows
,
maxRandomValue
);
double
accuracy
=
0.000001
;
/*
//Check inputs
if(!X.isSymmetric()){
cout<<"X is not symmetric, aborting\n";
return 0;
}
if(!X.isPositiveDefinite()){
cout<<"X is not positive definite, aborting\n";
return 0;
}
*/
/*
int Y_rows=Y.getRows();
int X_cols=X.getCols();
if(X_cols!=Y_rows){
cout<<"X and Y have invalid dimensions, aborting\n";
return 0;
}
*/
//Run CG solver
vector
<
double
>
solVec
=
cg
::
conjugateGradient
(
X_data
,
Y
,
accuracy
);
if
(
rank
==
0
){
ofstream
outFile
;
outFile
.
open
(
outFilename
);
outFile
<<
"%%Solution
\n
"
;
outFile
<<
solVec
.
size
()
<<
" 1 "
<<
solVec
.
size
()
<<
"
\n
"
;
for
(
int
i
=
0
;
i
<
solVec
.
size
();
i
++
){
outFile
<<
i
+
1
<<
" 1 "
<<
solVec
[
i
]
<<
"
\n
"
;
}
outFile
.
close
();
clock_t
end
=
clock
();
double
elapsed_secs
=
double
(
end
-
begin
)
/
CLOCKS_PER_SEC
;
cout
<<
"Time: "
<<
elapsed_secs
<<
" seconds
\n
"
;
}
MPI_Finalize
();
}
Event Timeline
Log In to Comment