-
Notifications
You must be signed in to change notification settings - Fork 0
/
heat_dist_times.c
186 lines (159 loc) · 5.62 KB
/
heat_dist_times.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <omp.h>
#include "randomwalk.h"
// for alloc_matrix()
#define SUCCESS 0
#define OUT_OF_MEMORY_ERROR 1
#define CONVERGENCE_THRESHOLD 0.05
typedef double Element_type;
////////////////////////////// FUNCTION PROTOTYPES //////////////////////////////
void alloc_matrix(
int nrows,
int ncols,
size_t element_size,
Element_type **matrix_storage,
Element_type ***matrix,
int *errvalue
);
/////////////////////////////////////////// main() /////////////////////////////////////
int main(int argc, char *argv[]) {
point2d current;
int t;
int i, j;
int height = 60, width = 60;
double oldvalue;
double maxdiff, diff;
double tolerance;
int location;
bool reached;
double **plate;
double *plate_storage;
int allocate;
unsigned long long count;
double elapsed_time;
FILE *fobs;
if (argc != 2) {
fprintf(stderr, "Command-line error\nUsage: %s <number of threads>", argv[0]);
exit(-1);
}
// Initialize number of threads
t = atoi(argv[1]);
/* Allocate space for the table */
alloc_matrix(height, width, sizeof(Element_type), &plate_storage, &plate, &allocate);
if (allocate != SUCCESS) {
fprintf(stderr, "Couldn't allocate memory for the plate!\n");
exit(-1);
}
elapsed_time -= omp_get_wtime();
/* Initialize some example boundary temperatures */
// Here, North and South are 0 degrees, and East and West are 100 degrees, respectively.
double boundary_temp[4] = {0, 100, 0, 100};
/*
Initialize temperatures at the four corners as the average
of the temperatures of the adjacent stages.
*/
plate[0][0] = (boundary_temp[0] + boundary_temp[3]) / 2;
plate[0][width-1] = (boundary_temp[0] + boundary_temp[1]) / 2;
plate[height-1][0] = (boundary_temp[3] + boundary_temp[2]) / 2;
plate[height-1][width-1] = (boundary_temp[2] + boundary_temp[1]) / 2;
/* Initialize the temperatures along the edges of the plate */
# pragma omp parallel for num_threads(t)
for (j = 1; j < width - 1; j++) {
plate[0][j] = boundary_temp[0];
plate[height-1][j] = boundary_temp[2];
}
# pragma omp parallel for num_threads(t)
for (i = 1; i < height - 1; i++) {
plate[i][0] = boundary_temp[3];
plate[i][width-1] = boundary_temp[2];
}
/* Initialize the interior temperatures to 0. */
# pragma omp parallel for num_threads(t)
for (i = 1; i < height - 1; i++)
for (j = 1; j < width - 1; j++)
plate[i][j] = 0.0;
/* Set the acceptable tolerance to a small value and start an iteration counter. */
tolerance = CONVERGENCE_THRESHOLD;
count = 0;
reached = false;
#pragma omp parallel shared(tolerance, reached)\
private(maxdiff, diff, current, oldvalue, location)\
reduction(+:count)\
num_threads(t)
{
while (reached != true) {
// reset maximum difference to 0 at the beginning of each iteration
maxdiff = 0;
// for each interior grid point [i, j]
#pragma omp for
for (int i = 1; i < height - 1; i++) {
for (int j = 1; j < width - 1; j++) {
// do a random walk until a boundary is reached
current.x = j;
current.y = i;
while (0 == (location = on_boundary(current, width, height))) {
current = next_point(current, next_dir());
}
/*
Get difference between old average at this point and average with the
new boundary point factored into it.
*/
oldvalue = plate[i][j];
plate[i][j] = (oldvalue*count + boundary_temp[location-1]) / (count + 1);
diff = fabs(plate[i][j] - oldvalue);
/*
If the difference at this point is the largest so far in this iteration,
then update maxdiff.
*/
if (diff > maxdiff)
maxdiff = diff;
}
}
if (maxdiff < tolerance)
reached = true;
count++;
}
}
// record the observations (elapsed time) in a file
elapsed_time += omp_get_wtime();
fobs = fopen("Observations.txt", "a");
fprintf(fobs, "%d\t%d\t%lf\n", height, t, elapsed_time);
fclose(fobs);
}
/////////////////////////////////////////// FUNCTIONS ///////////////////////////////////////
void alloc_matrix(
// function parameters
int nrows,
int ncols,
size_t element_size,
Element_type **matrix_storage,
Element_type ***matrix,
int *errval
)
{
void *ptr_to_row_in_storage;
void **matrix_row_start;
*matrix_storage = malloc(nrows * ncols * element_size);
if (*matrix_storage == NULL) {
*errval = OUT_OF_MEMORY_ERROR;
return;
}
*matrix = malloc (nrows * sizeof(void *));
if (*matrix == NULL) {
*errval = OUT_OF_MEMORY_ERROR;
return;
}
matrix_row_start = (void *) &(*matrix[0]);
ptr_to_row_in_storage = (void *) *matrix_storage;
for (int i = 0; i < nrows; i++ ) {
*matrix_row_start = (void *) ptr_to_row_in_storage;
matrix_row_start++;
ptr_to_row_in_storage += ncols * element_size;
}
*errval = SUCCESS;
}