This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlaplace2d_v2.cpp
More file actions
182 lines (146 loc) · 6.56 KB
/
laplace2d_v2.cpp
File metadata and controls
182 lines (146 loc) · 6.56 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
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
/* Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <omp.h>
#include <stdio.h>
#include <mpi.h>
#include <mpi_navier.h>
#define UP 0
#define DOWN 1
#define LEFT 2
#define RIGHT 3
int main(int argc, char** argv)
{
//Size along y
int jmax = 4094;
//Size along x
int imax = 4094;
//TODO initialize MPI
MPI_Init(&argc,&argv);
mpi_setup(&imax, &jmax);
int iter_max = 1000;
const double pi = 2.0 * asin(1.0);
const double tol = 1.0e-5;
double error = 1.0;
double *A;
double *Anew;
double *y0;
A = (double *)malloc((imax+2) * (jmax+2) * sizeof(double));
Anew = (double *)malloc((imax+2) * (jmax+2) * sizeof(double));
y0 = (double *)malloc((jmax+2) * sizeof(double));
memset(A, 0, (imax+2) * (jmax+2) * sizeof(double));
// set boundary conditions
//TODO: this loop should only done by processes who have no previous neighbour in the y direction (no up neighbour)
if(prev_y == MPI_PROC_NULL){
for (int i = 0; i < imax+2; i++)
A[(0)*(imax+2)+i] = 0.0;
}
//TODO: this loop should only done by processes who have no next neighbour in the y direction (no down neighbour)
if(next_y == MPI_PROC_NULL){
for (int i = 0; i < imax+2; i++)
A[(jmax+1)*(imax+2)+i] = 0.0;
}
//TODO: this loop should only done by processes who have no previous neighbour in the x direction (no left neighbour)
if(prev_x == MPI_PROC_NULL){
for (int j = 0; j < jmax+2; j++)
{
y0[j] = sin(pi * (j+gbl_j_begin) / (jmax_full+1)); //TODO: within sin(), j is a global index and jmax is the global size
//need to offset j by the beginning j index of this partition, and use the full jmax size
A[(j)*(imax+2)+0] = y0[j];
}
}
//TODO: this loop should only done by processes who have no next neighbour in the x direction (no right neighbour)
if(next_x == MPI_PROC_NULL){
for (int j = 0; j < imax+2; j++)
{
y0[j] = sin(pi * (j*gbl_j_begin)/ (jmax_full +1)); //TODO: within sin(), j is a global index and jmax is the global size
//need to offset j by the beginning j index of this partition, and use the full jmax size
A[(j)*(imax+2)+imax+1] = y0[j]*exp(-pi);
}
}
//TODO: Need to set A to dirty
//TODO: Only process 0 should print this, and with the full imax and jmax sizes
if (my_rank == 0)
printf("Jacobi r elaxation Calculation: %d x %d mesh\n", imax_full+2, jmax_full+2);
double t1 = omp_get_wtime();
int iter = 0;
//TODO: this loop should only done by processes who have no previous neighbour in the y direction
if(prev_y == MPI_PROC_NULL)
for (int i = 1; i < imax+2; i++)
Anew[(0)*(imax+2)+i] = 0.0;
//TODO: this loop should only done by processes who have no next neighbour in the y direction
if(next_y == MPI_PROC_NULL)
for (int i = 1; i < imax+2; i++)
Anew[(jmax+1)*(imax+2)+i] = 0.0;
//TODO: this loop should only done by processes who have no previous neighbour in the x direction
if(prev_x == MPI_PROC_NULL)
for (int j = 1; j < jmax+2; j++)
Anew[(j)*(imax+2)+0] = y0[j];
//TODO: this loop should only done by processes who have no next neighbour in the x direction
if(next_x == MPI_PROC_NULL)
for (int j = 1; j < jmax+2; j++)
Anew[(j)*(imax+2)+imax+1] = y0[j]*expf(-pi);
//TODO: Need to set Anew to dirty
while ( error > tol && iter < iter_max )
{
error = 0.0;
//TODO: need to make sure stencil accesses to A read correct data
exchange_halo(imax,jmax,A);
#pragma omp parallel for reduction(max:error)
for( int j = 1; j < jmax+1; j++ )
{
for( int i = 1; i < imax+1; i++)
{
Anew[(j)*(imax+2)+i] = 0.25f * ( A[(j)*(imax+2)+i+1] + A[(j)*(imax+2)+i-1]
+ A[(j-1)*(imax+2)+i] + A[(j+1)*(imax+2)+i]);
error = fmax( error, fabs(Anew[(j)*(imax+2)+i]-A[(j)*(imax+2)+i]));
}
}
//TODO: need global reduction for error
double error_global = 0;
MPI_Allreduce(&error, &error_global, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
error = error_global;
//No stencil accesses to Anew, no halo exchange necessary
for( int j = 1; j < jmax+1; j++ )
{
for( int i = 1; i < imax+1; i++)
{
A[(j)*(imax+2)+i] = Anew[(j)*(imax+2)+i];
}
}
//TODO: Only process 0 should print this
if(iter % 100 == 0 && my_rank == 0) printf("%5d, %0.6f\n", iter, error);
iter++;
}
double runtime = omp_get_wtime()-t1;
printf(" total: %f s\n", runtime);
//TODO finalize MPI
MPI_Finalize();
return 0;
}