OpenMP并行计算入门

个人理解

OpenMP是一种通过共享内存并行系统的多处理器程序设计的编译处理方案,通过预编译指令告诉编译器哪些代码块需要被并行化,通过拷贝代码块实现并行程序。对于循环的并行化我的理解大概是这样的:

  1. 首先,将循环分成线程数个分组,每个分组执行若干个指令,一个分组代表一个线程
  2. 其中有一个为主线程,其他的均不是主线程,每个分组分别执行自己组内的代码
  3. 当所有组别的代码执行完毕之后,在最后会和,通过主线程将结果带回
  4. 关闭其他所有线程(只留下主线程)

我觉得openmp编程中最需要注意的就是哪些代码块可以被并行化,如果不能,怎么修改能让他可以被并行化,以及考虑好数据依赖、循环依赖、数据竞争等问题,保证运算结果的准确性。

配置

我使用的是ubuntu18.04,配置起来比较简单,一般需要安装gcc的。

  1. sudo apt-get install gcc

在编译之前我们需要加上-fopenmp,然后运行就可以执行我们带openmp的程序了。

  1. gcc -fopenmp filename.cpp -o filename

然后我们运行就可以了。

指令学习

parallel

  1. #pragma omp parallel

parallel的作用是使得其作用的代码块被并行执行n次,n默认为cpu核心数目,也可以通过set方法预先设置parallel的线程数目。

  1. omp_set_num_threads(8);
  2. int num = 0;
  3. #pragma omp parallel private(num)
  4. {
  5. int num = omp_get_thread_num();
  6. printf("thread num:%d\n",num);
  7. fflush(stdout);
  8. }
  9. /*
  10. 我解决了输出问题。。
  11. thread num:0
  12. thread num:4
  13. thread num:3
  14. thread num:2
  15. thread num:1
  16. thread num:5
  17. thread num:6
  18. thread num:7
  19. */

for

  1. #pragma omp parallel for[clause[claue..]]
  1. #pragma omp parallel for
  2. for(int i=0;i<10;i++){
  3. //cout<<omp_get_thread_num()<<endl;
  4. printf("thread num is:%d\n",omp_get_thread_num());
  5. fflush(stdout);
  6. }
  7. /*
  8. 输出结果:
  9. thread num is:0
  10. thread num is:0
  11. thread num is:0
  12. thread num is:1
  13. thread num is:2
  14. thread num is:2
  15. thread num is:1
  16. thread num is:1
  17. thread num is:3
  18. thread num is:3
  19. */

for循环使用parallel for指令很简单,只是要考虑到并行时候的循环依赖问题,下面会讨论到。

注意

从这里开始我都会用printf进行输出,因为我试了试cout输出,发现输出老是有问题,就是会输出空白行,我认为是在并行过程中cout<<value和<<endl被当成了两个代码代码块,当有的线程输出了换行符之后,有的线程刚好也输出换行符,就会导致我们看到很多换行符。所以解决这个问题最好的办法是用printf输出,就不会出现那样的问题了。

sections

sections与for不同的是将代码块分成不同的功能模块,而for做的是将代码块分成不同的数据模块,其特点是“功能并行”,for的并行特点是“数据并行”。例如,通过sections将代码块分成不同的section,每个section可以是功能不同的函数,这就是与for最大的不同。

  1. #pragma omp parallel sections
  2. {
  3. #pragma omp section
  4. {
  5. printf("thread num:%d\n",omp_get_thread_num());
  6. fflush(stdout);
  7. }
  8. #pragma omp section
  9. {
  10. printf("thread num:%d\n",omp_get_thread_num());
  11. fflush(stdout);
  12. }
  13. #pragma omp section
  14. {
  15. printf("thread num:%d\n",omp_get_thread_num());
  16. fflush(stdout);
  17. }
  18. #pragma omp section
  19. {
  20. printf("thread num:%d\n",omp_get_thread_num());
  21. fflush(stdout);
  22. }
  23. #pragma omp section
  24. {
  25. printf("thread num:%d\n",omp_get_thread_num());
  26. fflush(stdout);
  27. }
  28. #pragma omp section
  29. {
  30. printf("thread num:%d\n",omp_get_thread_num());
  31. fflush(stdout);
  32. }
  33. }
  34. /*输出结果
  35. thread num:1
  36. thread num:1
  37. thread num:0
  38. thread num:3
  39. thread num:1
  40. thread num:2
  41. */

