0 引言

1 安装brew

Xcode-select -intall
/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

2 安装llvmlibomp

brew install llvm

安装完成之后,在终端输入

brew list

若在其中找到llvm,则代表安装成功。

  • 依然是在终端,输入:

brew install libomp

完成系列操作之后,在终端输入:

brew info libomp

可以在其中找到libomp所在的路径需要记住(后面有用)。

3 在终端进行测试

cd ~
mkdir omp_test
cd omp_test
touch test.c

当前文件夹目录下的终端中输入:

export OMP_NUM_THREADS=4
vi test.c
#include <stdio.h&gt;
#include <omp.h&gt;

int main(){
        int id, numb;
        omp_set_num_threads(4);
        #pragma omp parallel private(id, numb)
        {
                id = omp_get_thread_num();
                numb = omp_get_num_threads();
                printf("I am thread %d out of %dn", id, numb);
        }
        return 0;
}
gcc-12 -fopenmp test.c -o test

⚠️注意:若编译出现error直接看【4 编译过程注意事项】!!!

  • 执行:

./test
I am thread 1 out of 4
I am thread 2 out of 4
I am thread 0 out of 4
I am thread 3 out of 4

注意:结果中出现的数字不同也行,重要的是格式相同

4 编译过程注意事项

#include </opt/homebrew/Cellar/libomp/16.0.0/include/omp.h&gt;
  • 这样做虽然不优雅,但能用。

5 安装CLion并配置C/C++环境

6 在CLion配置OpenMP编译环境

/usr/bin/gcc						# C编译器
/usr/bin/g++						# C++编译器
cmake_minimum_required(VERSION 3.24)
project(omp_test)

set(CMAKE_C_COMPILER "/usr/bin/gcc") #这里写你的gcc路径
set(CMAKE_CXX_COMPILER "/usr/bin/g++") #这里写你的g++路径
set(OPENMP_LIBRARIES "/opt/homebrew/Cellar/libomp/16.0.0/lib") #这里写你的libomp路径, 通过brew安装的默认地址
set(OPENMP_INCLUDES "/opt/homebrew/Cellar/libomp/16.0.0/include")#这里写你的libomp路径

message("here")
message("${CMAKE_CXX_COMPILER}")

OPTION (USE_OpenMP "Use OpenMP to enable <omp.h>" ON)

# Find OpenMP
if(APPLE AND USE_OpenMP)
    if(CMAKE_C_COMPILER_ID MATCHES "Clang")
        set(OpenMP_C "${CMAKE_C_COMPILER}")
        set(OpenMP_C_FLAGS "-Xpreprocessor -fopenmp -lomp -Wno-unused-command-line-argument")
        set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
        set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
        set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
    endif()
    if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
        set(OpenMP_CXX_FLAGS "-Xpreprocessor -fopenmp -lomp -Wno-unused-command-line-argument")
        set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
        set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
        set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
    endif()
endif()

if(USE_OpenMP)
    find_package(OpenMP REQUIRED)
endif(USE_OpenMP)


if (OPENMP_FOUND)
    include_directories("${OPENMP_INCLUDES}")
    link_directories("${OPENMP_LIBRARIES}")
    set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
endif(OPENMP_FOUND)

add_executable(omp_test main.cpp)
set(OPENMP_LIBRARIES "/opt/homebrew/Cellar/libomp/16.0.0/lib") #这里写你的libomp路径, 通过brew安装的默认地址
set(OPENMP_INCLUDES "/opt/homebrew/Cellar/libomp/16.0.0/include")#这里写你的libomp路径

以上路径可以在终端,通过输入brew info libomp获得,获得方式可见【2 安装llvm和libomp】以及【4 编译过程注意事项

  • 至此,环境搭建正式成功(撒花!!!!)

7 编写第一个OpenMP程序

#include <stdio.h>
#include <omp.h>

int main(){
#pragma omp parallel sections
    {
    #pragma omp section
        for(int i = 0;i < 5;++ i){
            printf("section i:iteration %d by thread no.%dn", i, omp_get_thread_num());
        }
    #pragma omp section
        for(int j = 0;j < 5;++ j){
            printf("section j:iteration %d by thread no.%dn", j, omp_get_thread_num());
        }
    }
    return 0;
}
section i:iteration 0 by thread no.0
section i:iteration 1 by thread no.0
section i:iteration 2 by thread no.0
section i:iteration 3 by thread no.0
section i:iteration 4 by thread no.0
section j:iteration 0 by thread no.1
section j:iteration 1 by thread no.1
section j:iteration 2 by thread no.1
section j:iteration 3 by thread no.1
section j:iteration 4 by thread no.1

8 后言

当我在CLion中能够include一个不带绝对地址优雅的header时,我奇迹般发现在终端利用vim编写的也能不用带绝对地址头文件了,好神奇,现在还不知道是因为什么

配置完CLion后我从新整理心情尝试能够攻克一直无法在Xcode上配置环境的obstacle,真是可恶啊还是fail掉了,再修炼一会看看能不能处理这个麻烦。(其实Xcode好用,只是体量特别壮硕,有好多现在还用不上但是不得不安装的东西。)

其实过程中遇到了很多问题都还没有解决,希望大家能够多多指正!

9 引用/参考

在mac OS下安装并使用OpenMP

MacOS配置OpenMP多线程并行程序开发环境步骤(完整版

MacOS 配置Clion的C/C++环境的详细步骤及mac终端报错问题解决(完整版)

在mac系统下使用clion编译运行openmp项目

原文地址:https://blog.csdn.net/m0_61054560/article/details/129718607

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37534.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注