Rate This Document
Findability
Accuracy
Completeness
Readability

Cluster Mode

Replaceability

KML_SCASOLVER supports multiple processes and threads on multiple nodes, which can replace the multi-node and multi-process function of PETSc.

Table 1 describes the mapping between replaceable interfaces and corresponding interfaces of PETSc (only optimized interfaces are provided).

Table 1 Replacement mapping of PETSc

PETSc Interface

KML_SCASOLVER Interface

KSPCreate

KmlScaissCgInitStripesDI

KSPSetTolerances

KmlScaissCgSetDII

KSPSolve

KmlScaissCgSolveDI

KSPGetIterationNumber/ KSPGetResidualNorm

KmlScaissCgGetDII/ KmlScaissCgGetDID

KSPDestroy

KmlScaissCgCleanDI

The preceding interfaces are C interfaces. For details, see "Kunpeng Math Library Developer Guide" > "Applied Math Libraries" > "KML_SOLVER Library Functions" > "Function Syntax (Cluster Edition)" in Kunpeng HPCKit 26.1.RC1 Developer Guide.

Migrating the C-based Library

Method 1: Direct migration

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:
    1
    2
    3
    4
    KSP            ksp;
    PetscErrorCode ierr;
    ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);
    ierr = KSPSetOperators(ksp,A,A);
    
    After the migration:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    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:
    1
    ierr = KSPSetTolerances(ksp,1e-4,PETSC_DEFAULT,PETSC_DEFAULT,2000);
    
    After the migration:
    1
    2
    3
    4
    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:
    1
    2
    3
    4
    5
    6
    7
    8
    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:
    1
    2
    3
    4
    5
    6
    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:
    1
    2
    3
    4
    PetscInt       its;
    PetscScalar      norm;
    ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr);
    ierr = KSPGetResidualNorm(ksp,&norm);CHKERRQ(ierr);
    
    After the migration:
    1
    2
    3
    4
    int idata[] = {0};
    err = KmlScaissCgGetDII(&handle, KMLSS_ITERATION_COUNT, idata, 1); 
    double ddata[] = {0};
    err = KmlScaissCgGetDID(&handle, KMLSS_TOLERANCE, ddata, 1);
    
  5. Clearing
    Before the migration:
    1
    ierr = KSPDestroy(&ksp);
    
    After the migration:
    1
    err = KmlScaissCgCleanDI(&handle);
    
  6. Header files

    Before the migration:

    #include "petscksp.h"

    After the migration:

    #include "kml_scaiss.h"

  7. Compiling the links

    Replace the PETSc-related link options. For details, see "Installing KML" in Kunpeng HPCKit 26.1.RC1 Installation Guide.

Method 2: Migration through the adaptation layer

  1. Change #include "petscksp.h" to #include "petscksp_kp.h". For the MatCreateMPIAIJWithArrays and VecCreateMPIWithArray interfaces, add the ilower parameter (global subscript of the first row of a matrix on the current process) to the second parameter position.
  2. Load the HPCKit environment variables, and set the header file path of PETSc and the dynamic library path of the KML adaptation layer. After the setting is successful, recompile the application. For details about how to load the environment variables, see "Setting Environment Variables" in Kunpeng HPCKit 26.1.RC1 Installation Guide.
    mpicc program.c -lpetsc_kp -lkscasolver -lkservice -lklapack -lkblas -lkm -lm

* Real-world application scenarios often require flexible invocation. For more information, refer to the internal implementation of the adaptation layer in petsc_kunpeng.c.

Table 2 Adapted interfaces

PETSc Interface

PetscInitialize

PetscFinalize

MatCreateMPIAIJWithArrays

VecCreateMPIWithArray

KSPCreate

KSPSetOperators

KSPSetType

KSPSetTolerances

KSPSetUp

KSPSolve

KSPGetIterationNumber

KSPGetResidualNorm

KSPGetPC

PCSetType

KSPDestroy

VecDestroy

MatDestroy

VecGetArray