并行編程實驗資料報告材料_第1頁
并行編程實驗資料報告材料_第2頁
并行編程實驗資料報告材料_第3頁
免費預(yù)覽已結(jié)束,剩余17頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、爭申科技衣茅課程實驗報告課程名稱:并行編程計算機科學(xué)與技術(shù)學(xué)院目錄實驗一 21. 實驗?zāi)康呐c要求 22. 實驗容 23. 實驗結(jié)果 2實驗二 31. 實驗?zāi)康呐c要求 32. 算法描述 33. 實驗方案 34. 實驗結(jié)果與分析 5實驗三 61. 實驗?zāi)康呐c要求 62. 算法描述 63. 實驗方案 64. 實驗結(jié)果與分析 7實驗四 81. 實驗?zāi)康呐c要求 82. 算法描述 83. 實驗方案 84. 實驗結(jié)果與分析 11實驗五 121實驗?zāi)康呐c要求 122. 算法描述 123. 實驗方案 124. 實驗結(jié)果與分析 14PROJECT.2 16AIM: 16HYPOTHESIS.: 16METHODS

2、.: 16RESULT: 16DICUSSION&CONCLUSI.O.N 17REFERENC.E 18實驗一1. 實驗?zāi)康呐c要求become familiar with the parallel development environments, and the basic principles and methods of parallel programming and performance optimization by using tools and frameworks like pthread, OpenMP, MPI under Linux system.2. 實驗

3、容熟悉并行開發(fā)環(huán)境,掌握并行編程用到的工具如線程、 OpenMP,、MPI 等。3. 實驗結(jié)果通過上機操作熟悉了各種命令,編寫了簡單的程序熟悉開發(fā)環(huán)境。實驗二1. 實驗?zāi)康呐c要求a) master the basic principles and methods of parallel programming design and performance optimization using pthreadb) understand the basic method for data partition and task decomposition in parallel programmin

4、gc) implement the parallel algorithm of calculating the value of pi using pthreadd) then carries on the simple analysis and summary of the program execution results2. 算法描述采用蒙特卡洛方法計算圓周率, 利用單位圓與邊長為 1 的正方形面積之比 計算圓周率的近似值。比值的計算采用蒙特卡羅方法的隨即投點思想, 在正方形中隨機投入很多 點,使所投點在正方形中每一個位置的機會均等, 然后考察有多少個點落 在扇形,落在扇形的點的個數(shù)與投

5、點總數(shù)之比就是該比例的近似值。 每一個線程完成一次投點, n 個線程同時計算。3. 實驗方案開發(fā)與運行環(huán)境:使用筆記本電腦登錄實驗室服務(wù)器。實驗代碼如下:#include <pthread.h>#include <stdlib.h>#include <stdio.h>#define MaxThreadNum 100#define kSamplePoints 10000000#define kSpace 1 void *compute_pi(void *);int total_hits, hitsMaxThreadNumkSpace; int sample_p

6、oints_per_thread, num_threads;int main(void)int i;pthread_t p_threadsMaxThreadNum;pthread_attr_t attr;pthread_attr_init(&attr);pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM); printf("Enter num_threadsn");scanf("%d", &num_threads);total_hits = 0; sample_points_per_

7、thread = kSamplePoints / num_threads;for(i=0; i<num_threads; i+) hitsi0 = i; pthread_create(&p_threadsi, &attr, compute_pi, (void *)&hitsi);for(i=0; i<num_threads; i+) pthread_join(p_threadsi, NULL); total_hits += hitsi0;double pi = 4.0 * (double)total_hits / kSamplePoints; printf(

8、" Pi: %lfn", pi);return 0;void *compute_pi(void * s)unsigned int seed;int i;int *hit_pointer;double rand_no_x, rand_no_y; hit_pointer = (int *)s; seed = *hit_pointer;/ int local_hits = 0;for(i=0; i<sample_points_per_thread; i+) rand_no_x = (double)(rand_r(&seed)/(double)(RAND_MAX);r

9、an d_no_y = (double)(ra nd(&seed)/(double)(RAND_MAX); if(ra nd_no_x - 0.5)*(ra nd_no_x - 0.5) + (ran d_no_y - 0.5) (ran d_no_y - 0.5) < 0.25)一 一 (*hit_poi nter)+;seed *= i; pthread_exit(0); _4.實驗結(jié)果與分析實驗結(jié)果符合預(yù)期:pppuseEnter num_thread5Pi: 3.152斗日2實驗三1. 實驗?zāi)康呐c要求a) master the basic principles and m

