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

Migration Procedure

Migrating the Library That Uses the C Language

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

  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 ia[9];    
    int ja[17];
    double a[17];
    void* handle;
    int error = KmlIssCgInitDI(&handle, &n, a, ja, ia);
  2. 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:
    double b[8];    
    /* Solution vector */    
    double x[8];
    int nrhs = 1; /* Number of right-hand sides */       
    int ldx=n, ldb=n; /*!Leading dimension of B and X */
    int error = KmlIssCgSolveDI(&handle, &nrhs, x, &ldx, b, &ldb);
  3. Obtaining iteration parameters
    Before the migration:
    PetscInt       its;
    ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
    After the migration:
    int *data;  // user data.
    int nd = 1;
    int error = KmlIssCgGetDII(&handle, ITERATION_COUNT, data, nd);
  4. Clearing
    Before the migration:
    ierr = KSPDestroy(&ksp);
    After the migration:
    int error = KmlIssCgCleanDI(&handle);
  5. Header file

    Before the migration:

    #include "petscksp.h"

    After the migration:

    #include "kml_iss.h"

  6. Compiling the links

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