diff --git a/CMakeLists.txt b/CMakeLists.txt index a536991e..91005a79 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -427,6 +427,7 @@ find_package(METIS) if(METIS_FOUND) + include(CheckTypeSize) message(STATUS "METIS PATH ${METIS_INCLUDES} and metis libraries ${METIS_LIBRARIES}") # Make sure this path is correct # set(METISINCFILE "metis.h") # Adjust this to your actual path @@ -444,6 +445,9 @@ if(METIS_FOUND) # Check for real sizes using try_compile include(CheckCSourceCompiles) + + +#[=====[ # Function to check the size of a type function(check_metis_real_type type_name) set(source_code " @@ -455,25 +459,42 @@ if(METIS_FOUND) }") # Create a temporary source file - file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_size.c" "${source_code}") + #file(WRITE "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_size.c" "${source_code}") # Try to compile it - try_compile(COMPILER_RESULT "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp" - "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_size.c") + #try_compile(COMPILER_RESULT "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp" + # "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_size.c") # Check the result and read the output - if (COMPILER_RESULT) + + set(test_file "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_metis_size.c") + file(WRITE "${test_file}" "${source_code}") + + # Use try_run to compile AND execute + try_run(RUN_RESULT COMPILE_RESULT + "${CMAKE_BINARY_DIR}" + "${test_file}" + CMAKE_FLAGS "-DINCLUDE_DIRECTORIES=${METIS_INCLUDES}" + RUN_OUTPUT_VARIABLE type_size + COMPILE_OUTPUT_VARIABLE compile_log + ) + + if(COMPILE_RESULT AND (RUN_RESULT EQUAL 0)) + # if (COMPILER_RESULT) execute_process(COMMAND "${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/test_size" OUTPUT_VARIABLE type_size) string(STRIP "${type_size}" type_size) + message(STATUS "Metis ?: ${type_size};") if (type_name STREQUAL "float") set(PSB_METIS_REAL_32 "${type_size}" PARENT_SCOPE) + message(STATUS "Metis 32: ${type_size}") # add_definitions(-DPSB_METIS_REAL_32) set(CREALMETIS "#define PSB_METIS_REAL_32" PARENT_SCOPE) elseif (type_name STREQUAL "double") set(PSB_METIS_REAL_64 "${type_size}" PARENT_SCOPE) #add_definitions(-DPSB_METIS_REAL_64) - set(CREALMETIS "#define PSB_METIS_REAL_64" PARENT_SCOPE) + set(CREALMETIS "#define PSB_METIS_REAL_64" PARENT_SCOPE) + message(STATUS "Metis 64: ${type_size}") endif() else() message(WARNING "Failed to compile test for type size: ${type_name}") @@ -483,44 +504,65 @@ if(METIS_FOUND) # Check for both float and double check_metis_real_type(float) check_metis_real_type(double) - +#]=====] # Set HAVE_METIS if METIS is found #add_compile_options(-DPSB_HAVE_METIS) # set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -DPSB_HAVE_METIS") - - - - - # Determine METIS_INDEX based on real type sizes - if(DEFINED PSB_METIS_REAL_32) - set(METIS_INDEX 32) - elseif(DEFINED PSB_METIS_REAL_64) - set(METIS_INDEX 64) - else() - message(WARNING "Neither METIS_REAL_32 nor METIS_REAL_64 is defined.") - set(METIS_INDEX 64) # Default to 64 if not defined - endif() - - # Check conditions for LPK_SIZE and METIS_INDEX - if(LPK_SIZE STREQUAL "4") - if(METIS_INDEX STREQUAL "64") - # Mismatch between METIS size and PSBLAS LPK - message(FATAL " Mismatch between metis ${METIS_INDEX} size and psblas LPK size ${LPK_SIZE}") - set(METIS_FOUND FALSE) + # 1. Tell CMake where to find metis.h for the check +set(CMAKE_EXTRA_INCLUDE_FILES "${METIS_INCLUDES}/metis.h") + +# 2. Check the size of Metis's own type: real_t +# This replaces checking 'float' and 'double' separately +check_type_size("real_t" METIS_REAL_SIZE) +check_type_size("idx_t" METIS_IDX_SIZE) + +# 3. Handle the result +if(METIS_IDX_SIZE) + if(METIS_IDX_SIZE EQUAL 4) + set(CINTMETIS "#define PSB_METIS_32") + message(STATUS "Metis detected as 32-bit (idx_t is 4 bytes)") + set(METIS_INDEX 32) + elseif(METIS_IDX_SIZE EQUAL 8) + set(CINTMETIS "#define PSB_METIS_64") + message(STATUS "Metis detected as 64-bit (idx_t is 8 bytes)") + set(METIS_INDEX 64) endif() - endif() - - if(LPK_SIZE STREQUAL "8") - if(METIS_INDEX STREQUAL "32") - # Mismatch between METIS size and PSBLAS LPK - message(FATAL " Mismatch between metis ${METIS_INDEX} size and psblas LPK size ${LPK_SIZE}") - set(METIS_FOUND FALSE) +else() + message(WARNING "Could not determine size of idx_t from metis.h. Check METIS_INCLUDES.") +endif() +# 3. Handle the result +if(METIS_REAL_SIZE) + if(METIS_REAL_SIZE EQUAL 4) + set(CREALMETIS "#define PSB_METIS_REAL_32") + message(STATUS "Metis detected as 32-bit (real_t is 4 bytes)") + elseif(METIS_REAL_SIZE EQUAL 8) + set(CREALMETIS "#define PSB_METIS_REAL_64") + message(STATUS "Metis detected as 64-bit (real_t is 8 bytes)") endif() - endif() +else() + message(WARNING "Could not determine size of real_t from metis.h. Check METIS_INCLUDES.") +endif() + +# clean the variable +set(CMAKE_EXTRA_INCLUDE_FILES "") + +# Mismatch Logic Below +if(LPK_SIZE STREQUAL "4" AND METIS_INDEX EQUAL 64) + message(WARNING "Mismatch: Metis IDX is 64-bit but PSBLAS LPK is 32-bit") + set(METIS_FOUND FALSE) + set(CREALMETIS "") + set(CINTMETIS "") +elseif(LPK_SIZE STREQUAL "8" AND METIS_INDEX EQUAL 32) + set(METIS_FOUND FALSE) + message(WARNING "Mismatch: Metis IDX is 32-bit but PSBLAS LPK is 64-bit") + set(CREALMETIS "") + set(CINTMETIS "") +endif() + - if(METIS_FOUND) +if(METIS_FOUND) # Make sure this path is correct set(METISINCFILE "metis.h") # Adjust this to your actual path @@ -539,7 +581,7 @@ if(METIS_FOUND) set(CHAVEMETIS "#define PSB_HAVE_METIS") - set(CINTMETIS "#define PSB_METIS_${METIS_INDEX}") +# set(CINTMETIS "#define PSB_METIS_${METIS_INDEX}") # set(CREALMETIS "#define PSB_METIS_REAL_${LPK_SIZE}") # Configure the header file configure_file(${HEADER_TEMPLATE} ${HEADER_OUTPUT} @ONLY)