Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize openmp multithread programming in linux system

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is to share with you about how to achieve openmp multithreaded programming in the linux system, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Key syntax:

The code is as follows:

# inlcude

# 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.

{

}

.

}

# pragma omp barrier/nowait / / forcibly set mountain barriers / do not need to wait. If subsequent functions are not dependent 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 (2 + 3) / / sets 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 header file, just add-fopenmp when compiling.

For example, the emacs operation command is as follows

The code is as follows:

Emacs omp.c

# include

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)

{

Rank = omp_get_thread_num ()

Size = omp_get_num_threads ()

Printf ("using% d of% d now.\ n", rank, size)

}

Return 0

}

Ctrl+x s

Alt+x compile

Gcc-fopenmp-o omp omp.c

Alt+shift+1. / omp

What is Linux system Linux is a free-to-use and free-spread UNIX-like operating system, is a POSIX-based multi-user, multi-task, multi-threaded and multi-CPU operating system, using Linux can run major Unix tools, applications and network protocols.

The above is how to achieve openmp multithreaded programming in the linux system. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report