Initial checkin
This commit is contained in:
63
PoissonSolver/2D-mpi/src/affinity.c
Normal file
63
PoissonSolver/2D-mpi/src/affinity.c
Normal file
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifdef __linux__
|
||||
#ifdef _OPENMP
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sched.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/syscall.h>
|
||||
|
||||
#define MAX_NUM_THREADS 128
|
||||
#define gettid() syscall(SYS_gettid)
|
||||
|
||||
static int getProcessorID(cpu_set_t* cpu_set)
|
||||
{
|
||||
int processorId;
|
||||
|
||||
for ( processorId = 0; processorId < MAX_NUM_THREADS; processorId++ )
|
||||
{
|
||||
if ( CPU_ISSET(processorId,cpu_set) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
return processorId;
|
||||
}
|
||||
|
||||
int affinity_getProcessorId()
|
||||
{
|
||||
cpu_set_t cpu_set;
|
||||
CPU_ZERO(&cpu_set);
|
||||
sched_getaffinity(gettid(),sizeof(cpu_set_t), &cpu_set);
|
||||
|
||||
return getProcessorID(&cpu_set);
|
||||
}
|
||||
|
||||
void affinity_pinThread(int processorId)
|
||||
{
|
||||
cpu_set_t cpuset;
|
||||
pthread_t thread;
|
||||
|
||||
thread = pthread_self();
|
||||
CPU_ZERO(&cpuset);
|
||||
CPU_SET(processorId, &cpuset);
|
||||
pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
|
||||
}
|
||||
|
||||
void affinity_pinProcess(int processorId)
|
||||
{
|
||||
cpu_set_t cpuset;
|
||||
|
||||
CPU_ZERO(&cpuset);
|
||||
CPU_SET(processorId, &cpuset);
|
||||
sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
|
||||
}
|
||||
#endif /*_OPENMP*/
|
||||
#endif /*__linux__*/
|
14
PoissonSolver/2D-mpi/src/affinity.h
Normal file
14
PoissonSolver/2D-mpi/src/affinity.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef AFFINITY_H
|
||||
#define AFFINITY_H
|
||||
|
||||
extern int affinity_getProcessorId();
|
||||
extern void affinity_pinProcess(int);
|
||||
extern void affinity_pinThread(int);
|
||||
|
||||
#endif /*AFFINITY_H*/
|
37
PoissonSolver/2D-mpi/src/allocate.c
Normal file
37
PoissonSolver/2D-mpi/src/allocate.c
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
void* allocate (int alignment, size_t bytesize)
|
||||
{
|
||||
int errorCode;
|
||||
void* ptr;
|
||||
|
||||
errorCode = posix_memalign(&ptr, alignment, bytesize);
|
||||
|
||||
if (errorCode) {
|
||||
if (errorCode == EINVAL) {
|
||||
fprintf(stderr,
|
||||
"Error: Alignment parameter is not a power of two\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (errorCode == ENOMEM) {
|
||||
fprintf(stderr,
|
||||
"Error: Insufficient memory to fulfill the request\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (ptr == NULL) {
|
||||
fprintf(stderr, "Error: posix_memalign failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
13
PoissonSolver/2D-mpi/src/allocate.h
Normal file
13
PoissonSolver/2D-mpi/src/allocate.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __ALLOCATE_H_
|
||||
#define __ALLOCATE_H_
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void* allocate (int alignment, size_t bytesize);
|
||||
|
||||
#endif
|
53
PoissonSolver/2D-mpi/src/likwid-marker.h
Normal file
53
PoissonSolver/2D-mpi/src/likwid-marker.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* =======================================================================================
|
||||
*
|
||||
* Author: Jan Eitzinger (je), jan.eitzinger@fau.de
|
||||
* Copyright (c) 2020 RRZE, University Erlangen-Nuremberg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*
|
||||
* =======================================================================================
|
||||
*/
|
||||
#ifndef LIKWID_MARKERS_H
|
||||
#define LIKWID_MARKERS_H
|
||||
|
||||
#ifdef LIKWID_PERFMON
|
||||
#include <likwid.h>
|
||||
#define LIKWID_MARKER_INIT likwid_markerInit()
|
||||
#define LIKWID_MARKER_THREADINIT likwid_markerThreadInit()
|
||||
#define LIKWID_MARKER_SWITCH likwid_markerNextGroup()
|
||||
#define LIKWID_MARKER_REGISTER(regionTag) likwid_markerRegisterRegion(regionTag)
|
||||
#define LIKWID_MARKER_START(regionTag) likwid_markerStartRegion(regionTag)
|
||||
#define LIKWID_MARKER_STOP(regionTag) likwid_markerStopRegion(regionTag)
|
||||
#define LIKWID_MARKER_CLOSE likwid_markerClose()
|
||||
#define LIKWID_MARKER_RESET(regionTag) likwid_markerResetRegion(regionTag)
|
||||
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count) likwid_markerGetRegion(regionTag, nevents, events, time, count)
|
||||
#else /* LIKWID_PERFMON */
|
||||
#define LIKWID_MARKER_INIT
|
||||
#define LIKWID_MARKER_THREADINIT
|
||||
#define LIKWID_MARKER_SWITCH
|
||||
#define LIKWID_MARKER_REGISTER(regionTag)
|
||||
#define LIKWID_MARKER_START(regionTag)
|
||||
#define LIKWID_MARKER_STOP(regionTag)
|
||||
#define LIKWID_MARKER_CLOSE
|
||||
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count)
|
||||
#define LIKWID_MARKER_RESET(regionTag)
|
||||
#endif /* LIKWID_PERFMON */
|
||||
|
||||
#endif /*LIKWID_MARKERS_H*/
|
44
PoissonSolver/2D-mpi/src/main.c
Normal file
44
PoissonSolver/2D-mpi/src/main.c
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
#include <mpi.h>
|
||||
|
||||
#include "parameter.h"
|
||||
#include "solver.h"
|
||||
|
||||
|
||||
int main (int argc, char** argv)
|
||||
{
|
||||
int rank;
|
||||
Parameter params;
|
||||
Solver solver;
|
||||
|
||||
MPI_Init(&argc, &argv);
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
|
||||
initParameter(¶ms);
|
||||
|
||||
if ( argc != 2 ) {
|
||||
printf("Usage: %s <configFile>\n",argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
readParameter(¶ms, argv[1]);
|
||||
if ( rank == 0 ) {
|
||||
printParameter(¶ms);
|
||||
}
|
||||
|
||||
initSolver(&solver, ¶ms, 2);
|
||||
solve(&solver);
|
||||
getResult(&solver);
|
||||
|
||||
MPI_Finalize();
|
||||
return EXIT_SUCCESS;
|
||||
}
|
72
PoissonSolver/2D-mpi/src/parameter.c
Normal file
72
PoissonSolver/2D-mpi/src/parameter.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
//---
|
||||
#include "parameter.h"
|
||||
#include "util.h"
|
||||
#define MAXLINE 4096
|
||||
|
||||
void initParameter(Parameter *param) {
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.8;
|
||||
}
|
||||
|
||||
void readParameter(Parameter *param, const char *filename) {
|
||||
FILE *fp = fopen(filename, "r");
|
||||
char line[MAXLINE];
|
||||
int i;
|
||||
|
||||
if(!fp) {
|
||||
fprintf(stderr, "Could not open parameter file: %s\n", filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while(!feof(fp)) {
|
||||
line[0] = '\0';
|
||||
fgets(line, MAXLINE, fp);
|
||||
for(i = 0; line[i] != '\0' && line[i] != '#'; i++);
|
||||
line[i] = '\0';
|
||||
|
||||
char *tok = strtok(line, " ");
|
||||
char *val = strtok(NULL, " ");
|
||||
|
||||
#define PARSE_PARAM(p,f) if(strncmp(tok, #p, sizeof(#p) / sizeof(#p[0]) - 1) == 0) { param->p = f(val); }
|
||||
#define PARSE_STRING(p) PARSE_PARAM(p, strdup)
|
||||
#define PARSE_INT(p) PARSE_PARAM(p, atoi)
|
||||
#define PARSE_REAL(p) PARSE_PARAM(p, atof)
|
||||
|
||||
if(tok != NULL && val != NULL) {
|
||||
PARSE_REAL(xlength);
|
||||
PARSE_REAL(ylength);
|
||||
PARSE_INT(imax);
|
||||
PARSE_INT(jmax);
|
||||
PARSE_INT(itermax);
|
||||
PARSE_REAL(eps);
|
||||
PARSE_REAL(omg);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void printParameter(Parameter *param) {
|
||||
printf("Parameters:\n");
|
||||
printf("Geometry data:\n");
|
||||
printf("\tDomain box size (x, y): %e, %e\n", param->xlength, param->ylength);
|
||||
printf("\tCells (x, y): %d, %d\n", param->imax, param->jmax);
|
||||
printf("Iterative solver parameters:\n");
|
||||
printf("\tMax iterations: %d\n", param->itermax);
|
||||
printf("\tepsilon (stopping tolerance) : %e\n", param->eps);
|
||||
printf("\tomega (SOR relaxation): %e\n", param->omg);
|
||||
}
|
20
PoissonSolver/2D-mpi/src/parameter.h
Normal file
20
PoissonSolver/2D-mpi/src/parameter.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __PARAMETER_H_
|
||||
#define __PARAMETER_H_
|
||||
|
||||
typedef struct {
|
||||
double xlength, ylength;
|
||||
int imax, jmax;
|
||||
int itermax;
|
||||
double eps, omg, gamma;
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
void readParameter(Parameter*, const char*);
|
||||
void printParameter(Parameter*);
|
||||
#endif
|
278
PoissonSolver/2D-mpi/src/solver.c
Normal file
278
PoissonSolver/2D-mpi/src/solver.c
Normal file
@@ -0,0 +1,278 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <mpi.h>
|
||||
|
||||
#include "solver.h"
|
||||
#include "parameter.h"
|
||||
#include "allocate.h"
|
||||
|
||||
#define PI 3.14159265358979323846
|
||||
#define P(i,j) p[(j)*(imax+2) + (i)]
|
||||
#define RHS(i,j) rhs[(j)*(imax+2) + (i)]
|
||||
|
||||
static int sizeOfRank(int rank, int size, int N)
|
||||
{
|
||||
return N/size + ((N%size>rank) ? 1 : 0);
|
||||
}
|
||||
|
||||
static void print(Solver* solver)
|
||||
{
|
||||
double* p = solver->p;
|
||||
int imax = solver->imax;
|
||||
|
||||
printf("### RANK %d #######################################################\n", solver->rank);
|
||||
for( int j=0; j < solver->jmaxLocal+2; j++ ) {
|
||||
printf("%02d: ", j);
|
||||
for( int i=0; i < solver->imax+2; i++ ) {
|
||||
printf("%12.8f ", P(i, j));
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
fflush( stdout );
|
||||
}
|
||||
|
||||
static void exchange(Solver* solver)
|
||||
{
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL, MPI_REQUEST_NULL, MPI_REQUEST_NULL, MPI_REQUEST_NULL };
|
||||
|
||||
/* exchange ghost cells with top neighbor */
|
||||
if (solver->rank + 1 < solver->size) {
|
||||
int top = solver->rank + 1;
|
||||
double* src = solver->p + (solver->jmaxLocal) * (solver->imax+2) + 1;
|
||||
double* dst = solver->p + (solver->jmaxLocal+1) * (solver->imax+2) + 1;
|
||||
|
||||
MPI_Isend(src, solver->imax, MPI_DOUBLE, top, 1, MPI_COMM_WORLD, &requests[0]);
|
||||
MPI_Irecv(dst, solver->imax, MPI_DOUBLE, top, 2, MPI_COMM_WORLD, &requests[1]);
|
||||
}
|
||||
|
||||
/* exchange ghost cells with bottom neighbor */
|
||||
if (solver->rank > 0) {
|
||||
int bottom = solver->rank - 1;
|
||||
double* src = solver->p + (solver->imax+2) + 1;
|
||||
double* dst = solver->p + 1;
|
||||
|
||||
MPI_Isend(src, solver->imax, MPI_DOUBLE, bottom, 2, MPI_COMM_WORLD, &requests[2]);
|
||||
MPI_Irecv(dst, solver->imax, MPI_DOUBLE, bottom, 1, MPI_COMM_WORLD, &requests[3]);
|
||||
}
|
||||
|
||||
MPI_Waitall(4, requests, MPI_STATUSES_IGNORE);
|
||||
}
|
||||
|
||||
void getResult(Solver *solver)
|
||||
{
|
||||
double* Pall = NULL;
|
||||
int *rcvCounts, *displs;
|
||||
|
||||
if ( solver->rank == 0 ) {
|
||||
Pall = allocate(64, (solver->imax+2) * (solver->jmax+2) * sizeof(double));
|
||||
rcvCounts = (int*) malloc(solver->size * sizeof(int));
|
||||
displs = (int*) malloc(solver->size * sizeof(int));
|
||||
rcvCounts[0] = solver->jmaxLocal * (solver->imax+2);
|
||||
displs[0] = 0;
|
||||
int cursor = rcvCounts[0];
|
||||
|
||||
for ( int i=1; i < solver->size; i++ ) {
|
||||
rcvCounts[i] = sizeOfRank(i, solver->size, solver->jmax) * (solver->imax+2);
|
||||
displs[i] = cursor;
|
||||
cursor += rcvCounts[i];
|
||||
}
|
||||
}
|
||||
|
||||
int cnt = solver->jmaxLocal*(solver->imax+2);
|
||||
double* sendbuffer = solver->p + (solver->imax+2);
|
||||
MPI_Gatherv(sendbuffer, cnt, MPI_DOUBLE, Pall,
|
||||
rcvCounts, displs, MPI_DOUBLE, 0, MPI_COMM_WORLD);
|
||||
|
||||
if ( solver->rank == 0 ) {
|
||||
writeResult(solver, Pall, "p.dat");
|
||||
}
|
||||
}
|
||||
|
||||
void initSolver(Solver *solver, Parameter *params, int problem)
|
||||
{
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &(solver->rank));
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &(solver->size));
|
||||
solver->imax = params->imax;
|
||||
solver->jmax = params->jmax;
|
||||
solver->jmaxLocal = sizeOfRank(solver->rank, solver->size, solver->jmax);
|
||||
printf("RANK %d: %d\n", solver->rank, solver->jmaxLocal);
|
||||
|
||||
solver->dx = params->xlength/params->imax;
|
||||
solver->dy = params->ylength/params->jmax;
|
||||
solver->ys = solver->rank * solver->jmaxLocal * solver->dy;
|
||||
solver->eps = params->eps;
|
||||
solver->omega = params->omg;
|
||||
solver->itermax = params->itermax;
|
||||
|
||||
int imax = solver->imax;
|
||||
int jmax = solver->jmax;
|
||||
int jmaxLocal = solver->jmaxLocal;
|
||||
solver->p = allocate(64, (imax+2) * (jmaxLocal+2) * sizeof(double));
|
||||
solver->rhs = allocate(64, (imax+2) * (jmax+2) * sizeof(double));
|
||||
|
||||
double dx = solver->dx;
|
||||
double dy = solver->dy;
|
||||
double* p = solver->p;
|
||||
double* rhs = solver->rhs;
|
||||
|
||||
for( int j=0; j<jmaxLocal+2; j++ ) {
|
||||
double y = solver->ys + j * dy;
|
||||
for( int i=0; i<imax+2; i++ ) {
|
||||
P(i,j) = sin(4.0*PI*i*dx)+sin(4.0*PI*y);
|
||||
}
|
||||
}
|
||||
|
||||
if(problem == 2) {
|
||||
for( int j=0; j<jmax+2; j++ ) {
|
||||
for( int i=0; i<imax+2; i++ ) {
|
||||
RHS(i,j) = sin(2.0*PI*i*dx);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for( int j=0; j<jmax+2; j++ ) {
|
||||
for( int i=0; i<imax+2; i++ ) {
|
||||
RHS(i,j) = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void debug(Solver *solver)
|
||||
{
|
||||
int imax = solver->imax;
|
||||
int rank = solver->rank;
|
||||
double* p = solver->p;
|
||||
|
||||
/* for( int j=0; j < solver->jmaxLocal+2; j++ ) { */
|
||||
/* for( int i=0; i < solver->imax+2; i++ ) { */
|
||||
/* P(i, j) = (double) rank; */
|
||||
/* } */
|
||||
/* } */
|
||||
|
||||
/* for ( int i=0; i < solver->size; i++) { */
|
||||
/* if ( i == rank ) { */
|
||||
/* print(solver); */
|
||||
/* } */
|
||||
/* MPI_Barrier(MPI_COMM_WORLD); */
|
||||
/* } */
|
||||
|
||||
/* if ( rank == 0 ) { */
|
||||
/* printf("##########################################################\n"); */
|
||||
/* printf("## Exchange ghost layers\n"); */
|
||||
/* printf("##########################################################\n"); */
|
||||
/* } */
|
||||
/* exchange(solver); */
|
||||
|
||||
for ( int i=0; i < solver->size; i++) {
|
||||
if ( i == rank ) {
|
||||
print(solver);
|
||||
}
|
||||
MPI_Barrier(MPI_COMM_WORLD);
|
||||
}
|
||||
}
|
||||
|
||||
int solve(Solver *solver)
|
||||
{
|
||||
double r;
|
||||
int it = 0;
|
||||
double res;
|
||||
|
||||
int imax = solver->imax;
|
||||
int jmax = solver->jmax;
|
||||
int jmaxLocal = solver->jmaxLocal;
|
||||
double eps= solver->eps;
|
||||
double omega = solver->omega;
|
||||
int itermax = solver->itermax;
|
||||
|
||||
double dx2 = solver->dx * solver->dx;
|
||||
double dy2 = solver->dy * solver->dy;
|
||||
double idx2 = 1.0/dx2;
|
||||
double idy2 = 1.0/dy2;
|
||||
double factor = omega * 0.5 * (dx2*dy2) / (dx2+dy2);
|
||||
double* p = solver->p;
|
||||
double* rhs = solver->rhs;
|
||||
|
||||
res = eps + 1.0;
|
||||
|
||||
while((res >= eps) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
exchange(solver);
|
||||
|
||||
for( int j=1; j<jmaxLocal+1; j++ ) {
|
||||
for( int i=1; i<imax+1; i++ ) {
|
||||
|
||||
r = 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) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
}
|
||||
|
||||
if ( solver->rank == 0 ) {
|
||||
for( int i=1; i<imax+1; i++ ) {
|
||||
P(i,0) = P(i,1);
|
||||
}
|
||||
}
|
||||
|
||||
if ( solver->rank == (solver->size-1) ) {
|
||||
for( int i=1; i<imax+1; i++ ) {
|
||||
P(i,jmaxLocal+1) = P(i,jmaxLocal);
|
||||
}
|
||||
}
|
||||
|
||||
for( int j=1; j<jmaxLocal+1; j++ ) {
|
||||
P(0,j) = P(1,j);
|
||||
P(imax+1, j) = P(imax, j);
|
||||
}
|
||||
|
||||
MPI_Allreduce(&res, &res, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
|
||||
res = sqrt(res / (imax*jmax));
|
||||
#ifdef DEBUG
|
||||
if ( solver->rank == 0 ) {
|
||||
printf("%d Residuum: %e\n",it, res);
|
||||
}
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
if ( solver->rank == 0 ) {
|
||||
printf("Solver took %d iterations\n",it);
|
||||
}
|
||||
if( res < eps ){
|
||||
return 1;
|
||||
} else{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void writeResult(Solver* solver, double* m, char* filename)
|
||||
{
|
||||
int imax = solver->imax;
|
||||
int jmax = solver->jmax;
|
||||
double* p = solver->p;
|
||||
|
||||
FILE *fp;
|
||||
fp= fopen(filename, "w");
|
||||
|
||||
if (fp== NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for( int j=0; j<jmax+2; j++ ) {
|
||||
for( int i=0; i<imax+2; i++ ) {
|
||||
fprintf(fp, "%f ", m[j*(imax+2) + i]);
|
||||
}
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
29
PoissonSolver/2D-mpi/src/solver.h
Normal file
29
PoissonSolver/2D-mpi/src/solver.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __SOLVER_H_
|
||||
#define __SOLVER_H_
|
||||
#include "parameter.h"
|
||||
|
||||
typedef struct {
|
||||
double dx, dy;
|
||||
double ys;
|
||||
int imax, jmax;
|
||||
int jmaxLocal;
|
||||
int rank;
|
||||
int size;
|
||||
|
||||
double *p, *rhs;
|
||||
double eps, omega;
|
||||
int itermax;
|
||||
} Solver;
|
||||
|
||||
extern void debug(Solver*);
|
||||
extern void initSolver(Solver*, Parameter*, int problem);
|
||||
extern void getResult(Solver*);
|
||||
extern void writeResult(Solver*, double*, char*);
|
||||
extern int solve(Solver*);
|
||||
#endif
|
27
PoissonSolver/2D-mpi/src/timing.c
Normal file
27
PoissonSolver/2D-mpi/src/timing.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
double getTimeStamp()
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
|
||||
}
|
||||
|
||||
double getTimeResolution()
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_getres(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
|
||||
}
|
||||
|
||||
double getTimeStamp_()
|
||||
{
|
||||
return getTimeStamp();
|
||||
}
|
14
PoissonSolver/2D-mpi/src/timing.h
Normal file
14
PoissonSolver/2D-mpi/src/timing.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __TIMING_H_
|
||||
#define __TIMING_H_
|
||||
|
||||
extern double getTimeStamp();
|
||||
extern double getTimeResolution();
|
||||
extern double getTimeStamp_();
|
||||
|
||||
#endif // __TIMING_H_
|
21
PoissonSolver/2D-mpi/src/util.h
Normal file
21
PoissonSolver/2D-mpi/src/util.h
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __UTIL_H_
|
||||
#define __UTIL_H_
|
||||
#define HLINE "----------------------------------------------------------------------------\n"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(x,y) ((x)<(y)?(x):(y))
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(x,y) ((x)>(y)?(x):(y))
|
||||
#endif
|
||||
#ifndef ABS
|
||||
#define ABS(a) ((a) >= 0 ? (a) : -(a))
|
||||
#endif
|
||||
|
||||
#endif // __UTIL_H_
|
Reference in New Issue
Block a user