哪个section执行哪个指令完全取决于实现;如果section个数多于线程数,每个section都将会被执行,但是如果section数少于线程数,有些线程就不会执行任何操作。如果omp后面不加parallel,默认section是按照串行的顺序执行的,只有加了parallel,才会按照并行的方式执行section。

single

single指令效果是让一条代码块的语句只由一个线程来处理。

  1. void func(){
  2. printf("hello");
  3. //cout<<"hello"<<endl;
  4. }
  5. #pragma omp single
  6. {
  7. func();
  8. }
  9. /*
  10. 输出结果(只由一个线程执行一次):
  11. hello
  12. */

master

master指令表示只能由主线程来执行,其他线程不能执行该代码块的指令。

  1. #pragma omp parallel
  2. {
  3. #pragma omp master
  4. {
  5. for(int i=0;i<10;i++){
  6. cout<<"thread num is:"<<omp_get_thread_num()<<",i:"<<i<<endl;
  7. }
  8. }
  9. }
  10. /*
  11. 可以看出来,只有master线程执行了我们的for循环,输出有序。
  12. 输出结果:
  13. thread num is:0,i:0
  14. thread num is:0,i:1
  15. thread num is:0,i:2
  16. thread num is:0,i:3
  17. thread num is:0,i:4
  18. thread num is:0,i:5
  19. thread num is:0,i:6
  20. thread num is:0,i:7
  21. thread num is:0,i:8
  22. thread num is:0,i:9
  23. */

critical

critical指令表明该指令包裹的代码块只能由一个线程来执行,不能被多个线程同时执行。注意,如果多个线程试图执行同一个critical代码块的时候,其他线程会被堵塞,也就是排队等着,直到上一线程完成了代码块的操作,下一个线程才能继续执行这一代码块。

与single不同的是,single只能执行一次,并且是单线程执行,执行完了就不会再执行了,而critical可以多次执行。

  1. int sum = 0;
  2. #pragma omp parallel for shared(sum)
  3. for(int i=0;i<10;i++){
  4. #pragma omp critical
  5. {
  6. printf("thread:%d,sum:%d\n",omp_get_thread_num(),sum);
  7. sum++;
  8. }
  9. }
  10. cout<<sum<<endl;
  11. /*输出结果
  12. thread:0,sum:0
  13. thread:0,sum:1
  14. thread:0,sum:2
  15. thread:2,sum:3
  16. thread:2,sum:4
  17. thread:1,sum:5
  18. thread:1,sum:6
  19. thread:1,sum:7
  20. thread:3,sum:8
  21. thread:3,sum:9
  22. 10
  23. */

从句

可以通过参数private将变量变成每个线程私有的变量,这样,其他线程对该变量的修改不会影响其他线程的变量。这里空说可能不理解,但是可以通过下面的例子来理解。先来看看什么是循环依赖。

依赖(循环依赖)

  1. #include <iostream>
  2. #include <omp.h>
  3. using namespace std;
  4. int main(){
  5. int fib[1000];
  6. fib[0] = 1;
  7. fib[1] = 1;
  8. #pragma omp parallel for
  9. for(int i=2;i<20;i++){
  10. fib[i] = fib[i-2] +fib[i-1];
  11. }
  12. for(int i=0;i<20;i++){
  13. cout<<i<<":"<<fib[i]<<endl;
  14. }
  15. return 0;
  16. }
  17. /*
  18. 输出结果:
  19. 0:1
  20. 1:1
  21. 2:2
  22. 3:3
  23. 4:5
  24. 5:8
  25. 6:13
  26. 7:6
  27. 8:6
  28. 9:12
  29. 10:18
  30. 11:30
  31. 12:0
  32. 13:0
  33. 14:0
  34. 15:0
  35. 16:0
  36. 17:0
  37. 18:0
  38. 19:0
  39. */

可以看到,后面很多数据的结果都是0,还有很多算的是错的(7之后的数据),为什么呢?因为这里有循环依赖。循环依赖指的是不同线程之间有变量之间的依赖,这个例子中,由于线程之间有变量之间的依赖,所以必须要前面的线程算完了,后面的线程再在前面线程算的结果的基础上来算才能算出正确的结果,有些时候呢,我们可以通过更改逻辑来避免循环依赖,从而使循环在并行下也可以得到计算,并且不会算错。给个例子(这个例子是网上找的):

  1. double factor = 1;
  2. double sum = 0.0;
  3. int n = 1000;
  4. #pragma omp parallel for reduction(+:sum)
  5. for (int i = 0; i < n; i++)
  6. {
  7. sum += factor / (2 * i + 1);
  8. factor = -factor;
  9. }
  10. double pi_approx = 4.0*sum;
  11. cout<<pi_approx<<endl
  12. //上面的例子会输出错误的值,这是因为不同线程之间有循环依赖
  13. //消除循环依赖后
  14. #pragma omp parallel for reduction(+:sum)
  15. for (int i = 0; i < n; i++)
  16. {
  17. if(i%2){
  18. factor = 1;
  19. }else{
  20. factor = -1;
  21. }
  22. sum += factor / (2 * i + 1);
  23. }
  24. double pi_approx = 4.0*sum;
  25. cout<<pi_approx<<endl
  26. /*这下消除了循环依赖,但结果其实依然不正确,
  27. 这是因为不同的线程之间的factor其实还是shared的,
  28. 这样一个线程给shared赋值的时候可能会影响到其他线程对
  29. sum求和时读取factor变量的错误,因此我们只需要再改一步,
  30. 让factor变为private即可。
  31. */
  32. #pragma omp parallel for reduction(+:sum) private(factor)

schedule

schedule是对循环并行控制中的任务调度的指令,例如,我们在写parallel for的时候,我们开n个线程去并行执行一段for循环代码,但是我们怎么知道哪个线程执行哪次迭代的代码呢?我们能不能控制呢?schedule就是做这个工作的。parallel for下我们默认是通过static进行调度的,每个线程分配chunk_size个迭代任务,而这个chunk_size是通过迭代数除以线程数算出来的(最后一个线程可能分配的少一些)。

type

  • static:静态分配任务,指的是分配的过程跟运行的时候任务分配无关
  • dynamic:动态调度,不指定size,则按照谁先执行完任务下一次迭代就分配给谁,无法提前预知哪个线程执行哪次迭代。而指定size之后,表示每次执行完任务之后分配给一个线程的迭代任务数量。
  • guided:
  • runtime:根据环境变量确定是上面调度策略中的哪种。
  1. #pragma omp parallel schedule(type,size)
  2. //这里type可以选择static、dynamic、guided、runtime
  3. omp_set_num_threads(4);
  4. //int sum =0;
  5. #pragma omp parallel for schedule(static,4)
  6. for(int i=0;i<16;i++){
  7. //sum+=i;
  8. printf("thread is:%d,thread sum is:%d\n",omp_get_thread_num(),i);
  9. fflush(stdout);
  10. }
  11. /*
  12. 输出
  13. thread is:2,thread sum is:8
  14. thread is:2,thread sum is:9
  15. thread is:1,thread sum is:4
  16. thread is:1,thread sum is:5
  17. thread is:1,thread sum is:6
  18. thread is:1,thread sum is:7
  19. thread is:2,thread sum is:10
  20. thread is:2,thread sum is:11
  21. thread is:3,thread sum is:12
  22. thread is:3,thread sum is:13
  23. thread is:3,thread sum is:14
  24. thread is:3,thread sum is:15
  25. thread is:0,thread sum is:0
  26. thread is:0,thread sum is:1
  27. thread is:0,thread sum is:2
  28. thread is:0,thread sum is:3
  29. */
  30. //dynamic
  31. omp_set_num_threads(4);
  32. //int sum =0;
  33. #pragma omp parallel for schedule(dynamic,4)
  34. for(int i=0;i<16;i++){
  35. //sum+=i;
  36. printf("thread is:%d,thread sum is:%d\n",omp_get_thread_num(),i);
  37. fflush(stdout);
  38. }
  39. /*输出
  40. thread is:2,thread sum is:4
  41. thread is:2,thread sum is:5
  42. thread is:3,thread sum is:12
  43. thread is:2,thread sum is:6
  44. thread is:2,thread sum is:7
  45. thread is:0,thread sum is:0
  46. thread is:0,thread sum is:1
  47. thread is:0,thread sum is:2
  48. thread is:0,thread sum is:3
  49. thread is:3,thread sum is:13
  50. thread is:3,thread sum is:14
  51. thread is:3,thread sum is:15
  52. thread is:1,thread sum is:8
  53. thread is:1,thread sum is:9
  54. thread is:1,thread sum is:10
  55. thread is:1,thread sum is:11
  56. */

