▶ OpenMP 的任务并行 (task parallelism):显式定义一系列可执行的任务及其相互依赖关系,通过任务调度的方式多线程动态执行,支持任务的延迟执行 (deferred execution)

● 变量的数据域:并行区共享变量 → task 区也为共享;并行区私有变量 → task 区为 firstprivate;task 区其他变量 → 默认私有

● 范例代码

 #include <stdio.h>
#include <omp.h>
#include <time.h> int fib(int n)
{
int x, y;
if (n < )
return n;
#pragma omp task shared(x) // 创建关于 x 的 task
x = fib(n - );
#pragma omp task shared(y) // 创建关于 y 的 task
y = fib(n - );
#pragma omp taskwait // 等待两个 task 完成才嫩开始接下来的计算
return x + y;
} int main()
{
int res, n = ;
clock_t tick = clock();
#pragma omp parallel // task 要在并行区内调用
{
#pragma omp single // 根任务只调用 1 次
res = fib(n);
}
printf("Fib[%d] == %d, time = %f ms\n", n, res, float(clock() - tick)/);
getchar();
return ;
}

▶ 动态线程:系统动态选择并行区的线程数 (默认关闭)

● 用库函数打开 / 关闭动态线程,flag == 0 按优先级决定线程数,flag != 0 系统动态调节线程数

void omp_set_dynamic(int flag)

● 用环境变量打开 / 关闭动态线程

export OMP_DYNAMIC = true

● 检查动态线程是否打开

int omp_get_dynamic (void)

▶ 嵌套并行:并行区之内开启并行区 (默认开启)

● 用库函数打开 / 关闭嵌套并行

void omp_set_nested(int flag)

● 用环境变量打开 / 关闭嵌套并行

export OMP_NESTED = true
export OMP_NUM_THREADS = n1, n2, n3 # 每层嵌套的线程数

● 检查嵌套并行是否打开

int omp_get_nested (void)

▶ 动态线程和嵌套并行的范例代码

 #include <stdio.h>
#include <omp.h>
#include <time.h> int main()
{
omp_set_dynamic(); // 关闭动态线程
#pragma omp parallel num_threads(2)
{
#pragma omp single // 一个线程来执行,返回 2
printf("Outer: num_thds=%d\n\n", omp_get_num_threads()); omp_set_nested(); // 开启嵌套并行
#pragma omp parallel num_threads(3) // 内嵌一个 3 线程的并行块
{
#pragma omp single
printf("Inner: num_thds=%d\n", omp_get_num_threads()); // 返回 3
}
#pragma omp barrier omp_set_nested(); // 关闭嵌套并行
#pragma omp parallel num_threads(3) // 内嵌一个 3 线程的并行块
{
#pragma omp single
printf("Inner: num_thds=%d\n", omp_get_num_threads()); // 返回 1
}
#pragma omp barrier
} getchar();
return ;
}

▶ 线程私有型全局变量:将全局变量置为线程私有(对线程而言是全局变量),必须置于全局变量的声明列表之后

#pragma omp threadprivate (list)

● 范例代码

 #include <stdio.h>
#include <omp.h> int a, b, i, tid;
float x; #pragma omp threadprivate(a, x) int main(int argc, char *argv[])
{
omp_set_dynamic();
omp_set_num_threads(); printf("1st Parallel Region:\n");
#pragma omp parallel private(b, tid)
{
tid = omp_get_thread_num();
a = tid;
b = tid;
x = float(tid);
printf("Thread %d: a, b, x= %d, %d, %f\n", tid, a, b, x);
} printf("\n2nd Parallel Region:\n");
#pragma omp parallel private(tid)
{
tid = omp_get_thread_num();
printf("Thread %d: a, b, x= %d, %d, %f\n", tid, a, b, x);
} getchar();
return ;
}

● 输出结果,b 没有私有化,保持了第一并行区的结果

1st Parallel Region :
Thread : a, b, x = , , 0.000000
Thread : a, b, x = , , 1.000000
Thread : a, b, x = , , 3.000000
Thread : a, b, x = , , 2.000000 2nd Parallel Region :
Thread : a, b, x = , , 0.000000
Thread : a, b, x = , , 2.000000
Thread : a, b, x = , , 3.000000
Thread : a, b, x = , , 1.000000

▶ OpenMP 堆栈:除了主线程,每个线程的私有变量存储空间受线程堆栈大小控制,超出堆栈大小程序的行为不可控

● OpenMP 堆栈大小依赖实现:icc 默认 4 MB;gcc / gfortran 默认 2 MB;

● 可以通过环境变量修改默认堆栈大小:

export OMP_STACKSIZE=32M
export OMP_STACKSIZE=8192K

▶ 线程亲和性(affinity)和线程绑定(binding):线程亲和性决定 NUMA 架构的系统上线程在物理计算核心的映射策略;线程绑定显式确定线程与物理计算核心的对应关系,以提升性能

● OpenMP 3.1 开始提供线程绑定支持,OpenMP 4.5 开始较好支持,工具:numactl(参考http://www.glennklockwood.com/hpc-howtos/process-affinity.html)

export OMP_PROC_BIND=TRUE

● icc 可设置线程亲和性(参考https://software.intel.com/en-us/node/522691)

export KMP_AFFINITY = [<modifier>,...] <type> [,<permute>] [,<offset>]

▶ PETSc (Portable Extensible Toolkit for Sciencific Computation)讲座相关

● Advanced Sciencific Computing:

  ■ 应用上(Large and Complex)

  ■ 算法上(fully or semi implicit, multileve, nested, hierarchical, computer architure aware)

  ■ 并行化(Libraries, extensible solvers, composable)

● 部分幻灯片

● 终端中的代码

cd petsc-3.10./
module add mpich
module add petsc
cd src/vec/vec/examples/tutorials/
ls -al
make ex2
srun -c mpiexec -n ./ ex2 # 指定 核心

分布式计算课程补充笔记 part 3的更多相关文章

  1. 分布式计算课程补充笔记 part 4

    ▶ 并行通讯方式: map 映射 全局一到一 全局单元素计算操作 transpose 转置 一到一 单元素位移 gather 收集 多到一 元素搬运不计算 scatter 分散 一到多 元素搬运不计算 ...

  2. 分布式计算课程补充笔记 part 2

    ▶ 并行计算八字原则:负载均衡,通信极小 ▶ 并行计算基本形式:主从并行.流水线并行.工作池并行.功能分解.区域分解.递归分治 ▶ MPI 主要理念:进程 (process):无共享存储:显式消息传递 ...

  3. 分布式计算课程补充笔记 part 1

    ▶ 高性能计算机发展历程 真空管电子计算机,向量机(Vector Machine),并行向量处理机(Parallel Vector Processors,PVP),分布式并行机(Parallel Pr ...

  4. 分布式计算课程补充笔记 part 1.5

    ▶ 编写 SLURM 脚本 #!/bin/bash #SBATCH -J name # 任务名 #SBATCH -p gpu # 分区名,可为 cpu 或 gpu #SBATCH -N # 节点数 # ...

  5. (转载)林轩田机器学习基石课程学习笔记1 — The Learning Problem

    (转载)林轩田机器学习基石课程学习笔记1 - The Learning Problem When Can Machine Learn? Why Can Machine Learn? How Can M ...

  6. 03、同事分享课程的笔记 —《Android应用低功耗设计》

    这是安卓组的同事一个月前分享的一节课程,听课时写了一下笔记,之前是写在本子上的,感觉内容挺不错 的,就保存在博客了吧,方便回看. 他曾经在就职于英特尔公司,是与芯片设计相关的,这课程标题虽然是与安卓相 ...

  7. Coursera台大机器学习基础课程学习笔记1 -- 机器学习定义及PLA算法

    最近在跟台大的这个课程,觉得不错,想把学习笔记发出来跟大家分享下,有错误希望大家指正. 一机器学习是什么? 感觉和 Tom M. Mitchell的定义几乎一致, A computer program ...

  8. 分布式计算框架学习笔记--hadoop工作原理

    (hadoop安装方法:http://blog.csdn.net/wangjia55/article/details/53160679这里不再累述) hadoop是针对大数据设计的一个计算架构.如果你 ...

  9. [基础]斯坦福cs231n课程视频笔记(三) 训练神经网络

    目录 training Neural Network Activation function sigmoid ReLU Preprocessing Batch Normalization 权重初始化 ...

随机推荐

  1. mysql 判断某字段是否包含中文

    SELECT col FROM table WHERE LENGTH(col) != CHAR_LENGTH(col) LENGTH() 函数:返回字符串的长度,已字节符为单位 CHAR_LENGTH ...

  2. 阶段01Java基础day18集合框架04

    18.01_集合框架(Map集合概述和特点) A:Map接口概述 查看API可以知道: 将键映射到值的对象 一个映射不能包含重复的键 每个键最多只能映射到一个值 B:Map接口和Collection接 ...

  3. 容器的注入和container设计的思想——Injection Container 理解

    为什么会出现容器的注入? 容器:顾名思义,装东西的器物. 至于spring中bean,aop,ioc等一些都只是实现的方式:具体容器哪些值得我们借鉴,我个人觉得是封装的思想.将你一个独立的系统功能放到 ...

  4. Alpha冲刺9

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10034872.html 作业博客:https://edu.cnblogs.com/campus ...

  5. gzip 所使用压缩算法的基本原理(选摘)

    摘自:http://blog.csdn.net/ghevinn/article/details/45747465  gzip 所使用压缩算法的基本原理 gzip 对于要压缩的文件,首先使用LZ77算法 ...

  6. c#的默认访问权限

    1.命名空间下的元素的默认访问修饰符 public : 同一程序集的其他任何代码或引用该程序集的其他程序集都可以访问该类型或成员. internal : 同一程序集中的任何代码都可以访问该类型或成员, ...

  7. activity select problem(greedy algorithms)

    many activities will use the same place, every activity ai has its'  start time si and finish time f ...

  8. CentOS上升级gcc编译器使支持C++11

    首先向博主致敬,好的东西拿来共享了,用一下不错. https://blog.csdn.net/clirus/article/details/62424517 0. 目标  最近在学习c++11,我本机 ...

  9. PythonStudy——PyCharm 选择性忽略PEP8代码风格警告信息

    用了几天的PyCharm,发现确实在编写Python代码上非常好用,但有一点体验不太好,就是代码编写时要按照PEP8代码风格编写,不然会有波浪线的警告信息.解决方法如下: 方法一:将鼠标移到提示的地方 ...

  10. oracle-taf

    http://blog.sina.com.cn/s/blog_48567d850102wck0.html配置目标:把RAC系统配置为“主-备”模式,即平时所有连接都在rac01这个节点上,当rac01 ...