intelvisualfortran在visualstudio中如何正常的使用openmp并行程序文档格式.docx
《intelvisualfortran在visualstudio中如何正常的使用openmp并行程序文档格式.docx》由会员分享,可在线阅读,更多相关《intelvisualfortran在visualstudio中如何正常的使用openmp并行程序文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
![intelvisualfortran在visualstudio中如何正常的使用openmp并行程序文档格式.docx](https://file1.bdocx.com/fileroot1/2023-2/1/48f2bd7b-eec2-4caf-b01a-35d6074568ec/48f2bd7b-eec2-4caf-b01a-35d6074568ec1.gif)
However,theparallelversioncomputesthesuminadifferent
orderthantheserialversion;
someofthequantitiesaddedare
quitesmall,andsothiswillaffecttheaccuracyoftheresults.
Modified:
29January2003
Author:
JohnBurkardt
AFORTRAN90modulemaybeavailable:
useomp_lib
AFORTRAN77includefilemaybeavailable:
include'
omp_lib.h'
implicitnone
integer,parameter:
:
r4_logn_max=9
integerid
integernthreads
integeromp_get_num_procs
integeromp_get_num_threads
integeromp_get_thread_num
calltimestamp()
write(*,'
(a)'
)'
'
TEST_OMP'
FORTRAN90version'
EstimatethevalueofPIbysummingaseries.'
ThisprogramincludesOpenMPdirectives,which'
maybeusedtoruntheprograminparallel.'
Thenumberofprocessorsavailable:
'
(a,i8)'
OMP_GET_NUM_PROCS()='
omp_get_num_procs()
nthreads=4
(a,i8,a)'
CallOMP_SET_NUM_THREADS,andrequest'
&
nthreads,'
threads.'
callomp_set_num_threads(nthreads)
NotethatthecalltoOMP_GET_NUM_THREADSwillalwaysreturn1
ifcalledoutsideaparallelregion!
$OMPparallelprivate(id)
id=omp_get_thread_num()
(a,i3)'
Thisisprocess'
id
if(id==0)then
CallingOMP_GET_NUM_THREADSinsidea'
parallelregion,wegetthenumberof'
threadsis'
omp_get_num_threads()
endif
$OMPendparallel
callr4_test(r4_logn_max)
Normalendofexecution.'
stop
end
subroutiner4_test(logn_max)
R4_TESTestimatesthevalueofPIusingsingleprecision.
PIisestimatedusingNterms.Nisincreasedfrom10^2to10^LOGN_MAX.
ThecalculationisrepeatedusingbothsequentialandOpenMPenabledcode.
WallclocktimeismeasuredbycallingSYSTEM_CLOCK.
06January2003
integerclock_max
integerclock_rate
integerclock_start
integerclock_stop
realerror
realestimate
integerlogn
integerlogn_max
character(len=3)mode
integern
realr4_pi
realtime
R4_TEST:
EstimatethevalueofPI,'
usingsingleprecisionarithmetic.'
N=numberoftermscomputedandadded;
ESTIMATE=thecomputedestimateofPI;
ERROR=(thecomputedestimate-PI);
TIME=elapsedwallclocktime;
Notethatyoucan'
tincreaseNforever,because:
A)ROUNDOFFstartstobeaproblem,and'
B)maximumintegersizeisaproblem.'
(a,i12)'
Themaximuminteger:
huge(n)
NModeEstimateErrorTime'
n=1
dologn=2,logn_max
mode='
OMP'
callsystem_clock(clock_start,clock_rate,clock_max)
callr4_pi_est_omp(n,estimate)
callsystem_clock(clock_stop,clock_rate,clock_max)
time=real(clock_stop-clock_start)/real(clock_rate)
error=abs(estimate-r4_pi())
(i12,2x,a3,2x,f14.10,2x,g14.6,2x,g14.6)'
)&
n,mode,estimate,error,time
n=n*10
enddo
return
subroutiner4_pi_est_omp(n,estimate)
R4_PI_EST_OMPestimatesthevalueofPI,usingOpenMP.
Thecalculationisbasedontheformulafortheindefiniteintegral:
Integral1/(1+X**2)dx=Arctan(X)
Hence,thedefiniteintegral
Integral(0<
=X<
=1)1/(1+X**2)dx
=Arctan
(1)-Arctan(0)
=PI/4.
Astandardwaytoapproximateanintegralusesthemidpointrule.
IfwecreateNequallyspacedintervalsofwidth1/N,thenthe
midpointoftheI-thintervalis
X(I)=(2*I-1)/(2*N).
Theapproximationfortheintegralisthen:
Sum(1<
=I<
=N)(1/N)*1/(1+X(I)**2)
InordertocomputePI,wemultiplythisby4;
wealsocanpullout
thefactorof1/N,sothattheformulayouseeintheprogramlookslike:
(4/N)*Sum(1<
=N)1/(1+X(I)**2)
Untilroundoffbecomesanissue,greateraccuracycanbeachievedby
increasingthevalueofN.
Parameters:
Input,integerN,thenumberoftermstoaddup.
Output,realESTIMATE,theestimatedvalueofpi.
realh
integeri
realsum2
realx
h=1.0E+00/real(2*n)
sum2=0.0E+00
$OMPparalleldoprivate(x)shared(h)reduction(+:
sum2)
doi=1,n
x=h*real(2*i-1)
sum2=sum2+1.0E+00/(1.0E+00+x**2)
estimate=4.0E+00*sum2/real(n)
functionr4_pi()
R4_PIreturnsthevalueofpi.
02February2000
Output,realR4_PI,thevalueofpi.
r4_pi=3.14159265358979323846264338327950288419716939937510E+00
subroutinetimestamp()
TIMESTAMPprintsthecurrentYMDHMSdateasatimestamp.
Example:
May3120019:
45:
54.872AM
31May2001
None
character(len=8)ampm
integerd
character(len=8)date
integerh
integerm
integermm
character(len=9),parameter,dimension(12):
month=(/&
January'
'
February'
March'
April'
May'
June'
July'
August'
September'
October'
November'
December'
/)
integers
character(len=10)time
integervalues(8)
integery
character(len=5)zone
calldate_and_time(date,time,zone,values)
y=values
(1)
m=values
(2)
d=values(3)
h=values(5)
n=values(6)
s=values(7)
mm=values(8)
if(h<
12)then
ampm='
AM'
elseif(h==12)then
if(n==0.and.s==0)then
Noon'
else
PM'
h=h-12
Midnight'
(a,1x,i2,1x,i4,2x,i2,a1,i2.2,a1,i2.2,a1,i3.3,1x,a)'
trim(month(m)),d,y,h,'
n,'
s,'
.'
mm,trim(ampm)
=====================================
COPY上面的程序,可以完全运行成功,运行界面如下: