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 66994f8c authored by Dmitry Alexeev's avatar Dmitry Alexeev

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

parents 4a7c7797 3aed3e28
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
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