Difference between revisions of "GPU621/CamelCaseTeam"

From CDOT Wiki
Jump to: navigation, search
Line 16: Line 16:
 
C++ threading consists of the creation of a thread that works on a function. By having multiple threads, multiple functions are able to be performed at the same time which enables parallel processing.
 
C++ threading consists of the creation of a thread that works on a function. By having multiple threads, multiple functions are able to be performed at the same time which enables parallel processing.
  
<blockquote>
+
<syntaxhighlight lang="cpp">
<p>template<class Function, class... args></p>  
+
template<class Function, class... args>  
<p>explicit thread( Function&& f, Args&&... args);</p>
+
explicit thread( Function&& f, Args&&... args);
</blockquote>
+
</syntaxhighlight>
  
 
As seen above, a single thread takes a function through overloading and performs the task by passing the arguments to the function parameter which would execute separate to other threads. Once these threads are finished
 
As seen above, a single thread takes a function through overloading and performs the task by passing the arguments to the function parameter which would execute separate to other threads. Once these threads are finished
 
performing their function, the child thread must be joined using the .join() function in the thread library to the parent thread.
 
performing their function, the child thread must be joined using the .join() function in the thread library to the parent thread.
 +
 +
<syntaxhighlight lang="cpp">
 +
#include <thread>
 +
#include <iostream>
 +
#include <chrono>
 +
 +
void thread1() {
 +
std::this_thread::sleep_for(std::chrono::seconds(2));
 +
std::cout << "Hello from thread 1" << std::endl;
 +
}
 +
 +
void thread2() {
 +
std::this_thread::sleep_for(std::chrono::seconds(3));
 +
std::cout << "Hello from thread 2" << std::endl;
 +
}
 +
 +
void thread3() {
 +
std::this_thread::sleep_for(std::chrono::seconds(1));
 +
std::cout << "Hello from thread 3" << std::endl;
 +
}
 +
 +
int main() {
 +
std::cout << "Creating thread 1: " << std::endl;
 +
std::thread t1(thread1); //appears 2nd
 +
 +
std::cout << "Creating thread 2: " << std::endl;
 +
std::thread t2(thread2); //appears 3rd
 +
 +
std::cout << "Creating thread 3: " << std::endl;
 +
std::thread t3(thread3); //appears 1st
 +
 +
t1.join();
 +
t2.join();
 +
t3.join();
 +
}</syntaxhighlight>
 +
 +
The code above gives a simple example on how threads are created and joined back to the parent thread in the end.
  
 
== OpenMP Threading ==
 
== OpenMP Threading ==
Line 28: Line 65:
 
OpenMP threads consists of a parallel region that forks a master thread into multiple child threads that each performs a function in parallel amongst each other and joins back to its parent thread after reaching the end of the parallel region.
 
OpenMP threads consists of a parallel region that forks a master thread into multiple child threads that each performs a function in parallel amongst each other and joins back to its parent thread after reaching the end of the parallel region.
  
<blockquote>
+
<syntaxhighlight lang="cpp">
<p>#pragma omp construct [clause, ...]</p>
+
#pragma omp construct [clause, ...]
<p>structured block</p>
+
structured block
</blockquote>
+
</syntaxhighlight>
  
 
The construct identifies what block of code is being executed in parallel and the clause qualifies the parallel construct. In comparison to the C++ 11 thread library, the threads are performed in a region while the C++ 11 thread library have to be specifically created and joined back to the parent thread.
 
The construct identifies what block of code is being executed in parallel and the clause qualifies the parallel construct. In comparison to the C++ 11 thread library, the threads are performed in a region while the C++ 11 thread library have to be specifically created and joined back to the parent thread.

Revision as of 02:41, 22 July 2021


GPU621/DPS921 | Participants | Groups and Projects | Resources | Glossary

Project Name

C++11 Threads Library Comparison to OpenMP

Group Members

Andrei Fedchenko Hung Truong Ren Ren

Overview

Our team will do a research on comparison between c++ 11 threads library and openmp. We will code problems with both libraries to demonstrate the coding differences and do the performance analysis on those two approaches. We will highlight the techniques and best practices to achieve the best performance and avoid the pitfalls.

C++ Threading

C++ threading consists of the creation of a thread that works on a function. By having multiple threads, multiple functions are able to be performed at the same time which enables parallel processing.

template<class Function, class... args> 
explicit thread( Function&& f, Args&&... args);

As seen above, a single thread takes a function through overloading and performs the task by passing the arguments to the function parameter which would execute separate to other threads. Once these threads are finished performing their function, the child thread must be joined using the .join() function in the thread library to the parent thread.

#include <thread>
#include <iostream>
#include <chrono>

void thread1() {
	std::this_thread::sleep_for(std::chrono::seconds(2));
	std::cout << "Hello from thread 1" << std::endl;
}

void thread2() {
	std::this_thread::sleep_for(std::chrono::seconds(3));
	std::cout << "Hello from thread 2" << std::endl;
}

void thread3() {
	std::this_thread::sleep_for(std::chrono::seconds(1));
	std::cout << "Hello from thread 3" << std::endl;
}

int main() {
	std::cout << "Creating thread 1: " << std::endl;
	std::thread t1(thread1); //appears 2nd

	std::cout << "Creating thread 2: " << std::endl;
	std::thread t2(thread2); //appears 3rd

	std::cout << "Creating thread 3: " << std::endl;
	std::thread t3(thread3); //appears 1st

	t1.join();
	t2.join();
	t3.join();
}

The code above gives a simple example on how threads are created and joined back to the parent thread in the end.

OpenMP Threading

OpenMP threads consists of a parallel region that forks a master thread into multiple child threads that each performs a function in parallel amongst each other and joins back to its parent thread after reaching the end of the parallel region.

#pragma omp construct [clause, ...]
structured block

The construct identifies what block of code is being executed in parallel and the clause qualifies the parallel construct. In comparison to the C++ 11 thread library, the threads are performed in a region while the C++ 11 thread library have to be specifically created and joined back to the parent thread.

References

Thread Support Library https://en.cppreference.com/w/cpp/thread