OpenMP for fortran(一)

标签:
fortranopenmp并行计算 |
分类: fortran |
博主一直对并行计算很感兴趣,感觉MPI有点复杂,先拿OpenMP打打下手,网上各种OpenMP教程很多,但大部分都是讲c的,讲fortran的很少,而且基本不讲如何让编译器支持openmp,博主参考http://fcode.cn/上给的教程,成功写出了第一个Openmp的fortran并行代码。
首先,CVF不支持openmp,我特地装了IVF和Visual studio,安装完成后进行如下设置:
(1) 在Visual studio中新建fortran
控制台程序,选择项目(Project) -> 属性(property) -> Fortran -> 语
言(Language),在Process OpenMP Directives 选项中选择Generate Parallel Code
(/Qopenmp),
点击确定以打开OpenMP 支持。
http://s13/mw690/003cvAxDzy6RCINEx3Kbc&690for
(2) 设置环境变量:我的电脑-> 属性-> 高级->
环境变量,在系统变量栏中新建一个
OMP_NUM_THREADS 变量,值设为2,即为程序执行的线程数(线程数一般设为处理器
核数)。
第一个程序:hello,openmp!
program test
implicit none
!$OMP PARALLEL
write(*,*)"hello,openmp!"
!$OMP END PARALLEL
end
http://s1/mw690/003cvAxDzy6RCIY0aME30&690for
第二个程序:
program test
implicit none
integer i
i=5
!$OMP PARALLEL
write(*,*)"hello,openmp!",i
!$OMP END PARALLEL
end
http://s5/mw690/003cvAxDzy6RCIY6ogA74&690for
第三个小程序:
program test
use omp_lib
implicit none
integer i
!$OMP PARALLEL
i=omp_get_num_threads()
write(*,*)'nums of threads are',i
!$OMP END PARALLEL
end
http://s7/mw690/003cvAxDzy6RCK3ugIK96&690for