Commit d0329454 authored by ursular's avatar ursular
Browse files

added ex10

parent 03a14425
#ifndef ARRAYOFPARTICLE_H
#define ARRAYOFPARTICLE_H
#include "common.h"
class ArrayOfParticles
{
public:
int Np;
double * x;
double * y;
double * u;
double * v;
double * gamma;
ArrayOfParticles(int Np) : Np(Np)
{
x = new double[Np];
y = new double[Np];
u = new double[Np];
v = new double[Np];
gamma = new double[Np];
for (int i=0; i<Np; i++)
{
x[i] = 0.;
y[i] = 0.;
u[i] = 0.;
v[i] = 0.;
gamma[i] = 0.;
}
}
~ArrayOfParticles()
{
delete [] x;
delete [] y;
delete [] u;
delete [] v;
delete [] gamma;
}
ArrayOfParticles& operator=(ArrayOfParticles &p)
{
Np = p.Np;
for (int i=0; i<Np; i++)
{
x[i] = p.x[i];
y[i] = p.y[i];
u[i] = p.u[i];
v[i] = p.v[i];
gamma[i] = p.gamma[i];
}
return *this;
}
void AdvectEuler(double dt)
{
/* TODO 10.d: update of particle positions based on forward Euler scheme */
}
void ClearVelocities()
{
for (int i=0; i<Np; i++)
{
u[i] = 0.;
v[i] = 0.;
}
}
};
#endif /* ARRAYOFPARTICLE_H */
# TODO 10.b: DEFINE TOOLS
CC =
.PHONY: all
all: sheet
sheet: mainSheet.o VelocitySolverNSquared.o
# TODO 10.b: FILL IN COMPILE COMMAND HERE
%.o: %.cpp
$(CC) -c $^ -o $@
.PHONY: clean
clean:
rm -f *.o
rm -f sheet
#include "VelocitySolverNSquared.h"
VelocitySolverNSquared::VelocitySolverNSquared(ArrayOfParticles & dstParticles, ArrayOfParticles & srcParticles, const int rank, const int size)
: dstParticles(dstParticles), srcParticles(srcParticles), rank(rank), size(size), timeC(0), timeT(0)
{
}
void VelocitySolverNSquared::ComputeVelocity()
{
const double i2pi = 0.5/M_PI;
const int N = dstParticles.Np;
for (int pass=0; pass<size; pass++)
{
// 1. exchange
Timer timerT;
timerT.start();
/* TODO 10.e: exchange particles */
timerT.stop();
timeT += timerT.get_timing();
// 2. compute local
Timer timerC;
timerC.start();
/* TODO 10.e: compute induced velocities by received particles at owned particle positions */
timerC.stop();
timeC += timerC.get_timing();
}
}
#ifndef VELOCITYSOLVERNSQUARED_H
#define VELOCITYSOLVERNSQUARED_H
#include "common.h"
#include "ArrayOfParticles.h"
class VelocitySolverNSquared
{
private:
ArrayOfParticles & dstParticles;
ArrayOfParticles & srcParticles;
const int rank, size;
public:
VelocitySolverNSquared(ArrayOfParticles & dstParticles, ArrayOfParticles & srcParticles,const int rank,const int size);
~VelocitySolverNSquared(){}
void ComputeVelocity();
double timeC, timeT;
};
#endif /* VELOCITYSOLVERNSQUARED_H */
#ifndef COMMON_H
#define COMMON_H
#include <iostream>
#include <vector>
#include <cmath>
#include <mpi.h>
#include "timer.hpp"
#endif /* COMMON_H */
#include <fstream>
#include <sstream>
#include <iomanip>
#include "common.h"
#include "ArrayOfParticles.h"
#include "VelocitySolverNSquared.h"
using namespace std;
void WriteTimeToFile(const int Nranks, const double time, const char * sFileName)
{
ofstream outfile;
outfile.open(sFileName, ios::out | ios::app);
outfile << Nranks << " " << scientific << setprecision(6) << time << "\n";
outfile.close();
}
void Dump(ArrayOfParticles& dstParticles, ArrayOfParticles& allParticles, const int Np, const int NpProcess, const int step, const int rank)
{
/* TODO 10.f: write particle position, velocity and circulation to file: only rank 0 should write the data */
}
void DumpMPI(ArrayOfParticles& dstParticles, const int NpProcess, const int step, const int rank)
{
/* TODO 10.f: write particle position, velocity and circulation to file: all ranks should write their data to a common file */
}
int main (int argc, char ** argv)
{
int rank=0; // id of rank
int size=1; // number of ranks
/* TODO 10.b: initialization of MPI and setting of variables rank and size */
// number of processes
if (rank==0)
std::cout << "Running with " << size << " MPI processes\n";
// timer setup
Timer timerIC, timerSim;
// time integration setup
const double dt = 0.001;
const double tfinal = 2.5;
const int ndump = 10;
// output
const bool bAnimation = true;
const bool bVerbose = true;
const bool dump_mpi = true;
// number of particles
const int N = 10000;
size_t Np = (N/size)*size; // ensures that the number of particles is divisible by the number of workers
timerIC.start();
/* TODO 10.b: distribute particles such that each rank gets NpProcess particles */
size_t NpProcess = 0;
// particle vectors
// dstParticles: particles owned by rank
// srcParticles: particles with which the interaction has to be computed
ArrayOfParticles dstParticles(NpProcess), srcParticles(NpProcess);
ArrayOfParticles allParticles(rank==0 ? Np : 0); // list of all particles for output
const double dx = 1./Np;
double totGamma=0.; // total circulation is sum over gamma of all particles
const double Gamma_s = 1.;
// initialize particles: position and circulation
/* TODO 10.c: initialize particles (position and circulation) and compute total circulation totGamma */
for (size_t i=0; i<NpProcess; i++)
{
}
timerIC.stop();
if (rank==0)
{
std::cout << "Number of particles: " << Np << std::endl;
std::cout << "Number of particles per process: " << NpProcess << std::endl;
std::cout << "Initial circulation: " << totGamma << std::endl;
std::cout << "IC time: " << timerIC.get_timing() << std::endl;
}
// initialize velocity solver
VelocitySolverNSquared VelocitySolver(dstParticles, srcParticles, rank, size);
timerSim.start();
double t=0;
int it=0;
for (it=1; it<=std::ceil(tfinal/dt); it++)
{
// reset particle velocities
dstParticles.ClearVelocities();
// compute velocities corresponding to time n
VelocitySolver.ComputeVelocity();
// dump the particles
if ((it-1)%ndump==0 && bAnimation)
{
if (!dump_mpi)
Dump(dstParticles,allParticles,Np,NpProcess,it-1,rank);
else
DumpMPI(dstParticles,NpProcess,it-1,rank);
}
// update time
if (rank==0)
t += dt;
/* TODO 10.b: communicate time to all other ranks */
if (it%ndump==0 && rank==0 && bVerbose)
std::cout << "Iteration " << it << " time " << t << std::endl;
// advance particles in time to n+1 using forward Euler
dstParticles.AdvectEuler(dt);
// update "source" particles
srcParticles = dstParticles;
}
// dump final state
if ((it-1)%ndump==0 && bAnimation)
{
if (!dump_mpi)
Dump(dstParticles,allParticles,Np,NpProcess,it-1,rank);
else
DumpMPI(dstParticles,NpProcess,it-1,rank);
}
if (bVerbose)
std::cout << "Bye from rank " << rank << std::endl;
timerSim.stop();
if (rank==0)
{
char buf[500];
sprintf(buf, "timing.dat");
WriteTimeToFile(size,timerSim.get_timing(),buf);
std::cout << "#Ranks, Time - " << size << "\t" << timerSim.get_timing() << "\t( " << VelocitySolver.timeT << "\t" << VelocitySolver.timeC << " )\n";
}
/* TODO 10.b: finalize MPI */
return 0;
}
// Timer for the HPCSE I course
//
#ifndef HPCSE_TIMER_HPP
#define HPCSE_TIMER_HPP
#include <sys/time.h>
class Timer {
public:
Timer() {
start_time.tv_sec = 0;
start_time.tv_usec = 0;
stop_time.tv_sec = 0;
stop_time.tv_usec = 0;
}
inline void start() {
gettimeofday(&start_time, NULL);
}
inline void stop() {
gettimeofday(&stop_time, NULL);
}
double get_timing() const {
return (stop_time.tv_sec - start_time.tv_sec) + (stop_time.tv_usec - start_time.tv_usec)*1e-6;
}
private:
struct timeval start_time, stop_time;
};
#endif //HPCSE_TIMER_HPP
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment