#include "femlite_c_api.h" #include #include static int check_ierr(int ierr, int code) { int err_code = 0; char message[256]; if (ierr == 0) { return 0; } (void)femlite_get_last_error(&err_code, message, (int)sizeof(message)); fprintf(stderr, "test_external_diag_c_api failed at %d: ierr=%d code=%d %s\n", code, ierr, err_code, message); return code; } static int check_close(double actual, double expected, int code) { double scale = fmax(1.0, fabs(expected)); if (fabs(actual - expected) > 1.0e-12 * scale) { fprintf(stderr, "test_external_diag_c_api mismatch at %d: %.17g vs %.17g\n", code, actual, expected); return code; } return 0; } static int read_diag(int *valid, int *nrow, int *ncol, int *nnz, int *class_id, int *sym_pat, int *sym_val, int *status, double *matrix_norm, double *rhs_norm, double *initial_resid, double *initial_rel) { return femlite_get_linear_solver_external_diag( valid, nrow, ncol, nnz, class_id, sym_pat, sym_val, status, matrix_norm, rhs_norm, initial_resid, initial_rel); } static int check_default_diag(void) { int ierr = 0; int valid = -1; int nrow = -1; int ncol = -1; int nnz = -1; int class_id = -1; int sym_pat = -1; int sym_val = -1; int status = -99; double matrix_norm = -1.0; double rhs_norm = -1.0; double initial_resid = -1.0; double initial_rel = -1.0; int failed = 0; ierr = read_diag(&valid, &nrow, &ncol, &nnz, &class_id, &sym_pat, &sym_val, &status, &matrix_norm, &rhs_norm, &initial_resid, &initial_rel); if (ierr != 0) return check_ierr(ierr, 10); if (valid != 0) return 11; if (nrow != 0 || ncol != 0 || nnz != 0) return 12; if (class_id != FEMLITE_CSR_CLASS_GENERAL) return 13; if (sym_pat != 0 || sym_val != 0) return 14; if (status != 0) return 15; failed = check_close(matrix_norm, 0.0, 16); if (failed != 0) return failed; failed = check_close(rhs_norm, 0.0, 17); if (failed != 0) return failed; failed = check_close(initial_resid, 0.0, 18); if (failed != 0) return failed; return check_close(initial_rel, 0.0, 19); } static int check_backend_config(void) { int ierr = 0; int backend = -1; int available = -1; int capabilities = -1; int prepared = -1; int command_ran = -1; int command_exit = -1; int finish_status = -1; double residual = -1.0; double relative = -1.0; double backward = -1.0; ierr = femlite_set_linear_solver_external_backend( FEMLITE_LS_EXT_BACKEND_SUPERLU); if (ierr != 0) return check_ierr(ierr, 30); ierr = femlite_get_linear_solver_external_info(&backend, &available, &capabilities); if (ierr != 0) return check_ierr(ierr, 31); if (backend != FEMLITE_LS_EXT_BACKEND_SUPERLU) return 32; if (available != 0 || capabilities != 0) return 33; ierr = femlite_configure_linear_solver_external_file( "capi_ext_file", "", 0, 1, 0, 1); if (ierr != 0) return check_ierr(ierr, 35); ierr = femlite_set_linear_solver_external_backend( FEMLITE_LS_EXT_BACKEND_FILE); if (ierr != 0) return check_ierr(ierr, 36); ierr = femlite_get_linear_solver_external_info(&backend, &available, &capabilities); if (ierr != 0) return check_ierr(ierr, 37); if (backend != FEMLITE_LS_EXT_BACKEND_FILE) return 38; if (available != 1) return 39; if ((capabilities & FEMLITE_LS_EXT_CAP_DIRECT) == 0 || (capabilities & FEMLITE_LS_EXT_CAP_GENERAL) == 0) { return 40; } ierr = femlite_get_linear_solver_external_file_diag( &prepared, &command_ran, &command_exit, &finish_status, &residual, &relative, &backward); if (ierr != 0) return check_ierr(ierr, 41); if (prepared != 0 || command_ran != 0 || command_exit != 0) return 42; ierr = femlite_set_linear_solver_external_backend( FEMLITE_LS_EXT_BACKEND_NONE); if (ierr != 0) return check_ierr(ierr, 34); return 0; } static int check_null_guards(void) { int valid = 0; int nrow = 0; int ncol = 0; int nnz = 0; int class_id = 0; int sym_pat = 0; int sym_val = 0; int status = 0; double matrix_norm = 0.0; double rhs_norm = 0.0; double initial_resid = 0.0; double initial_rel = 0.0; char path[128]; int ierr = 0; ierr = read_diag(NULL, &nrow, &ncol, &nnz, &class_id, &sym_pat, &sym_val, &status, &matrix_norm, &rhs_norm, &initial_resid, &initial_rel); if (ierr != -1) return 50; ierr = read_diag(&valid, &nrow, &ncol, &nnz, &class_id, &sym_pat, &sym_val, &status, NULL, &rhs_norm, &initial_resid, &initial_rel); if (ierr != -1) return 51; if (femlite_configure_linear_solver_external_file( NULL, "", 0, 1, 0, 1) != -1) return 52; if (femlite_get_linear_solver_external_file_diag( NULL, &status, &status, &status, &matrix_norm, &rhs_norm, &initial_resid) != -1) return 53; if (femlite_get_linear_solver_external_file_paths( NULL, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path)) != -1) { return 54; } if (femlite_get_linear_solver_external_file_paths( path, 0, path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path), path, (int)sizeof(path)) != -1) { return 55; } return 0; } int main(void) { int ierr = 0; int failed = 0; ierr = femlite_initialize(); if (ierr != 0) return check_ierr(ierr, 1); failed = check_default_diag(); if (failed != 0) return failed; failed = check_backend_config(); if (failed != 0) return failed; failed = check_null_guards(); if (failed != 0) return failed; ierr = femlite_finalize(); if (ierr != 0) return check_ierr(ierr, 2); return 0; }