Difference between revisions of "GPU621/NoName"
(→Progress) |
|||
Line 6: | Line 6: | ||
#Danylo Medinski [mailto:ddmedinski@myseneca.ca?subject=GPU621%20from%20CDOT%20Wiki] Research etc. | #Danylo Medinski [mailto:ddmedinski@myseneca.ca?subject=GPU621%20from%20CDOT%20Wiki] Research etc. | ||
== Progress == | == Progress == | ||
− | |||
'''Oct 17th:''' | '''Oct 17th:''' | ||
Line 16: | Line 15: | ||
# Created Wiki page | # Created Wiki page | ||
---- | ---- | ||
+ | |||
+ | == OpenMp vs C++ 11 Threads == | ||
+ | |||
+ | ===What is C++ 11 Threads=== | ||
+ | With the introduction of C++ 11, there were major changes and additions made to the C++ Standard libraries. One of the most significant changes was the inclusion of multi-threading libraries. Before C++ 11 in order to implement multi-threading, external libraries or language extensions such as OpenMp was required. | ||
+ | The C++ 11 thread support library includes these 4 files to enable multi-threading | ||
+ | |||
+ | * <thread> - class and namespace for working with threads | ||
+ | * <mutex> - provides support for mutual exclusion | ||
+ | * <contition_variable> - a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable. | ||
+ | * <future> - Describes components that a C++ program can use to retrieve in one thread the result (value or exception) from a function that has run in the same thread or another thread. | ||
+ | |||
+ | ===Programming Models=== | ||
+ | ====SPMD==== | ||
+ | |||
+ | An example of the SPMD programming model in STD Threads using an atomic barrier | ||
+ | |||
+ | #include <iostream> | ||
+ | #include <iomanip> | ||
+ | #include <cstdlib> | ||
+ | #include <chrono> | ||
+ | #include <vector> | ||
+ | #include <thread> | ||
+ | #include <atomic> | ||
+ | using namespace std::chrono; | ||
+ | |||
+ | std::atomic<double> pi; | ||
+ | |||
+ | void reportTime(const char* msg, steady_clock::duration span) { | ||
+ | auto ms = duration_cast<milliseconds>(span); | ||
+ | std::cout << msg << " - took - " << | ||
+ | ms.count() << " milliseconds" << std::endl; | ||
+ | } | ||
+ | void run(int ID, double stepSize, int nthrds, int n) | ||
+ | { | ||
+ | double x; | ||
+ | double sum = 0.0; | ||
+ | for (int i = ID; i < n; i = i + nthrds){ | ||
+ | x = (i + 0.5)*stepSize; | ||
+ | sum += 4.0 / (1.0 + x*x); | ||
+ | } | ||
+ | sum = sum * stepSize; | ||
+ | pi = pi + sum; | ||
+ | } | ||
+ | |||
+ | int main(int argc, char** argv) { | ||
+ | if (argc != 3) { | ||
+ | std::cerr << argv[0] << ": invalid number of arguments\n"; | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | int n = atoi(argv[1]); | ||
+ | int numThreads = atoi(argv[2]); | ||
+ | |||
+ | steady_clock::time_point ts, te; | ||
+ | |||
+ | // calculate pi by integrating the area under 1/(1 + x^2) in n steps | ||
+ | ts = steady_clock::now(); | ||
+ | |||
+ | std::vector<std::thread> threads(numThreads); | ||
+ | |||
+ | double stepSize = 1.0 / (double)n; | ||
+ | |||
+ | for (int ID = 0; ID < numThreads; ID++) { | ||
+ | int nthrds = std::thread::hardware_concurrency(); | ||
+ | if (ID == 0) numThreads = nthrds; | ||
+ | threads[ID] = std::thread(run, ID, stepSize, 8, n); | ||
+ | } | ||
+ | |||
+ | te = steady_clock::now(); | ||
+ | |||
+ | for (int i = 0; i < numThreads; i++){ | ||
+ | threads[i].join(); | ||
+ | } | ||
+ | |||
+ | std::cout << "n = " << n << std::fixed << std::setprecision(15) << "\n pi(exact) = " << 3.141592653589793 << "\n pi(calcd) = " << pi << std::endl; | ||
+ | |||
+ | reportTime("Integration", te - ts); | ||
+ | |||
+ | // terminate | ||
+ | char c; | ||
+ | std::cout << "Press Enter key to exit ... "; | ||
+ | std::cin.get(c); | ||
+ | } |
Revision as of 02:51, 24 November 2016
Contents
NoName
Our project: C++11 Threads Library Comparison to OpenMP
Group Members
Progress
Oct 17th:
- Picked topic
- Picked presentation date.
- Gathering information
Oct 20th:
- Created Wiki page
OpenMp vs C++ 11 Threads
What is C++ 11 Threads
With the introduction of C++ 11, there were major changes and additions made to the C++ Standard libraries. One of the most significant changes was the inclusion of multi-threading libraries. Before C++ 11 in order to implement multi-threading, external libraries or language extensions such as OpenMp was required. The C++ 11 thread support library includes these 4 files to enable multi-threading
- <thread> - class and namespace for working with threads
- <mutex> - provides support for mutual exclusion
- <contition_variable> - a synchronization primitive that can be used to block a thread, or multiple threads at the same time, until another thread both modifies a shared variable (the condition), and notifies the condition_variable.
- <future> - Describes components that a C++ program can use to retrieve in one thread the result (value or exception) from a function that has run in the same thread or another thread.
Programming Models
SPMD
An example of the SPMD programming model in STD Threads using an atomic barrier
#include <iostream> #include <iomanip> #include <cstdlib> #include <chrono> #include <vector> #include <thread> #include <atomic> using namespace std::chrono; std::atomic<double> pi; void reportTime(const char* msg, steady_clock::duration span) { auto ms = duration_cast<milliseconds>(span); std::cout << msg << " - took - " << ms.count() << " milliseconds" << std::endl; } void run(int ID, double stepSize, int nthrds, int n) { double x; double sum = 0.0; for (int i = ID; i < n; i = i + nthrds){ x = (i + 0.5)*stepSize; sum += 4.0 / (1.0 + x*x); } sum = sum * stepSize; pi = pi + sum; } int main(int argc, char** argv) { if (argc != 3) { std::cerr << argv[0] << ": invalid number of arguments\n"; return 1; } int n = atoi(argv[1]); int numThreads = atoi(argv[2]); steady_clock::time_point ts, te; // calculate pi by integrating the area under 1/(1 + x^2) in n steps ts = steady_clock::now(); std::vector<std::thread> threads(numThreads); double stepSize = 1.0 / (double)n; for (int ID = 0; ID < numThreads; ID++) { int nthrds = std::thread::hardware_concurrency(); if (ID == 0) numThreads = nthrds; threads[ID] = std::thread(run, ID, stepSize, 8, n); } te = steady_clock::now(); for (int i = 0; i < numThreads; i++){ threads[i].join(); } std::cout << "n = " << n << std::fixed << std::setprecision(15) << "\n pi(exact) = " << 3.141592653589793 << "\n pi(calcd) = " << pi << std::endl; reportTime("Integration", te - ts); // terminate char c; std::cout << "Press Enter key to exit ... "; std::cin.get(c); }