10、ethods of parallel programming design and performance optimization using OpenMPb) implement the parallel algorithm of calculating the value of pi using OpenMPc) carries on the simple analysis and summary of the program execution resultsd) compare it with the results of Lab22. 算法描述與實驗二相似,同樣采用蒙特卡羅方法計算

11、 pi 值,算法不再詳細(xì)描述。3. 實驗方案實驗環(huán)境:使用筆記本電腦登錄實驗室服務(wù)器。實驗代碼如下:#include <omp.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#define SEED 35791246main(int argc, char* argv)int numiter=0;/loop timesdouble x,y,z,pi;int i,count; /* # of points in the 1st quadrant of unit circle */ pr

12、intf("Enter the number of iterations used to estimate pi: "); scanf("%d",&niter);/* initialize random numbers */ srand(SEED);count=0;int chunk;/ sizechunk = 1;#pragma omp parallel shared(chunk) private(i,x,y,z) reduction(+:count) #pragma omp for schedule(dynamic,chunk)for ( i

13、=0; i<niter; i+) x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX;z = x*x+y*y; if (z<=1) coun t+; pi=(double)co un t/niter*4; printf("# loop times= %d , estimate of pi is %g n",niter,pi);Openmp自動將for循環(huán)分解成多個線程并行執(zhí)行。4.實驗結(jié)果與分析實驗結(jié)果如下:pppuserSSOtfinodeiSl 1訪豈$. / 3En±er the nu

14、mber of iterations lib已d to estImate pl:10&M# IcFctp 七i mES=f est i.rnat e erf pi. i s 3.133&Time: D + Epppu«r2S&gnode231 lsb3JJ . /3二n七已r th亡 number o4 iteration£ used to estimate pi: 'i lopp time5=t 好耳tinrat亡 of pi i旨 3.14159rimen 4i7與實驗二相比本實驗是使用ope nmp自動分解多線程并行,精度并無明顯 區(qū)別

15、,與所選算法有關(guān)。循環(huán)次數(shù)越多,得到的結(jié)果就越精確。實驗四1. 實驗?zāi)康呐c要求a) master the basic principles and methods of parallel programming design and performance optimization using MPIb) implement the parallel algorithm of calculating the value of pi using MPIc) carries on the simple analysis and summary of the program execution re

16、sultsd) compare it with the results of Lab2 and Lab32. 算法描述本實驗采用與實驗一實驗二相同的蒙特卡羅算法實現(xiàn) pi 值得計算,即利 用單位圓與邊長為 1 的正方形面積之比計算圓周率的近似值。 比值的計算采用蒙特卡羅方法的隨即投點思想, 在正方形中隨機投入很多 點,使所投點在正方形中每一個位置的機會均等, 然后考察有多少個點落 在扇形,落在扇形的點的個數(shù)與投點總數(shù)之比就是該比例的近似值。3. 實驗方案Mpi 是一種基于消息傳遞的并行編程技術(shù), 各個進程有獨立的堆棧和代碼 段,進程之間的信息交互通過調(diào)用通信函數(shù)完成?;镜?API 如下:in

