系统结构实验二_第1页
系统结构实验二_第2页
系统结构实验二_第3页
系统结构实验二_第4页
系统结构实验二_第5页
已阅读5页,还剩12页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、计算机系统结构实验报告专业:计算机科学与技术指导老师:雷咏梅姓名:钱成浩学号: 12121469计算机工程与科学学院实验二多核环境下OpenMP并行编程实验2-1. OpenMP程序的编译和运行实验目的1)在Linux平台上编译和运行OpenMP程序;2)在Windows平台上编译和运行OpenMP程序。3)掌握OpenMP并行编程基础。实验环境1)硬件环境:i5双核四线程pc机2)软件环境:centos6+gcc,以及 win8.1+Visual Studio2013运行内容代码如下:#include #include int main()(int nthreads,tid;omp_set_

2、num_threads(8);#pragma omp parallel private(nthreads,tid)(tid=omp_get_thread_num();printf(nHello World from OMP thread %dn/tid);if(tid=O)(nthreads=omp_get_num_threads();printf(HNumber of threads is %dn,nthreads);运行结果linux11.c:7: error: expected ? . ? . before ? . zip?rooteMyVPS2316 -# vim l.crootMyV

3、PS2316 - # gcc -fopeninp -02 -o l.out l.crootMyVPS2316 -# ./I.outHello World, from OMP thread 0Number of threads is 2Hello World from OMP thread 1aa(v) xmcd n(w)tietiXoolor Icon-. norcti O : O 11-1 3asper-llks. X6BC 0:1.900.1-1C . el llbICE.1686 O:l.O.6-l.e!6 ICOe O:1.2.1-2.16 1XKX11.1686 O;1.6.O-2.

4、2.el6 llkXll-connon. norcH O: 1.6.0-2.2.e!6 llkXau. 1666 O: 1.0.6-4 . 16 IxbXcotnposicc. ICO6 0 ;0.4.3-4 .el iXDXcuraor.16Q6 O:1.1.14-2.1.e!6 1.1666 0:1.1.3-4 .!tiell&Xex.lCeO O:1.3.2-2.1.eie IXbXrxxca.1606 O:5.O.l-Z.l.el6 llbxrc.1686 0:2.3.1-2.e!6 llkXl. 1COO 0:1 .*7.2-2.2 .el iXbXlnerama.1B6 O:1.1

5、.3-2.1.e!6 libXrandr.1686 0:1.4.1-2.1.e!6 llkXrender 1606 0:0.9.8-2 1 .16 1XKXC:.X68 0:1.1.4-6.l.eie lXk3Po. 1686 0 s 1.2.1-3 . o!6_5 llbpno-1666 2:1.2.4-1 XXbVasnl.1CB 0:2.3-C.eie_S lXKCMaX.X6e6 0:0.1.12-3.elG llkC.irr . 16G6 0:3 . 4-lO.16_5 lXKxck. lee O:1.9.1-2.el panao.X686 0:1.28.1-10.el plxman

