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 54f25445 authored by Donjan Rodic's avatar Donjan Rodic
Browse files

added examples for week 02

parent 5ff6677c
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#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;
// print the result
std::cout << simpson(func,a,b,nsteps) << std::endl;
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#include <cmath>
#include <iostream>
#include <thread>
// 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 result1; // the integral of the first half
// spawn a thread for the first half of the interval
std::thread t( [&] () { result1 = simpson(func,a,a+(b-a)/2.,nsteps/2);} );
// locally integrate the second half
double result2 = simpson(func,a+(b-a)/2.,b,nsteps/2);
t.join(); // wait for the thread to join
std::cout << result1 + result2 << std::endl;
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#include <cmath>
#include <iostream>
#include <thread>
#include <future>
// 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;
// create a packaged task
std::packaged_task<double()> pt(std::bind(simpson,func,a,a+(b-a)/2.,nsteps/2));
std::future<double> fi = pt.get_future(); // get the future return value
std::thread t (std::move(pt)); // launch the thread
// locally integrate the second half
double result = simpson(func,a+(b-a)/2.,b,nsteps/2);
// wait for the task to finish and the future to be ready
// fi.wait(); // not needed since it is implicit in fi.get() below
std::cout << result+fi.get() << std::endl;
t.join();
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#include <cmath>
#include <iostream>
#include <thread>
#include <future>
// 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;
// even easier: launch an asynchronous function call
std::future<double> fi = std::async(simpson,func,a,a+(b-a)/2.,nsteps/2);
// locally integrate the second half
double result = simpson(func,a+(b-a)/2.,b,nsteps/2);
std::cout << result+fi.get() << std::endl;
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include "simpson.hpp"
#include <cmath>
#include <iostream>
#include <thread>
#include <future>
// 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;
// even easier: launch an asynchronous function call
// force it to be asynchronous and thus in a seperate thread
std::future<double> fi = std::async(std::launch::async,simpson,func,a,a+(b-a)/2.,nsteps/2);
// locally integrate the second half
double result = simpson(func,a+(b-a)/2.,b,nsteps/2);
std::cout << result+fi.get() << std::endl;
return 0;
}
\ No newline at end of file
// Example codes for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#include <iostream>
#include <thread>
#include <vector>
#include <mutex>
std::mutex io_mutex; // global
struct sync
{
sync( std::ostream& os )
: os(os)
, lock(io_mutex) {}
template <class T>
std::ostream& operator<<(T const& x)
{
return os << x;
}
private:
std::ostream& os;
std::lock_guard<std::mutex> lock;
};
void printer( int n )
{
for ( int i = 0; i < 100; ++i) {
sync(std::cout)
<< "do not garble thread "
<< n << ": " << i << std::endl;
}
}
int main()
{
std::vector<std::thread> threads;
for (int n = 1; n < 10; ++n)
threads.push_back(std::thread(printer, n));
for (std::thread& t : threads)
t.join();
}
// syncoed ip for HPC course
// (c) 2012 Matthias Troyer, ETH Zurich
#ifndef HPC12_SYNCIO_HPP
#define HPC12_SYNCIO_HPP
#include <thread>
#include <mutex>
std::mutex io_mutex; // global
struct sync
{
sync( std::ostream& os )
: os(os)
, lock(io_mutex) {}
template <class T>
std::ostream& operator<<(T const& x)
{
return os << x;
}
private:
std::ostream& os;
std::lock_guard<std::mutex> lock;
};
#endif
\ No newline at end of file
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