Ubuntu19.10下TAU的配置及梯形积分法的实现
目錄
- 前言
- 一、 下載并安裝TAU的前置事項
- 二、 編譯并安裝TAU
- 三、 編寫代碼并運行
- (1)串行部分
- (2)并行部分
- (3)結果分析
前言
TAU是一種可以在Ubuntu下對并行運算的進程進行性能評估的軟件,但是國內目前少有中文的經驗,特此分享。
一、 下載并安裝TAU的前置事項
http://tau.uoregon.edu/pdt.tar.gz
http://tau.uoregon.edu/tau.tgz
二、 編譯并安裝TAU
-
命令行:在終端輸入./configure -mpi …等參數進行配置編譯等
-
圖形界面:需要安裝java,在終端輸入bash ./tau_setup即可,下圖即為對應的圖形界面
./installtau -mpi -mpiinc=… -mpilib=… -tag=openmpi -pdt=… -j8
- 首先需要安裝vim:sudo apt-get install vim。
- 安裝完成后,輸入:vim ~\.bashrc,按i進行編輯,在最下方加入以下代碼。如圖紅框所示,注意,對應的位置請按照自己安裝的環境變化。
配置成功后,即可直接調用tau_cc.sh,pprof,paraprof等指令。
三、 編寫代碼并運行
(1)串行部分
接下來,編寫一個串行的梯形積分法程序,并編譯運行。串行代碼如下:
//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編譯會報錯,直接gcc編譯即可。并行的程序才需要用tau_cc.sh編譯。
(2)并行部分
然后,編寫并行的程序。其中,solution1采用的方法是常規的手動分配給每個線程不同的計算內容。并用MPI_recv和MPI_send進行匯總。如果comm_sz%n != 0,即comm_sz除不盡n,就把剩下的部分平均分配到每個線程上。
//并行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; //這樣整除可能會剩下一部分沒算,所以之后需要補上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進行自動全局操作,比較方便。分別編譯運行后比較其性能。
//并行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; }進行編譯:tau_cc.sh solution1.c -o solution1。編譯完成之后,進行性能剖析。使用指令:mpirun solution1。這樣,會產生幾個profile文件,性能剖析就完成了。如下圖所示:
然后,打開文件。使用指令pprof和paraprof,觀察生成內容。生成的文件中可以看到分配的CPU個數,程序開始時間與停止時間等基本信息。也可以對不同的進程、不同的函數耗費的時間進行對比分析。
(3)結果分析
- solution1
- solution2
觀察可以發現,使用MPI_reduce會多出個user events。相比之下兩者開銷差別不是很大。
總結
以上是生活随笔為你收集整理的Ubuntu19.10下TAU的配置及梯形积分法的实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Qt】QSettings介绍【转】
- 下一篇: 容联云通讯短信接口 Python3 文档