-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
216 lines (197 loc) · 5.03 KB
/
matrix.cpp
File metadata and controls
216 lines (197 loc) · 5.03 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mpi.h>
#include <string>
#include "matrix.hpp"
extern "C"
{
#include "mmio.h"
}
/* Performs the matrix-vector product */
double* PMV(double* matrix, double* vector, unsigned int n, unsigned int m)
{
unsigned int i, j;
double* result = new double[n];
for(i = 0; i < n; i++)
{
result[i] = 0;
for(j = 0; j < m; j++)
{
result[i] += matrix[i * m + j] * vector[j];
}
}
return result;
}
/* Create and initialize a matrix of dimension n*m */
double* init_matrix(unsigned int n, unsigned int m)
{
srand(1337);
double* tab = new double[n * m];
for(unsigned int i = 0; i < n; i++)
{
for(unsigned int j = 0; j < m; j++)
{
tab[i * m + j] = ((double)rand() / (double)RAND_MAX) * 10;
}
}
return tab;
}
/* Print the matrix to a file */
void dump_result(double* tab, unsigned n, unsigned int m, std::string f_name)
{
std::ofstream resultFile;
int w_rank(-1);
MPI_Comm_rank(MPI_COMM_WORLD, &w_rank);
f_name += std::to_string(w_rank);
f_name += ".txt";
resultFile.open(f_name, std::ios::out);
for(unsigned int i = 0; i < n; i++)
{
for(unsigned int j = 0; j < m; j++)
{
resultFile.width(6);
resultFile.precision(4);
resultFile << tab[i * m + j] << " ";
}
resultFile << std::endl;
}
resultFile.close();
}
double* read_from_file(std::string file_name, unsigned int* n, unsigned int* m)
{
std::ifstream input_file;
char* line;
double* data;
unsigned int i;
line = new char[4096];
input_file.open(file_name, std::ios::in);
input_file >> line;
*n = strtol(line, nullptr, 10);
input_file >> line;
*m = strtol(line, nullptr, 10);
i = 0;
if(*n != 0 && *m != 0)
{
data = new double[(*n) * (*m)];
}
while(input_file.eof() != true && i != (*n) * (*m))
{
input_file >> line;
data[i] = strtod(line, nullptr);
i++;
}
input_file.close();
return data;
}
/* Loads a matrix in MM format */
double* read_from_file_mm(std::string file_name, unsigned int* n, unsigned int* m)
{
FILE* mm_file;
MM_typecode matcode;
double* sparse_data;
double* data_matrix;
unsigned int* row;
unsigned int* column;
int nb_row, nb_column, nb_data;
int ret_code;
nb_row = 0;
nb_column = 0;
nb_data = 0;
ret_code = 0;
mm_file = fopen(file_name.c_str(), "r");
if(mm_file == NULL)
{
std::cerr << "Could not open file: " << file_name << std::endl;
MPI_Finalize();
exit(1);
}
if(mm_read_banner(mm_file, &matcode) != 0)
{
std::cerr << "Could not process Matrix Market banner" << std::endl;
}
// Obtain number of rows number of columns and number of elements
if((ret_code = mm_read_mtx_crd_size(mm_file, &nb_row, &nb_column, &nb_data)) != 0)
{
std::cerr << "Could not process Matrix Market dimensions" << std::endl;
}
*m = nb_column;
*n = nb_row;
row = new unsigned int[nb_data];
column = new unsigned int[nb_data];
sparse_data = new double[nb_data];
data_matrix = new double[(*n) * (*n)];
// Read the elements of the file
for(int i = 0; i < nb_data; i++)
{
fscanf(mm_file, "%d %d %lg\n", &row[i], &column[i], &sparse_data[i]);
row[i]--;
column[i]--;
}
// Create data matrix
for(int i = 0; i < nb_row; i++)
{
for(int j = 0; j < nb_column; j++)
{
data_matrix[i * nb_row + j] = 0;
}
}
for(int i = 0; i < nb_data; i++)
{
data_matrix[row[i] * nb_row + column[i]] = sparse_data[i];
}
fclose(mm_file);
delete[] row;
delete[] column;
delete[] sparse_data;
return data_matrix;
}
/* Displays program help */
void print_help()
{
std::cout.width(18);
std::cout << std::left << "pmv_mpi";
std::cout << std::right << " Size of matrix or source file " << std::endl;
std::cout.width(18);
std::cout << std::left << " -n ";
std::cout << std::right << " Specify the dimension of the square matrix " << std::endl;
std::cout.width(18);
std::cout << std::left << " -f ";
std::cout << std::right << " Specify the name of the input file " << std::endl;
std::cout.width(18);
std::cout << std::left << " -d ";
std::cout
<< std::right
<< " Create the matrix files generated during processing in a human-readable format "
<< std::endl;
std::cout.width(18);
std::cout << std::left << " -r ";
std::cout << std::right << " Create matrix output files in human-readable format "
<< std::endl;
std::cout.width(18);
std::cout << std::left << " -c ";
std::cout << std::right << " Use COO format to store matrices in memory " << std::endl;
std::cout.width(18);
std::cout << std::left << " -v ";
std::cout << std::right << " Activates vector distribution " << std::endl;
}
double* init_sparse_matrix(unsigned int n, unsigned int m, float p)
{
srand(1337);
double* tab = new double[n * m];
for(unsigned int i = 0; i < n; i++)
{
for(unsigned int j = 0; j < m; j++)
{
if(rand() % 100 < p * 100)
{
tab[i * m + j] = ((double)rand() / (double)RAND_MAX) * 10;
}
else
{
tab[i * m + j] = 0;
}
}
}
return tab;
}