Parallel processing 用ksp准则求解线性系统的PETSc
我开始使用PETSc库并行求解线性方程组。我已经安装了所有软件包,成功地构建并运行了petsc/src/ksp/ksp/examples/tutorials/folder中的示例,例如ex.c 但我无法理解如何通过从文件中读取矩阵A、X和B来填充它们 在这里,我提供了ex2.c文件中的代码:Parallel processing 用ksp准则求解线性系统的PETSc,parallel-processing,mpi,linear-equation,petsc,Parallel Processing,Mpi,Linear Equation,Petsc,我开始使用PETSc库并行求解线性方程组。我已经安装了所有软件包,成功地构建并运行了petsc/src/ksp/ksp/examples/tutorials/folder中的示例,例如ex.c 但我无法理解如何通过从文件中读取矩阵A、X和B来填充它们 在这里,我提供了ex2.c文件中的代码: /* Program usage: mpiexec -n <procs> ex2 [-help] [all PETSc options] */ static char help[] = "
/* Program usage: mpiexec -n <procs> ex2 [-help] [all PETSc options] */
static char help[] = "Solves a linear system in parallel with KSP.\n\
Input parameters include:\n\
-random_exact_sol : use a random exact solution vector\n\
-view_exact_sol : write exact solution vector to stdout\n\
-m <mesh_x> : number of mesh points in x-direction\n\
-n <mesh_n> : number of mesh points in y-direction\n\n";
/*T
Concepts: KSP^basic parallel example;
Concepts: KSP^Laplacian, 2d
Concepts: Laplacian, 2d
Processors: n
T*/
/*
Include "petscksp.h" so that we can use KSP solvers. Note that this file
automatically includes:
petscsys.h - base PETSc routines petscvec.h - vectors
petscmat.h - matrices
petscis.h - index sets petscksp.h - Krylov subspace methods
petscviewer.h - viewers petscpc.h - preconditioners
*/
#include <C:\PETSC\include\petscksp.h>
#undef __FUNCT__
#define __FUNCT__ "main"
int main(int argc,char **args)
{
Vec x,b,u; /* approx solution, RHS, exact solution */
Mat A; /* linear system matrix */
KSP ksp; /* linear solver context */
PetscRandom rctx; /* random number generator context */
PetscReal norm; /* norm of solution error */
PetscInt i,j,Ii,J,Istart,Iend,m = 8,n = 7,its;
PetscErrorCode ierr;
PetscBool flg = PETSC_FALSE;
PetscScalar v;
#if defined(PETSC_USE_LOG)
PetscLogStage stage;
#endif
PetscInitialize(&argc,&args,(char *)0,help);
ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr);
ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Compute the matrix and right-hand-side vector that define
the linear system, Ax = b.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Create parallel matrix, specifying only its global dimensions.
When using MatCreate(), the matrix format can be specified at
runtime. Also, the parallel partitioning of the matrix is
determined by PETSc at runtime.
Performance tuning note: For problems of substantial size,
preallocation of matrix memory is crucial for attaining good
performance. See the matrix chapter of the users manual for details.
*/
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
ierr = MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr);
ierr = MatSeqAIJSetPreallocation(A,5,PETSC_NULL);CHKERRQ(ierr);
/*
Currently, all PETSc parallel matrix formats are partitioned by
contiguous chunks of rows across the processors. Determine which
rows of the matrix are locally owned.
*/
ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
/*
Set matrix elements for the 2-D, five-point stencil in parallel.
- Each processor needs to insert only elements that it owns
locally (but any non-local elements will be sent to the
appropriate processor during matrix assembly).
- Always specify global rows and columns of matrix entries.
Note: this uses the less common natural ordering that orders first
all the unknowns for x = h then for x = 2h etc; Hence you see J = Ii +- n
instead of J = I +- m as you might expect. The more standard ordering
would first do all variables for y = h, then y = 2h etc.
*/
ierr = PetscLogStageRegister("Assembly", &stage);CHKERRQ(ierr);
ierr = PetscLogStagePush(stage);CHKERRQ(ierr);
for (Ii=Istart; Ii<Iend; Ii++) {
v = -1.0; i = Ii/n; j = Ii - i*n;
if (i>0) {J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (i<m-1) {J = Ii + n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j>0) {J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
if (j<n-1) {J = Ii + 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);}
v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr);
}
/*
Assemble matrix, using the 2-step process:
MatAssemblyBegin(), MatAssemblyEnd()
Computations can be done while messages are in transition
by placing code between these two statements.
*/
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = PetscLogStagePop();CHKERRQ(ierr);
/* A is symmetric. Set symmetric flag to enable ICC/Cholesky preconditioner */
ierr = MatSetOption(A,MAT_SYMMETRIC,PETSC_TRUE);CHKERRQ(ierr);
/*
Create parallel vectors.
- We form 1 vector from scratch and then duplicate as needed.
- When using VecCreate(), VecSetSizes and VecSetFromOptions()
in this example, we specify only the
vector's global dimension; the parallel partitioning is determined
at runtime.
- When solving a linear system, the vectors and matrices MUST
be partitioned accordingly. PETSc automatically generates
appropriately partitioned matrices and vectors when MatCreate()
and VecCreate() are used with the same communicator.
- The user can alternatively specify the local vector and matrix
dimensions when more sophisticated partitioning is needed
(replacing the PETSC_DECIDE argument in the VecSetSizes() statement
below).
*/
ierr = VecCreate(PETSC_COMM_WORLD,&u);CHKERRQ(ierr);
ierr = VecSetSizes(u,PETSC_DECIDE,m*n);CHKERRQ(ierr);
ierr = VecSetFromOptions(u);CHKERRQ(ierr);
ierr = VecDuplicate(u,&b);CHKERRQ(ierr);
ierr = VecDuplicate(b,&x);CHKERRQ(ierr);
/*
Set exact solution; then compute right-hand-side vector.
By default we use an exact solution of a vector with all
elements of 1.0; Alternatively, using the runtime option
-random_sol forms a solution vector with random components.
*/
ierr = PetscOptionsGetBool(PETSC_NULL,"-random_exact_sol",&flg,PETSC_NULL);CHKERRQ(ierr);
if (flg) {
ierr = PetscRandomCreate(PETSC_COMM_WORLD,&rctx);CHKERRQ(ierr);
ierr = PetscRandomSetFromOptions(rctx);CHKERRQ(ierr);
ierr = VecSetRandom(u,rctx);CHKERRQ(ierr);
ierr = PetscRandomDestroy(&rctx);CHKERRQ(ierr);
} else {
ierr = VecSet(u,1.0);CHKERRQ(ierr);
}
ierr = MatMult(A,u,b);CHKERRQ(ierr);
/*
View the exact solution vector if desired
*/
flg = PETSC_FALSE;
ierr = PetscOptionsGetBool(PETSC_NULL,"-view_exact_sol",&flg,PETSC_NULL);CHKERRQ(ierr);
if (flg) {ierr = VecView(u,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create the linear solver and set various options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Create linear solver context
*/
ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
/*
Set operators. Here the matrix that defines the linear system
also serves as the preconditioning matrix.
*/
ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
/*
Set linear solver defaults for this problem (optional).
- By extracting the KSP and PC contexts from the KSP context,
we can then directly call any KSP and PC routines to set
various options.
- The following two statements are optional; all of these
parameters could alternatively be specified at runtime via
KSPSetFromOptions(). All of these defaults can be
overridden at runtime, as indicated below.
*/
ierr = KSPSetTolerances(ksp,1.e-2/((m+1)*(n+1)),1.e-50,PETSC_DEFAULT,
PETSC_DEFAULT);CHKERRQ(ierr);
/*
Set runtime options, e.g.,
-ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol>
These options will override those specified above as long as
KSPSetFromOptions() is called _after_ any other customization
routines.
*/
ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Solve the linear system
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Check solution and clean up
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
Check the error
*/
ierr = VecAXPY(x,-1.0,u);CHKERRQ(ierr);
ierr = VecNorm(x,NORM_2,&norm);CHKERRQ(ierr);
ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
/* Scale the norm */
/* norm *= sqrt(1.0/((m+1)*(n+1))); */
/*
Print convergence information. PetscPrintf() produces a single
print statement from all processes that share a communicator.
An alternative is PetscFPrintf(), which prints to a file.
*/
ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of error %A iterations %D\n",
norm,its);CHKERRQ(ierr);
/*
Free work space. All PETSc objects should be destroyed when they
are no longer needed.
*/
ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr);
ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr);
/*
Always call PetscFinalize() before exiting a program. This routine
- finalizes the PETSc libraries as well as MPI
- provides summary and diagnostic information if certain runtime
options are chosen (e.g., -log_summary).
*/
ierr = PetscFinalize();
return 0;
}
/*程序用法:mpiexec-n ex2[-help][所有PETSc选项]*/
静态字符帮助[]=“与KSP并行求解线性系统。\n\
输入参数包括:\n\
-随机精确解:使用随机精确解向量\n\
-查看精确解:将精确解向量写入标准输出\n\
-m:x方向上的网格点数量\n\
-n:y方向上的网格点数量\n\n“;
/*T
概念:KSP ^基本并行示例;
概念:KSP^Laplacian,2d
概念:拉普拉斯,2d
处理器:n
T*/
/*
包括“petscksp.h”,以便我们可以使用KSP解算器。请注意,此文件
自动包括:
h-基本PETSc例程petscvec.h-向量
petscmat.h-矩阵
h-指数集petscksp.h-Krylov子空间方法
h-查看器petscpc.h-预处理程序
*/
#包括
#未定义函数__
#定义函数“main”
int main(int argc,char**args)
{
向量x,b,u;/*近似解,RHS,精确解*/
Mat A;/*线性系统矩阵*/
KSP KSP;/*线性解算器上下文*/
PetsCandom rctx;/*随机数生成器上下文*/
PetscReal范数;/*解误差范数*/
PetscInt i,j,Ii,j,Istart,Iend,m=8,n=7,its;
PetscErrorCode-ierr;
PetscBool flg=PETSC_FALSE;
petscv;
#如果已定义(PETSC_使用_日志)
PetscLogStage;
#恩迪夫
PetscInitialize(&argc,&args,(char*)0,help);
ierr=petscoptiongetint(PETSC_NULL,“-m”,&m,PETSC_NULL);CHKERRQ(ierr);
ierr=petscoptiongetint(PETSC_NULL,“-n”,&n,PETSC_NULL);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
计算定义的矩阵和右侧向量
线性系统,Ax=b。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/*
创建平行矩阵,仅指定其全局维度。
使用MatCreate()时,可以在以下位置指定矩阵格式:
此外,矩阵的并行分区是
由PETSc在运行时确定。
性能调整注意事项:对于规模较大的问题,
矩阵内存的预分配对于获得良好的性能至关重要
性能。有关详细信息,请参阅用户手册的矩阵章节。
*/
ierr=MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
ierr=MatsetSize(A,PETSC_decise,PETSC_decise,m*n,m*n);CHKERRQ(ierr);
ierr=MatSetFromOptions(A);CHKERRQ(ierr);
ierr=matmpaijsetpreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr);
ierr=MatSeqAIJSetPreallocation(A,5,PETSC_NULL);CHKERRQ(ierr);
/*
目前,所有PETSc并行矩阵格式都是按
跨处理器的连续行块。确定
矩阵的行是本地拥有的。
*/
ierr=材料所有权范围(A,&Istart,&Iend);CHKERRQ(ierr);
/*
平行设置二维五点模具的矩阵元素。
-每个处理器只需要插入它拥有的元素
本地(但任何非本地元素都将发送到
矩阵组装期间的适当处理器)。
-始终指定矩阵项的全局行和列。
注意:这使用了不太常见的先订购的自然顺序
x=h的所有未知数,然后是x=2h的未知数,等等;因此你可以看到J=Ii+-n
而不是你所期望的J=I+-m。更标准的顺序
首先对y=h执行所有变量,然后是y=2h等。
*/
ierr=PetscLogStageRegister(“装配”和“阶段”);CHKERRQ(ierr);
ierr=PetscLogStagePush(stage);CHKERRQ(ierr);
对于(Ii=Istart;Ii0){J=Ii-n;ierr=MatSetValues(A,1,&Ii,1,&J,&v,插入_值);CHKERRQ(ierr);}
如果(i0){J=Ii-1;ierr=MatSetValues(A,1,&Ii,1,&J,&v,插入_值);CHKERRQ(ierr);}
如果(j是的,当你开始学习时,这可能会有点让人望而生畏。2006年的教程中有一个很好的过程介绍;PetSC网页上的内容通常都很好
其中的关键部分是:
ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
实际创建PetSC矩阵对象,Mat A
ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);CHKERRQ(ierr);
设置大小;在这里,矩阵是m*n x m*n
,因为它是在m x n
二维网格上操作的模板
ierr = MatSetFromOptions(A);CHKERRQ(ierr);
如果您想控制A的设置方式,这只需要获取您在运行时可能提供的任何PetSC命令行选项,并将它们应用到矩阵中;否则,如果它将是一个密集矩阵,您可以使用例如将其创建为AIJ格式矩阵(默认值)
ierr = MatMPIAIJSetPreallocation(A,5,PETSC_NULL,5,PETSC_NULL);CHKERRQ(ierr);
ierr = MatSeqAIJSetPreallocation(A,5,PETSC_NULL);CHKERRQ(ierr);
现在我们已经得到了一个AIJ矩阵,这些调用只是预先分配稀疏矩阵,假设每行5个非零。这是为了性能。请注意,必须调用MPI和Seq函数,以确保这在1个处理器和多个处理器上都能正常工作;这看起来总是很奇怪,但就是这样
好了,现在矩阵都设置好了,我们从这里开始讨论问题的实质
首先,我们找出这个特定进程拥有哪些行。该分布是按行分布的,这对于典型的稀疏矩阵是一个很好的分布
ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
因此,在这次调用之后,每个处理器都有自己的Istart和Iend版本,并且它的this processors任务更新从Istart end开始的行,在Iend之前结束,正如您在这个for循环中看到的:
for (Ii=Istart; Ii<Iend; Ii++) {
v = -1.0; i = Ii/n; j = Ii - i*n;
我取出的if语句只是避免设置那些不存在的值,而CHKERRQ
宏只是在ierr!=0时打印出一个有用的错误
J = Ii - n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
J = Ii + n; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
J = Ii - 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
J = Ii + 1; ierr = MatSetValues(A,1,&Ii,1,&J,&v,INSERT_VALUES);CHKERRQ(ierr);
v = 4.0; ierr = MatSetValues(A,1,&Ii,1,&Ii,&v,INSERT_VALUES);CHKERRQ(ierr);
}
ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);