17、t MPI_Init(int *argc, char *argv)MPInit是MPI程序的第一個調(diào)用,它完成MPI程序的所有初始化 工作, 啟動 MPI 環(huán)境,標(biāo)志并行代碼的開始。int MPI_Finalize(void)MPI_Finalize 是 MPI 程序的最后一個調(diào)用,它結(jié)束 MPI 程序的運 行,標(biāo) 志并行代碼的結(jié)束, 結(jié)束除主進程外其它進程。 其之后串行代碼 仍可在 主進程 (rank = 0) 上繼續(xù)運行。int MPI_Comm_size(MPI_Comm comm, int *size);獲取進程個數(shù) p。int MPI_Comm_rank(MPI_Comm comm,

18、 int *rank);MPI獲取當(dāng)前進程的 RANK rank值取址圍是 0p-1 , RANKfi唯一的表示了進程的ID,其中Rank=0的為主進程int MPI_Send(void* buf, int count, MPI_Datatype datatype, int dest,int tag,MPI_Comm comm);發(fā)送函數(shù):當(dāng)前進程將以 buf 為初始地址,長度為 count 且元素類型 為 datatype 的信息發(fā)動給 rank 值為 dest 的進程,這條消息的標(biāo)識符 為 tag 。其中datatype有MPINT, MPI_FLOA等常用類型,Tag的作用是用于 區(qū)分一

19、對進程之間發(fā)送的不同信息int MPI_Recv(void* buf, int count, MPI_Datatype datatype, int source, int tag, MPI_Comm comm, MPI_Status *status);接受函數(shù):從 rank 值為 source 的進程接受標(biāo)識符為 tag 的信息,存 入以 buf 為初始地址, 長度為 count 的存儲區(qū)域中, 類型為 datatype. 實驗環(huán)境:使用筆記本電腦登錄實驗室服務(wù)器。具體代碼如下:#include<stdio.h>#include<stdlib.h>#include<

20、;math.h>#include<time.h>#include<mpi.h>void read_num(long long int *num_point,int my_rank,MPI_Comm comm);void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MPI_Comm comm,int my_rank);int main

21、(int argc,char* argv)longlongintnum_in_cycle,num_point,total_num_in_cycle,local_num_point;int my_rank,comm_sz;MPI_Comm comm;MPIni t(NULL,NULL);/初始化comm=MPI_COMM_WORLD;MPI_Comm_size(comm,&comm_sz);/得到進程總數(shù)MPI_Comm_ra nk(comm,&my_ran k);得到進程編號 read_num(&num_point,my_rank,comm);/ 讀取輸入數(shù)據(jù)compu

22、te_pi(num_point,&num_in_cycle,&local_num_point,comm_sz,&total_num_in _cycle,comm,my_rank);MPI_Finalize();return 0;void read_num(long long int* num_point,int my_rank,MPI_Comm comm) if(my_rank=0)printf("please input num in sqaure n"); scanf("%lld",num_point);/*廣播函數(shù)int M

23、PI_Bcast(void* data_p /in/outint count /inMPI_Datatype datatype /in int source_proc /in MPI_Comm comm /in) */MPI_Bcast(num_point,1,MPI_LONG_LONG,0,comm);void compute_pi(long long int num_point,long long int* num_in_cycle,long long int* local_num_point,int comm_sz,long long int *total_num_in_cycle,MP

24、I_Comm comm,int my_rank)*num_in_cycle=0; *local_num_point=num_point/comm_sz; double x,y,distance_squared; srand(time(NULL);for(long long int i=0;i< *local_num_point;i+) x=(double)rand()/(double)RAND_MAX; x=x*2-1; y=(double)rand()/(double)RAND_MAX; y=y*2-1; distance_squared=x*x+y*y; if(distance_sq

25、uared<=1) *num_in_cycle=*num_in_cycle+1; /* 全局函數(shù) MPI_Reduce( void* input_data_p /in void* output_data_p /out int count /in MPI_Datatype datatype /in MPI_Op oprtator /in int dest_process /in MPI_Comm comm /in) */MPI_Reduce(num_in_cycle,total_num_in_cycle,1,MPI_LONG_LONG,MPI_SUM,0,co mm);if(my_rank

26、=0)double pi=(double)*total_num_in_cycle/(double)num_point*4; printf("the estimate value of pi is %lfn",pi);4.實驗結(jié)果與分析實驗結(jié)果如下:pppuser?3-0gnode231 lati斗玉 f斗 input square nuntjer:1600©the estimate of pi is 久"56W Time: 9.19pppuser23flnode 151 lat4f . f4- inpLi七 square number :±he

27、 estimate 口F pi. is 3.1413&0 lime: 0.13由結(jié)果可知循環(huán)次數(shù)越多得到的pi值就越精確。與實驗二和實驗三相比, 相同循環(huán)次數(shù)下采用mpi運算速度更快。實驗五1實驗?zāi)康呐c要求1.understand deeply the architecture of GPGPU and master the CUDA programming model2.implement the parallel algorithm of calculating the value of pi using CUDA3. carries on the simple analysis

