In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to carry out openmp multithread programming on the Linux system". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to carry out openmp multithreading programming on the Linux system".
OpenMP is a multiprocessor and multithreaded programming language, which can support multiple platforms, including Linux systems, so how to do openmp multithreading programming under Linux? Let's get to know it.
Key syntax:
The code is as follows:
# inlcude "omp.h"
# pragma omp parallel for
# pragma omp for reduction (+: variable)
# pragma omp critical// lock
{
}
# pragma omp parallel for private (XMague y) / / each thread copies the xQuery variable independently and does not interfere with each other. If it is not set, it is a shared variable by default
# pragma omp parallel for schedule (static/dynamic/guided, k) / / the total workload is divided into nmax k blocks, and then multi-thread scheduling
# pragma omp parallel sections
{
# pragma omp section// to ensure that there are no variable dependencies between functions under several section
.
# pragma omp section
.
}
# pragma omp parallel
{
. ()
# pragma omp master/single / / ensure that only the main thread / a thread can access the following function. The difference is that there is no barrier barrier for using master. If single is used, the thread that finishes first waits for the thread that has not finished.
{
}
.
} "/ p"p" # pragma omp barrier/nowait / / forcibly set a mountain barrier / do not need to wait, if the subsequent function does not depend on the previous multithreading, you can use nowait
# pragma omp parallel for firstprivate (variable) / lastprivate (variable) / / assign an initial value to each multithread / assign a value for the main thread to use when the multithread returns to the main thread
And then there is OpenMP's API:
The code is as follows:
Int omp_get_num_threads (); / / get the number of threads currently in use
Int omp_get_num_threads (2Accord 3 /. ) / / set the number of threads to be used
Nt omp_get_thread_num (void); / / returns the current thread number
Int omp_get_num_procs (void); / / returns the number of available processing cores
Under ubuntu, there is no need to add a "omp.h" header file, just add-fopenmp when compiling.
For example, the emacs operation command is as follows
The code is as follows:
Emacs omp.c "/ p"p" # include "stdio.h"
Int main ()
{
Int rank, size
# pragma omp parallel num_thread (3) private (rank) / / num_threads to control the number of threads
/ / or use omp_set_num_threads (3); "/ p"p" {
Rank = omp_get_thread_num ()
Size = omp_get_num_threads ()
Printf ("using% d of% d now.\ n", rank, size)
}
Return 0
} "/ p"p" ctrl+x s
Alt+x compile
Gcc-fopenmp-o omp omp.c
Alt+shift+1 . / omp
Thank you for your reading, the above is the content of "how to carry out openmp multithreaded programming on the Linux system". After the study of this article, I believe you have a deeper understanding of the problem of how to carry out openmp multithreaded programming on the Linux system. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.