Commit 54f25445 by Donjan Rodic

### added examples for week 02

parent 5ff6677c
 // Example codes for HPC course // (c) 2012 Matthias Troyer, ETH Zurich #include "simpson.hpp" #include #include // 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 #include #include // 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 #include #include #include // 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 pt(std::bind(simpson,func,a,a+(b-a)/2.,nsteps/2)); std::future 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 #include #include #include // 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 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 #include #include #include // 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 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 #include #include #include std::mutex io_mutex; // global struct sync { sync( std::ostream& os ) : os(os) , lock(io_mutex) {} template std::ostream& operator<<(T const& x) { return os << x; } private: std::ostream& os; std::lock_guard 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 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 #include std::mutex io_mutex; // global struct sync { sync( std::ostream& os ) : os(os) , lock(io_mutex) {} template std::ostream& operator<<(T const& x) { return os << x; } private: std::ostream& os; std::lock_guard 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!