-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
203 lines (172 loc) · 6.73 KB
/
io.c
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
/* I/O related functions for heat equation solver */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <mpi.h>
#include "heat.h"
/* Output routine that prints out a picture of the temperature
* distribution. */
void write_field(field *temperature, int iter, parallel_data *parallel)
{
char filename[64];
/* The actual write routine takes only the actual data
* (without ghost layers) so we need array for that. */
int height, width;
double *full_data;
int coords[2];
int ix, jy;
int i, p;
height = temperature->nx_full;
width = temperature->ny_full;
if (parallel->rank == 0) {
/* Copy the inner data */
full_data = malloc_2d(height, width);
for (i = 0; i < temperature->nx; i++)
memcpy(&full_data[idx(i, 0, width)],
&temperature->data[idx(i+1, 1, temperature->ny + 2)],
temperature->ny * sizeof(double));
/* Receive data from other ranks */
for (p = 1; p < parallel->size; p++) {
MPI_Cart_coords(parallel->comm, p, 2, coords);
ix = coords[0] * temperature->nx;
jy = coords[1] * temperature->ny;
MPI_Recv(&full_data[idx(ix, jy, width)], 1,
parallel->subarraytype, p, 22,
parallel->comm, MPI_STATUS_IGNORE);
}
/*
sprintf(filename, "%s_%04d.png", "heat", iter);
save_png(full_data, height, width, filename, 'c');
free_2d(full_data);
*/
} else {
/* Send data */
MPI_Ssend(temperature->data, 1,
parallel->subarraytype, 0,
22, parallel->comm);
}
}
/* Read the initial temperature distribution from a file and
* initialize the temperature fields temperature1 and
* temperature2 to the same initial state. */
void read_field(field *temperature1, field *temperature2, char *filename,
parallel_data *parallel)
{
FILE *fp;
int nx, ny, i, j;
double *full_data;
int coords[2];
int ix, jy, p;
int count;
fp = fopen(filename, "r");
/* Read the header */
count = fscanf(fp, "# %d %d \n", &nx, &ny);
if (count < 2) {
fprintf(stderr, "Error while reading the input file!\n");
MPI_Abort(MPI_COMM_WORLD, -1);
}
parallel_setup(parallel, nx, ny);
set_field_dimensions(temperature1, nx, ny, parallel);
set_field_dimensions(temperature2, nx, ny, parallel);
/* Allocate arrays (including ghost layers) */
temperature1->data =
malloc_2d(temperature1->nx + 2, temperature1->ny + 2);
temperature2->data =
malloc_2d(temperature2->nx + 2, temperature2->ny + 2);
if (parallel->rank == 0) {
/* Full array */
full_data = malloc_2d(nx, ny);
/* Read the actual data */
for (i = 0; i < nx; i++) {
for (j = 0; j < ny; j++) {
count = fscanf(fp, "%lf", &full_data[idx(i, j, ny)]);
}
}
/* Copy to own local array */
for (i = 0; i < temperature1->nx; i++) {
memcpy(&temperature1->data[idx(i+1, 1, temperature1->ny + 2)],
&full_data[idx(i, 0, ny)], temperature1->ny * sizeof(double));
}
/* Send to other processes */
for (p = 1; p < parallel->size; p++) {
MPI_Cart_coords(parallel->comm, p, 2, coords);
ix = coords[0] * temperature1->nx;
jy = coords[1] * temperature1->ny;
MPI_Send(&full_data[idx(ix, jy, ny)], 1, parallel->subarraytype,
p, 44, parallel->comm);
}
} else {
/* Receive data */
MPI_Recv(temperature1->data, 1,
parallel->subarraytype, 0,
44, parallel->comm, MPI_STATUS_IGNORE);
}
/* Set the boundary values */
for (i = 0; i < temperature1->nx + 1; i++) {
temperature1->data[idx(i, 0, temperature1->ny + 2)] =
temperature1->data[idx(i, 1, temperature1->ny + 2)];
temperature1->data[idx(i, temperature1->ny + 1, temperature1->ny + 2)] =
temperature1->data[idx(i, temperature1->ny, temperature1->ny + 2)];
}
for (j = 0; j < temperature1->ny + 2; j++) {
temperature1->data[idx(0, j, temperature1->ny + 2)] =
temperature1->data[idx(1, j, temperature1->ny + 2)];
temperature1->data[idx(temperature1->nx + 1, j, temperature1->ny + 2)] =
temperature1->data[idx(temperature1->nx, j, temperature1->ny + 2)];
}
copy_field(temperature1, temperature2);
if (parallel->rank == 0) {
free_2d(full_data);
}
fclose(fp);
}
/* Write a restart checkpoint that contains field dimensions, current
* iteration number and temperature field. */
void write_restart(field *temperature, parallel_data *parallel, int iter)
{
MPI_File fp;
MPI_Offset disp;
// open the file and write the dimensions
MPI_File_open(MPI_COMM_WORLD, CHECKPOINT,
MPI_MODE_CREATE | MPI_MODE_WRONLY, MPI_INFO_NULL, &fp);
if (parallel->rank == 0) {
MPI_File_write(fp, &temperature->nx_full, 1, MPI_INT,
MPI_STATUS_IGNORE);
MPI_File_write(fp, &temperature->ny_full, 1, MPI_INT,
MPI_STATUS_IGNORE);
MPI_File_write(fp, &iter, 1, MPI_INT, MPI_STATUS_IGNORE);
}
disp = 3 * sizeof(int);
MPI_File_set_view(fp, 0, MPI_DOUBLE, parallel->filetype, "native",
MPI_INFO_NULL);
MPI_File_write_at_all(fp, disp, temperature->data,
1, parallel->restarttype, MPI_STATUS_IGNORE);
MPI_File_close(&fp);
}
/* Read a restart checkpoint that contains field dimensions, current
* iteration number and temperature field. */
void read_restart(field *temperature, parallel_data *parallel, int *iter)
{
MPI_File fp;
MPI_Offset disp;
int nx, ny;
// open the file and write the dimensions
MPI_File_open(MPI_COMM_WORLD, CHECKPOINT, MPI_MODE_RDONLY,
MPI_INFO_NULL, &fp);
// read grid size and current iteration
MPI_File_read_all(fp, &nx, 1, MPI_INT, MPI_STATUS_IGNORE);
MPI_File_read_all(fp, &ny, 1, MPI_INT, MPI_STATUS_IGNORE);
MPI_File_read_all(fp, iter, 1, MPI_INT, MPI_STATUS_IGNORE);
// set correct dimensions to MPI metadata
parallel_setup(parallel, nx, ny);
// set local dimensions and allocate memory for the data
set_field_dimensions(temperature, nx, ny, parallel);
allocate_field(temperature);
disp = 3 * sizeof(int);
MPI_File_set_view(fp, 0, MPI_DOUBLE, parallel->filetype, "native",
MPI_INFO_NULL);
MPI_File_read_at_all(fp, disp, temperature->data,
1, parallel->restarttype, MPI_STATUS_IGNORE);
MPI_File_close(&fp);
}