主题:【求助】:请教信息技术方面的大牛有关多线程的问题 -- newtime
请教为何多线程执行起来花费时间比单线程要多呢?
非常感谢!!!
问题如下:
函数是用MONT CARLO方法计算圆周率。单线程计算函数两次共耗费大约是97s;而两个线程分别计算函数一次耗费的时间是392s.
环境:
CPU: i7920 4核
内存:3GB
操作系统:Windows Vista
编译器:Intel Fortran 90
程序如下:
多线程主程序:
Program TESTPROC0
use ifmt
use threadfunction
implicit none
integer,parameter::n_thread=2,n_c=2
integer(INT_PTR_KIND())::Threads_Handle(n_thread),waitresult,cend,retlog,retint,&
thread_handle_1,thread_handle_2
integer(INT_PTR_KIND()), PARAMETER :: security = 0
integer(INT_PTR_KIND()), PARAMETER :: stack_size = 0
integer(INT_PTR_KIND()) :: thread_id,rc
integer(4)::i,slot
real(kind(1d0))::pi,time_begin,time_end,t0,t1
call cpu_time(time_begin)
slot=0
Thread_Handle_1= CreateThread(security,stack_size,Thread_Proc0,loc(i), 0, thread_id)
Thread_Handle_2=CreateThread(security,stack_size,Thread_Proc0,loc(i), 0, thread_id)
waitresult=waitforsingleobject(thread_handle_1,infinite)
waitresult=waitforsingleobject(thread_handle_2,infinite)
cend=closehandle(thread_handle_1)
cend=closehandle(thread_handle_2)
call cpu_time(time_end)
t1=time_end-time_begin
write(6,*) "multithread computation takes time",t1,t1/n_c
pause
end program TESTPROC0
单线程主程序:
Program TESTPROC0
use threadfunction
implicit none
integer,parameter::n_c=2
integer(INT_PTR_KIND()) :: thread_id,rc
integer(4)::i,slot
real(kind(1d0))::pi,time_begin,time_end,t0,t1
call cpu_time(time_begin)
do i=1,n_c
pi=Thread_Proc0(i)
enddo
call cpu_time(time_end)
t0=time_end-time_begin
write(6,*) "computation takes time",t0,t0/n_c
pause
end program TESTPROC0
函数:
Module threadfunction
contains
real(kind(1d0)) function Thread_Proc0(arg) implicit none
!DEC$ ATTRIBUTES STDCALL, ALIAS:"_thread_proc0" :: Thread_Proc0
integer(4),intent(in):: arg
integer(4),parameter::j=999999999
integer(4)::i
real(kind(1d0))::x,y,u,q
q=0
do i=1,j
call random_number(x)
call random_number(y)
u=x**2.0d0+y**2.0d0
if (u<1.0d0) then
q=q+1.0d0
endif
enddo
Thread_proc0=q/j*4
end function Thread_Proc0
end Module threadfunction
- 相关回复 上下关系8
🙂【求助】:请教信息技术方面的大牛有关多线程的问题
🙂问题已经解决 newtime 字262 2009-08-17 02:14:40
🙂祝贺一下 1 autoeagle 字144 2009-08-17 02:27:51
🙂关于并行计算的一些基本概念的回忆 11 autoeagle 字1416 2009-08-16 17:47:47
🙂多线程多进程的优劣不可一概而论 浪人甲 字111 2009-08-20 19:36:27
🙂有什么并行计算baby level的书推荐么?谢谢啊 不高兴 字0 2009-08-20 10:23:02
🙂多线程这玩意纯粹是微软推广出来的 太极掌 字28 2009-08-19 02:37:33
🙂多线程的初衷不就是为了省资源吗 HAL 字108 2009-08-25 00:22:54