To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 02fdb4c3 authored by chatzidp's avatar chatzidp
Browse files

Merge branch 'master' of gitlab.ethz.ch:hpcse_hs16/lecture

parents 3f9f1c3e c396e4f5
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#include <omp.h>
#include <cmath>
#include <iostream>
// The function to integrate
double func(double x)
{
return x * std::sin(x);
}
int main()
{
double a; // lower bound of integration
double b; // upper bound of integration
unsigned int nsteps; // number of subintervals for integration
// read the parameters
std::cin >> a >> b >> nsteps;
double result=0.;
#pragma omp parallel shared(result)
{
#pragma omp sections reduction(+:result)
{
#pragma omp section
{
result = simpson(func,a,a+0.5*(b-a),nsteps/2);
}
#pragma omp section
{
result = simpson(func,a+0.5*(b-a),b,nsteps/2);
}
}
}
std::cout << result << std::endl;
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include <iostream>
#include <omp.h>
int main() {
#pragma omp parallel
#pragma omp single
std::cout << "Only thread " << omp_get_thread_num()
<< " of " << omp_get_num_threads() << " is printing.\n";
return 0;
}
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include <iostream>
#include "mutex.hpp"
int main()
{
mutex m;
#pragma omp parallel for
for (int i=0; i < 100; ++i) {
{
lock_guard<mutex> lock(m);
std::cout << "Hello from the " << i << "-th iteration\n";
}
}
}
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include <iostream>
int main()
{
#pragma omp parallel for
for (int i=0; i < 100; ++i) {
// do some (fake) work
int j=i;
#pragma omp critical
std::cout << "Hello from the " << j << "-th iteration\n";
}
}
\ No newline at end of file
all: integral_seq integral_mt
integral_seq: integral_seq.cpp
g++ -Wall -O3 --std=c++11 -o integral_seq integral_seq.cpp
integral_mt: integral_mt.cpp
g++ -Wall -O3 --std=c++11 -pthread -o integral_mt integral_mt.cpp
clean:
rm -f integral_seq integral_mt
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <thread>
#include <mutex>
#include "timer.hpp"
double f(double x)
{
return std::log(x)*std::sqrt(x);
}
// WolframAlpha: integral_1^4 log(x) sqrt(x) dx = 4/9 (4 log(64)-7) ~~ 4.28245881486164
int main(int argc, char *argv[])
{
double a = 1.0;
double b = 4.0;
unsigned long n = 200UL*35389440UL;
unsigned int nthreads = 1;
// Get number of threads from program arguments (if provided)
if (argc > 1)
nthreads = atoi(argv[1]);
unsigned long nsteps = n / nthreads;
if(nsteps * nthreads != n)
{
std::cout << "WARNING: n=" << n << " is not a multiple of nthreads=" << nthreads << ". ";
n = nsteps * nthreads;
std::cout << "Using n=" << n << " instead." << std::endl;
}
const double dx = (b-a)/n;
std::vector<std::thread> threads(nthreads);
std::pair<double,std::mutex> S;
S.first = 0;
timer t;
t.start();
for (unsigned thr = 0; thr < nthreads; thr++) {
#if DBG
std::cout << "spawning thread " << thr << std::endl;
#endif
threads[thr] = std::thread([&S,a,nsteps,dx,thr]() {
double local_S = 0;
const double x0 = a + (thr * nsteps + 0.5)*dx;
for (unsigned long i = 0; i < nsteps; i++) {
double xi = x0 + i*dx;
local_S += f(xi);
}
local_S *= dx;
std::lock_guard<std::mutex> l(S.second);
S.first += local_S;
});
}
for (std::thread& thr : threads)
thr.join();
t.stop();
std::cout << "Threads=" << nthreads << " Time=" << t.get_timing() << " seconds, Result=" << std::setprecision(8) << S.first << std::endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <cmath>
#include "timer.hpp"
double f(double x)
{
return std::log(x)*std::sqrt(x);
}
// WolframAlpha: integral_1^4 log(x) sqrt(x) dx = 4/9 (4 log(64)-7) ~~ 4.28245881486164
int main(int argc, char *argv[])
{
double a = 1.0;
double b = 4.0;
unsigned long const n = 1e9;
const double dx = (b-a)/n;
double S = 0;
timer t;
t.start();
for (unsigned long i = 0; i < n; i++) {
double xi = a + (i + 0.5)*dx;
S += f(xi);
}
S *= dx;
t.stop();
std::cout << " Time=" << t.get_timing() << " seconds, Result=" << std::setprecision(8) << S << std::endl;
return 0;
}
rm -f out.txt
for i in 1 2 4 6 8 10 12 14 16 18 20 22 24
do
./integral_mt $i >> out.txt
done
// Timer for the HPCSE I course
//
#ifndef HPCSE16_TIMER_HPP
#define HPCSE16_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 //HPCSE16_TIMER_HPP
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <cassert>
#include <vector>
#include <cmath>
#include "timer.hpp"
typedef double value_type;
typedef std::size_t size_type;
class Diffusion2D {
public:
Diffusion2D(const value_type D,
const value_type L,
const size_type N,
const value_type dt)
: D_(D), L_(L), N_(N), Ntot(N_*N_), dt_(dt)
{
/// real space grid spacing
dr_ = L_ / (N_ - 1);
/// stencil factor
fac_ = dt_ * D_ / (dr_ * dr_);
rho_.resize(Ntot, 0.);
rho_tmp.resize(Ntot, 0.);
initialize_density();
}
void advance()
{
/// Open boundaries; central differences in space, forward Euler
/// in time
for(size_type i = 0; i < N_; ++i)
for(size_type j = 0; j < N_; ++j) {
rho_tmp[i*N_ + j] = rho_[i*N_ + j] +
fac_
*
(
(j == N_-1 ? 0. : rho_[i*N_ + (j+1)]) +
(j == 0 ? 0. : rho_[i*N_ + (j-1)]) +
(i == N_-1 ? 0. : rho_[(i+1)*N_ + j]) +
(i == 0 ? 0. : rho_[(i-1)*N_ + j]) -
4.*rho_[i*N_ + j]
);
}
/// use swap instead of rho_=rho_tmp. this is much more efficient, because it does not have to copy
/// element by element.
std::swap(rho_tmp, rho_);
}
void write_density(std::string const& filename) const
{
std::ofstream out_file(filename, std::ios::out);
for(size_type i = 0; i < N_; ++i) {
for(size_type j = 0; j < N_; ++j)
out_file << (i*dr_ - L_/2.) << '\t' << (j*dr_ - L_/2.) << '\t' << rho_[i*N_ + j] << "\n";
out_file << "\n";
}
out_file.close();
}
private:
void initialize_density()
{
/// initialize rho(x,y,t=0)
value_type bound = 1/2.;
for (size_type i = 0; i < N_; ++i) {
for (size_type j = 0; j < N_; ++j) {
if (std::abs(i*dr_ - L_/2.) < bound && std::abs(j*dr_ - L_/2.) < bound) {
rho_[i*N_ + j] = 1;
} else {
rho_[i*N_ + j] = 0;
}
}
}
}
value_type D_, L_;
size_type N_, Ntot;
value_type dr_, dt_, fac_;
std::vector<value_type> rho_, rho_tmp;
};
int main(int argc, char* argv[])
{
if (argc < 5) {
std::cerr << "Usage: " << argv[0] << " D L N dt" << std::endl;
return 1;
}
const value_type D = std::stod(argv[1]);
const value_type L = std::stod(argv[2]);
const size_type N = std::stoul(argv[3]);
const value_type dt = std::stod(argv[4]);
Diffusion2D system(D, L, N, dt);
system.write_density("density_000.dat");
const value_type tmax = 10000 * dt;
value_type time = 0;
timer t;
t.start();
while (time < tmax) {
system.advance();
time += dt;
}
t.stop();
std::cout << "Timing : " << N << "\t1\t" << t.get_timing() << std::endl;
system.write_density("density_serial.dat");
return 0;
}
// Timer for the HPCSE I course
//
#ifndef HPCSE14_TIMER_HPP
#define HPCSE14_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 //HPCSE14_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