-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencilCW2working.c
301 lines (255 loc) · 8.7 KB
/
stencilCW2working.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
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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
#include <mpi.h>
// Define output file name
#define OUTPUT_FILE "stencil.pgm"
void stencil(const int nx, const int ny, const int height, const int width,
float* image, float* tmp_image);
void init_image(const int nx, const int ny, const int width, const int height,
float* image);
void output_image(const char* file_name, const int nx, const int ny,
const int width, const int height, float* image);
double wtime(void);
void swapHalos(int rank, int nprocs, float* image, int width, int height);
void initiliaseArrays(int width, int height, float* image, float* tmp_image);
int main(int argc, char* argv[])
{
MPI_Init(&argc, &argv);
int nprocs, rank;
MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Check usage
if (argc != 4) {
fprintf(stderr, "Usage: %s nx ny niters\n", argv[0]);
exit(EXIT_FAILURE);
}
// Initiliase problem dimensions from command line arguments
int nx = atoi(argv[1]);
int ny = atoi(argv[2]);
int niters = atoi(argv[3]);
// Calculate section of image allocated to this process
int NumOfCols = nx / nprocs;
int remainder = nx % nprocs;
int start;
if (rank < remainder) {
NumOfCols++;
start = rank * NumOfCols + rank;
}
else{
start = rank * NumOfCols + remainder;
}
int end = start + NumOfCols + 1;
// we pad the outer edge of the image to avoid out of range address issues in
// stencil
int width = NumOfCols + 2;
int height = ny + 2;
// Allocate the image
float* image = malloc(sizeof(float) * width * height);
float* tmp_image = malloc(sizeof(float) * width * height);
initiliaseArrays(width, height, image, tmp_image);
// // Set the input image
if (rank == 0){
float* whole_image = malloc(sizeof(float) * (nx+2) * (ny+2));
init_image(nx, ny, nx+2, ny+2, whole_image);
for (int r = 1; r < nprocs; r++){
int send_NumOfCols = (nx / nprocs) + 2;
int send_start;
if (r < remainder){
send_start = (r * (send_NumOfCols-2)) + r;
send_NumOfCols++;
} else {
send_start = (r * (send_NumOfCols-2)) + remainder ;
}
float* send_image = malloc(sizeof(float) * send_NumOfCols * height);
for (int j = 0; j < height; ++j) {
for (int i = 0; i < send_NumOfCols; ++i) {
send_image[j + i * height] = whole_image[j + (i+send_start) * height];
}
}
MPI_Send(send_image, (send_NumOfCols) * height, MPI_FLOAT, r, 1, MPI_COMM_WORLD);
}
for (int j = 0; j < height; ++j) {
for (int i = 0; i < width; ++i) {
image[(j) + (i) * height] = whole_image[j + i * height];
}
}
} else {
int recv_NumOfCols = (nx / nprocs)+2;
if (rank < remainder){
recv_NumOfCols++;
}
MPI_Recv(image, (recv_NumOfCols) * height, MPI_FLOAT, 0, MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
}
// Call the stencil kernel
double tic = wtime();
for (int t = 0; t < niters; ++t) {
stencil(nx, ny, height, width, image, tmp_image);
swapHalos(rank, nprocs, tmp_image, width, height);
stencil(nx, ny, height, width, tmp_image, image);
swapHalos(rank, nprocs, image, width, height);
}
double toc = wtime();
if (rank == 0){
printf("------------------------------------\n");
printf(" image:%d processes:%d runtime: %lf s\n",nx,nprocs, toc - tic);
printf("------------------------------------\n");
float* outputimage = malloc(sizeof(float) * (nx + 2) * (ny + 2));
for (int r = 1; r < nprocs; r++){
//recieve section of image from each rank and put into output image
int recv_NumOfCols = nx / nprocs +2;
int recv_start;
if (r < remainder){
recv_start = r * (recv_NumOfCols-2) + r+1;
recv_NumOfCols++;
} else {
recv_start = r * (recv_NumOfCols-2) + remainder+1;
}
int recv_end = recv_start + recv_NumOfCols - 1;
float* recv_image = malloc(sizeof(float) * (recv_NumOfCols+2) * height);
MPI_Recv(recv_image, (recv_NumOfCols+2) * height, MPI_FLOAT, r,MPI_ANY_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
for (int j = 0; j < height; ++j) {
for (int i = 1; i < recv_NumOfCols-1; ++i) {
outputimage[j + (recv_start-1+i) * height] = recv_image[j + i * height];
}
}
//store in image
}
for (int j = 0; j < height; ++j) {
for (int i = 0; i < NumOfCols+1; ++i) {
outputimage[j + i * height] = image[j + i * height];
}
}
output_image(OUTPUT_FILE, nx, ny, nx+2, ny+2, outputimage);
}
else{
// send image to rank 0
MPI_Send(image, width*height, MPI_FLOAT, 0, 1, MPI_COMM_WORLD);
}
// free(image);
// free(tmp_image);
MPI_Finalize();
}
void stencil(const int nx, const int ny, const int height, const int width,
float* image, float* tmp_image)
{
for (int j = 1; j < height - 1; ++j) {
for (int i = 1; i < width - 1; ++i) {
tmp_image[j + i * height] = image[j + i * height] * 0.6f;
tmp_image[j + i * height] += image[j + (i - 1) * height] * 0.1f;
tmp_image[j + i * height] += image[j + (i + 1) * height] * 0.1f;
tmp_image[j + i * height] += image[j - 1 + i * height] * 0.1f;
tmp_image[j + i * height] += image[j + 1 + i * height] * 0.1f;
}
}
}
// Create the input image
void init_image(const int nx, const int ny, const int width, const int height,
float* image)
{
// Zero everything
for (int j = 0; j < ny + 2; ++j) {
for (int i = 0; i < nx + 2; ++i) {
image[j + i * height] = 0;
}
}
const int tile_size = 64;
// checkerboard pattern
for (int jb = 0; jb < ny; jb += tile_size) {
for (int ib = 0; ib < nx; ib += tile_size) {
if ((ib + jb) % (tile_size * 2)) {
const int jlim = (jb + tile_size > ny) ? ny : jb + tile_size;
const int ilim = (ib + tile_size > nx) ? nx : ib + tile_size;
for (int j = jb + 1; j < jlim + 1; ++j) {
for (int i = ib + 1; i < ilim + 1; ++i) {
image[j + i * height] = 100;
}
}
}
}
}
}
// Routine to output the image in Netpbm grayscale binary image format
void output_image(const char* file_name, const int nx, const int ny,
const int width, const int height, float* image)
{
// Open output file
FILE* fp = fopen(file_name, "w");
if (!fp) {
fprintf(stderr, "Error: Could not open %s\n", OUTPUT_FILE);
exit(EXIT_FAILURE);
}
// Ouptut image header
fprintf(fp, "P5 %d %d 255\n", nx, ny);
// Calculate maximum value of image
// This is used to rescale the values
// to a range of 0-255 for output
float maximum = 0;
for (int j = 1; j < ny + 1; ++j) {
for (int i = 1; i < nx + 1; ++i) {
if (image[j + i * height] > maximum) maximum = image[j + i * height];
}
}
// Output image, converting to numbers 0-255
for (int j = 1; j < ny + 1; ++j) {
for (int i = 1; i < nx + 1; ++i) {
fputc((char)(255 * image[j + i * height] / maximum), fp);
}
}
// Close the file
fclose(fp);
}
// Get the current time in seconds since the Epoch
double wtime(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec * 1e-6;
}
void swapHalos(int rank, int nprocs, float* image, int width, int height){
float* haloLeftSend = malloc(sizeof(float) * height);
float* haloLeftRecv = malloc(sizeof(float) * height);
float* haloRightSend= malloc(sizeof(float) * height);
float* haloRightRecv= malloc(sizeof(float) * height);
for (int j = 0; j < height; ++j) {
haloLeftSend[j] = image[j + height];
haloRightSend[j] = image[j + (width-2) * height];
}
int RightRank = rank + 1;
if (rank == nprocs - 1) {
RightRank = MPI_PROC_NULL;
}
else {
RightRank = rank + 1;
}
int LeftRank;
if (rank == 0){
LeftRank = MPI_PROC_NULL;
}
else{
LeftRank = rank - 1;
}
MPI_Sendrecv(haloLeftSend, height, MPI_FLOAT, LeftRank, 1, haloRightRecv, height,
MPI_FLOAT,RightRank, MPI_ANY_TAG,MPI_COMM_WORLD, MPI_STATUS_IGNORE);
MPI_Sendrecv(haloRightSend, height, MPI_FLOAT,RightRank, 1, haloLeftRecv, height,
MPI_FLOAT,LeftRank, MPI_ANY_TAG,MPI_COMM_WORLD, MPI_STATUS_IGNORE);
if (rank > 0){
for (int j = 0; j < height; ++j) {
image[j] = haloLeftRecv[j];
}
}
if (rank != nprocs-1){
for (int j = 0; j < height; ++j) {
image[j + (width-1) * height] = haloRightRecv[j];
}
}
}
void initiliaseArrays(int width, int height, float* image, float* tmp_image) {
for (int j = 0; j < height; ++j) {
for (int i = 1; i < width; ++i) {
tmp_image[j + i * height] = 0;
image[j + i * height] = 0;
}
}
}