数值模拟与并行编程计算

Numerical Simulation and Parallel ProgrammingNotebookVersion1.02Haide

0 Information

1. Learn numerical simulations for simple physical models described by differential equations.
2. Learn the basic of MPI (Message-Passing Interface) parallel programming.

Keyword: Heat conduction equation, finite difference method, MPI parallelization

1 Schedule This part has 30 credit hours in total.
1-4: How to program on Linux server.
5-8: Learn numerical simulation for 1-D heat conduction.
9-12: Learn numerical simulation program for 2-D heat conduction.
13-16: Learn the basic of parallel programming with MPI.
17-24: Parallelize the 2-D heat conduction simulation program with MPI.It is recommended that make use of the class timeeffectively. Keep up with the programming even ifyou don’tunderstand something.2 How to submit a report Explain at the beginning (or in the middle) of the class.
• Time Limit: Further notice.
• How to submit: Print in A4 paper. Single-sided printing,double-sided printing, black and whiteprinting, color printing, are acceptable.

3 Academic Integrity

It is unacceptable to submit work that is not your own. Assignments will bechecked for similarity and copying to ensure that students work is their own. Information sources mustbe paraphrased (put in your OWN words - not copied directly) and the reference must be included. Youare also responsible for complying with any additional rules related to assessments communicated to youby your instructors. Assignments that are found to breach this may be given zero. Furtheractions maybe taken.

4 Precautions about this part

• Represent numerical values (for example, temperature value) in double decision.
• It will not lose points if your program have to be recompiled every time when the setting changes.
• In the sample program, “ ” means a half-width space.
5 Precautions about report
• When visualizing the computing results, use the actual coordinates or time, instead of the numberof steps (space or time).
• Don’t insert too many pictures or graphs unless it is necessary.
• All pictures and graphs should be mentioned in the report.
• If you cite a paper or a website, identify it as a reference.
• Formulas should be concise, and be explained in words.