28、and summary of the program execution results4. propose optimization solution based on the execution results and hardware environment5. compare it with the results of Lab2 ,Lab3 and Lab42. 算法描述采用積分法計算 pi 值:積分法計算pi值的基本思想是利用1/(1+xA2)的原函數(shù)為arctanx,再利用 積分的基本步驟:分割,求和,取極限。將函數(shù)圖形與Y軸和直線X=1圍成的面積盡可能細(xì)分,然后求和,最后乘以相

29、應(yīng)常數(shù),可以得到一個非常近似 的 pi 值。3. 實驗方案CUDA在執(zhí)行的時候是讓host里面的一個一個的kernel按照線程網(wǎng)格的概 念在顯卡硬件(GPU上執(zhí)行。每一個線程網(wǎng)格又可以包含多個線程塊( block ),每一個線程塊中又可以包含多個線程( thread )。基本 API 如下:cudaError_t cudaMalloc (void *devPtr,size_tsize );在設(shè)備端分配 size 大小的空間,起始地址為 devPtrcudaError_t cudaMemcpy (void * dst, const void * src,size_tcount,enum cuda

30、MemcpyKind kind);將以 src 為地址長度為 count 的數(shù)據(jù)賦值到 dst 為起始地址的 存區(qū)域中,常用的 kind 有 cudaMemcpyHostToDevice, cudaMemcpyDeviceToHostcudaError_t cudaFree (void *devPtr);在設(shè)備端清理以 devPtr 為起始地址的存空間將任務(wù)合理的分配到 grid 和 thread 中,有助于提升程序的性能:grid of thread :Kemtl Opel onGlX*60-> CORESIJunber af Bloc ks ?513rnre;)dlrt>i、

31、32 Threads vvra sizeHIOCH1loochdi'- x-'DicctOim x具體實驗代碼如下:#in elude <stdio.h>global_ void kern el(double *gpu_p) int gpu_co unt = 0;for (i nt gpu_i = 1; gpu_i <= 1000; gpu_i+) if (gpu_cou nt % 2 = 0)gpu_pgpu_i= gpu_pgpu-1 - 4 / (double)(2*gpu-1); else gpu_pgpu_i=gpu_pgpu_i-1 + 4 / (

