1 / 12
文档名称:

多核编程实验报告.doc

格式:doc   大小:395KB   页数:12页
下载后只包含 1 个 DOC 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

多核编程实验报告.doc

上传人:phl0420371 2019/3/9 文件大小:395 KB

下载得到文件列表

多核编程实验报告.doc

相关文档

文档介绍

文档介绍:实验一:Windows*Threads多线程编程模块一:,输出结果:(1)简答与思考:修改后的HelloThreads的代码://:定¡§义°?控?制?台¬¡§应®|用®?程¨¬序¨°的Ì?入¨?口¨²点Ì?。¡ê个人收集整理勿做商业用途#include""#include<>constintnumThreads=4;DWORDWINAPIhelloFunc(LPVOIDpArg){ intj=*(int*)pArg;printf("HelloThread%d\n",j); return0;}int_tmain(intargc,_TCHAR*argv[]){ HANDLEhThread[numThreads];inttNum[4]; for(inti=0;i<numThreads;i++) {tNum[i]=i; hThread[i]= CreateThread(NULL,0,helloFunc,&tNum[i],0,NULL);个人收集整理勿做商业用途 } WaitForMultipleObjects(numThreads,hThread,TRUE,INFINITE);个人收集整理勿做商业用途 return0;}运行结果图:(2)实验总结:模块二:,Pi的值为:(截图)(3),Pi的值为:(截图)(4)简答与思考:?请写出并行化的思路与具体的代码。//:定¡§义°?控?制?台¬¡§应®|用®?程¨¬序¨°的Ì?入¨?口¨²点Ì?。¡ê个人收集整理勿做商业用途//#include""#include<>#include<>staticlongnum_steps=1000000000;doublestep,pi;CRITICAL_SECTIONgCS; //定¡§义°?全¨?局?的Ì?临¢¨´界?变À?量¢?gCS个人收集整理勿做商业用途constintnumThreads=2;doubledAllSum;DWORDWINAPIPiCalculationThread(LPVOIDp){ intj=*(int*)p; doubledSum=0; doubledx; intii; printf("Thisisthethread%dcomputing:\n",j); for(ii=j;ii<num_steps;ii+=numThreads){dx=(ii+)*step;dSum=dSum+/(+dx*dx); } EnterCriticalSection(&gCS); //进?入¨?临¢¨´界?区? dAllSum+=dSum; LeaveCriticalSection(&gCS); //离¤?开a临¢¨´界?区? printf("Thethread%dcomputationhasfinished:\n",j);个人收集整理勿做商业用途 return0;}int_tmain(intargc,_TCHAR*argv[]){ clock_tstart1; clock_tstop1; start1=clock(); //开a始º?计?时º¡À HANDLEPiCalculation[numThreads]; inttmp[numThreads];step=/(double)num_steps; InitializeCriticalSection(&gCS); for(intia=0;ia<numThreads;ia++){ tmp[ia]=ia; PiCalculation[ia]=CreateThread(NULL,0,PiCalculationThread,&tmp[ia],0,NULL);个人收集整理勿做商业用途} WaitForMultipleObjects(numThreads,PiCalculation,TRUE,INFINITE);个人收集整理勿做商业用途 DeleteCriticalSection(&gCS); pi=step*dAllSum; printf("Pi=%\n",pi); stop1=clock(); //停ª¡ê止1计?时º¡À printf("Thetimeofcalculationwas%fseconds\n",(double)(stop1-start1)/); //输º?出?运?行D时º¡À间?个人收集整理勿做商业用途},哪些变量是需要保护的?采取什么方法实现的?dAllSum变量需要保护通过设置临界区来保护的,详细请看程序