




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、課 程 實 驗 報 告課程名稱: 并 行 編 程 專業(yè)班級: 學(xué) 號: 姓 名: 指導(dǎo)教師: 報告日期: 計算機科學(xué)與技術(shù)學(xué)院目錄實驗一21. 實驗?zāi)康呐c要求22. 實驗內(nèi)容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é)果與分析14PROJECT216AIM:16HYPOTHESIS:16METHOD
2、S:16RESULT:16DICUSSION&CONCLUSION17REFERENCE18實驗一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. 實驗內(nèi)容熟悉并行開發(fā)環(huán)境,
3、掌握并行編程用到的工具如線程、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 programmingc) implemen
4、t 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的正方形面積之比計算圓周率的近似值。比值的計算采用蒙特卡羅方法的隨即投點思想,在正方形中隨機投入很多點,使所投點在正方形中每一個位置的機會均等,然后考察有多少個點落在扇形內(nèi),落在扇形內(nèi)的點的個數(shù)與投點總數(shù)之比就是該比例的近似值。每一個
5、線程完成一次投點,n個線程同時計算。3. 實驗方案開發(fā)與運行環(huán)境:使用筆記本電腦登錄實驗室服務(wù)器。實驗代碼如下:#include #include #include #define MaxThreadNum 100 #define kSamplePoints #define kSpace 1 void *compute_pi(void *); int total_hits, hitsMaxThreadNumkSpace; int sample_points_per_thread, num_threads; int main(void) int i; pthread_t p_threadsMax
6、ThreadNum; 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_thread = kSamplePoints / num_threads; for(i=0; inum_threads; i+) hitsi0 = i; pthread_create(&p_threadsi
7、, &attr, compute_pi, (void *)&hitsi); for(i=0; inum_threads; i+) pthread_join(p_threadsi, NULL); total_hits += hitsi0; double pi = 4.0 * (double)total_hits / kSamplePoints; printf( Pi: %lfn, pi); return 0; void *compute_pi(void * s) unsigned int seed; int i; int *hit_pointer; double rand_no_x, rand_
8、no_y; hit_pointer = (int *)s; seed = *hit_pointer; / int local_hits = 0; for(i=0; isample_points_per_thread; i+) rand_no_x = (double)(rand_r(&seed)/(double)(RAND_MAX); rand_no_y = (double)(rand_r(&seed)/(double)(RAND_MAX); if(rand_no_x - 0.5)*(rand_no_x - 0.5) + (rand_no_y - 0.5) * (rand_no_y - 0.5)
9、 0.25) (*hit_pointer)+; seed *= i; pthread_exit(0); 4. 實驗結(jié)果與分析實驗結(jié)果符合預(yù)期:實驗三1. 實驗?zāi)康呐c要求a) master the basic principles and methods of parallel programming design and performance optimization using OpenMPb) implement the parallel algorithm of calculating the value of pi using OpenMPc) carries on the simp
10、le analysis and summary of the program execution resultsd) compare it with the results of Lab22. 算法描述與實驗二相似,同樣采用蒙特卡羅方法計算pi值,算法不再詳細(xì)描述。3. 實驗方案實驗環(huán)境:使用筆記本電腦登錄實驗室服務(wù)器。實驗代碼如下:#include #include #include #include #define SEED main(int argc, char* argv) int numiter=0;/loop times double x,y,z,pi; int i,count;
11、/* # of points in the 1st quadrant of unit circle */ printf(Enter the number of iterations used to estimate pi: ); scanf(%d,&niter);/* initialize random numbers */ srand(SEED); count=0; int chunk;/ size chunk = 1;#pragma omp parallel shared(chunk) private(i,x,y,z) reduction(+:count) #pragma omp for
12、schedule(dynamic,chunk) for ( i=0; initer; i+) x = (double)rand()/RAND_MAX; y = (double)rand()/RAND_MAX; z = x*x+y*y; if (z=1) count+; pi=(double)count/niter*4; printf(# loop times= %d , estimate of pi is %g n,niter,pi);Openmp自動將for循環(huán)分解成多個線程并行執(zhí)行。4. 實驗結(jié)果與分析實驗結(jié)果如下:與實驗二相比本實驗是使用openmp自動分解多線程并行,精度并無明顯區(qū)別,
13、與所選算法有關(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 res
14、ultsd) compare it with the results of Lab2 and Lab32. 算法描述本實驗采用與實驗一實驗二相同的蒙特卡羅算法實現(xiàn)pi值得計算,即利用單位圓與邊長為1的正方形面積之比計算圓周率的近似值。比值的計算采用蒙特卡羅方法的隨即投點思想,在正方形中隨機投入很多點,使所投點在正方形中每一個位置的機會均等,然后考察有多少個點落在扇形內(nèi),落在扇形內(nèi)的點的個數(shù)與投點總數(shù)之比就是該比例的近似值。3. 實驗方案Mpi是一種基于消息傳遞的并行編程技術(shù),各個進程有獨立的堆棧和代碼段,進程之間的信息交互通過調(diào)用通信函數(shù)完成?;镜腁PI如下:int MPI_Init(int
15、 *argc, char *argv)MPI_Init 是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, int *rank); MPI獲取當(dāng)前進程的RA
16、NK,rank值取址范圍是0p-1,RANK值唯一 的表示了進程的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有MPI_INT, MPI_FLOAT等常用類型,Tag的作用是用于區(qū)分一對進程之間發(fā)送的不同信息int MPI_Recv(void* buf, i
17、nt 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#include#include#include#includevoid read_num(long long int *num_point,int my_rank,MPI_Comm comm);void
18、 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(int argc,char* argv) long long int num_in_cycle,num_point,total_num_in_cycle,local_num_point; int my_rank,comm_sz; MPI_Comm
19、 comm; MPI_Init(NULL,NULL);/初始化 comm=MPI_COMM_WORLD; MPI_Comm_size(comm,&comm_sz);/得到進程總數(shù) MPI_Comm_rank(comm,&my_rank);/得到進程編號 read_num(&num_point,my_rank,comm);/讀取輸入數(shù)據(jù) compute_pi(num_point,&num_in_cycle,&local_num_point,comm_sz,&total_num_in_cycle,comm,my_rank); MPI_Finalize(); return 0;void read_n
20、um(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 MPI_Bcast( void* data_p /in/out int count /in MPI_Datatype datatype /in int source_proc /in MPI_Comm comm /in ) */ MPI_Bcast(num_point,1,MPI_LONG_LONG,0,comm)
21、;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) *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 *lo
22、cal_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_squared=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
23、 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,comm); if(my_rank=0) double pi=(double)*total_num_in_cycle/(double)num_point*4; printf(the estimate value of pi is %lfn,pi); 4. 實驗結(jié)果與分析實驗結(jié)果如下:由結(jié)果可知循環(huán)次數(shù)越多得到的pi值就越精確。與實驗二和實驗三相
24、比,相同循環(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 and summary of the program execution pose optimization solution
25、based on the execution results and hardware pare it with the results of Lab2 ,Lab3 and Lab42.算法描述采用積分法計算pi值:積分法計算pi值的基本思想是利用1/(1+x2)的原函數(shù)為arctanx,再利用積分的基本步驟:分割,求和,取極限。將函數(shù)圖形與Y軸和直線X=1圍成的面積盡可能細(xì)分,然后求和,最后乘以相應(yīng)常數(shù),可以得到一個非常近似的pi值。3.實驗方案CUDA在執(zhí)行的時候是讓host里面的一個一個的kernel按照線程網(wǎng)格的概念在顯卡硬件(GPU)上執(zhí)行。每一個線程
26、網(wǎng)格又可以包含多個線程塊(block),每一個線程塊中又可以包含多個線程(thread)。基本API如下: cudaError_tcudaMalloc(void*devPtr,size_tsize); 在設(shè)備端分配size大小的空間,起始地址為devPtrcudaError_t cudaMemcpy (void * dst, const void * src,size_t count,enum cudaMemcpyKind kind); 將以src為地址長度為count的數(shù)據(jù)賦值到dst為起始地址的內(nèi) 存區(qū)域中,常用的kind有cudaMemcpyHostToDevice, cudaMemcp
27、yDeviceToHostcudaError_tcudaFree(void*devPtr);在設(shè)備端清理以devPtr為起始地址的內(nèi)存空間 將任務(wù)合理的分配到grid和thread中,有助于提升程序的性能:grid of thread:具體實驗代碼如下:#include _global_ void kernel(double *gpu_p) int gpu_count = 0; for (int gpu_i = 1; gpu_i = 1000; gpu_i+) if (gpu_count % 2 = 0) gpu_pgpu_i= gpu_pgpu_i-1 - 4 / (double)(2*gp
28、u_i-1); else gpu_pgpu_i = gpu_pgpu_i-1 + 4 / (double)(2*gpu_i-1); gpu_count = gpu_count + 1; _syncthreads();int main(int argc, char *argv) int i; int count=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= (doubl
29、e *)malloc(1000 *sizeof(double); cudaMalloc(void *) &g_p,1000 * sizeof(double); cudaMemcpy(g_p, p,1000 * sizeof(double), cudaMemcpyHostToDevice); kernel ( g_p); cudaMemcpy(p, g_p,1000 * sizeof(double), cudaMemcpyDeviceToHost); cudaFree(g_p); for (i = 1; i = 1000; i+) printf( %2.4ft, pi); printf( blocks = %d block size= %dt,n_blocks, block_size ); system(pause); return 0; 4.實驗結(jié)果與分析實驗結(jié)果如下:與前三次實驗采用的蒙特卡羅法相比,積分法的精度更高,使用cuda計算pi值,速度更快。PROJECT2AIM:master the two typical par
溫馨提示
- 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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- DB32/T 4155.10-2021全民健康信息平臺共享數(shù)據(jù)集規(guī)范第10部分:醫(yī)療質(zhì)控
- DB32/T 4057-2021禽肉中銅、鎘等18種元素含量的測定電感耦合等離子體質(zhì)譜法
- DB32/T 4002-2021大跨徑懸索橋預(yù)制平行鋼絲索股通用技術(shù)條件
- DB32/T 3872-2020電動滑板車安全技術(shù)規(guī)范
- DB32/T 3763-2020新型冠狀病毒肺炎疫情防控居家隔離技術(shù)規(guī)范
- DB32/T 3761.48-2021新型冠狀病毒肺炎疫情防控技術(shù)規(guī)范第48部分:人員密集型場所快速調(diào)查和處置
- DB32/T 3729-2020融合媒體內(nèi)容平臺運營及托管服務(wù)音視頻文件交互規(guī)范
- DB32/T 3607-2019監(jiān)獄醫(yī)院設(shè)施設(shè)備配置規(guī)范
- DB32/T 3533-2019梨樹單主枝連體型栽培技術(shù)規(guī)程
- DB32/T 3519-2019芋頭脫毒快繁技術(shù)規(guī)程
- 服飾終端銷售問與答全冊
- 2025航天知識競賽考試題庫(含答案)
- 人工智能技術(shù)在混合式日語教學(xué)中的應(yīng)用及效果評估
- 鄭州電子商務(wù)職業(yè)學(xué)院《文化創(chuàng)意產(chǎn)業(yè)管理學(xué)》2023-2024學(xué)年第二學(xué)期期末試卷
- 2024建安杯信息通信建設(shè)行業(yè)安全競賽題庫(試題含答案1-464題)
- 基于動態(tài)勢能獎勵機制的雙足機器人穩(wěn)定行走控制研究
- 查找身邊的安全隱患
- 乳腺癌手術(shù)的整體治療
- 2023年陜西省普通高校職業(yè)教育單獨招生考試英語試題及答案
- 工程師轉(zhuǎn)正工作總結(jié)
- 8 推翻帝制 民族覺醒 說課稿 -2023-2024學(xué)年道德與法治五年級下冊統(tǒng)編版
評論
0/150
提交評論