我要评分
获取效率
正确性
完整性
易理解

Migration Procedure

Migrating the Library That Uses the C Language

Both PETSc and KML_SCASOLVER provide C interfaces. However, the interface names and receiving parameters are inconsistent. Therefore, the corresponding function needs to be switched. The following are examples:

  1. Initialization
    Before the migration:
    KSP            ksp;
    PetscErrorCode ierr;
    ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);
    ierr = KSPSetOperators(ksp,A,A);
    After the migration:
    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); 
  2. Setting parameters for finding the solution
    Before the migration:
    ierr = KSPSetTolerances(ksp,1e-4,PETSC_DEFAULT,PETSC_DEFAULT,2000);
    After the migration:
    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);
  3. Finding the solution
    Before the migration:
    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);
    After the migration:
    int nb=1;
    double* x[8];
    double* b[8];
    int ldx=4;
    int ldb=4;
    err = KmlScaissCgSolveDI(&handle, nb, x, ldx, b, ldb);
  4. Obtaining iteration parameters
    Before the migration:
    PetscInt       its;
    PetscScalar      norm;
    ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
    ierr = KSPGetResidualNorm(ksp,&norm);CHKERRQ(ierr);
    After the migration:
    int idata[] = {max_iters};
    err = KmlScaissCgGetDII(&handle, KMLSS_ITERATION_COUNT, data, 1); 
    double ddata[] = {eps};
    err = KmlScaissCgGetDID(&handle, KMLSS_TOLERANCE, ddata, 1);
  5. Clearing
    Before the migration:
    ierr = KSPDestroy(&ksp);
    After the migration:
    err = KmlScaissCgCleanDI(&handle);
  6. Header file

    Before the migration:

    #include "petscksp.h"

    After the migration:

    #include "kml_scaiss.h"

  7. Compiling the links

    Replace the link options related to PETSc. For details, see "Installing KML > Verifying the Installation" in Kunpeng Math Library Developer Guide.