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).
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:
- InitializationBefore 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);
- Setting parameters for finding the solution
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);
- Finding the solutionBefore 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);
- Obtaining iteration parametersBefore 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);
- Clearing
After the migration:
1err = KmlScaissCgCleanDI(&handle);
- Header files
#include "petscksp.h"
After the migration:
#include "kml_scaiss.h"
- 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
- 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.
- 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.
PETSc Interface |
|---|
PetscInitialize |
PetscFinalize |
MatCreateMPIAIJWithArrays |
VecCreateMPIWithArray |
KSPCreate |
KSPSetOperators |
KSPSetType |
KSPSetTolerances |
KSPSetUp |
KSPSolve |
KSPGetIterationNumber |
KSPGetResidualNorm |
KSPGetPC |
PCSetType |
KSPDestroy |
VecDestroy |
MatDestroy |
VecGetArray |