Table of contents
Install the OMP third-party library (the first method):
Test OpenMP in the vim editor:
Install llvm framework (second method):
introduction:
I have recently entered the pit of parallel program development. Today I borrowed a few books on parallel program development in the library on a whim and learned about OpenMP. Since I am an Apple computer, most of the online tutorials are based on parallel program development on the Windows side. The environment is built, or the OpenMP development environment based on Xcode on the MacOS side is built. This article will talk about how to build a multi-threaded development environment on a Mac computer. There are still some detailed knowledge in it that I don't understand thoroughly, and I will continue to follow up.
process:
In the previous article: MacOS configures the detailed steps of Clion's C/C++ environment and solves the problem of mac terminal error (full version) https://blog.csdn.net/weixin_45571585/article/details/126977413?spm=1001.2014. 3001.5502
We have installed the Brew plug-in on MacOS and successfully configured the brew plug-in and installed the gcc compiler through the brew plug-in. Brew is actually a package management tool based on MacOS. We can install and uninstall plug-ins very conveniently through brew. Today we also need to install OpenMP through brew.
Install the OMP third-party library (the first method):
Open the terminal and enter the command:
brew Install libomp
Note: At this point we need to proceed as an ordinary user, and cannot switch the administrator identity, otherwise it will fail!
Enter the command terminal and the following interface appears:
If the terminal appears:
brew reinstall libomp
This sentence means that the libomp plugin has been installed.
Test OpenMP in the vim editor:
Now we compile a multi-threaded program in the vim editor to test the omp third-party library:
Step 1 :
Open the terminal and enter the command: mkdir test to create a folder named test on the desktop;
Step 2 :
Enter the command: cd test to enter the test folder;
Step 3 :
Enter the command: touch ac to create a common file ac;
Step 4 :
Enter the command: vim ac Use the vim editor to edit the ac file, and the following interface appears:
Step 5 :
Press the I key to enter the "INSERT" mode and enter the sample program:
#include<stdio.h>
#include<omp.h>
int main()
{
#pragma omp parallel
printf("Hello from thread %d,nthreads %d\n",omp_get_thread_num(),omp_get_num_threads());
return 0;
}
Step 6 :
After the input is complete, press the Esc key, enter the ":" key, enter wq, save, and exit.
Step 7 :
Enter the command in the terminal:
gcc -Xpreprocessor -fopenmp -lomp a.c
Use the gcc compiler and multi-threaded third-party library to compile the program ac
At this time, two files appear in the test folder, namely the ordinary file ac and the compiled a.out, as shown in the figure:
Enter the command ./ac to run the multi-threaded program, as shown in the figure:
The thread program numbers that output Hello are 0, 3, 4, 6, 1, 5, 2, and 7, and the total number of threads is 8.
The multi-threaded parallel program runs successfully, the omp third-party library is installed, and the OpenMP environment is built.
Install llvm framework (second method):
At the same time, we can also directly install the llvm framework through brew. The llvm framework itself contains the OpenMP API. Enter the command:
brew Install llvm libomp
Enter the command and the following interface will appear:
As shown in the figure, the terminal reports the first error:
problem lies in:
Error: No such file or directory @ rb_sysopen -
[email protected]_2.monterey.bottle.tar.gz
Don't panic at this time, the error indicates that our system lacks an independent dependency package:
We enter the command:
brew install [email protected]
As shown in the figure, the independent dependent packages have been installed.
Then we enter the command again:
brew Install llvm libomp
As shown in the figure, the llvm framework has been installed.
Let's write another multi-threaded parallel program for testing:
The multi-threaded program is running and the OpenMP environment is successfully built.
References:
Mac solves error: No such file or directory @ rb_sysopen_kissludo's Blog-CSDN Blog
Timothy G. Mattson He Yun Alice E. Conesy- OpenMP Core Technology Guide- Mechanical Industry Press