-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstencilBackup.c
175 lines (147 loc) · 4.74 KB
/
stencilBackup.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
#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,
float* image, float* tmp_image);
void init_image(const int nx, const int ny, const int width, const int height,
float* image, float* tmp_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);
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);
printf("Rank %d \n", 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 NumOfRows = ny / nprocs;
int remainder = ny % nprocs;
if (rank < remainder) {
NumOfRows++;
}
printf("Number of rows %d \n",NumOfRows);
int start = rank * NumOfRows;
int end = start + NumOfRows + 2;
// we pad the outer edge of the image to avoid out of range address issues in
// stencil
int width = nx + 2;
int height = ny + 2;
// Allocate the image
float* image = malloc(sizeof(float) * width * height);
float* tmp_image = malloc(sizeof(float) * width * height);
// Set the input image
init_image(nx, ny, width, height, image, tmp_image);
// Call the stencil kernel
MPI_Barrier(MPI_COMM_WORLD);
double tic = wtime();
for (int t = 0; t < niters; ++t) {
stencil(nx, ny, height, image, tmp_image);
stencil(nx, ny, height, tmp_image, image);
}
MPI_Barrier(MPI_COMM_WORLD);
double toc = wtime();
// Output
if (rank == 0){
printf("------------------------------------\n");
printf(" runtime: %lf s\n", toc - tic);
printf("------------------------------------\n");
}
output_image(OUTPUT_FILE, nx, ny, width, height, image);
free(image);
free(tmp_image);
MPI_Finalize();
}
void stencil(const int nx, const int ny, const int height,
float* image, float* tmp_image)
{
float tenth = 0.1;
float threeFifths = 0.6;
for (int j = 1; j < ny + 1; ++j) {
for (int i = 1; i < nx + 1; ++i) {
tmp_image[j + i * height] = image[j + i * height] * threeFifths;
tmp_image[j + i * height] += image[j + (i - 1) * height] * tenth;
tmp_image[j + i * height] += image[j + (i + 1) * height] * tenth;
tmp_image[j + i * height] += image[j - 1 + i * height] * tenth;
tmp_image[j + i * height] += image[j + 1 + i * height] * tenth;
}
}
}
// Create the input image
void init_image(const int nx, const int ny, const int width, const int height,
float* image, float* tmp_image)
{
// Zero everything
for (int j = 0; j < ny + 2; ++j) {
for (int i = 0; i < nx + 2; ++i) {
image[j + i * height] = 0;
tmp_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;
}