从以上两个输出呀我们可以看出,static的线程id和输出的值我们是可以预知的,但是dynamic的输出值和线程id不是对应的,多次输出可以看出来,是动态分配的。

ordered

ordered指令顾名思义,可以让循环代码中的某些代码执行按照一定的顺序来执行。下面的实例告诉我们了ordered是怎么工作的。

  1. #pragma omp parallel for ordered
  2. for(int i=0;i<20;i++){
  3. //#pragma omp ordered
  4. {
  5. cout<<"thread num is:"<<omp_get_thread_num();
  6. }
  7. cout<<",i:"<<i<<endl;
  8. }
  9. /*
  10. 可以看出ordered下for循环按照顺序执行,线程也是按照顺序执行,等价于用不同的线程去串行执行一段代码
  11. 输出结果:
  12. thread num is:0,i:0
  13. thread num is:0,i:1
  14. thread num is:0,i:2
  15. thread num is:1,i:3
  16. thread num is:1,i:4
  17. thread num is:1,i:5
  18. thread num is:2,i:6
  19. thread num is:2,i:7
  20. thread num is:2,i:8
  21. thread num is:3,i:9
  22. thread num is:3,i:10
  23. thread num is:3,i:11
  24. thread num is:4,i:12
  25. thread num is:4,i:13
  26. thread num is:5,i:14
  27. thread num is:5,i:15
  28. thread num is:6,i:16
  29. thread num is:6,i:17
  30. thread num is:7,i:18
  31. thread num is:7,i:19
  32. */

private

表明某个变量是私有的

  1. #pragma omp parallel private(name)
  2. //代码是循环依赖的代码

上面有跑例子的

firstprivate

表明某个变量线程私有,但是在进入线程之前先给私有变量赋全局变量的初值

  1. //用法
  2. #pragma omp parallel firstprivate(name)
  3. //例子
  4. int sum = 0;
  5. #pragma omp parallel for firstprivate(sum)
  6. for(int i=0;i<20;i++){
  7. sum+=i;
  8. printf("thread num is:%d,sum is:%d\n",omp_get_thread_num(),sum);
  9. //cout<<"thread num is:"<<omp_get_thread_num()<<",sum is:"<<sum<<endl;
  10. }
  11. cout<<"real sum is:"<<sum<<endl;
  12. /*下面是输出结果,这里可以看出不同线程计算得到的sum是不一样的,
  13. 并且不影响全局变量sum 的值。sum在最开始都初始化为0,输出的是加上i之后的值。
  14. */
  15. /*
  16. thread num is:0,sum is:0
  17. thread num is:0,sum is:1
  18. thread num is:0,sum is:3
  19. thread num is:0,sum is:6
  20. thread num is:0,sum is:10
  21. thread num is:3,sum is:15
  22. thread num is:3,sum is:31
  23. thread num is:3,sum is:48
  24. thread num is:3,sum is:66
  25. thread num is:3,sum is:85
  26. thread num is:2,sum is:10
  27. thread num is:2,sum is:21
  28. thread num is:2,sum is:33
  29. thread num is:2,sum is:46
  30. thread num is:1,sum is:5
  31. thread num is:1,sum is:11
  32. thread num is:1,sum is:18
  33. thread num is:2,sum is:60
  34. thread num is:1,sum is:26
  35. thread num is:1,sum is:35
  36. real sum is:0
  37. */

lastprivate

