您好,欢迎访问三七文档
CPU信息:openMP:#includestdafx.h#includeWindows.h#includemath.h#includetime.h#includeomp.h#includeiostreamusingnamespacestd;int_tmain(intargc,_TCHAR*argv[]){printf(求水仙花数\n);cout并行结果:endl;clock_tt1=clock();#pragmaompparallelforfor(inti=100;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}clock_tt2=clock();doublept=t2-t1-0.0;printf(并行时间%f\n,pt-0.0);clock_tt3=clock();for(inti=100;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}clock_tt4=clock();doublest=t4-t3-0.0;printf(串行时间%f\n,st-0.0);printf(加速比%f\n,st/pt);system(pause);return0;}运行结果:MPI://MPI.cpp:定义控制台应用程序的入口点。#includestdafx.h#includempi.h#includewindows.h#includestdio.h#includeiostream#includemath.h#includetime.husingnamespacestd;int_tmain(intargc,char*argv[]){intid,n,numproc;clock_tst;MPI_Init(&argc,&argv);MPI_Comm_rank(MPI_COMM_WORLD,&id);MPI_Comm_size(MPI_COMM_WORLD,&numproc);if(id==0){cout并行结果:endl;st=clock();}MPI_Bcast(&n,1,MPI_INT,0,MPI_COMM_WORLD);for(inti=100+id;i1000;i+=numproc){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}if(id==0){clock_tet=clock();doublept=et-st-0.0;cout并行用时:ptendl;cout串行结果:endl;clock_tt1=clock();for(inti=100;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}clock_tt2=clock();doublest=t2-t1-0.0;cout串行用时:stendl;cout加速比:st/ptendl;}MPI_Finalize();return0;}运行结果:Win32://Win32_flower.cpp:定义控制台应用程序的入口点。#includestdafx.h#includeWindows.h#includetime.h#includeiostreamusingnamespacestd;HANDLEfinish[2];HANDLEfinish3;DWORDWINAPIThreadOne(LPVOIDparam){for(inti=100;i500;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}SetEvent(finish[0]);return0;}DWORDWINAPIThreadTwo(LPVOIDparam){for(inti=500;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}SetEvent(finish[1]);return0;}DWORDWINAPIThreadThree(LPVOIDparam){for(inti=100;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}SetEvent(finish3);return0;}int_tmain(intargc,_TCHAR*argv[]){clock_tt1=clock();cout并行结果:endl;finish[0]=CreateEvent(NULL,false,false,NULL);finish[1]=CreateEvent(NULL,false,false,NULL);HANDLEthread1=CreateThread(NULL,0,ThreadOne,NULL,0,NULL);HANDLEthread2=CreateThread(NULL,0,ThreadTwo,NULL,0,NULL);WaitForMultipleObjects(2,finish,true,INFINITE);clock_tt2=clock();doublept=t2-t1-0.0;printf(并行时间:%f\n,pt);doubleti=t2-t1;clock_tt3=clock();cout串行结果:endl;finish3=CreateEvent(NULL,false,false,NULL);HANDLEthread3=CreateThread(NULL,0,ThreadThree,NULL,0,NULL);WaitForSingleObject(finish3,INFINITE);clock_tt4=clock();doublest=t4-t3-0.0;printf(串行时间:%f\n,st);printf(加速比:%f\n,st/pt);system(pause);return0;}运行结果:MFC:#includestdafx.h#includeafxmt.h#includeiostream#includeafxwin.husingnamespacestd;CEventfaxEvent(false);CEventfaxEvent1(false);CEventfaxEvent2(false);CSemaphoreg_clsSemaphore(2,2);UINTThreadProc1(LPVOIDparam){g_clsSemaphore.Lock();for(inti=100;i500;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}g_clsSemaphore.Unlock();SetEvent(faxEvent1);return0;}UINTThreadProc2(LPVOIDparam){g_clsSemaphore.Lock();for(inti=500;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}g_clsSemaphore.Unlock();SetEvent(faxEvent2);return0;}UINTThreadProc3(LPVOIDparam){g_clsSemaphore.Lock();for(inti=100;i1000;i++){intj=i/100;intk=(i-100*j)/10;intl=i%10;Sleep(1);if(j*j*j+k*k*k+l*l*l==i)printf(%d\n,i);}g_clsSemaphore.Unlock();SetEvent(faxEvent);return0;}int_tmain(intargc,_TCHAR*argv[]){clock_tt1=clock();cout并行结果;endl;AfxBeginThread(ThreadProc1,NULL);AfxBeginThread(ThreadProc2,NULL);WaitForSingleObject(faxEvent1,INFINITE);WaitForSingleObject(faxEvent2,INFINITE);clock_tt2=clock();doublept=t2-t1;printf(并行时间:%f\n,pt);clock_tt3=clock();cout串行结果;endl;AfxBeginThread(ThreadProc3,NULL);WaitForSingleObject(faxEvent,INFINITE);clock_tt4=clock();doublest=t4-t3;printf(串行时间:%f\n,st);printf(加速比:%f,st/pt);system(pause);return0;}运行结果:.NET:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Diagnostics;classThreads{staticvoidMain(){Stopwatcht1=newStopwatch();Workwork1=newWork(0);ThreadStartthread1=newThreadStart(work1.Work1);Threadnewthread1=newThread(thread1);Workwork2=newWork(1);ThreadStartthread2=newThreadStart(work2.Work1);Threadnewthread2=newThread(thread2);Console.Write(并行结果:\n);t1.Start();newthread1.Start();newthread2.Start();newthread1.Join();newthread2.Join();t1.Stop();TimeSpantimeSpan=t1.Elapsed;doublept=timeSpan.TotalMilliseconds;Console.Write(并行时间:{0}\n,pt);Console.Write(串行结果:\n);t1.Start();newWork(0).func();t1.Stop();TimeSpantimeSpan2=t1.Elapsed;doublest=timeSpan2.TotalMilliseconds;Console.WriteLine(串行时间:{0},st);Console.WriteLine(加速比:{0},st/pt);Console.Read();}}classWork{priv
本文标题:7种方法求水仙花
链接地址:https://www.777doc.com/doc-2932334 .html