Skip to content

Commit 280e16f

Browse files
authored
Add the USE_KML option and change abstol/orfac for KML (deepmodeling#7370)
1 parent 0e094f6 commit 280e16f

6 files changed

Lines changed: 87 additions & 8 deletions

File tree

CMakeLists.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ option(USE_CUDA_MPI "Enable CUDA-aware MPI" OFF)
1717
option(USE_CUDA_ON_DCU "Enable CUDA on DCU" OFF)
1818
option(USE_ROCM "Enable ROCm" OFF)
1919
option(USE_DSP "Enable DSP" OFF)
20+
option(USE_KML "Enable Kunpeng Math Library" OFF)
2021
option(USE_SW "Enable SW Architecture" OFF)
2122

2223
option(USE_ABACUS_LIBM "Build libmath from source to speed up" OFF)
@@ -337,6 +338,50 @@ if (USE_DSP)
337338
target_link_libraries(${ABACUS_BIN_NAME} ${MT_HOST_DIR}/hthreads/lib/libhthread_device.a)
338339
target_link_libraries(${ABACUS_BIN_NAME} ${MT_HOST_DIR}/hthreads/lib/libhthread_host.a)
339340
endif()
341+
342+
343+
if(USE_KML)
344+
add_compile_definitions(__KML)
345+
message(STATUS "Huawei KML support enabled. Defining __KML.")
346+
# TODO: Create FindKML.cmake
347+
# if(NOT DEFINED KML_ROOT)
348+
# if(DEFINED ENV{KML_ROOT})
349+
# set(KML_ROOT $ENV{KML_ROOT})
350+
# else()
351+
# message(WARNING "KML_ROOT is not set. Trying default system paths for KML.")
352+
# endif()
353+
# endif()
354+
#
355+
# find_library(KML_BLAS_LIB NAMES kblas PATHS ${KML_ROOT}/lib ${KML_ROOT}/lib64 NO_DEFAULT_PATH)
356+
# find_library(KML_LAPACK_LIB NAMES klapack_full PATHS ${KML_ROOT}/lib ${KML_ROOT}/lib64 NO_DEFAULT_PATH)
357+
# find_library(KML_SCALAPACK_LIB NAMES kscalapack_full PATHS ${KML_ROOT}/lib ${KML_ROOT}/lib64 NO_DEFAULT_PATH)
358+
# find_library(KML_FFTW_LIB NAMES fftw3 PATHS ${KML_ROOT}/lib ${KML_ROOT}/lib64 NO_DEFAULT_PATH)
359+
#
360+
# set(KML_LIBS_FOUND TRUE)
361+
# foreach(LIB_VAR KML_BLAS_LIB KML_LAPACK_LIB KML_SCALAPACK_LIB KML_FFTW_LIB)
362+
# if(NOT ${LIB_VAR})
363+
# message(WARNING "${LIB_VAR} not found in KML_ROOT! Please check your KML installation.")
364+
# set(KML_LIBS_FOUND FALSE)
365+
# endif()
366+
# endforeach()
367+
#
368+
# if(KML_LIBS_FOUND)
369+
# target_link_libraries(abacus PUBLIC
370+
# ${KML_BLAS_LIB}
371+
# ${KML_LAPACK_LIB}
372+
# ${KML_SCALAPACK_LIB}
373+
# ${KML_FFTW_LIB}
374+
# )
375+
# message(STATUS "Huawei KML libraries found and linked successfully.")
376+
# else()
377+
# message(FATAL_ERROR "Failed to find all required KML libraries. Aborting.")
378+
# endif()
379+
#
380+
# set(BLAS_libraries ${KML_BLAS_LIB})
381+
# set(LAPACK_libraries ${KML_LAPACK_LIB})
382+
endif(USE_KML)
383+
384+
340385
if (USE_SW)
341386
add_compile_definitions(__SW)
342387
set(SW ON)

source/source_base/module_external/lapack_connector.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@
3030
#include "../complexmatrix.h"
3131
#include "../global_function.h"
3232

33+
#include <limits>
34+
// =========================================================
35+
// Tolerances for LAPACK/ScaLAPACK eigenvalue routines
36+
// =========================================================
37+
// Huawei KML strictly validates input parameters.
38+
// It rejects abstol=0 and orfac=-1, which are standard
39+
// defaults in open-source LAPACK to trigger internal logic.
40+
// We must explicitly pass the mathematically equivalent defaults.
41+
#ifdef __KML
42+
constexpr double LAPACK_ABSTOL = 2*std::numeric_limits<double>::min(); // 2*PDLAMCH('S')
43+
constexpr double LAPACK_ORFAC = 2.0e-12; // Default value
44+
#else
45+
constexpr double LAPACK_ABSTOL = 0.0;
46+
constexpr double LAPACK_ORFAC = -1.0;
47+
#endif
48+
// =========================================================
49+
3350
//Naming convention of lapack subroutines : ammxxx, where
3451
//"a" specifies the data type:
3552
// - s stands for float

source/source_base/module_external/scalapack_connector.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
#ifndef SCALAPACK_CONNECTOR_H
22
#define SCALAPACK_CONNECTOR_H
33

4+
#include <limits>
5+
// =========================================================
6+
// Tolerances for LAPACK/ScaLAPACK eigenvalue routines
7+
// =========================================================
8+
// Huawei KML strictly validates input parameters.
9+
// It rejects abstol=0 and orfac=-1, which are standard
10+
// defaults in open-source ScaLAPACK to trigger internal logic.
11+
// We must explicitly pass the mathematically equivalent defaults.
12+
#ifdef __KML
13+
constexpr double SCALAPACK_ABSTOL = 2*std::numeric_limits<double>::min(); // 2*PDLAMCH('S')
14+
constexpr double SCALAPACK_ORFAC = 2.0e-12; // Default value
15+
#else
16+
constexpr double SCALAPACK_ABSTOL = 0.0;
17+
constexpr double SCALAPACK_ORFAC = -1.0;
18+
#endif
19+
// =========================================================
20+
421
#ifdef __MPI
522

623
#include <complex>

source/source_hsolver/diago_lapack.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ std::pair<int, std::vector<int>> DiagoLapack<T>::dsygvx_once(const int ncol,
9898
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
9999
int M = 0, NZ = 0, lwork = -1, liwork = -1, info = 0;
100100
double vl = 0, vu = 0;
101-
const double abstol = 0, orfac = -1;
101+
const double abstol = LAPACK_ABSTOL, orfac = LAPACK_ORFAC;
102102
std::vector<double> work(3, 0);
103103
std::vector<int> iwork(1, 0);
104104
std::vector<int> ifail(PARAM.globalv.nlocal, 0);
@@ -206,7 +206,7 @@ std::pair<int, std::vector<int>> DiagoLapack<T>::zhegvx_once(const int ncol,
206206
const char jobz = 'V', range = 'I', uplo = 'U';
207207
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
208208
int M = 0, NZ = 0, lwork = -1, lrwork = -1, liwork = -1, info = 0;
209-
const double abstol = 0, orfac = -1;
209+
const double abstol = LAPACK_ABSTOL, orfac = LAPACK_ORFAC;
210210

211211
const double vl = 0, vu = 0;
212212
std::vector<std::complex<double>> work(1, 0);

source/source_hsolver/diago_pxxxgvx.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ void pxxxgvx_diag(const int* const desc,
502502
int lrwork = -1;
503503
int liwork = -1;
504504
int info = 0;
505-
const typename GetTypeReal<T>::type abstol = 0;
506-
const typename GetTypeReal<T>::type orfac = -1;
505+
const typename GetTypeReal<T>::type abstol = SCALAPACK_ABSTOL;
506+
const typename GetTypeReal<T>::type orfac = SCALAPACK_ORFAC;
507507
const typename GetTypeReal<T>::type vl = 0;
508508
const typename GetTypeReal<T>::type vu = 0;
509509
std::vector<T> work(1, 0);
@@ -667,4 +667,4 @@ template void pxxxgvx_diag(const int* const desc,
667667

668668
#endif
669669

670-
} // namespace hsolver
670+
} // namespace hsolver

source/source_hsolver/diago_scalapack.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ namespace hsolver
9595
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
9696
int M = 0, NZ = 0, lwork = -1, liwork = -1, info = 0;
9797
double vl = 0, vu = 0;
98-
const double abstol = 0, orfac = -1;
98+
const double abstol = SCALAPACK_ABSTOL, orfac = SCALAPACK_ORFAC;
9999
std::vector<double> work(3, 0);
100100
std::vector<int> iwork(1, 0);
101101
std::vector<int> ifail(PARAM.globalv.nlocal, 0);
@@ -219,7 +219,7 @@ namespace hsolver
219219
const char jobz = 'V', range = 'I', uplo = 'U';
220220
const int itype = 1, il = 1, iu = PARAM.inp.nbands, one = 1;
221221
int M = 0, NZ = 0, lwork = -1, lrwork = -1, liwork = -1, info = 0;
222-
const double abstol = 0, orfac = -1;
222+
const double abstol = SCALAPACK_ABSTOL, orfac = SCALAPACK_ORFAC;
223223
//Note: pzhegvx_ has a bug
224224
// We must give vl,vu a value, although we do not use range 'V'
225225
// We must give rwork at least a memory of sizeof(double) * 3
@@ -445,4 +445,4 @@ namespace hsolver
445445
}
446446
}
447447

448-
} // namespace hsolver
448+
} // namespace hsolver

0 commit comments

Comments
 (0)