表明某个变量线程私有,但是在线程结束之后将最后一个section的值赋给全局变量,本来也没闹清楚,后来实验了一下,大致清楚了,因为不同section之间计算的结果不一样嘛,就是最后执行完运算的那个section把值赋给全局变量

  1. #pragma omp parallel lastprivate(name)
  2. /*
  3. 下面是具体实验
  4. 可以看到,全局的sum输出的其实是thread3最后的结果,因为thread3到最后执行的时候sum=8,i=9,加起来就是17了,所以在最后取最后一个thread计算的结果赋值给全局变量sum。
  5. */
  6. int sum = 0;
  7. #pragma omp parallel for lastprivate(sum)
  8. for(int i=0;i<10;i++){
  9. printf("thread:%d,sum:%d\n",omp_get_thread_num(),sum);
  10. sum +=i;
  11. }
  12. cout<<sum<<endl;
  13. /*
  14. thread:2,sum:0
  15. thread:2,sum:6
  16. thread:1,sum:0
  17. thread:1,sum:3
  18. thread:1,sum:7
  19. thread:3,sum:0
  20. thread:3,sum:8
  21. thread:0,sum:32767
  22. thread:0,sum:32767
  23. thread:0,sum:32768
  24. 17*/

shared

表明某个变量是线程之间共享的.

  1. #pragma omp parallel shared(name)
  2. //这个默认就是shared的,不需要代码实验

default

要么是shared,要么是private.

  1. #pragma omp parallel default(shared)
  2. //一样不需要代码实验

reduction

通过operator规约,避免数据竞争,表明某个变量私有,在线程结束后加到全局变量上去,reduction每个线程创建变量的副本,按照operator进行初始化值,然后在线程结束的时候都加到全局变量之上。

  1. #pragma omp parallel reduction(operation:name)
  2. //实验在上面循环依赖的代码那里
  1. int res = 10;
  2. #pragma omp parallel for reduction(+:res)
  3. for(int i=0;i<10000;i++){
  4. res = res + i;
  5. }
  6. /*
  7. 这个程序会创建n个线程(默认取决与你的cpu数量),
  8. 每个线程里的res被初始化为0并且线程私有,
  9. 当每个线程都计算完成之后,将自己线程内的res值加到全局的res上去。
  10. 最后跟不parallel的结果一样,但是实现过程不一样。
  11. */

在循环次数比较多的情况下,会发生数据竞争(因为默认在并行体外定义的变量是shared的,在里面定义的是private的,shared的数据会由于在并行体内被修改而影响其他线程的赋值,所以会发生数据竞争),所以通过reduction规约,可以避免这种情况发生。

operator对应的初始化值的表(表格是网上找的):

操作 操作符 初始值
加法 + 0
乘法 * 1
减法 - 0
逻辑与 && 0
逻辑或 || 0
按位与 & 1
按位或 | 0
按位异或 ^ 0
相等 true
不等 false
最大值 max 最小负值
最小值 min 最大正值

实验部分

由于我随便跑了一跑高斯模糊算法的手动实现(之前用python)写过,这次由于学了OpenMP这个并行编程的东西,打算跑一跑看看到底能提高多快(我的算法是最原始的算法),我的算法需要有opencv库(虽然opencv中已经有GaussianBLur函数,而且速度很快),但我主要想弄清楚高斯模糊算法到底怎么回事才用这么复杂的矩阵计算做的。

在qt下需要在pro文件中加入以下配置,这里由于我用到了opencv,所以还需要加入这样的配置:

  1. QMAKE_CXXFLAGS+= -fopenmp
  2. LIBS+= -lgomp -lpthread
  3. INCLUDEPATH+=/usr/local/include\
  4. /usr/local/include/opencv\
  5. /usr/local/include/opencv2
  6. LIBS+=/usr/local/lib/libopencv_highgui.so\
  7. /usr/local/lib/libopencv_core.so\
  8. /usr/local/lib/libopencv_imgproc.so\
  9. /usr/local/lib/libopencv_imgcodecs.so

