Ubuntu19.10下TAU的配置及梯形积分法的实现
目錄
- 前言
- 一、 下載并安裝TAU的前置事項(xiàng)
- 二、 編譯并安裝TAU
- 三、 編寫(xiě)代碼并運(yùn)行
- (1)串行部分
- (2)并行部分
- (3)結(jié)果分析
前言
TAU是一種可以在Ubuntu下對(duì)并行運(yùn)算的進(jìn)程進(jìn)行性能評(píng)估的軟件,但是國(guó)內(nèi)目前少有中文的經(jīng)驗(yàn),特此分享。
一、 下載并安裝TAU的前置事項(xiàng)
http://tau.uoregon.edu/pdt.tar.gz
http://tau.uoregon.edu/tau.tgz
二、 編譯并安裝TAU
-
命令行:在終端輸入./configure -mpi …等參數(shù)進(jìn)行配置編譯等
-
圖形界面:需要安裝java,在終端輸入bash ./tau_setup即可,下圖即為對(duì)應(yīng)的圖形界面
./installtau -mpi -mpiinc=… -mpilib=… -tag=openmpi -pdt=… -j8
- 首先需要安裝vim:sudo apt-get install vim。
- 安裝完成后,輸入:vim ~\.bashrc,按i進(jìn)行編輯,在最下方加入以下代碼。如圖紅框所示,注意,對(duì)應(yīng)的位置請(qǐng)按照自己安裝的環(huán)境變化。
配置成功后,即可直接調(diào)用tau_cc.sh,pprof,paraprof等指令。
三、 編寫(xiě)代碼并運(yùn)行
(1)串行部分
接下來(lái),編寫(xiě)一個(gè)串行的梯形積分法程序,并編譯運(yùn)行。串行代碼如下:
//serial.c #include <stdio.h> #define MAXN 100000double f(double x); double integ(double a, double b);void main() {double a = 0, b = 19.9;printf("%.10lf\n", integ(a, b)); }double integ(double a, double b) { int i, n = MAXN; double approx, h = 0, x_i;approx = (f(a) + f(b)) / 2.0; h = (b - a)/n; for(i = 0; i <= n-1; i++){x_i = a + i * h;approx += f(x_i);}approx = h * approx;return approx; }double f(double x) {double fx = x * x;return fx; }注意,串行的程序編譯如果使用tau_cc.sh編譯會(huì)報(bào)錯(cuò),直接gcc編譯即可。并行的程序才需要用tau_cc.sh編譯。
(2)并行部分
然后,編寫(xiě)并行的程序。其中,solution1采用的方法是常規(guī)的手動(dòng)分配給每個(gè)線程不同的計(jì)算內(nèi)容。并用MPI_recv和MPI_send進(jìn)行匯總。如果comm_sz%n != 0,即comm_sz除不盡n,就把剩下的部分平均分配到每個(gè)線程上。
//并行solution1.c #include <stdio.h> #include <mpi.h>double f(double x) {return x * x; }double Trap(double left_endpt, double right_endpt, int trap_count, double base_len) {double estimate, x;int i;estimate = (f(left_endpt) + f(right_endpt)) / 2.0;for (i = 0; i < trap_count - 1; i++){x = left_endpt + (i + 1) * base_len;estimate += f(x);}return estimate * base_len; }int main() {int my_rank, comm_sz, n = 99999, local_n;double a = 0.0, b = 19.9, h, local_a, local_b, remain = 0.0;double local_int, total_int;int source;MPI_Init(NULL, NULL);MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);h = (b - a) / n; //h is the same for all processeslocal_n = n / comm_sz; //這樣整除可能會(huì)剩下一部分沒(méi)算,所以之后需要補(bǔ)上local_a = a + my_rank * local_n * h;local_b = local_a + local_n * h;local_int = Trap(local_a, local_b, local_n, h);if (my_rank != 0){MPI_Send(&local_int, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);}else{total_int = local_int;for (source = 1; source < (comm_sz); source++){MPI_Recv(&local_int, 1, MPI_DOUBLE, source, 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);total_int += local_int;}// 如果除不盡if (n % comm_sz){remain = Trap(h * local_n * comm_sz, b, n % comm_sz, h);}total_int += remain;}if (my_rank == 0){printf("With n = %d trapezoids, our estimate\n", n);printf("of the integral from %f to %f = %.10e\n", a, b, total_int);}MPI_Finalize();return 0; }solution2是使用MPI_reduce進(jìn)行自動(dòng)全局操作,比較方便。分別編譯運(yùn)行后比較其性能。
//并行solution2.c #include <stdio.h> #include <mpi.h>double f(double x) {return x * x; }double Trap(double left_endpt, double right_endpt, int trap_count, double base_len) {double estimate, x;int i;estimate = (f(left_endpt) + f(right_endpt)) / 2.0;for (i = 0; i < trap_count - 1; i++){x = left_endpt + (i + 1) * base_len;estimate += f(x);}return estimate * base_len; }int main() {int my_rank, comm_sz, n = 99999, local_n;double a = 0.0, b = 19.9, h, local_a, local_b, remain = 0.0;double local_int, total_int;int source;MPI_Init(NULL, NULL);MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);MPI_Comm_size(MPI_COMM_WORLD, &comm_sz);h = (b - a) / n;local_n = n / comm_sz; local_a = a + my_rank * local_n * h;local_b = local_a + local_n * h;local_int = Trap(local_a, local_b, local_n, h);MPI_Reduce(&local_int, &total_int, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);if (n % comm_sz){remain = Trap(h * local_n * comm_sz, b, n % comm_sz, h);total_int += remain;}if (my_rank == 0){printf("With n = %d trapezoids, our estimate\n", n);printf("of the integral from %f to %f = %.10e\n", a, b, total_int);}MPI_Finalize();return 0; }進(jìn)行編譯:tau_cc.sh solution1.c -o solution1。編譯完成之后,進(jìn)行性能剖析。使用指令:mpirun solution1。這樣,會(huì)產(chǎn)生幾個(gè)profile文件,性能剖析就完成了。如下圖所示:
然后,打開(kāi)文件。使用指令pprof和paraprof,觀察生成內(nèi)容。生成的文件中可以看到分配的CPU個(gè)數(shù),程序開(kāi)始時(shí)間與停止時(shí)間等基本信息。也可以對(duì)不同的進(jìn)程、不同的函數(shù)耗費(fèi)的時(shí)間進(jìn)行對(duì)比分析。
(3)結(jié)果分析
- solution1
- solution2
觀察可以發(fā)現(xiàn),使用MPI_reduce會(huì)多出個(gè)user events。相比之下兩者開(kāi)銷差別不是很大。
總結(jié)
以上是生活随笔為你收集整理的Ubuntu19.10下TAU的配置及梯形积分法的实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【Qt】QSettings介绍【转】
- 下一篇: ubuntu安装keepass2 中文乱