Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F120202656
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
Wed, Jul 2, 15:33
Size
3 KB
Mime Type
text/x-c
Expires
Fri, Jul 4, 15:33 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
27155015
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 <chrono>
#include "cg.h"
using namespace std::chrono;
int rows;
/**
Loads a .mtx file into a vector
**/
vector<double> load_file(string filename){
vector<double> X_data;
string line;
ifstream myfile (filename);
bool firstLine=0;
int cols=0;
int nonZeroEntries=0;
int nzeCounter=0;
int X_size;
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){
throw invalid_argument("Input file did not contain a square matrix\n");
}
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";
}
return X_data;
}
/**
Writes a vector to a .mtx file
**/
void write_sol(string outFilename ,vector<double> solVec){
ofstream outFile;
outFile.open (outFilename);
outFile <<solVec.size()<<" 1 "<<solVec.size()<<"\n";
for(int i=0;i<solVec.size();i++){
outFile <<i+1<<" 1 "<<solVec[i]<<"\n";
}
outFile.close();
}
int main(int argc, char **argv){
MPI::Init(argc,argv);
high_resolution_clock::time_point t1 = high_resolution_clock::now();
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int size;
MPI_Comm_size(MPI_COMM_WORLD, &size);
int maxRandomValue=100;
double accuracy = 0.000001;
int mode = atoi(argv[1]);
int isBlock = atoi(argv[3]);
vector<double> X_data;
int cols;
string outFilename;
if(mode==0){
//generate random matrix
rows=atoi(argv[2]);
cols=rows;
vector<double> tmp(rows*cols);
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
double d = (rand()%maxRandomValue)+1;
if(i!=j){
tmp[i*cols+j]=d;
tmp[j*cols+i]=d;
} else{
tmp[i*cols+j]=d;
}
}
}
outFilename="rand_"+to_string(rows)+".mtx";
X_data=tmp;
} else{
//load .mtx file
string filename=argv[2];
X_data=load_file(filename);
outFilename=filename.substr(0, filename.size()-4)+"_sol.mtx";
}
vector<double> Y=operation::randomVector(rows,maxRandomValue);
//Run CG solver
vector<double> solVec = cg::conjugateGradient(X_data,Y,accuracy,isBlock);
if(rank==0){
write_sol(outFilename,solVec);
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
cout<<"Number of processes: "<<size<<"\n";
cout << "Took " << time_span.count() << " seconds.\n";
}
MPI_Finalize();
}
Event Timeline
Log In to Comment