下面是parallel和不parallel的结果对比:

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. #include "opencv2/opencv.hpp"
  4. #include <omp.h>
  5. #include <vector>
  6. #include <time.h>
  7. #include <math.h>
  8. #define PI 3.1415926
  9. using namespace cv;
  10. using namespace std;
  11. Mat getWeight(int r=3){
  12. int l = r*2+1;
  13. float sum=0;
  14. Mat temp(l,l,CV_32FC1);
  15. //
  16. #pragma omp parallel for reduction(+:sum)
  17. for(int i=0;i<temp.rows;i++){
  18. for(int j=0;j<temp.cols;j++){
  19. temp.at<float>(i,j) = 0.5/(PI*r*r)*exp(-((i-l/2.0)*(i-l/2.0) + (j-l/2.0)*(j-l/2.0))/2.0/double(r)/double(r));
  20. sum = sum+ temp.at<float>(i,j);
  21. //cout<<sum<<endl;
  22. }
  23. }
  24. return temp/sum;
  25. }
  26. float Matrix_sum(Mat src){
  27. float temp=0;
  28. for(int i=0;i<src.rows;i++){
  29. for(int j=0;j<src.cols;j++){
  30. temp +=src.at<float>(i,j);
  31. }
  32. }
  33. return temp;
  34. }
  35. Mat GaussianBlur_parallel(Mat src,Mat weight,int r=3){
  36. Mat out(src.size(),CV_32FC1);
  37. Mat temp;
  38. int rows = src.rows-r;//shared
  39. int cols = src.cols-r;//shared
  40. #pragma omp parallel for
  41. for(int i=r;i<rows;i++){
  42. for(int j=r;j<cols;j++){
  43. Mat temp(src,Range(i-r,i+r+1),Range(j-r,j+r+1));//int to float32
  44. temp.convertTo(temp,CV_32FC1);
  45. out.at<float>(i,j) = Matrix_sum(temp.mul(weight));
  46. //cout<<"thread num is:"<<omp_get_num_threads()<<endl;
  47. }
  48. }
  49. out.convertTo(out,CV_8U);
  50. return out;
  51. }
  52. Mat GaussianBlur_normal(Mat src,Mat weight,int r=3){
  53. Mat out(src.size(),CV_32FC1);
  54. for(int i=r;i<src.rows-r;i++){
  55. for(int j=r;j<src.cols-r;j++){
  56. Mat temp(src,Range(i-r,i+r+1),Range(j-r,j+r+1));
  57. temp.convertTo(temp,CV_32FC1);
  58. //cout<<Matrix_sum(temp.mul(weight))<<endl;
  59. //cout<<"("<<i-r<<","<<i+r+1<<")"<<","<<"("<<j-r<<","<<j+r+1<<")"<<"raw:"<<src.size()<<endl;
  60. out.at<float>(i,j) = Matrix_sum(temp.mul(weight));
  61. }
  62. }
  63. out.convertTo(out,CV_8U);
  64. return out;
  65. }
  66. int main(int argc, char *argv[])
  67. {
  68. QApplication a(argc, argv);
  69. omp_set_num_threads(8);
  70. vector<Mat> split_img,out_img;
  71. Mat src = imread("/home/xueaoru/projects/srm/car.jpg"),normal_img,parallel_img;
  72. Mat weight = getWeight();
  73. split(src,split_img);
  74. out_img.clear();
  75. double start_time = (double)getTickCount(),end_time;
  76. for(auto s:split_img){
  77. out_img.push_back(GaussianBlur_normal(s,weight));
  78. }
  79. end_time = (double)getTickCount();
  80. cout<<"Normal compute time:"<<(end_time-start_time)*1000/getTickFrequency()<<"ms"<<endl;
  81. merge(out_img,normal_img);
  82. imshow("normal GaussianBlur",normal_img);
  83. out_img.clear();
  84. start_time = (double)getTickCount();
  85. for(auto s:split_img){
  86. out_img.push_back(GaussianBlur_parallel(s,weight));
  87. }
  88. end_time = (double)getTickCount();
  89. cout<<"parallel compute time:"<<(end_time-start_time)*1000/getTickFrequency()<<"ms"<<endl;
  90. merge(out_img,parallel_img);
  91. imshow("parallel GaussianBlur",parallel_img);
  92. imshow("raw",src);
  93. return a.exec();
  94. }
  95. /*
  96. 输出结果如下:
  97. Normal compute time:1609.72ms
  98. parallel compute time:871.309ms
  99. */

