Fully working BasicSolver
This commit is contained in:
@@ -39,9 +39,20 @@ $(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
.PHONY: clean distclean vis vis_clean tags info asm format
|
||||
|
||||
clean:
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='pressure.dat'" ./surface.plot
|
||||
@gnuplot -e "filename='velocity.dat'" ./vector.plot
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
|
||||
clean: vis_clean
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
@@ -1,10 +1,11 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= ICC
|
||||
# Supported: true, false
|
||||
ENABLE_MPI ?= true
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: rb, mg
|
||||
SOLVER ?= mg
|
||||
# Run in debug settings ?= mg
|
||||
# Supported: v1, v2, v3
|
||||
COMM_TYPE ?= v3
|
||||
|
||||
#Feature options
|
||||
|
@@ -33,14 +33,14 @@ jmax 128 # number of interior cells in y-direction
|
||||
# ---------
|
||||
|
||||
te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 2 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
@@ -48,6 +48,6 @@ postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
itermax 1000 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
omg 1.9 # relaxation parameter for SOR iteration
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
#===============================================================================
|
||||
|
9
BasicSolver/2D-mpi/residual.plot
Normal file
9
BasicSolver/2D-mpi/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
@@ -14,7 +14,7 @@ static int sum(int* sizes, int position)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = 0; i < position; i += position) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -67,6 +67,7 @@ int commIsBoundary(Comm* c, int direction)
|
||||
|
||||
void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
// printf("Rank : %d In exchange \n", c->rank);
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
@@ -174,6 +175,17 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
#ifdef _MPI
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = sizeOfRank(c->coords[JDIM], c->size, jmax);
|
||||
|
||||
c->neighbours[BOTTOM] = c->rank == 0 ? -1 : c->rank - 1;
|
||||
c->neighbours[TOP] = c->rank == (c->size - 1) ? -1 : c->rank + 1;
|
||||
c->neighbours[LEFT] = -1;
|
||||
c->neighbours[RIGHT] = -1;
|
||||
|
||||
c->coords[IDIM] = 0;
|
||||
c->coords[JDIM] = c->rank;
|
||||
|
||||
c->dims[IDIM] = 1;
|
||||
c->dims[JDIM] = c->size;
|
||||
#else
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = jmax;
|
||||
@@ -182,15 +194,33 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
Comm* newcomm;
|
||||
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
int result = MPI_Comm_dup(MPI_COMM_WORLD, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
newcomm->neighbours[BOTTOM] = newcomm->rank == 0 ? -1 : newcomm->rank - 1;
|
||||
newcomm->neighbours[TOP] = newcomm->rank == (newcomm->size - 1) ? -1 : newcomm->rank + 1;
|
||||
newcomm->neighbours[LEFT] = -1;
|
||||
newcomm->neighbours[RIGHT] = -1;
|
||||
|
||||
newcomm->coords[IDIM] = 0;
|
||||
newcomm->coords[JDIM] = newcomm->rank;
|
||||
|
||||
newcomm->dims[IDIM] = 1;
|
||||
newcomm->dims[JDIM] = newcomm->size;
|
||||
|
||||
|
||||
#endif
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
|
@@ -4,18 +4,18 @@
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "comm.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int position)
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]),
|
||||
sum(newSizesI, coords[IDIM]) };
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
@@ -290,14 +290,21 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
|
||||
{
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
memcpy(&newcomm->neighbours, &oldcomm->neighbours, sizeof(oldcomm->neighbours));
|
||||
memcpy(&newcomm->coords, &oldcomm->coords, sizeof(oldcomm->coords));
|
||||
memcpy(&newcomm->dims, &oldcomm->dims, sizeof(oldcomm->dims));
|
||||
|
||||
newcomm->imaxLocal = imaxLocal/2;
|
||||
newcomm->jmaxLocal = jmaxLocal/2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
@@ -329,7 +336,7 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -11,11 +11,11 @@
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int position)
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]),
|
||||
sum(newSizesI, coords[IDIM]) };
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
@@ -271,14 +271,18 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
|
||||
{
|
||||
#if defined _MPI
|
||||
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
@@ -310,7 +314,7 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -49,6 +49,9 @@ int main(int argc, char** argv)
|
||||
commInit(&d.comm, argc, argv);
|
||||
initParameter(&p);
|
||||
|
||||
FILE* fp;
|
||||
if (commIsMaster(&d.comm)) fp = initResidualWriter();
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
@@ -79,16 +82,21 @@ int main(int argc, char** argv)
|
||||
double tau = d.tau;
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
double res = 0.0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
while (t <= te) {
|
||||
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
setSpecialBoundaryCondition(&d);
|
||||
computeFG(&d);
|
||||
computeRHS(&d);
|
||||
solve(&s, d.p, d.rhs);
|
||||
res = solve(&s, d.p, d.rhs);
|
||||
adaptUV(&d);
|
||||
|
||||
if (commIsMaster(&d.comm)) writeResidual(fp, t, res);
|
||||
|
||||
t += d.dt;
|
||||
|
||||
#ifdef VERBOSE
|
||||
@@ -106,7 +114,7 @@ int main(int argc, char** argv)
|
||||
if (commIsMaster(s.comm)) {
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
}
|
||||
|
||||
if (commIsMaster(&d.comm)) fclose(fp);
|
||||
writeResults(&d);
|
||||
commFinalize(s.comm);
|
||||
return EXIT_SUCCESS;
|
||||
|
@@ -8,7 +8,7 @@
|
||||
#include <mpi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "progress.h"
|
||||
|
||||
static double _end;
|
||||
@@ -49,3 +49,22 @@ void stopProgress()
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
FILE* initResidualWriter()
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("residual.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fp;
|
||||
|
||||
}
|
||||
|
||||
void writeResidual(FILE* fp, double ts, double res)
|
||||
{
|
||||
fprintf(fp, "%f, %f\n", ts, res);
|
||||
}
|
||||
|
@@ -10,5 +10,6 @@
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress();
|
||||
|
||||
extern FILE* initResidualWriter(void);
|
||||
extern void writeResidual(FILE*, double, double);
|
||||
#endif
|
||||
|
@@ -14,9 +14,9 @@
|
||||
#define FINEST_LEVEL 0
|
||||
#define COARSEST_LEVEL (s->levels - 1)
|
||||
// #define S(i, j) s[(j) * (imaxLocal + 2) + (i)]
|
||||
#define E(i, j) e[(j) * (imaxLocal + 2) + (i)]
|
||||
#define R(i, j) r[(j) * (imaxLocal + 2) + (i)]
|
||||
#define OLD(i, j) old[(j) * (imaxLocal + 2) + (i)]
|
||||
#define E(i, j) e[(j) * (imaxLocal + 2) + (i)]
|
||||
#define R(i, j) r[(j) * (imaxLocal + 2) + (i)]
|
||||
#define OLD(i, j) old[(j) * (imaxLocal + 2) + (i)]
|
||||
|
||||
static void restrictMG(Solver* s, int level, Comm* comm)
|
||||
{
|
||||
@@ -109,7 +109,6 @@ static void setBoundaryCondition(Solver* s, double* p, int imaxLocal, int jmaxLo
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
static double smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
@@ -144,8 +143,7 @@ static double smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
P(i, j) -= factor *
|
||||
(RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) *
|
||||
idy2));
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
@@ -231,10 +229,10 @@ static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm
|
||||
restrictMG(s, level, comm);
|
||||
|
||||
Comm newcomm;
|
||||
commUpdateDatatypes(s->comm, &newcomm, comm->imaxLocal / 2, comm->jmaxLocal / 2);
|
||||
commUpdateDatatypes(s->comm, &newcomm, comm->imaxLocal, comm->jmaxLocal);
|
||||
|
||||
|
||||
// MGSolver on residual and error.
|
||||
// TODO: What if there is a rest?
|
||||
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm);
|
||||
|
||||
commFreeCommunicator(&newcomm);
|
||||
@@ -290,7 +288,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
}
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
double res = multiGrid(s, p, rhs, 0, s->comm);
|
||||
|
||||
@@ -299,4 +297,6 @@ void solve(Solver* s, double* p, double* rhs)
|
||||
printf("Residuum: %.6f\n", res);
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@@ -23,7 +23,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
s->comm = &d->comm;
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
@@ -101,4 +101,6 @@ void solve(Solver* s, double* p, double* rhs)
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@@ -25,5 +25,5 @@ typedef struct {
|
||||
} Solver;
|
||||
|
||||
void initSolver(Solver*, Discretization*, Parameter*);
|
||||
void solve(Solver*, double*, double*);
|
||||
double solve(Solver*, double*, double*);
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user