OpenMP-CODE

编译制导函数

for

加大并行力度,尽量并行外层循环
在外层循环上用parallel,内层用for,并同时注意同步

#prapma omp parallel
{
	#pragma omp for
   for(){
           // 循环体内不能使用break语句
   }
}

#pragma omp parallel for

for 后可以用 nowait 取消结束的隐式同步

simd
#pragma omp parallel for simd reduction(+:ret)
for(int i=0; i<num; i++){
	ret += a[i]*a[i];
}
task

解决递归问题
eg.并行化处理Fibonacci数
taskwait 用于 task 构造之后,用于等待前面一个or多个task执行完成

#pragma omp task

#pragma omp taskwait

#pragma omp taskyield
sections / section

划分段,每段由一个线程执行
section后可以用 nowait 取消结束的隐式同步

#pragam omp parallel sections
{
	#pragma omp section
	{
	}
	#pragma omp section
	{
	}
}
single
#pragam omp single
{
}
barrier
#pragma omp barrier
critical

临界区

#pragma omp critical
{
}
atomic

原子操作
只作用到在 ±*/ &^| >> <<

#pragme omp atomic

常用子句

collapse

一般用于同时线程化两层循环(外层easy)

#pragam omp parallel for callapse(2)
	for( )
		for( )
reduction

归约

#pragma omp parallel for reduction(+:PI)
schedule

指定采用的负载均衡策略及每次分发数据大小

#pragma omp parallel for schedule(dynamic, 2)
if

确定是否并行

普通函数

总线程数
int omp_get_num_threads(void);
设置线程数
void omp_set_threads(int num);
上限线程
int omp_get_max_threads(void);
线程索引
int omp_get_thread_num(void);
是否并行
 int omp_in_parallel();

锁函数

OpenMP(四)线程同步之互斥锁函数

环境变量

export OMP_PROC_BIND = true

OpenMP环境变量

OpenMP学习笔记整理(一)——环境变量+嵌套并行

要在 Visual Studio Code 中使用 OpenMP,需要进行以下步骤: 1. 安装 MinGW-w64:这是一个在 Windows 上使用 GCC 编译器的开发环境。你可以从 MinGW-w64 官方网站(https://mingw-w64.org/doku.php)下载最新版本的安装程序。 2. 运行 MinGW-w64 安装程序:安装程序提供了一些选项,例如架构(32位或64位)和线程模型(posix 或 win32)。选择与你的系统匹配的选项,并选择安装 OpenMP 支持。 3. 配置 Visual Studio Code:打开 Visual Studio Code,并在左侧侧边栏中选择“扩展”按钮。搜索并安装名为“C/C++”的扩展。 4. 打开项目:在 Visual Studio Code 中打开你的 C/C++ 项目。确保项目文件夹中包含一个名为 `.vscode` 的文件夹。 5. 配置编译器:在 `.vscode` 文件夹中创建一个名为 `tasks.json` 的文件,并添加以下内容: ```json { "version": "2.0.0", "tasks": [ { "type": "shell", "label": "build with OpenMP", "command": "g++", "args": [ "-fopenmp", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe", "${file}" ], "group": { "kind": "build", "isDefault": true }, "problemMatcher": [] } ] } ``` 这个配置文件指示 Visual Studio Code 使用 g++ 编译器来构建项目,并启用 OpenMP 支持。 6. 编译项目:在 Visual Studio Code 中打开你的 C/C++ 文件,并按下 `Ctrl + Shift + B`(或选择“终端”菜单中的“运行生成任务”)。选择 `build with OpenMP` 任务来编译项目。 7. 运行项目:在 Visual Studio Code 中按下 `F5` 键,或选择“调试”菜单中的“开始调试”选项,以运行你的项目。 以上步骤将帮助你在 Visual Studio Code 中安装并配置 OpenMP。在配置编译器时,确保你已正确设置 g++ 的路径。如果遇到任何问题,请参考 Visual Studio Code 的文档或搜索相关问题的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值