-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgetto_1.py
115 lines (85 loc) · 3.47 KB
/
Progetto_1.py
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
# Metodi del Calcolo Scientifico
# Progetto_1 Python
# Mohammad Alì Manan (817205)
# Francesco Porto (816042)
# Stranieri Francesco (816551)
# https://docs.scipy.org/doc/scipy-0.14.0/reference/sparse.html
import scipy.io
# https://scikit-sparse.readthedocs.io/en/latest/cholmod.html
# https://readthedocs.org/projects/scikit-sparse/downloads/pdf/latest/
from sksparse.cholmod import cholesky
from numpy import ones, empty
from numpy.linalg import norm
import time
import os
import psutil
import pathlib
import platform as _platform
from datetime import datetime
import gc
def matrixSolver(matrix):
fileNameMatrix = str(matrix).split(os.sep)[2]
matrixName = fileNameMatrix.split(".")[0]
print('MatrixName', matrixName)
matrixProblem = scipy.io.loadmat(matrix)
# print(sorted(matrixProblem.keys()))
# print(matrixProblem['Problem'])
# A = (matrixProblem['Problem'])['A'][0][0]
matrixSize = ((matrixProblem['Problem'])['A'][0][0]).shape[0]
print('MatrixSize', matrixSize)
nonZero = ((matrixProblem['Problem'])['A'][0][0]).getnnz()
xEs = ones(matrixSize)
b = ((matrixProblem['Problem'])['A'][0][0])*xEs
x = empty(matrixSize)
logFileWrite = True
try:
start = time.process_time()
startMemoryAllocated = psutil.virtual_memory().used
R = cholesky((matrixProblem['Problem'])['A'][0][0])
x = R(b)
endMemoryAllocated = psutil.virtual_memory().used
# print((endMemoryAllocated.rss - startMemoryAllocated.rss) / 1000)
executionTime = str(round((time.process_time() - start) * 1000))
memoryAllocated = str(
round((endMemoryAllocated - startMemoryAllocated) / 1000))
relativeError = str("{:.7g}".format(norm(x - xEs) / norm(xEs)))
except:
executionTime = '0'
memoryAllocated = '0'
relativeError = '0'
logFileWrite = False
return (matrixName, matrixSize, nonZero,
executionTime, memoryAllocated, relativeError, logFileWrite)
def main():
matrixFolder = "SuiteSparse/MAT/"
matrixList = list(pathlib.Path(matrixFolder).glob('*.mat'))
fileName = "result.txt"
formatOut = "%d-%m-%Y %H:%M:%S"
platform = _platform.system()
if platform == 'Darwin':
platform = 'MacOS'
for matrix in matrixList:
(matrixName, matrixSize, nonZero,
executionTime, memoryAllocated, relativeError, logFileWrite) = matrixSolver(matrix)
gc.collect()
print('ExecutionTime(ms)', executionTime)
print('MemoryAllocated(KB)', memoryAllocated)
print('RelativeError', relativeError)
print()
# logFile
if logFileWrite:
date = datetime.now().strftime(formatOut)
with open(fileName, "a") as logFile:
logFile.write('Date: ' + date + "\n" +
'Platform: ' + platform + "\n" +
'Language: ' + 'Python' + "\n" +
'MatrixName: ' + matrixName + "\n" +
'MatrixSize: ' + str(matrixSize) +
" x " + str(matrixSize) + "\n" +
'NonZero: ' + str(nonZero) + "\n" +
'RelativeError: ' + relativeError + "\n" +
'ExecutionTime(ms): ' + executionTime + "\n" +
'MemoryAllocated(KB): ' + memoryAllocated + "\n" +
"\n")
if __name__ == "__main__":
main()