[OpenMP] 并行计算入门的更多相关文章

  1. 在fortran下进行openmp并行计算编程

    最近写水动力的程序,体系太大,必须用并行才能算的动,无奈只好找了并行编程的资料学习了.我想我没有必要在博客里开一个什么并行编程的教程之类,因为网上到处都是,我就随手记点重要的笔记吧.这里主要是open ...

  2. openmp并行计算

    #include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...

  3. 并行计算之OpenMP中的任务调度

    本文参考<OpenMP中的任务调度>博文,主要讲的是OpenMP中的schedule子句用法. 一.应用需求 在OpenMP并行计算中,任务调度主要用于并行的for循环.当for循环中每次 ...

  4. OpenMP 入门

    OpenMP 入门 简介 OpenMP 一个非常易用的共享内存的并行编程框架,它提供了一些非常简单易用的API,让编程人员从复杂的并发编程当中释放出来,专注于具体功能的实现.openmp 主要是通过编 ...

  5. 通过 GCC 学习 OpenMP 框架

     OpenMP 框架是使用 C.C++ 和 Fortran 进行并发编程的一种强大方法.GNU Compiler Collection (GCC) V4.4.7 支持 OpenMP 3.0 标准,而 ...

  6. openMP的一点使用经验【非原创】

    按照百科上说的,针对于openmp的编程,最简单的就是在开头加个#include<omp.h>,然后在后面的for上加一行#pragma omp parallel for即可,下面的是较为 ...

  7. CentOS6中OpenMP的运行时间或运行性能分析

    OpenMp作为单机多核心共享内存并行编程的开发工具,具有编码简洁等,容易上手等特点. 关于OpenMP的入门,博主饮水思源(见参考资料)有了深入浅出,循序渐进的分析.做并行开发,做性能分析是永远逃避 ...

  8. 并行计算之OpenMP入门简介

    在上一篇文章中介绍了并行计算的基础概念,也顺便介绍了OpenMP. OpenMp提供了对于并行描述的高层抽象,降低了并行编程的难度和复杂度,这样程序员可以把更多的精力投入到并行算法本身,而非其具体实现 ...

  9. OpenMP 入门教程

    前两天(其实是几个月以前了)看到了代码中有 #pragma omp parallel for 一段,感觉好像是 OpenMP,以前看到并行化的东西都是直接躲开,既然躲不开了,不妨研究一下: OpenM ...

随机推荐

  1. HDU - 1114 Piggy-Bank 完全背包(背包恰好装满)

    Piggy-Bank Before ACM can do anything, a budget must be prepared and the necessary financial support ...

  2. .Net HttpWebRequest 爬虫核心爬取

    1 爬虫,爬虫攻防 2 下载html 3 xpath解析html,获取数据和深度抓取(和正则匹配) 4 多线程抓取 熟悉http协议 提供两个方法Post和Get public static stri ...

  3. 纯CSS,多个半圆以中心点旋转

    效果图: html代码: <div style=" background:#000; position: relative; width:300px; height:300px;&qu ...

  4. 剑指Offer的学习笔记(C#篇)-- 替换空格

    题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 一 . 自己的想法 老实说,貌 ...

  5. ios 自定义cell类中获取当前controller push

    有时候在自定义cell的过程中,当cell中又button的时候,把button的点击时间写在cell中的时候,需要获取到cell的父视图控制器然后push,可以自建一个类,命名为: GetCurre ...

  6. hammerjs jquery的选项使用方法,以给swipe设置threshold和velocity为例

    先包含hammer.min.js和 jquery.hammer.js,然后: var $ele = $('#ele'); //复用jquerydom对象,建个变量 $ele.hammer().on(& ...

  7. Linux上部署黑马旅游网Bug集锦

  8. F-三生三世

    链接:https://ac.nowcoder.com/acm/contest/892/F 题意: 秦皇岛的海风轻轻地唱着歌唤醒了水上的涟漪,冬日的阳光把沙滩洒满了金黄. BD哥在沙滩上留下了一串串脚印 ...

  9. 洛谷1220(区间dp)

    要点 处于什么位置的题常用一个套路就是搞完\([l,r]\)以后处于0(l)或1(r)的状态,即\(dp[i][j][0/1]\). 对于此题dp意义为已经搞完\([l,r]\)的时最小的已耗电能,转 ...

  10. Codeforces 1111D(退背包、排列组合)

    要点 优质题解 因为只有某type坏人全部分布在同一撇时,才能一次消灭.所以题目安排完毕后一定是type(x)和type(y)占一半,其余占另一半. 实际情况只有52*52种,则预处理答案 枚举某两种 ...