Repository navigation
Expand file tree
/
Copy pathutils.cpp
More file actions
358 lines (317 loc) · 11 KB
/
Copy pathutils.cpp
File metadata and controls
358 lines (317 loc) · 11 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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#include "utils.h"
#include "source_base/module_external/blacs_connector.h"
#include "source_base/module_external/scalapack_connector.h"
#include <complex>
#include <cstring>
#include <fstream>
#include <iostream>
#include <mpi.h>
#include <sstream>
#ifdef __MPI
void initBlacsGrid(int loglevel,
MPI_Comm comm,
int nFull,
int nblk,
int& blacs_ctxt,
int& narows,
int& nacols,
int desc[])
{
std::stringstream outlog;
char BLACS_LAYOUT = 'C';
int ISRCPROC = 0; // fortran array starts from 1
int nprows, npcols;
int myprow, mypcol;
int nprocs, myid;
int info;
MPI_Comm_size(comm, &nprocs);
MPI_Comm_rank(comm, &myid);
// set blacs parameters
for (npcols = int(sqrt(double(nprocs))); npcols >= 2; --npcols)
{
if (nprocs % npcols == 0)
break;
}
nprows = nprocs / npcols;
if ((loglevel > 0 && myid == 0) || loglevel > 1)
{
outlog.str("");
outlog << "myid " << myid << ": nprows: " << nprows << " ; npcols: " << npcols << std::endl;
std::cout << outlog.str();
}
// int comm_f = MPI_Comm_c2f(comm);
blacs_ctxt = Csys2blacs_handle(comm);
Cblacs_gridinit(&blacs_ctxt, &BLACS_LAYOUT, nprows, npcols);
if ((loglevel > 0 && myid == 0) || loglevel > 1)
{
outlog.str("");
outlog << "myid " << myid << ": Cblacs_gridinit done, blacs_ctxt: " << blacs_ctxt << std::endl;
std::cout << outlog.str();
}
Cblacs_gridinfo(blacs_ctxt, &nprows, &npcols, &myprow, &mypcol);
if ((loglevel > 0 && myid == 0) || loglevel > 1)
{
int mypnum = Cblacs_pnum(blacs_ctxt, myprow, mypcol);
int prow, pcol;
Cblacs_pcoord(blacs_ctxt, myid, &prow, &pcol);
outlog.str("");
outlog << "myid " << myid << ": myprow: " << myprow << " ;mypcol: " << mypcol << std::endl;
outlog << "myid " << myid << ": mypnum: " << mypnum << std::endl;
outlog << "myid " << myid << ": prow: " << prow << " ;pcol: " << pcol << std::endl;
std::cout << outlog.str();
}
narows = numroc_(&nFull, &nblk, &myprow, &ISRCPROC, &nprows);
nacols = numroc_(&nFull, &nblk, &mypcol, &ISRCPROC, &npcols);
descinit_(desc, &nFull, &nFull, &nblk, &nblk, &ISRCPROC, &ISRCPROC, &blacs_ctxt, &narows, &info);
if ((loglevel > 0 && myid == 0) || loglevel > 1)
{
outlog.str("");
outlog << "myid " << myid << ": narows: " << narows << " nacols: " << nacols << std::endl;
outlog << "myid " << myid << ": blacs parameters setting" << std::endl;
outlog << "myid " << myid << ": desc is: ";
for (int i = 0; i < 9; ++i)
outlog << desc[i] << " ";
outlog << std::endl;
std::cout << outlog.str();
}
}
#endif
// load matrix from the file
void loadMatrix(const char FileName[], int nFull, double* a, int* desca, int blacs_ctxt)
{
int nprows, npcols, myprow, mypcol;
Cblacs_gridinfo(blacs_ctxt, &nprows, &npcols, &myprow, &mypcol);
int myid = Cblacs_pnum(blacs_ctxt, myprow, mypcol);
const int ROOT_PROC = 0;
std::ifstream matrixFile;
if (myid == ROOT_PROC)
matrixFile.open(FileName);
double* b; // buffer
const int MAX_BUFFER_SIZE = 1e9; // max buffer size is 1GB
int N = nFull;
int M
= std::max(1, std::min(nFull, (int)(MAX_BUFFER_SIZE / nFull / sizeof(double)))); // at lease 1 row, max size 1GB
if (myid == ROOT_PROC)
b = new double[M * N];
else
b = new double[1];
// set descb, which has all elements in the only block in the root process
// block size is M x N, so all elements are in the first process
int descb[9] = {1, blacs_ctxt, M, N, M, N, 0, 0, M};
int ja = 1, ib = 1, jb = 1;
for (int ia = 1; ia < nFull; ia += M)
{
int thisM = std::min(M, nFull - ia + 1); // nFull-ia+1 is number of the last few rows to be read from file
// read from the file
if (myid == ROOT_PROC)
{
for (int i = 0; i < thisM; ++i)
{
for (int j = 0; j < N; ++j)
{
matrixFile >> b[i + j * M];
}
}
}
// gather data rows by rows from all processes
Cpdgemr2d(thisM, N, b, ib, jb, descb, a, ia, ja, desca, blacs_ctxt);
}
if (myid == ROOT_PROC)
matrixFile.close();
delete[] b;
}
void saveLocalMatrix(const char filePrefix[], int narows, int nacols, double* a)
{
char FileName[80];
int myid;
std::ofstream matrixFile;
#ifdef __MPI
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
#else
myid = 0;
#endif
sprintf(FileName, "%s_%3.3d.dat", filePrefix, myid);
matrixFile.open(FileName);
matrixFile.flags(std::ios_base::scientific);
matrixFile.precision(17);
matrixFile.width(24);
for (int i = 0; i < narows; ++i)
{
for (int j = 0; j < nacols; ++j)
{
matrixFile << a[i + j * narows] << " ";
}
matrixFile << std::endl;
}
matrixFile.close();
}
// use pdgemr2d to collect matrix from all processes to root process
// and save to one completed matrix file
void saveMatrix(const char FileName[], int nFull, double* a, int* desca, int blacs_ctxt)
{
int nprows, npcols, myprow, mypcol;
Cblacs_gridinfo(blacs_ctxt, &nprows, &npcols, &myprow, &mypcol);
int myid = Cblacs_pnum(blacs_ctxt, myprow, mypcol);
const int ROOT_PROC = 0;
std::ofstream matrixFile;
if (myid == ROOT_PROC) // setup saved matrix format
{
matrixFile.open(FileName);
matrixFile.flags(std::ios_base::scientific);
matrixFile.precision(17);
matrixFile.width(24);
}
double* b; // buffer
const int MAX_BUFFER_SIZE = 1e9; // max buffer size is 1GB
int N = nFull;
int M
= std::max(1, std::min(nFull, (int)(MAX_BUFFER_SIZE / nFull / sizeof(double)))); // at lease 1 row, max size 1GB
if (myid == ROOT_PROC)
b = new double[M * N];
else
b = new double[1];
// set descb, which has all elements in the only block in the root process
int descb[9] = {1, blacs_ctxt, M, N, M, N, 0, 0, M};
int ja = 1, ib = 1, jb = 1;
for (int ia = 1; ia < nFull; ia += M)
{
int thisM = std::min(M, nFull - ia + 1); // nFull-ia+1 is the last few row to be saved
// gather data rows by rows from all processes
Cpdgemr2d(thisM, N, a, ia, ja, desca, b, ib, jb, descb, blacs_ctxt);
// write to the file
if (myid == ROOT_PROC)
{
for (int i = 0; i < thisM; ++i)
{
for (int j = 0; j < N; ++j)
{
matrixFile << b[i + j * M] << " ";
}
matrixFile << std::endl;
}
}
}
if (myid == ROOT_PROC)
matrixFile.close();
delete[] b;
}
// load matrix from the file
void loadMatrix(const char FileName[], int nFull, std::complex<double>* a, int* desca, int blacs_ctxt)
{
int nprows, npcols, myprow, mypcol;
Cblacs_gridinfo(blacs_ctxt, &nprows, &npcols, &myprow, &mypcol);
int myid = Cblacs_pnum(blacs_ctxt, myprow, mypcol);
const int ROOT_PROC = 0;
std::ifstream matrixFile;
if (myid == ROOT_PROC)
matrixFile.open(FileName);
std::complex<double>* b; // buffer
const int MAX_BUFFER_SIZE = 1e9; // max buffer size is 1GB
int N = nFull;
int M = std::max(
1,
std::min(nFull, (int)(MAX_BUFFER_SIZE / nFull / (2 * sizeof(double))))); // at lease 1 row, max size 1GB
if (myid == ROOT_PROC)
b = new std::complex<double>[M * N];
else
b = new std::complex<double>[1];
// set descb, which has all elements in the only block in the root process
// block size is M x N, so all elements are in the first process
int descb[9] = {1, blacs_ctxt, M, N, M, N, 0, 0, M};
int ja = 1, ib = 1, jb = 1;
for (int ia = 1; ia < nFull; ia += M)
{
int thisM = std::min(M, nFull - ia + 1); // nFull-ia+1 is number of the last few rows to be read from file
// read from the file
if (myid == ROOT_PROC)
{
for (int i = 0; i < thisM; ++i)
{
for (int j = 0; j < N; ++j)
{
matrixFile >> b[i + j * M];
}
}
}
// gather data rows by rows from all processes
Cpzgemr2d(thisM, N, b, ib, jb, descb, a, ia, ja, desca, blacs_ctxt);
}
if (myid == ROOT_PROC)
matrixFile.close();
delete[] b;
}
void saveLocalMatrix(const char filePrefix[], int narows, int nacols, std::complex<double>* a)
{
char FileName[80];
int myid;
std::ofstream matrixFile;
#ifdef __MPI
MPI_Comm_rank(MPI_COMM_WORLD, &myid);
#else
myid = 0;
#endif
sprintf(FileName, "%s_%3.3d.dat", filePrefix, myid);
matrixFile.open(FileName);
matrixFile.flags(std::ios_base::scientific);
matrixFile.precision(17);
matrixFile.width(24);
for (int i = 0; i < narows; ++i)
{
for (int j = 0; j < nacols; ++j)
{
matrixFile << a[i + j * narows] << " ";
}
matrixFile << std::endl;
}
matrixFile.close();
}
// use pzgemr2d to collect matrix from all processes to root process
// and save to one completed matrix file
void saveMatrix(const char FileName[], int nFull, std::complex<double>* a, int* desca, int blacs_ctxt)
{
int nprows, npcols, myprow, mypcol;
Cblacs_gridinfo(blacs_ctxt, &nprows, &npcols, &myprow, &mypcol);
int myid = Cblacs_pnum(blacs_ctxt, myprow, mypcol);
const int ROOT_PROC = 0;
std::ofstream matrixFile;
if (myid == ROOT_PROC) // setup saved matrix format
{
matrixFile.open(FileName);
matrixFile.flags(std::ios_base::scientific);
matrixFile.precision(17);
matrixFile.width(24);
}
std::complex<double>* b; // buffer
const int MAX_BUFFER_SIZE = 1e9; // max buffer size is 1GB
int N = nFull;
int M
= std::max(1, std::min(nFull, (int)(MAX_BUFFER_SIZE / nFull / sizeof(double)))); // at lease 1 row, max size 1GB
if (myid == ROOT_PROC)
b = new std::complex<double>[M * N];
else
b = new std::complex<double>[1];
// set descb, which has all elements in the only block in the root process
int descb[9] = {1, blacs_ctxt, M, N, M, N, 0, 0, M};
int ja = 1, ib = 1, jb = 1;
for (int ia = 1; ia < nFull; ia += M)
{
int transM = std::min(M, nFull - ia + 1); // nFull-ia+1 is the last few row to be saved
// gather data rows by rows from all processes
Cpzgemr2d(transM, N, a, ia, ja, desca, b, ib, jb, descb, blacs_ctxt);
// write to the file
if (myid == ROOT_PROC)
{
for (int i = 0; i < transM; ++i)
{
for (int j = 0; j < N; ++j)
{
matrixFile << b[i + j * M] << " ";
}
matrixFile << std::endl;
}
}
}
if (myid == ROOT_PROC)
matrixFile.close();
delete[] b;
}