Download presentation
Presentation is loading. Please wait.
1
Chapter 15 C-implementation PA = LU Speaker: Lung-Sheng Chien
2
OutLine Data structure of full matrix Implementation of PA=LU Visual Studio 2005: How To Linux machine: How to compile
3
6-224 12-8610 3-1393 -641-18 Logical index : 2D row-major based 6-224 12-8610 3-1393 -641-18 col-major based 6-224 12-8610 3-1393 -641-18 row-major versus col-major We choose col-major representation
4
6 12 3 -6 -2 -8 -13 4 2 6 9 1 4 10 3 -18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 col-major based 6-224 12-8610 3-1393 -641-18 0 col-major: C starts index from 0 is useless 1 2 is forbidden legal matrix.h 3
5
Relaxation for high precision package http://crd.lbl.gov/~dhbailey/mpdist/ Large 1D array, “int” is 4-byte, only supports up to 2G elements We use col-major, however you can implement both. global.h
6
matrix.h Data structure of matrix and its method 1 constructor ( 建構子 ): zeros 2 destructor ( 解構子 ): dealloc 3 Index rule: we do it explicitly 4 Arithmetic: matvec, matsub, norm 5 I/O: disp
7
matrix.cpp 1 empty handler 2 1 2 3 4 5 3 contiguous memory block 6 12 3 1 2 3 constructor [1] 5 set parameter
8
matrix.cpp 1 2 3 4 5 6 12 3 -6 -2 -8 -13 4 2 6 9 1 4 10 3 -18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 constructor [2] 4 4 pointer arithmetic low high
9
destructor matrix.cpp 1 2 3 6 12 3 -6 -2 -8 -13 4 2 6 9 1 4 10 3 -18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 0 1 free physic storage 2 free pointer array 3 free matrix handler
10
matrix.cpp Input/Output 6-224 12-8610 3-1393 -641-18 Show in standard form: human readable
11
main.cpp Simple driver Execution result
12
matrix.cpp duplicate of a matrix / vector We require matrix handler to verify configuration, this is very important for debugging. verification
13
y = Ax matrix.cpp Outer-product formulation
14
Matrix / vector norm matrix.cpp Maximum of absolute column sum Maximum of absolute row sum 2-norm is difficult to compute, we will discuss this later
15
OutLine Data structure of full matrix Implementation of PA=LU Visual Studio 2005: How To Linux machine: How to compile
16
Algorithm ( PA = LU ) [1] Given full matrix, construct initial lower triangle matrix use permutation vector to record permutation matrix (1) we store lower triangle matrix L into storage of matrix A (2) The reason to construct type int_matrix is for permutation matrix P, since we can check dimension between A and P. verification
17
we have compute update original matrix stores in lower triangle matrix for let,and Algorithm ( PA = LU ) [2] Note that P is a column vector
18
Algorithm ( PA = LU ) [3] find a such that 1 swap rowand row 2 define permutation matrix then after swapping rows, we have, NOT efficient
19
computefor 3 Algorithm ( PA = LU ) [4] compute by swappingand then ifthen endif 4
20
Algorithm ( PA = LU ) [5] we store lower triangle matrix into storage of matrix A, this code is different from code in MATLAB. decompose 5 How to computeefficiently?
21
Algorithm ( PA = LU ) [6] component-wise update : Observation: we implement with column-major form, column-wise update is good
22
Algorithm ( PA = LU ) [7] endfor by updating by updating matrix then where (recursion is done)
23
1 0.251 -0.501 0.5-2/111/111 12-8610 -117.50.5 4-13 3/11 6-224 12-8610 3-1393 -641-18 Algorithm ( PA = LU ) [8] Store L and U into original A Question 1: how to write down a test driver Question 2: how to do backward and forward
24
main.cpp Test driver [1] 6-224 12-8610 3-1393 -641-18 Remember to duplicate matrix A since A is modified when execute LU decomposition. 1
25
Test driver [2] 2 3 4 5 6 5 6 7 8 -6.9306 17.9583 26.5833 7.3333 Linear solver: do backward and forward
26
Exercise Implement PA = LU. Implement forward and backward. Write a driver to test your linear solver. Any other implementation for full matrix?
27
OutLine Data structure of full matrix Implementation of PA=LU Visual Studio 2005: How To Linux machine: How to compile
28
Visual Studio 2005: How To [1] Create a new project: File Project
29
Visual Studio 2005: How To [2] Choose Win32 console application, this is the same as what we do in VC 6.0 Project name: vc 2005 will create a directory, the same name as project name
30
Visual Studio 2005: How To [3] We choose “next” bottom to set an empty project, DO NOT press Finish bottom.
31
Visual Studio 2005: How To [4] Empty project, this is very important
32
Visual Studio 2005: How To [5] 1 Empty project 2 Add new item (source file or header file) to this project 3 Add “main.cpp” to this project
33
Visual Studio 2005: How To [6] Write source code “hello world” Compile: Build Build matrix
34
Visual Studio 2005: How To [7] Result of compilation, including linking: success Additional directory “debug” appears Executable file “matrix.exe” in this debug directory
35
Visual Studio 2005: How To [8] Execute matrix.exe: Debug Start Without Debugging Execution in VC 6.0
36
Visual Studio 2005: How To [9] Suppose we want to add several source files into this project. Copy files into directory /matrix/matrix 1 2 Add source files into the project by Project Add Existing item
37
Visual Studio 2005: How To [10] Select the files you want to add All souce files in this project
38
Visual Studio 2005: How To [11] Remember that executable file is in directory /matrix/debug, you can use command window to execute matrix.exe
39
Visual Studio 2005: How To [12] There is another “debug” directory in /matrix/matrix, this directory contains object files, no executable file
40
OutLine Data structure of full matrix Implementation of PA=LU Visual Studio 2005: How To Linux machine: How to compile
41
Linux machine: How to compile [1] Suppose we put all source file into directory matrix_constructor and upload to workstation. 1 2 Remove all VC related files. 3 use command “qmake -project” to generate project file
42
Linux machine: How to compile [2] 4 use command “qmake matrix_constructor.pro” to generate Makefile Later on, we will introduce Makefile 5 use command “make” to compile your codes and generate executable file
43
6 Execute executable file by “./matrix_constructor” Linux machine: How to compile [3]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.