Ubuntu安装OpenMP及案例

Ubuntu安装OpenMP及案例

安装

  1. 进入官网OpenMP,下载稳定版本。
  2. 解压后进入文件夹,运行./configure --prefix=/home/jame/Public/openmp(自定义路径)
  3. 编译运行
make -j4
sudo make install
  1. 路径配置
sudo vim ~/.bashrc

添加

export PATH="$PATH:/home/jame/Public/openmp/bin"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/jame/Public/openmp/lib"
  1. 案例测试
    进入/home/jame/Downloads/openmpi-4.1.5/examples
    运行(使用四个线程运行)
mpirun -np 4 hello_c

指令教程

直接参考指令教程
个人测试代码:

#include<omp.h>
#include<iostream>
#include <math.h>
#include <unistd.h>
using namespace std;

long long get_microseconds(void){
    // 微秒单位
    struct timespec now;
    clock_gettime(CLOCK_MONOTONIC, &now);
    return now.tv_sec * 1000000 + now.tv_nsec / 1000;
}

int main()
{

	omp_set_num_threads(5);

// ********************************* section test *******************************

#pragma omp parallel sections
{
		
#pragma omp section
{
    for(int i=0;i<2;i++){
        double temp = sin(i*0.01) * cos(i*0.01);
        cout << "1 " << omp_get_thread_num() << endl;
        usleep(1000000);
    }
}
            
#pragma omp section
{
    for(int i=0;i<2;i++){
        double temp = sin(i*0.01) * cos(i*0.01);
        cout << "2 " <<omp_get_thread_num() << endl;
        usleep(1000000);
    }
}

}

// ********************************* static test *******************************

auto start_time = get_microseconds();

#pragma omp parallel for schedule(static)
    for(int i=0;i<8;i++){
        for(int i=0;i<250*250;i++){
            double temp = sin(i*0.01) * cos(i*0.01);
        }
        cout << omp_get_thread_num() << endl;
    }

auto end_time = get_microseconds();
cout << "time: "<< (- start_time + end_time) * 0.000001 << endl;
}

CMakeLists关键配置:

set(CMAKE_CXX_FLAGS "-Wall -fopenmp")

find_package(OpenMP REQUIRED)
if(OpenMP_FOUND)
    message(STATUS "########## Found openmp ##########") 
    set(CMAKE_C_FLAGS ${CMAKE_C_FLAGS} ${OPENMP_C_FLAGS})
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${OPENMP_CXX_FLAGS})
else()
    message(FATAL_ERROR "Openmp not found!")
endif()
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值