Commit 3874f572 authored by Kirill Terekhov's avatar Kirill Terekhov

update petsc interface

Updated petsc interface to petsc-3.5.2, fixed code returing converge
reason. Tuned cmake scripts for petsc to work on windows.
parent 7967b63c
......@@ -6,9 +6,28 @@
macro (CONVERT_CYGWIN_PATH _path)
if (WIN32)
EXECUTE_PROCESS(COMMAND cygpath.exe -m ${${_path}}
OUTPUT_VARIABLE ${_path})
string (STRIP ${${_path}} ${_path})
if( NOT DEFINED CYGPATH_EXECUTABLE OR NOT EXISTS ${CYGPATH_EXECUTABLE} )
find_program(CYGPATH_EXECUTABLE NAMES cygpath HINTS
"/usr/bin/"
"c:/cygwin/bin"
"c:/cygwin64/bin"
"${CYGWIN_INSTALL_PATH}/bin")
if(NOT EXISTS ${CYGPATH_EXECUTABLE})
message(SEND_ERROR "cannot find cygpath.exe, please set CYGWIN_INSTALL_PATH variable with path where you have installed cygwin or CYGPATH_EXECUTABLE with path to cygpath.exe")
endif()
endif()
# message(STATUS "convert input")
# message(${${_path}})
string (REGEX REPLACE "\\?space\\?" " " ${_path} ${${_path}})
# message(STATUS "replace spaces")
# message(${${_path}})
EXECUTE_PROCESS(COMMAND ${CYGPATH_EXECUTABLE} -m "${${_path}}"
OUTPUT_VARIABLE ${_path})
string (STRIP ${${_path}} ${_path})
string(REGEX REPLACE "\\\\" "/" ${_path} ${${_path}})
# message(STATUS "convert output")
# message(${${_path}})
endif (WIN32)
endmacro (CONVERT_CYGWIN_PATH)
......@@ -80,6 +80,17 @@ find_path (PETSC_DIR include/petsc.h
find_program (MAKE_EXECUTABLE NAMES make gmake)
#oops, make not found
if( NOT EXISTS MAKE_EXECUTABLE )
if( WIN32 )
find_program (MAKE_EXECUTABLE NAMES make gmake HINTS
"/usr/bin"
"c:/cygwin/bin"
"c:/cygwin64/bin"
"${CYGWIN_INSTALL_PATH}/bin")
endif()
endif()
if (PETSC_DIR AND NOT PETSC_ARCH)
set (_petsc_arches
$ENV{PETSC_ARCH} # If set, use environment variable first
......@@ -120,7 +131,8 @@ elseif (PETSC_DIR)
endif ()
if (petsc_conf_rules AND petsc_conf_variables )
# AND NOT petsc_config_current)
#RESTORE
#AND NOT petsc_config_current)
petsc_get_version()
# Put variables into environment since they are needed to get
......@@ -137,14 +149,22 @@ if (petsc_conf_rules AND petsc_conf_variables )
include ${petsc_conf_rules}
include ${petsc_conf_variables}
show :
-@echo -n \${\${VARIABLE}}
\$(info \${\${VARIABLE}})
")
macro (PETSC_GET_VARIABLE name var)
set (${var} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
execute_process (COMMAND ${MAKE_EXECUTABLE} --no-print-directory -f ${petsc_config_makefile} show VARIABLE=${name}
# message(STATUS "execute command with ${name} ${var}")
# message("${MAKE_EXECUTABLE} -s --no-print-directory -f ${petsc_config_makefile} show VARIABLE=${name}")
execute_process (COMMAND ${MAKE_EXECUTABLE} -s --no-print-directory -f ${petsc_config_makefile} show VARIABLE=${name}
OUTPUT_VARIABLE ${var}
RESULT_VARIABLE petsc_return)
# message(STATUS "returned")
# protect backslashed spaces from disappearing
string(REGEX REPLACE "\\\\ " "?space?" ${var} ${${var}})
# foreach(D ${${var}})
# message("${D}")
# endforeach()
endmacro (PETSC_GET_VARIABLE)
petsc_get_variable (PETSC_LIB_DIR petsc_lib_dir)
petsc_get_variable (PETSC_EXTERNAL_LIB_BASIC petsc_libs_external)
......@@ -158,7 +178,7 @@ show :
include (ResolveCompilerPaths)
# Extract include paths and libraries from compile command line
resolve_includes (petsc_includes_all "${petsc_cpp_line}")
resolve_includes (petsc_includes_all ${petsc_cpp_line})
#on windows we need to make sure we're linking against the right
#runtime library
......@@ -183,7 +203,7 @@ show :
include (CorrectWindowsPaths)
convert_cygwin_path(petsc_lib_dir)
message (STATUS "petsc_lib_dir ${petsc_lib_dir}")
#message (STATUS "petsc_lib_dir ${petsc_lib_dir}")
macro (PETSC_FIND_LIBRARY suffix name)
set (PETSC_LIBRARY_${suffix} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE) # Clear any stale value, if we got here, we need to find it again
......@@ -270,6 +290,11 @@ int main(int argc,char *argv[]) {
mark_as_advanced (PETSC_INCLUDE_DIR PETSC_INCLUDE_CONF)
set (petsc_includes_minimal ${PETSC_INCLUDE_CONF} ${PETSC_INCLUDE_DIR})
#message(STATUS "petsc_includes_minimal")
#foreach(D ${petsc_includes_minimal})
#message(${D})
#endforeach()
petsc_test_runs ("${petsc_includes_minimal}" "${PETSC_LIBRARIES_TS}" petsc_works_minimal)
if (petsc_works_minimal)
message (STATUS "Minimal PETSc includes and libraries work. This probably means we are building with shared libs.")
......@@ -322,3 +347,5 @@ include (FindPackageHandleStandardArgs)
find_package_handle_standard_args (PETSc
"PETSc could not be found. Be sure to set PETSC_DIR and PETSC_ARCH."
PETSC_INCLUDES PETSC_LIBRARIES PETSC_EXECUTABLE_RUNS)
......@@ -25,7 +25,7 @@
# Always runs the given test, use this when you need to re-run tests
# because parent variables have made old cache entries stale. The LANGUAGE
# variable is either C or CXX indicating which compiler the test should
# use.
# use.
# MULTIPASS_C_SOURCE_RUNS (Name INCLUDES LIBRARIES SOURCE RUNS)
# DEPRECATED! This is only included for backwards compatability. Use
# the more general MULTIPASS_SOURCE_RUNS instead.
......@@ -46,7 +46,7 @@ macro (FIND_PACKAGE_MULTIPASS _name _current)
# The name of the stored value for the given state
set (_stored_var PACKAGE_MULTIPASS_${_NAME}_${_state})
if (NOT "${${_stored_var}}" STREQUAL "${${_NAME}_${_state}}")
set (_states_current "NO")
set (_states_current "NO")
endif (NOT "${${_stored_var}}" STREQUAL "${${_NAME}_${_state}}")
set (${_stored_var} "${${_NAME}_${_state}}" CACHE INTERNAL "Stored state for ${_name}." FORCE)
list (REMOVE_AT _args 0)
......@@ -68,7 +68,7 @@ macro (FIND_PACKAGE_MULTIPASS _name _current)
if (_cmd STREQUAL "DEPENDENTS")
list (REMOVE_AT _args 0)
foreach (dep ${_args})
set (${_NAME}_${dep} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
set (${_NAME}_${dep} "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
endforeach (dep)
endif (_cmd STREQUAL "DEPENDENTS")
set (${_NAME}_FOUND "NOTFOUND" CACHE INTERNAL "Cleared" FORCE)
......@@ -91,6 +91,8 @@ macro (MULTIPASS_SOURCE_RUNS includes libraries source runs language)
math (EXPR _tmp "${MULTIPASS_TEST_COUNT} + 1") # Why can't I add to a cache variable?
set (MULTIPASS_TEST_COUNT ${_tmp} CACHE INTERNAL "Unique test ID")
set (testname MULTIPASS_TEST_${MULTIPASS_TEST_COUNT}_${runs})
message(STATUS "includes ${includes}")
message(STATUS "libraries ${libraries}")
set (CMAKE_REQUIRED_INCLUDES ${includes})
set (CMAKE_REQUIRED_LIBRARIES ${libraries})
if(${language} STREQUAL "C")
......@@ -104,3 +106,5 @@ endmacro (MULTIPASS_SOURCE_RUNS)
macro (MULTIPASS_C_SOURCE_RUNS includes libraries source runs)
multipass_source_runs("${includes}" "${libraries}" "${source}" ${runs} "C")
endmacro (MULTIPASS_C_SOURCE_RUNS)
......@@ -49,6 +49,7 @@ macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
# If it's a library path, add it to the list
string (REGEX REPLACE "^-L" "" token ${token})
string (REGEX REPLACE "//" "/" token ${token})
# string (REGEX REPLACE "\\?space\\?" " " token ${token})
convert_cygwin_path(token)
list (APPEND _directory_list ${token})
elseif (token MATCHES "^(-l([^\" ]+|\"[^\"]+\")|[^\" ]+\\.(a|so|dll|lib))")
......@@ -70,10 +71,11 @@ macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
set (_lib "NOTFOUND" CACHE FILEPATH "Cleared" FORCE)
find_library (_lib ${token} HINTS ${_directory_list} ${_root})
if (_lib)
string (REPLACE "//" "/" _lib ${_lib})
string (REPLACE "//" "/" _lib ${_lib})
list (APPEND _libs_found ${_lib})
else (_lib)
message (STATUS "Unable to find library ${token}")
# list (APPEND _libs_found ${token})
endif (_lib)
endif (token MATCHES "-L([^\" ]+|\"[^\"]+\")")
endforeach (token)
......@@ -88,11 +90,16 @@ macro (RESOLVE_LIBRARIES LIBS LINK_LINE)
endmacro (RESOLVE_LIBRARIES)
macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
string (REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
string(REGEX MATCHALL "-I([^\" ]+|\"[^\"]+\")" _all_tokens "${COMPILE_LINE}")
set (_incs_found)
foreach (token ${_all_tokens})
string (REGEX REPLACE "^-I" "" token ${token})
string (REGEX REPLACE "//" "/" token ${token})
# message("include before")
# message("${token}")
# string (REGEX REPLACE "\\?space\\?" " " token ${token})
# message("include after")
# message("${token}")
convert_cygwin_path(token)
if (EXISTS ${token})
list (APPEND _incs_found ${token})
......@@ -103,3 +110,5 @@ macro (RESOLVE_INCLUDES INCS COMPILE_LINE)
list (REMOVE_DUPLICATES _incs_found)
set (${INCS} "${_incs_found}")
endmacro (RESOLVE_INCLUDES)
......@@ -2059,7 +2059,7 @@ namespace INMOST
sol->RealParameter("divtol") = divergance_tolerance;
sol->RealParameter(":tau") = preconditioner_drop_tolerance;
sol->RealParameter(":tau2") = preconditioner_reuse_tolerance;
sol->EnumParameter(":fill") = preconditioner_fill_level;
sol->EnumParameter(":fill") = static_cast<INMOST_DATA_ENUM_TYPE>(preconditioner_fill_level);
sol->EnumParameter(":scale_iters") = preconditioner_rescale_iterations;
if (!sol->isInitialized())
......
#define _CRT_SECURE_NO_WARNINGS
#include "inmost_solver.h"
#if defined(USE_SOLVER)
#if defined(USE_SOLVER_PETSC)
......@@ -11,7 +12,7 @@ bool SolverIsInitializedPetsc()
PetscBool isInitialized;
PetscErrorCode ierr = PetscInitialized(&isInitialized);
if( ierr != PETSC_SUCCESS ) throw INMOST::ErrorInSolver;
return isInitialized;
return (isInitialized == PETSC_TRUE);
}
void SolverInitializePetsc(int * argc,char *** argv, const char * file_options)
......@@ -19,6 +20,9 @@ void SolverInitializePetsc(int * argc,char *** argv, const char * file_options)
if( !SolverIsInitializedPetsc() )
{
PetscErrorCode ierr = PetscInitialize(argc,argv,file_options,"solver");
//prevent petsc from catching signals, petsc error handling is misleading for
//unexperienced user and results in the opinion that errors emerge from petsc
PetscPopSignalHandler();
if( ierr != PETSC_SUCCESS ) throw INMOST::ErrorInSolver;
}
}
......@@ -28,7 +32,7 @@ bool SolverIsFinalizedPetsc()
PetscBool isFinalized;
PetscErrorCode ierr = PetscFinalized(&isFinalized);
if( ierr != PETSC_SUCCESS ) throw INMOST::ErrorInSolver;
return isFinalized;
return (isFinalized == PETSC_TRUE);
}
void SolverFinalizePetsc()
......@@ -268,7 +272,12 @@ void SolverSetMatrixPetsc(void * data, void * matrix_data, bool same_pattern, bo
{
KSP * m = static_cast<KSP *>(data);
Mat * mat = static_cast<Mat *>(matrix_data);
PetscErrorCode ierr = KSPSetOperators(*m,*mat,*mat,reuse_preconditioner? SAME_PRECONDITIONER : (same_pattern? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN));
PetscErrorCode ierr;
if( reuse_preconditioner )
ierr = KSPSetReusePreconditioner(*m,PETSC_TRUE);
else
ierr = KSPSetReusePreconditioner(*m,PETSC_FALSE);
ierr = KSPSetOperators(*m,*mat,*mat);//,reuse_preconditioner? SAME_PRECONDITIONER : (same_pattern? SAME_NONZERO_PATTERN : DIFFERENT_NONZERO_PATTERN));
if( ierr != PETSC_SUCCESS ) throw INMOST::ErrorInSolver;
}
......@@ -319,7 +328,7 @@ double SolverResidualNormPetsc(void * data)
const char * SolverConvergedReasonPetsc(void * data)
{
static char reason[4096];
static char reason_str[4096];
KSPConvergedReason reason;
KSP * ksp = static_cast<KSP *>(data);
PetscErrorCode ierr = KSPGetConvergedReason(*ksp,&reason);
......@@ -328,60 +337,60 @@ const char * SolverConvergedReasonPetsc(void * data)
{
case KSP_CONVERGED_RTOL:
case KSP_CONVERGED_RTOL_NORMAL:
strcpy(reason,"norm decreased by a factor of relative tolerance");
strcpy(reason_str,"norm decreased by a factor of relative tolerance");
break;
case KSP_CONVERGED_ATOL:
case KSP_CONVERGED_ATOL_NORMAL:
strcpy(reason,"norm less then absolute tolerance");
strcpy(reason_str,"norm less then absolute tolerance");
break;
case KSP_CONVERGED_ITS:
strcpy(reason,"converged by direct solver");
strcpy(reason_str,"converged by direct solver");
break;
case KSP_CONVERGED_CG_NEG_CURVE:
case KSP_CONVERGED_CG_CONSTRAINED:
strcpy(reason,"converged due to some condition in conjugate gradient method");
strcpy(reason_str,"converged due to some condition in conjugate gradient method");
break;
case KSP_CONVERGED_STEP_LENGTH:
strcpy(reason,"converged due to step length");
strcpy(reason_str,"converged due to step length");
break;
case KSP_CONVERGED_HAPPY_BREAKDOWN :
strcpy(reason,"converged due to happy breakdown");
strcpy(reason_str,"converged due to happy breakdown");
break;
case KSP_CONVERGED_ITERATING:
strcpy(reason,"converged");
strcpy(reason_str,"converged");
break;
case KSP_DIVERGED_NULL:
strcpy(reason,"diverged due to null");
strcpy(reason_str,"diverged due to null");
break;
case KSP_DIVERGED_ITS:
strcpy(reason,"diverged due to maximum iteration number");
strcpy(reason_str,"diverged due to maximum iteration number");
break;
case KSP_DIVERGED_DTOL:
strcpy(reason,"diverged as divergence tolerance was reached");
strcpy(reason_str,"diverged as divergence tolerance was reached");
break;
case KSP_DIVERGED_BREAKDOWN:
strcpy(reason,"diverged due to breakdown in method");
strcpy(reason_str,"diverged due to breakdown in method");
break;
case KSP_DIVERGED_BREAKDOWN_BICG:
strcpy(reason,"diverged due to breakdown in Biconjugate Gradients method");
strcpy(reason_str,"diverged due to breakdown in Biconjugate Gradients method");
break;
case KSP_DIVERGED_NONSYMMETRIC:
strcpy(reason,"diverged since matrix is nonsymmetric");
strcpy(reason_str,"diverged since matrix is nonsymmetric");
break;
case KSP_DIVERGED_INDEFINITE_PC:
strcpy(reason,"diverged since preconditioner is not defined");
strcpy(reason_str,"diverged since preconditioner is not defined");
break;
case KSP_DIVERGED_NANORINF:
strcpy(reason,"diverged since not a number or infinite was encountered");
strcpy(reason_str,"diverged since not a number or infinite was encountered");
break;
case KSP_DIVERGED_INDEFINITE_MAT:
strcpy(reason,"diverged due to matrix is not definite");
strcpy(reason_str,"diverged due to matrix is not definite");
break;
default:
strcpy(reason,"reason code was not defined in manual");
strcpy(reason_str,"reason code was not defined in manual");
break;
}
return reason;
return reason_str;
}
#endif
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment