迁移步骤
C语言迁移步骤
PETSc提供C接口,KML_SCASOLVER同样也提供了C接口,但接口名及接收参数不一致,需要进行对应功能的切换,举例如下:
- 初始化。
迁移前:
KSP ksp; PetscErrorCode ierr; ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); ierr = KSPSetOperators(ksp,A,A);
迁移后:int n = 8; int nstripes = 1; int stripeWidth[1]; int stripeRow[1]; int ia[9]; int ja[17]; double a[17]; KmlScasolverTask* handle; int err; err = KmlScaissCgInitStripesDI(&handle, n, nstripes , stripeWidth, stripeRow, a, ja, ia, MPI_COMM_WORLD);
- 设定求解参数
迁移后:
double eps = 1e-4; err = KmlScaissCgSetDID(&handle, KMLSS_THRESHOLD, &eps, 1); assert(err == 0); int max_iters = 2000; err = KmlScaissCgSetDII(&handle, KMLSS_MAX_ITERATION_COUNT, &max_iters, 1);
- 求解。
迁移前:
Vec u,x,b; ierr = VecCreate(PETSC_COMM_WORLD,&u); ierr = VecSetSizes(u,PETSC_DECIDE,rdim); ierr = VecSetFromOptions(u);CHKERRQ(ierr); ierr = VecDuplicate(u,&b);CHKERRQ(ierr); ierr = VecDuplicate(b,&x);CHKERRQ(ierr); ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr);
迁移后:int nb=1; double* x[8]; double* b[8]; int ldx=4; int ldb=4; err = KmlScaissCgSolveDI(&handle, nb, x, ldx, b, ldb);
- 获取迭代参数。
迁移前:
PetscInt its; PetscScalar norm; ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); ierr = KSPGetResidualNorm(ksp,&norm);CHKERRQ(ierr);
迁移后:int idata[] = {max_iters}; err = KmlScaissCgGetDII(&handle, KMLSS_ITERATION_COUNT, data, 1); double ddata[] = {eps}; err = KmlScaissCgGetDID(&handle, KMLSS_TOLERANCE, ddata, 1);
- 清除。
迁移后:
err = KmlScaissCgCleanDI(&handle);
- 头文件。
#include "petscksp.h"
迁移后:
#include "kml_scaiss.h"
- 编译链接。
父主题: 集群版