32、double)(2*gpu_i-1);gpu_co unt = gpu_co unt + 1; 一 一_syn cthreads();int main (i nt argc, char *argv) int i;int coun t=0;double p100;double *g_p;int block_size = 32; const int N = 1000;int n_blocks = N/block_size + (N%block_size = 0 ? 0:1); p0 = 0;g_p= (double *)malloc(1000 *sizeof(double); cudaMalloc

33、(void *) &g_p,1000 * sizeof(double);cudaMemcpy(g_p, p,1000 * sizeof(double), cudaMemcpyHostToDevice);kernel <<< n_blocks, block_size >>>( g_p);cudaMemcpy(p, g_p,1000 * sizeof(double), cudaMemcpyDeviceToHost);cudaFree(g_p);for (i = 1; i <= 1000; i+) printf( "%2.4ft"

34、, pi);printf( "blocks = %d block size= %dt",n_blocks, block_size ); system("pause");return 0;4. 實驗結(jié)果與分析實驗結(jié)果如下:-3,1547-3.14B4-3.154B-/=叫呂孑J.1349-Hl4呂2-5.13 5-6'3.14753.1357-3.1474-3+135&-3.1474-3.1358-3,1473-3.13593.1468-3.1364-3.1457-3+1365-3.1467-3.1365-3.1466-3.13663.14

35、52-3.1370-3.1462-3.1370-3.14&1-3*1371-3.1461-3.13713.1457-3.1375-3.1457*3.1375-3.1457-S.1375-3.1456-3,13763.1454-3.137S-3-1453-3.1379-3.1453-3.1379-3.1453-3.13793.14-51-3.13S1-3.1459-3.13B2-3.1450-3.1382-3.1450-5.13B23,1440-3.13&4-3.1440-3*1364-3.1447-3.1364(1447-3.13553.144&-耳1386-3.144

36、5-3*13B71445-3*1387-31445-3.13073.1444-3.1MB-3,1443-3>130&-314斗了-3.1389-3.1443-3.13593.1442-3.1442-3.1390-3.1442-3.1390-3.1442-3.13M3.1440-3.1392-3.144&-3.1392-3.1440-3.1392-3.1446-3,13923.1439-3.139i-3.1435-3.1393-1.1459-3.1439-3.1393-3,1394-3.1453-3.1394-3.1430-3.1394-3,1433-5-13943.143

37、7'3.1395-3.1437-3+1395-.1437-3.1395-3.1436-3.13953.143&-3,143&-3.1395-3,1436-5.1396-3.1435-3.15963.1435-3.1397-3.1435-3*1397-3.1435-3*1397-3.1435-3.13973.1434-3.1593-3.1434*3.1393-3.1434-1.1398-3.1434*3.139B3.1433-3.139&-3,. 1433-3.1399-3 1433-3.1399-3.1433-3.13993.1433-3.1599-3.1435

38、-3.1599-3.1452-3.1393-竄 1432-3.13953,1432-3.14W-3.1432-3+1400-3,1432-3.14W-3.1432-3.14&e3.1431-3.1401-31431-3.1491-31431-3.1461-3.1431-31401弓.1431-3.1401-3.1431-3+1401-3.14S1-3.14C1-3.1431-3,14013.1430-3.14&2-3.143ft-3,14023.1430-3.14C2-3.1430-3.14023.1436*3.1402-3.1430-3.1402-3.143®-3.

39、1462-3.143®-3.14023.14-29-3.14C3-3.1429-3.1403-3.1429-3.14C3-3.1429-3.14033,1+29-3.1403-3.1429-3*1403-3.1429-3.1405-5,1429-3,14«351429-3.1W-3.1423'5.1 4W-3.1428-5il4«3-3h142S-孟昭如3.142S-3.1444-3,1428-3+1404-3.1420-3,1464-3.1428-3.14A43.1428-3.1404-3.1428-3.1404-3.1428-3.1464-3.1428

40、-3,14&43.1427-3.1404-3.1427-3.1404-3.1427-3.14©4-3.1427-3.14*43.1427-3.1405-3.1427-3.1斗匪-3.1427-3.1427-5.1斗05與前三次實驗采用的蒙特卡羅法相比,積分法的精度更高,使用cuda計算pi值,速度更快。PR0JECT2AIM:master the two typical parallel program developme nt tools (Ope nMPand MPI)un dersta nd the similarities and differe nces betwee n the two toolsduri ng the process of parallel program des

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論