-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgetto_1.cpp
More file actions
199 lines (168 loc) · 6.83 KB
/
Progetto_1.cpp
File metadata and controls
199 lines (168 loc) · 6.83 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Metodi del Calcolo Scientifico
// Progetto_1 C++
// Mohammad Alì Manan (817205)
// Francesco Porto (816042)
// Stranieri Francesco (816551)
#include <dirent.h>
#include <iostream>
#include <fstream>
#include <math.h>
#include <chrono>
#include <ctime>
#include "eigen-3.3.7/Eigen/Sparse"
#include "eigen-3.3.7/unsupported/Eigen/SparseExtra"
#if defined(_WIN32) || defined(__CYGWIN__)
#include <windows.h>
#include <psapi.h>
std::string platform("Windows");
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
#include <unistd.h>
#include <sys/resource.h>
std::string platform("Linux");
#elif defined(__APPLE__) && defined(__MACH__)
#include <mach/mach.h>
std::string platform("MacOS");
#else
#error Platform not supported!
#endif
// https://stackoverflow.com/questions/669438/how-to-get-memory-usage-at-runtime-using-c
/*
* Author: David Robert Nadeau
* Site: http://NadeauSoftware.com/
* License: Creative Commons Attribution 3.0 Unported License
* http://creativecommons.org/licenses/by/3.0/deed.en_US
*/
size_t getCurrentRSS();
struct matrixSolved
{
int matrixSize;
int nonZero;
int memoryAllocated;
int executionTime;
double relativeError;
bool logFileWrite;
};
matrixSolved matrixSolver(const char *matrixList);
using namespace Eigen;
using namespace std;
using namespace std::chrono;
typedef SparseMatrix<double> SpMat;
int main()
{
DIR *matrixFolder;
char *matrixName, *matrixExtension;
struct dirent *dir;
const char *matrixPath = "SuiteSparse/MTX/";
matrixFolder = opendir(matrixPath);
if (!matrixFolder)
{
cout << "Fatal Error! Folder NOT Found." << endl;
return -1;
}
while ((dir = readdir(matrixFolder)) != NULL)
{
matrixName = strtok(dir->d_name, ".");
matrixExtension = strtok(NULL, ".");
if (matrixExtension == NULL || strcmp(matrixExtension, "mtx") != 0)
continue;
int matrixListLength = sizeof(matrixPath) + sizeof(matrixName) + sizeof(matrixExtension) + 2;
char matrixList[matrixListLength];
strcpy(matrixList, matrixPath);
strcat(matrixList, matrixName);
strcat(matrixList, ".");
strcat(matrixList, matrixExtension);
cout << "MatrixName " << matrixName << endl;
matrixSolved matrixSolved = matrixSolver(matrixList);
cout << "ExecutionTime(ms) " << matrixSolved.executionTime << endl;
cout << "MemoryAllocated(KB) " << matrixSolved.memoryAllocated << endl;
cout << "RelativeError " << matrixSolved.relativeError << endl;
// logFile
if(matrixSolved.logFileWrite){
time_t now = system_clock::to_time_t(system_clock::now());
char date[20];
strftime(date, sizeof(date), "%d-%m-%Y %H:%M:%S", localtime(&now));
ofstream logFile;
logFile.open("result.txt", ios_base::app);
logFile << "Date: " << date << "\n";
logFile << "Platform: " << platform << "\n";
logFile << "Language: "
<< "C++"
<< "\n";
logFile << "MatrixName: " << matrixName << "\n";
logFile << "MatrixSize: " << matrixSolved.matrixSize << " x " << matrixSolved.matrixSize << "\n";
logFile << "NonZero: " << matrixSolved.nonZero << "\n";
logFile << "RelativeError: " << matrixSolved.relativeError << "\n";
logFile << "ExecutionTime(ms): " << matrixSolved.executionTime << "\n";
logFile << "MemoryAllocated(KB): " << matrixSolved.memoryAllocated << "\n";
logFile
<< "\n";
logFile.close();
}
}
closedir(matrixFolder);
}
size_t getCurrentRSS()
{
#if defined(_WIN32) || defined(__CYGWIN__)
PROCESS_MEMORY_COUNTERS info;
GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info));
return (size_t)info.WorkingSetSize;
#elif defined(__linux__) || defined(__linux) || defined(linux) || defined(__gnu_linux__)
long rss = 0L;
FILE *fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
return (size_t)0L; /* Can't open? */
if (fscanf(fp, "%*s%ld", &rss) != 1)
{
fclose(fp);
return (size_t)0L; /* Can't read? */
}
fclose(fp);
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
#elif defined(__APPLE__) && defined(__MACH__)
struct mach_task_basic_info info;
mach_msg_type_number_t infoCount = MACH_TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO,
(task_info_t)&info, &infoCount) != KERN_SUCCESS)
return (size_t)0L; /* Can't access? */
return (size_t)info.resident_size;
#else
#error Platform not supported!
return (size_t)0L; /* Unsupported. */
#endif
}
matrixSolved matrixSolver(const char *matrixList)
{
matrixSolved matrixSolved;
// https://eigen.tuxfamily.org/dox/group__TutorialSparse.html
SpMat A;
loadMarket(A, matrixList);
matrixSolved.matrixSize = A.rows();
cout << "MatrixSize " << matrixSolved.matrixSize << endl;
matrixSolved.nonZero = A.nonZeros();
VectorXd xEs = VectorXd::Ones(matrixSolved.matrixSize);
VectorXd b = A * xEs;
VectorXd x = VectorXd::Zero(matrixSolved.matrixSize);
matrixSolved.logFileWrite = true;
try
{
size_t memoryAllocatedStart = getCurrentRSS();
high_resolution_clock::time_point start = high_resolution_clock::now();
SimplicialLLT<SpMat> solver(A);
x = solver.solve(b);
high_resolution_clock::time_point stop = high_resolution_clock::now();
duration<double, milli> differenceTime = (stop - start);
size_t memoryAllocatedEnd = getCurrentRSS();
matrixSolved.memoryAllocated = (memoryAllocatedEnd - memoryAllocatedStart) / 1000;
matrixSolved.executionTime = round(differenceTime.count());
matrixSolved.relativeError = (x - xEs).norm() / xEs.norm();
}
catch (...)
{
matrixSolved.memoryAllocated = 0;
matrixSolved.executionTime = 0;
matrixSolved.relativeError = 0;
matrixSolved.logFileWrite = false;
}
return matrixSolved;
}