MacOS configures OpenMP multi-threaded parallel program development environment steps (full version)

Table of contents

 

introduction:

process:

Install the OMP third-party library (the first method):

Test OpenMP in the vim editor:

Step 1 :

Step 2 :

Step 3 :

Step 4 :

 Step 5 :

Step 6 : 

Step 7 :

Install llvm framework (second method):

References:


 

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:

b8b33f78d4514b5ab4d4feda817785e0.png

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:

c4a8226add2f458eaa4040a1474e1ae9.png

 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:

b85b12a3146c453a9fa679725d46a99f.png

Enter the command ./ac to run the multi-threaded program, as shown in the figure:

2dfbb51179924537a9f27a763f69f278.png

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:

b8edc74fb2864f238466a98f71456ed0.png

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]

94088a948b744478b788fef1535e20f9.png

As shown in the figure, the independent dependent packages have been installed.

Then we enter the command again: 

brew Install llvm libomp

90757d73b589493ab9cf7360cb64f287.png 

As shown in the figure, the llvm framework has been installed.

 Let's write another multi-threaded parallel program for testing:

d41f580b79014ad6ae08ac3e1a32b593.png

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

Configure OpenMP_excelsiorX blog in MacOS+VSCode environment - CSDN blog_vscode configuration openmp shock

Timothy G. Mattson He Yun Alice E. Conesy- OpenMP Core Technology Guide- Mechanical Industry Press

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_45571585/article/details/127158586