1 How to program on Linux serverImportant information of server
• Server IP address: Further notice
• User Account: Further notice
• Password: Further notice
Basic Linux Commands The server is a Linux system (a type ofoperating system) which isconsiderably different from Windows, and similar to Mac. In order to be more productive in this class,you need to learn some basic about how to get things done in the Linux system.

  1. ls (short for list). To find out what is in your currentdirectory, type$ lsTo list all files in your directory including those whosenames begin with a dot (hidden files), type$ ls -a
  2. mkdir (make directory). Make a subdirectory in your current working directory, for example test.Type$ mkdir test
  3. cd (change directory)To change to the directory you have just made, type$ cd testExercise 1a Make another directory inside the test directory called backups
  4. The directories . and ..In Linux, “.” means the current directory, ”..” means the parent of the current directory, so typing$ cd .. (NOTE: there is a space between cd and the dot)
    will take you one directory up the hierarchy.array is A[j ∗ n + i] (0

We can confirm whether our program runs correctly by checking the values of matrix C after calculation.For measuring the execution time, we can write a function get time as following.

#include 
double get_time ()
{
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec + (double)tv.tv_usec*1e-6;
}
Then we can get the elapsed time (seconds) like
t0 = get_time ();
/* process to be measured */
t1 = get_time ();
time = t1 - t0;

In addition to the execution time, we can calculate FLOPS (Floatingpoint Operations Per Second) too.FLOPS = Times of Floating-point OperationsElapsed Time (second) (1.4)Since the number of floating-point operations in Eq. (1.1) is 2n3, so flops in our program can be computedasflops = 2.0 (double)n (double)n* (double)n/time;Then we display the execution time and FLOPS.printf ("n!=!%d,!!time!(sec)!=!%8.3e,!!FLOPS!=!%8.3e\n", n, time, flops);Finally, free memory allocated in step 2.free (a);

Most compilers have optimization option. Depending on this option, our program is optimizedat compile time. Please check how the program performance influenced by optimization option.Write a program that performs the calculation in Eq. (1.1). Then compile it like$gcc main.c -OX -lmThe letter after the hyphen − is the uppercase letter O, and the X part is a number from0(zero) to 3. The higher thenumber, the stronger the compiler optimization (0 means no optimization). ”-lm” may be necessary if you include math.h in your program.Set n = 1000, 2000, 3000, change the optimization option from-O0 to -O3 and measure theexecution time and FLOPS for each case.
Task 1.2The main part of this program (the part that calculates Eq.(1.1)) is a triple loop structure, andthe loop variables are i, j, k. The order of these loops can be changed. For example, j → k →i does not change the final result, but the memory access pattern (the wayof reading/writingthe elements of array) will be different. Set n = 1000, 2000, 3000, change the loop order (6 typesin total), and check the performance. Note that, in order to eliminate the effects of compileroptimization, please compile your program with optimization option -O0. If the execution time(performance) differs, pleaseexplain it as far as you can understand.

2 Numerical simulation

conductionComputer simulations are widely used in the fields of engineering and natural sciences. Generally, whensimulating a certain physical phenomenon, four steps are included.

1. Modeling: derive one or more partial differential equations (PDEs) to describe the phenomenon.
2. Discretization: transform partial differential equations into discrete form.
3. Calculation: solve discretized problems with computer, get the numerical solutions of partial differential equations.
4. Visualization: Display calculation results into visible form.

We will experience the above flow through a simple physicalphenomenon as an example.Problem setting: One-dimensional heat conductionConsider the conduction of heatalong a stick of length L as shown in Fig. 2.1. We don’t care about the cross-sectional area since it’s verysmall relative to the length. Let the left endpoint be the original point (x = 0), and the x-axis is taken inthe length direction. Heat conduction happens along ±x direction. In addition, heat exchange betweenthis stick and the outside world occurs only at the end points.Fig 2.1: A stickDerivation of partial differential equations Let u(x, t)denote thetemperature at position x
at time t, 0 ≤ x ≤ L, 0 ≤ t ≤ T. 1 This 1-D heat conductionphenomenon can be described by a partialdifferential equation. (See Task 2.3 for

1 /∗ Include required header files ∗/
2 #define L 1.0 //Length of bar
3 #define T 1.0 //Maximun time to simulation
4 #define alpha 1.0 //Thermal diffusivity
5 #define T SPAN 20 //Result output frequency (adjusted appropriately)
6
7 int main(int argc, char ∗argv[])
8 {
9 /∗ Declaration of variables (add each necessary variable) ∗/
10 double ∗u, ∗uu, dx, dt;
11 int Mx, N;
12
13 /∗ Get the values of Mx and N from the arguments ∗/
14
15 /∗ Dynamic allocation of memory, variable setting, etc. ∗/
16
17 for(i = 0; i <= MX; i++)
18 {
19 /∗ Set initial conditions for u ∗/
20 }
21 print data(...); //Output the initial state to a file
22
23 for(n = 1; n <= N; n++)
24 {
25 for(i = 1; i < MX; i++)
26 {
27 /∗ Calculate the value (uu) of the next time step from the current value (u) ∗/
28 }
29 for(i = 1; i < MX; i++)
30 {
31 /∗ Copy the value of uu to u ∗/
32 }
33 if(n%T SPAN == 0)
34 {
35 print data(...); //Output to a file at a regular frequency
36 }
37 }
38
39 /∗ Free memory ∗/
40
41 return 0;
42 }

1 void print data(int Mx, int n, double dx, double dt, double ∗u)
2 {
3 FILE ∗fp;
4 char sfile[256];
5 int i;
6
7 sprintf(sfile, "data_%06d.dat", n);
8 fp = fopen(sfile, "w");
9 fprintf(fp, "#time!=!%lf\n", (double)n ∗ dt);
10 fprintf(fp, "#x!u\n");
11 for(i = 0; i <= MX; i++)
12 {
13 fprintf(fp, "%lf!%12lf\n", (double)i∗dx, u[i]);
14 }
15 fclose(fp);
16 return;
17 }

Now we can use gnuplot to display data in a single file. First, create a script files shown in Programs2.3 in the same folder with your program. Then run the following command from the terminal.
$gnuplot sample0.gp

5 Parallelization of 2-D heat

conduction simulation with MPIParallelization policy: Datadistribution and communication In 2-D thermal heatconduction simulation, the values of ui,j , (1 ≤ i ≤ Mx − 1, 1 ≤ j ≤ My − 1) are calculated at eachtime step. For the calculation of ui,j , only the values of previous time step are necessary. There is nodependency among unknowns at different locations. In other words, it is possible to calculate ui,j atdifferent points at the same time. Therefore, we divide the whole area into several regions and let each
processor in charge of one region.
Task 5.As before, discretize it with central difference (number of divisions: M), we getui+1 − 2ui + ui−1∆x2 = 0 ⇔ 2ui − ui+1 − ui−1 = 0, (i ≤ i ≤ M − 1), (6.2)where u0 and uM are determined by boundary conditions. This is a linear system with M − 1 unknowns.There are various methods for solving this linear system. For example, LUdecomposition (Gaussianelimination), Jacobi method and CG method. In general, when the coefficient matrix is large and sparse(mostly zero components), LU decomposition is not workable. We will solve it Ax = b.Please give the elements of coefficient matrix A and the right-hand side vector b.Task∗ 6.2Use conjugate gradient method to find the steady state of 1-D heat conduction. The boundarycondition is u0 = 0.2, uM = 1.0. Compare the results with that we simulated before.

你可能感兴趣的:(算法)