高性能计算报告.docx
裔雌饿奸弄就险想告同学胜名:-名号;班号;116122指导老师:郭明强中电池质右喉(款法)信息工程学就第一题.编写console程序1 .由下列图看出,电脑是双核CPU.多线程程序,采用windowsAPI函数创立线程代码#includez/stdafx. h#include<iostream>#includez/windows. h usingnamespace std; DWORDWINAPI first(PVOIDpParam) (for (int i = 0;i < 10;i+) (printf(ln); )return 0;)DWORDWINAPI second (PVOIDpParam) ( for (int i = 0;i < 10;i+) (printf(2n); return 0;)int main(intargc, char * argv)HANDLE hHandle_Calc2;hHandle_CalcO = CreateThread (NULL, 0, first, NULL, 0, NULL); hHandle_Calcl= CreateThread(NULL, 0, second, NULL, 0, NULL); WaitForMultipleObjects(2, hHandleCalc, true, INFINITE);)其次题多线程实现计算e和兀的乘积代码#include,zstdafx. h#inc ludez,windows. h Jtdefinenum steps 2000000 #includc<iostream> usingnamespace std;计算eDWORDWINAPI ThreadCalc_E(PVOIDpParam)计算 e 子函数 double factorial = 1; int i = 1; double e = 1;for (;i < num steps;i+) ( factorial *= i;e += 1. 0 / factorial;)* (doub1e*)pParam)二 e;printf (,ze done E =%2. 5fn,z, e); return 0;)计算PIDWORDWINAPI ThreadCalc_PI(PVOIDpParam)计算 pi 子函数 int i = 0; double pi = 0;for (;i < num steps * 10;i+) (pi += 1.0/ (i*4. 0 + 1. 0);pi 1.0 / (i*4.0 + 3. 0); pi = pi*4. 0;*( (double*)pParam) = pi;printf (z,pi done PI = %2. 5fn,pi);return 0;int main(intargc, char * argv口)进程的主线程入口点 (HANDLE hHandle_Calc2;double result_e, result_pi;hHandle Calc0 = CreateThread(NULL, 0, ThreadCalc_E, (void*)(&result e), 0, NULL); hHandle_Calcl = CreateThread(NULL, 0, ThreadCalc PI, (void*)(&result_pi), 0, NULL);附加线程的创立 WaitForMultipleObjects(2, hHandleCalc, true, INFINITE);等待子线程计算结束printf (,ze* pi = %2. 5fn,z, result_e*result_pi);打印出 e*pi 结果 return 0;)第三题1 .Parallel 语句#includez/stdafx. h ttincludewindows. h #includez/omp. h #include<iostream> usingnamespace std; void main() (Spragma omp parallel num_threads(8) ( printf ("Hello, World!, Threadld=%dnz,, omp get_thread num(); )2 .For语句#includezzstdafx. h#include,zwindows. h#include,zomp. h#include<iostream> usingnamespace std; void main () (int j = 0;Spragma omp parallel (pragma omp for for (j = 0;j < 4;j+) printf j=%d, threadid=%dn,z, j, omp get thread num();3.Sections和section语句的用法#include,zstdafx. h#inc 1 udez,windows. h#includez/omp. h#includc<iostream> usingnamespace std;void main(intargc, char * argv)ttpragma omp sectionprintf(,z section ttpragma omp sectionprintf(,z sectionpragma omp sectionprintf(,z sectionSpragma omp sectionprintf(/z section1 threadid = %d2 threadid = %d3 threadid = %d4 threadid = %d#pragma omp parallel sectionsn,omp_get_thread_num();n,omp_get_thread_num();n, omp_get_thread num();n,omp get_threadnum ();4.Threadprivate语句 的用 法#include,zstdafx. h#include,zwindows. h#includezzomp. h#include<iostream> usingnamespace std; int a, b, i, tid; float x;pragma omp threadprivate(a, x)void main () 关闭动态线程安排omp_set dynamic(0);printf (zzlst Parallel Region: n",);pragma omp parallel private(b, tid) (tid = omp get thread num();a = tid; b = tid;x = 1. l*tid + 1. 0;printf ("Threading %d: a, b, x = %d %d %fn,z, tid, a, b, x);/end of parallel section -v* i*X*4* *!* *4* w ,、.Ul J_ f J L,£ 1 个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个个 j , printf (主线程中串行线程n);printf Cnd Parallel Region: n,z);Spragma omp parallel private(tid)(tid = omp get thread num();printf (''Threading %d: a, b, x = %d %d %fn,z, tid, a, b, x);/end of parallel section5.reduction语句的用法#include,zstdafx. h#includez/windows. h#include/zomp. h#include<iostream> usingnamespace std;#include<omp. h>void main()(int i, n, chunk;float a100, b100, result;变量的初始化n = 100;chunk = 10;result = 0. 0;for (i = 0;i < n;i+) (ai = i*2.0;bi= i*3.0;)pragma omp parallel fordcfault(shared) private(i)schedule(static, chunk)reduction(+:result) for (i = 0;i < n;i+)(result = result + (ai * bi);printf (z,Final result= %fn,result);第四题OpenMP实例分析与比拟串行#include,zstdafx. h#includc<time. h>#definenum steps 20000000 int main(intargc, char *argv口) ( double start, stop;double e, pi, factorial, product; int i;启动定时器start = clock();首先运用taylor绽开运算e printf (,ze startednz/); e = 1;factorial = 1;for (i = 1;i < num_steps;i+) ( factorial *= i;e += 1.0/ factorial;)printf (e donen);然后计算pi运用taylor绽开 printf (,zpi startednz/); pi = 0;for (i = 1;i < numsteps * 10;i+) (pi += L0/(i*4.0 + 1.0);pi -= L0/(i*4.0 + 3. 0); )pi = pi*4. 0;printf (,zpi donen);product = e*pi;stop = clock ();printf (Z/Reached result %f in %. 3f secondn,product, (stop - start)/1000); return 0;I并行ttincludestdafx. h#include<time. h>#definenum steps 20000000int main(intargc, char * argv)double start, stop;任务开头double e, pi, factorial, product;int i;启动定时器start = clock();启动两个进程分别计算e piSpragma omp parallel sections shared(e, pi)(Spragma omp section(printf C'e startedn/z);e = 1;factorial = 1;for (i = 1;i < numsteps;i+) (factorial *= i;e += 1. 0 / factorial;)printf (z/e donenz/);)pragma omp section(printf (z,pi startednz,);pi = 0;for (i = 1;i < num_steps * 10;i+) (pi += 1. 0/(i*4. 0 + 1. 0);pi -= L0/(i*4.0 + 3. 0); pi = pi*4. 0;printf (,zpi donen);)/omp sections 两个线程合并为主线程product = e*pi;stop = clock ();printf (''reached result %f in %. 3f secondn/z, product, (stop - start)/1000); return 0;由下面两个图可知,并行比串行运行速度快,CPU使用效率高L串行2.并行