-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
151 lines (132 loc) · 4.9 KB
/
main.cpp
File metadata and controls
151 lines (132 loc) · 4.9 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
/*
* This file is part of the SUperman repository: https://github.com/SU-HPC/SUPerman
* Author: Deniz Elbek
*
* Please see the papers:
*
* @article{Elbek2026SUperman,
* title = {SUperman: Efficient permanent computation on GPUs},
* author = {Elbek, Deniz and Ta{\c{s}}yaran, Fatih and U{\c{c}}ar, Bora and Kaya, Kamer},
* journal = {Computer Physics Communications},
* volume = {321},
* pages = {110027},
* year = {2026},
* doi = {10.1016/j.cpc.2026.110027}
*
* @article{Elbek2025CodeGeneration,
* title = {Fully-Automated Code Generation for Efficient Computation of Sparse Matrix Permanents on GPUs},
* author = {Elbek, Deniz and Kaya, Kamer},
* journal = {arXiv preprint arXiv:2501.15126},
* year = {2025},
* doi = {10.48550/arXiv.2501.15126},
* }
*/
#include "IO.h"
#include <iostream>
#include <iomanip>
#include "AlgorithmSelector.h"
#ifdef MPI_AVAILABLE
#include "mpi_wrapper.h"
#endif
#define S double
#define C double
double avgN = 0;
double avgNNZ = 0;
unsigned count = 0;
bool printing = true;
bool sparse = false;
int main(int argv, char* argc[])
{
int rank = 0;
int numberOfProcessors = 1;
#ifdef MPI_AVAILABLE
initMPI(nullptr, nullptr);
mpiCommRank(getMPI_COMM_WORLD(), &rank);
mpiCommSize(getMPI_COMM_WORLD(), &numberOfProcessors);
#endif
Settings settings;
settings.rank = rank;
settings.processorNum = numberOfProcessors;
IO::readSettings<S>(settings, argv, argc);
if (!settings.complex)
{
std::stringstream stream;
stream << "MATRIX NAME: " << settings.filename << std::endl;
print(stream, rank, settings.PID, 1);
Matrix<S>* matrix = IO::readMatrix<S>(settings.filename, settings);
std::vector<std::vector<unsigned>> rowComponents;
std::vector<std::vector<unsigned>> colComponents;
unsigned biggestComponentSize;
findCCs<S>(matrix, rowComponents, colComponents, biggestComponentSize);
AlgorithmSelector<C, S>::Algorithm algorithm = AlgorithmSelector<C, S>::selectAlgorithm(matrix, &settings, biggestComponentSize);
bool connectedComponentBased = (settings.algorithm != REGEFFICIENTCODEGENERATION && settings.algorithm != NAIVECODEGENERATION);
Result result;
if (connectedComponentBased)
{
result.time = 0;
result.permanent = 0;
if (matrix->nov != 0) result.permanent = 1;
if (rowComponents.size() > 1)
{
printing = false;
}
for (unsigned component = 0; component < rowComponents.size(); ++component)
{
std::vector<unsigned>& rowVerts = rowComponents[component];
std::vector<unsigned>& colVerts = colComponents[component];
if (rowVerts.size() != colVerts.size())
{
result.permanent = 0;
break;
}
Matrix<S>* block = new Matrix<S>(rowVerts.size());
for (unsigned i = 0; i < rowVerts.size(); ++i)
{
for (unsigned j = 0; j < colVerts.size(); ++j)
{
block->mat[i * rowVerts.size() + j] = matrix->mat[rowVerts[i] * matrix->nov + colVerts[j]];
}
}
int nnz = getNNZ(block);
block->sparsity = (double(nnz) / (block->nov * block->nov)) * 100;
Result bres = algorithm(block, &settings);
result.time += bres.time;
result.permanent *= bres.permanent;
delete block;
}
}
else
{
result = algorithm(matrix, &settings);
}
avgN /= count;
avgNNZ /= count;
stream = std::stringstream();
stream << "Submatrix count: " << count << " - Average N: " << avgN << " - Average NNZ: " << avgNNZ << std::endl;
if (std::isnan(result.permanent))
{
stream << "Permanent computation has overflowed." << " - Computed in: " << result.time << " seconds." << std::endl;
}
else
{
unsigned maxPrec = effectivePrecision(result.permanent);
stream << "Permanent: " << std::setprecision (maxPrec) << result.permanent << " - Computed in: " << result.time << " seconds." << std::endl;
}
print(stream, rank, settings.PID, -1);
delete matrix;
}
else
{
std::stringstream stream;
stream << "MATRIX NAME: " << settings.filename << std::endl;
print(stream, rank, settings.PID, 1);
Matrix<std::complex<S>>* matrix = IO::readComplex<S>(settings.filename, settings);
recompilationStatus(settings.pipe, 0, settings.rank);
complexPerman<C, S>(matrix, &settings);
delete matrix;
}
#ifdef MPI_AVAILABLE
finalizeMPI();
#endif
return 0;
}