6、.1686 O:0.32.4-4.a1 pkQConflQ.1686 1:0.2391 9hared-siXKM-xnro. 1B 0:0.70-6 .elCoanp 1 f (roocdMyVPS2316 - (rooc eiMyVPS2 316 (roo9MyVPS2316 - (root9Myvps23i (roec0MyVPS2316 - (rooc0MyVPS2316 Coanp 1 f (roocdMyVPS2316 - (rooc eiMyVPS2 316 (roo9MyVPS2316 - (root9Myvps23i (roec0MyVPS2316 - (rooc0MyVPS2

7、316 - KelXo Woria from - Nuviter o mreads Hello Hello Hello Hello Hllo MelloWoria World woria Worll World WorldVI l.c vun l.c via l.c vim l.c qcc - Copemnp -02 -o 1. ouc l.c -/I.ou Oi 82chral Sthread 3 ,cKread 61 cnread *7Windowsy helloworld (正在运行) Microsoft Visual Studioo - e bi F Q1 . | C Debug |w

8、in?I L,0 无 Application Insights Wifi程:【2352 helloworld.exeF ra生命融蟾堆就:Xft(F)视图(V) J5S(P) 生成侣)调试(D) 团队(M) IRfT) 测试(S)体系结构(C) 分析(N)an(w)帮助(H) VSource.cpp XS belloworldasinclude S belloworldasinclude include int mainOC:UserswangyiDesktopxtjghelloworldDebughello.alnOHllo Uorld fro* OHP thread 0Foor of th

9、readc is 2Hllo Uorld fro* OHP thread 1int nthreads tid:/ /o(np_set-nua-ThrJ pragma omp parallel tid = onp_get. printf(xHello if (lid = 0)nthreads = printf (*X( getcharO;1iource.cpp p X! C:UserswangyiDesktQpxtjghelloworldompDebugh. - 口SJhelloworldomp-Sinclude iource.cpp p X! C:UserswangyiDesktQpxtjgh

10、elloworldompDebugh. - 口SJhelloworldomp-Sinclude om|: include st int main()int nthr omp_set_ pragma ompHollo Uorld Frou OMPNumber of threads isIHqIIoIhoIIoHelloelloIH.11。HolloHelloUorldUorldUorldUorldUorldUorld Uorldfrou frau frou frosFroi frou froiiOMP OMP OMP OMP OMP OMP OMPthread 0 8thread 2 threa

11、d 3 thread 7 thread H thread 5 thread 6 thread 1tid prin实验2-2矩阵乘法的OpenMP实现及性能分析实验目的1)用OpenMP实现最基本的数值算法“矩阵乘法”2)掌握for编译制导语句3)对并行程序进行简单的性能调优实验要求1)运行并测试OpenMP编写两个n阶的方阵a和b的相乘程序,结果存放在方阵c中,其中乘法用 for编译制导语句实现并行化操作,并调节for编译制导中schedule的参数,使得执行时间最 短。要求在window环境(不用虚拟机),在linux环境(用和不用虚拟机情况下)测试程序的 性能,并写出详细的分析报告。实验内

12、容1.用OpenMP编写两个n阶的方阵a和b的相乘程序,结果存放在方阵c中,其中乘法用for 编译制导语句实现并行化操作,并调节f。编译制导中schedule的参数,使得执行时间最短, 写出代码。方阵a和b的初始值如下:2,3,4,刀 +13,4,5,. .刀+ 2,以+ 1, + 2,.2一1输入:方阵的阶n、并行域的线程数 输出:c中所有元素之和、程序的执行时间提示:a,b,c的元素定义为int型,c中所有元素之和定义为long long型。Windows 计时:用time.h中的clock_t clock( void )函数得到当前程序执行的时间在windows下使用Microsofe

13、Visual Studio编程.源代码如下:?iinclude?iinclude?iincludevoid comput (float* A, float* B, float* C)/两个矩阵相乘传统方法(int x, y;for (y = 0; y4; y+)for (x = 0; x4; x+)(C4 * y + x = A4 * y + 0 * B4 * 0 + x + A4 * y + 1 * B4 * 1 + xA4 * y + 2 * B4 * 2 + x + A4 * y + 3 * B4 * 3 + x;)int mainOdouble duration;clock_t s,

14、f;int x = 0;int y = 0;int n = 0;int k = 0;float A = 1, 2, 3, 4,5, 6, 7, 8,9, 10, 11, 12,13, 14, 15, 16 ;float B = 0.lf, 0.2f, 0.3f, 0.4f,0. 5f, 0. 6f, 0. 7f, 0. 8f,0. 9f, 0. lOf, 0. Ilf, 0.12f,0. 13f, 0. 14f, 0. 15ft 0. 16f );float C16;s = clockO ;/ffpragma omp parallel if(false) for (n = 0; n100000

15、0; n+) comput (A, B, C);f = clockO ;duration = (double) (f - s) / CLOCKS_PER_SEC; printf (/zs1, 000, 000 :%fn”, duration);for (y = 0; y4; y+)for (x = 0; x4; x+)(printfC%f/, Cy * 4 + x); printfCnw);printf Cn=n;s = clockO ;/parallel 2pragma omp parallel forfor (n = 0; n2; n+)/CPU是核线程的for (k = 0; k5000

16、00; k+)每个线程管个循环 (comput (A, B, C);f = clockO ;duration = (double)(f - s) / CLOCKS_PER_SEC; printf (p2- 1, 000, 000:%fnz/, duration);/parallel 3 s = clockO ;pragma omp parallel forfor (n = 0; n4; n+) /CPU是核线程的for (k = 0; k250000: k+)每个线程管个循环comput (A, B, C);f = clockO ;duration = (double) (f - s) / C

17、LOCKS_PER_SEC;printf Cp3- 1, 000,000:%fn”, duration);/parallel 1 s = clockO ;pragma omp parallel for for (n = 0; n1000000; n+) comput (A, B, C);f = clockO ;duration = (double) (f - s) / CLOCKS_PER_SEC;printf (,zpl- 1, 000, 000 :%fn”, duration);for (y = 0; y4; y+)for (x = 0; x4; x+)(printfC%f/, Cy *

18、4 + x);printf(n);getchar0;return 0;运行结果Linux (shell 中运行)4.c:lll: error: expected identitier or ? . ? . Detore ? . ? . token root0MyVPS2316 vim 4.c root8MyVPS2316 gcc -foperw -02 -o 4.out 4.c rootgMyVPS2316 * ./4.out 31,000,000 :0.0600004.320000,2.260000,2.630000,3.000000,10.840000,6.420000,7.670000,

19、8.920000.17.360001,10.580000,12.710000,14.840000,23.879999,14.740001,17.750000,20.760000f p2- 1,000,000:0.460000 p3- 1,000,000:0.460000 pl- 1,000,000 :0.3200004.320000,2.260000,2.630000,3.000000, 10.840000,6.420000,7.670000,8.920000, 17.360001,10.580000,12.710000,14.840000, 23.879999,14.740001,17.75

20、0000,20.760000, rooteMyVPS2316 - QWindows线程为2E C:UserswangyiDesktopxtjgparallelDebugparallel.e. 口S-1,000.003 :6.1360304.320000.2. 250009.2.630000.3.OOOOGO,1G.839999、6.U2OQ0Q,7.67OQ0Q,8.920690.17. 359999,10.580001,12.719000,14.840000,23. 879999.14.74Q0O1.17.759000.20.759998.p2- 1,000,003:0.100603p3-

21、1.000.609:0.105609p1- 1,000,000 :0.130300320000,2.2SOOO0,2.630000.3.000000.10.839999、6.H20Q0O.7.GTOQQO.8.920600.17.359999,10.5800G1,12 710000,14 840000,23.879999,14.7H0OO1.17.750000,20.759998,IQQ拼音辎入法线程为4编辑系统变量变星名(N):OMP NUM THREADS变星值(V):4|变星名(N):OMP NUM THREADS变星值(V):4|确定取消E C:UserswangyiDesktopxt

22、jgparallelDebugparallel.e. 口s-i,000,603 :e.1510304.320000.2 . 250009.2.630000.3.OOOOGO, 1G.839999、 6.U2O00Q.7.67OQ0Q,8.920690.1 7.359999,19.580001,12.710000,14.840000, 23. 879999.14.740001.17.753000.20.759998.p2- 1,000,600:0.06603p3 1.000.600:0.116000p1- 1,000,090 :0.162000320000,2.250009,2.630000,3

23、.000009.10.839999、6.426600.7.GTOQQO.8.920600.17.359999,10.580001,12 710000,14 849000, 23.879999,14.7H0OO1.17.750000,20.759998,线程为82)请自己找一个需要大量计算但是程序不是很长的程序,实 现OMP的多线程并行计算,要求写出并行算法,并分析并行 的效果(注:必须核对串行和并行的计算结果,保证正确性) 运行代码:1#include2#include3#include4#include5#include6using std:cout;7using std:setprecis

24、ion;8using std:endl;9void derivative(double xz double double result)10(11result = (x + y)*(x + y) - x*x)/y;121314int main ()15(16double duration;17docket s, f;18int n = 0;19int k = 0;20double x = 2.0z y = 0.01;21double result;22s = clock();23#pragma omp parallel if (false)24for (n = 0; n1000000; n+)

25、2526derivative (x, y, result);27/*for (int i = 0; i 10; +i)28(29derivative(x, y, result);30cout setprecision(20) result endl;31y /= 10;32333435363738394041424344454647484950515253545556575859606162636465666768697071)f = clock();duration = (double)(f - s) / CLOCKS_PER_SEC; cout ns1000000:H duration e

26、ndl;for (int i = 0; i10; +i) cout setprecision(20) result endl; y /= 10;)cout n=n endl;s = clock();#pragma omp parallel for for (n = 0; n2; n+) for (k = 0; k500000; k+)( derivative(x, y, result);)f = clock();duration = (double)(f - s) / CLOCKS_PER_SEC; cout up2-1000000:H duration endl; s = clock();#pragma omp parallel for for (n = 0; n4; n+) for (k = 0; k250000; k+)( derivative(x, y, result);)f = clock();duration = (double)(f - s) / CLOCKS_PER_SEC;cout np3-1000000:H duration endl; s = clock();#pragma omp parallel for for (n = 0; n1000000; n

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论