#include<iostream>#include<omp.h>#include<string>intmain(int argc,char*argv[]){ std::cout <<"number of available processors: "<<omp_get_num_procs()<< std::endl; std::cout <<"number of threads: "<<omp_get_max_threads() << std::endl;auto n = std::stol(argv[1]); std::cout <<"we will form sum of numbers from 1 to "<< n << std::endl; // start timerauto t0 =omp_get_wtime();auto s =0LL;#pragmaompparallelforreduction(+ : s)for (auto i =1; i <= n; i++) { s += i; } // stop timerauto t1 =omp_get_wtime(); std::cout <<"sum: "<< s << std::endl; std::cout <<"elapsed wall clock time: "<< t1 - t0 <<" seconds"<< std::endl;return0;}
program example
use omp_lib
implicit none
integer(8) :: i, n, s
character(len=32) :: arg
real(8) :: t0, t1
print *, "number of available processors:", omp_get_num_procs()
print *, "number of threads:", omp_get_max_threads()
call get_command_argument(1, arg)
read(arg , *) n
print *, "we will form sum of numbers from 1 to", n
! start timer
t0 = omp_get_wtime()
s = 0
!$omp parallel do reduction(+:s)
do i = 1, n
s = s + i
end do
! stop timer
t1 = omp_get_wtime()
print *, "sum:", s
print *, "elapsed wall clock time (seconds):", t1 - t0
end program
$ ./example 1000000000
number of available processors: 4
number of threads: 4
we will form sum of numbers from 1 to 1000000000
sum: 500000000500000000
elapsed wall clock time: 1.08343 seconds
为了比较,我们可以重新运行这个例子,并将OpenMP线程的数量设置为1:
$ env OMP_NUM_THREADS=1 ./example 1000000000
number of available processors: 4
number of threads: 1
we will form sum of numbers from 1 to 1000000000
sum: 500000000500000000
elapsed wall clock time: 2.96427 seconds