Repository navigation
Expand file tree
/
Copy pathutil.cpp
More file actions
120 lines (96 loc) · 2.81 KB
/
Copy pathutil.cpp
File metadata and controls
120 lines (96 loc) · 2.81 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
#include "util.hpp"
void read_matrix(char *filename, MyReal **var, int dimx, int dimy) {
FILE *file;
MyReal tmp;
/* Open file */
file = fopen(filename, "r");
if (file == NULL) {
printf("Can't open %s \n", filename);
exit(1);
}
/* Read data */
printf("Reading file %s\n", filename);
for (int ix = 0; ix < dimx; ix++) {
for (int iy = 0; iy < dimy; iy++) {
fscanf(file, "%lf", &tmp);
var[ix][iy] = tmp;
}
}
fclose(file);
}
void read_vector(char *filename, MyReal *var, int dimx) {
FILE *file;
MyReal tmp;
/* Open file */
file = fopen(filename, "r");
if (file == NULL) {
printf("Can't open %s \n", filename);
exit(1);
}
/* Read data */
printf("Reading file %s\n", filename);
for (int ix = 0; ix < dimx; ix++) {
fscanf(file, "%lf", &tmp);
var[ix] = tmp;
}
fclose(file);
}
void write_vector(char *filename, MyReal *var, int dimN) {
FILE *file;
int i;
/* open file */
file = fopen(filename, "w");
if (file == NULL) {
printf("Can't open %s \n", filename);
exit(1);
}
/* Write data */
printf("Writing file %s\n", filename);
for (i = 0; i < dimN; i++) {
fprintf(file, "%1.14e\n", var[i]);
}
/* close file */
fclose(file);
}
void MPI_GatherVector(MyReal *sendbuffer, int localsendcount,
MyReal *recvbuffer, int rootprocessID, MPI_Comm comm) {
int comm_size;
MPI_Comm_size(comm, &comm_size);
int *recvcount = new int[comm_size];
int *displs = new int[comm_size];
/* Gather the local send counts and store in recvcount vector on root */
MPI_Gather(&localsendcount, 1, MPI_INT, recvcount, 1, MPI_INT, rootprocessID,
comm);
/* Compute displacement vector */
displs[0] = 0;
for (int i = 1; i < comm_size; i++) {
displs[i] = displs[i - 1] + recvcount[i - 1];
}
/* Gatherv the vector */
MPI_Gatherv(sendbuffer, localsendcount, MPI_MyReal, recvbuffer, recvcount,
displs, MPI_MyReal, rootprocessID, comm);
/* Clean up */
delete[] recvcount;
delete[] displs;
}
void MPI_ScatterVector(MyReal *sendbuffer, MyReal *recvbuffer,
int localrecvcount, int rootprocessID, MPI_Comm comm) {
int comm_size;
MPI_Comm_size(comm, &comm_size);
int *sendcount = new int[comm_size];
int *displs = new int[comm_size];
/* Gather the local recveive counts and store in sendcount for root */
MPI_Gather(&localrecvcount, 1, MPI_INT, sendcount, 1, MPI_INT, rootprocessID,
comm);
/* Compute displacement vector */
displs[0] = 0;
for (int i = 1; i < comm_size; i++) {
displs[i] = displs[i - 1] + sendcount[i - 1];
}
/* Gatherv the vector */
MPI_Scatterv(sendbuffer, sendcount, displs, MPI_MyReal, recvbuffer,
localrecvcount, MPI_MyReal, rootprocessID, comm);
/* Clean up */
delete[] sendcount;
delete[] displs;
}