Merge branch 'emscripten_support' into 'master'

Emscripten support

See merge request ngsolve/netgen!589
This commit is contained in:
Hochsteger, Matthias 2023-07-28 13:01:41 +02:00
commit 088434d2bb
8 changed files with 67 additions and 34 deletions

View File

@ -95,7 +95,7 @@ if(USE_PYTHON)
file(TO_CMAKE_PATH ${PYTHON_PACKAGES_INSTALL_DIR} PYTHON_PACKAGES_INSTALL_DIR) file(TO_CMAKE_PATH ${PYTHON_PACKAGES_INSTALL_DIR} PYTHON_PACKAGES_INSTALL_DIR)
endif(USE_PYTHON) endif(USE_PYTHON)
if(APPLE) if(APPLE AND NOT EMSCRIPTEN)
set(NG_INSTALL_DIR_BIN_DEFAULT Contents/MacOS) set(NG_INSTALL_DIR_BIN_DEFAULT Contents/MacOS)
set(NG_INSTALL_DIR_LIB_DEFAULT Contents/MacOS) set(NG_INSTALL_DIR_LIB_DEFAULT Contents/MacOS)
set(NG_INSTALL_DIR_CMAKE_DEFAULT Contents/Resources/CMake) set(NG_INSTALL_DIR_CMAKE_DEFAULT Contents/Resources/CMake)
@ -104,7 +104,7 @@ if(APPLE)
set(NG_INSTALL_DIR_INCLUDE_DEFAULT Contents/Resources/include) set(NG_INSTALL_DIR_INCLUDE_DEFAULT Contents/Resources/include)
set(NG_RPATH_TOKEN "@loader_path") set(NG_RPATH_TOKEN "@loader_path")
else(APPLE) else(APPLE AND NOT EMSCRIPTEN)
set(NG_INSTALL_DIR_BIN_DEFAULT bin) set(NG_INSTALL_DIR_BIN_DEFAULT bin)
set(NG_INSTALL_DIR_LIB_DEFAULT lib) set(NG_INSTALL_DIR_LIB_DEFAULT lib)
if(WIN32) if(WIN32)
@ -117,7 +117,7 @@ else(APPLE)
set(NG_INSTALL_DIR_INCLUDE_DEFAULT include) set(NG_INSTALL_DIR_INCLUDE_DEFAULT include)
set(NG_RPATH_TOKEN "\$ORIGIN") set(NG_RPATH_TOKEN "\$ORIGIN")
endif(APPLE) endif(APPLE AND NOT EMSCRIPTEN)
set(NG_INSTALL_DIR_PYTHON ${NG_INSTALL_DIR_PYTHON_DEFAULT} CACHE STRING "Install directory for Python files") set(NG_INSTALL_DIR_PYTHON ${NG_INSTALL_DIR_PYTHON_DEFAULT} CACHE STRING "Install directory for Python files")
set(NG_INSTALL_DIR_BIN ${NG_INSTALL_DIR_BIN_DEFAULT} CACHE STRING "Install directory for executables") set(NG_INSTALL_DIR_BIN ${NG_INSTALL_DIR_BIN_DEFAULT} CACHE STRING "Install directory for executables")

View File

@ -14,9 +14,16 @@ set (SUBPROJECT_ARGS
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dependencies PREFIX ${CMAKE_CURRENT_BINARY_DIR}/dependencies
) )
if (EMSCRIPTEN)
set (SUBPROJECT_ARGS
${SUBPROJECT_ARGS}
CMAKE_COMMAND emcmake ${CMAKE_COMMAND})
endif()
# only show output on failure in ci-builds # only show output on failure in ci-builds
if(DEFINED ENV{CI}) if(DEFINED ENV{CI})
set (SUBPROJECT_ARGS set (SUBPROJECT_ARGS
${SUBPROJECT_ARGS}
LOG_DOWNLOAD ON LOG_DOWNLOAD ON
LOG_BUILD ON LOG_BUILD ON
LOG_INSTALL ON LOG_INSTALL ON
@ -134,25 +141,27 @@ endif(BUILD_OCC)
endif(USE_OCC) endif(USE_OCC)
if(BUILD_ZLIB) if(BUILD_ZLIB)
set(ZLIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/dependencies/zlib) set(ZLIB_ROOT ${CMAKE_CURRENT_BINARY_DIR}/dependencies/zlib)
ExternalProject_Add(project_zlib ExternalProject_Add(project_zlib
${SUBPROJECT_ARGS} ${SUBPROJECT_ARGS}
URL https://github.com/madler/zlib/archive/refs/tags/v1.2.11.zip URL https://github.com/madler/zlib/archive/refs/tags/v1.2.11.zip
URL_MD5 9d6a627693163bbbf3f26403a3a0b0b1 URL_MD5 9d6a627693163bbbf3f26403a3a0b0b1
DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external_dependencies DOWNLOAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external_dependencies
CMAKE_ARGS CMAKE_ARGS
-DCMAKE_INSTALL_PREFIX=${ZLIB_DIR} -DCMAKE_INSTALL_PREFIX=${ZLIB_ROOT}
${SUBPROJECT_CMAKE_ARGS} ${SUBPROJECT_CMAKE_ARGS}
UPDATE_COMMAND "" # Disable update UPDATE_COMMAND "" # Disable update
BUILD_IN_SOURCE 1 BUILD_IN_SOURCE 1
) )
list(APPEND NETGEN_DEPENDENCIES project_zlib) list(APPEND NETGEN_DEPENDENCIES project_zlib)
list(APPEND NETGEN_CMAKE_PREFIX_PATH ${ZLIB_DIR})
if(WIN32) if(WIN32)
# force linking the static library # force linking the static library
set(ZLIB_INCLUDE_DIRS ${ZLIB_DIR}/include) set(ZLIB_INCLUDE_DIRS ${ZLIB_ROOT}/include)
set(ZLIB_LIBRARIES ${ZLIB_DIR}/lib/zlibstatic.lib) set(ZLIB_LIBRARIES ${ZLIB_ROOT}/lib/zlibstatic.lib)
elseif(EMSCRIPTEN)
set(ZLIB_INCLUDE_DIRS ${ZLIB_ROOT}/include)
set(ZLIB_LIBRARIES ${ZLIB_ROOT}/lib/libz.a)
endif(WIN32) endif(WIN32)
else() else()
include(cmake/external_projects/zlib.cmake) include(cmake/external_projects/zlib.cmake)
@ -252,6 +261,7 @@ set_vars( NETGEN_CMAKE_ARGS
OpenCascade_ROOT OpenCascade_ROOT
ZLIB_INCLUDE_DIRS ZLIB_INCLUDE_DIRS
ZLIB_LIBRARIES ZLIB_LIBRARIES
ZLIB_ROOT
NGLIB_LIBRARY_TYPE NGLIB_LIBRARY_TYPE
NGCORE_LIBRARY_TYPE NGCORE_LIBRARY_TYPE

View File

@ -17,6 +17,13 @@ add_library(ngcore ${NGCORE_LIBRARY_TYPE}
string(REPLACE "|" ";" ng_compile_flags_replace_sep "${NG_COMPILE_FLAGS}") string(REPLACE "|" ";" ng_compile_flags_replace_sep "${NG_COMPILE_FLAGS}")
target_compile_options(ngcore PUBLIC ${ng_compile_flags_replace_sep}) target_compile_options(ngcore PUBLIC ${ng_compile_flags_replace_sep})
if(EMSCRIPTEN)
target_link_options(ngcore PUBLIC -sALLOW_MEMORY_GROWTH)
# target_link_options(ngcore PUBLIC -sINITIAL_MEMORY=1gb)
target_link_options(ngcore PUBLIC -pthread -sPTHREAD_POOL_SIZE=16)
target_compile_options(ngcore PUBLIC -pthread)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9) if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9)
target_link_libraries(ngcore PUBLIC stdc++fs) target_link_libraries(ngcore PUBLIC stdc++fs)
endif() endif()

View File

@ -23,7 +23,7 @@ namespace ngcore
// ********* STUFF FOR GETBACKTRACE *************************** // ********* STUFF FOR GETBACKTRACE ***************************
#ifdef __GNUC__ #if defined __GNUC__ && !defined __EMSCRIPTEN__
#include <execinfo.h> #include <execinfo.h>
#include <string.h> #include <string.h>
@ -226,7 +226,7 @@ static bool dummy = []()
return true; return true;
}(); }();
#else // __GNUC__ #else // __GNUC__ and not __EMSCRIPTEN__
namespace ngcore namespace ngcore
{ {

View File

@ -71,6 +71,8 @@ namespace ngcore
unsigned long long tics; unsigned long long tics;
__asm __volatile("mrs %0, CNTVCT_EL0" : "=&r" (tics)); __asm __volatile("mrs %0, CNTVCT_EL0" : "=&r" (tics));
return tics; return tics;
#elif defined(__EMSCRIPTEN__)
return std::chrono::high_resolution_clock::now().time_since_epoch().count();
#else #else
#warning "Unsupported CPU architecture" #warning "Unsupported CPU architecture"
return 0; return 0;

View File

@ -4452,30 +4452,30 @@ namespace netgen
} }
return 1; return 1;
if ( /* hp */ 1) // needed for old, simple hp-refinement // if ( /* hp */ 1) // needed for old, simple hp-refinement
{ // {
// trigs with 2 or more segments are illegal // // trigs with 2 or more segments are illegal
int i; // int i;
int nseg = 0; // int nseg = 0;
if (!segmentht) // if (!segmentht)
{ // {
cerr << "no segmentht allocated" << endl; // cerr << "no segmentht allocated" << endl;
return 0; // return 0;
} // }
// Point3d cp(0.5, 0.5, 0.5); // // Point3d cp(0.5, 0.5, 0.5);
for (i = 1; i <= 3; i++) // for (i = 1; i <= 3; i++)
{ // {
INDEX_2 i2(el.PNumMod (i), el.PNumMod (i+1)); // INDEX_2 i2(el.PNumMod (i), el.PNumMod (i+1));
i2.Sort(); // i2.Sort();
if (segmentht -> Used (i2)) // if (segmentht -> Used (i2))
nseg++; // nseg++;
} // }
if (nseg >= 2) // if (nseg >= 2)
return 0; // return 0;
} // }
return 1; // return 1;
} }
double Mesh :: CalcTotalBad (const MeshingParameters & mp ) double Mesh :: CalcTotalBad (const MeshingParameters & mp )

View File

@ -9,5 +9,11 @@ target_link_libraries(nglib PUBLIC ngcore)
target_link_libraries( nglib PRIVATE ${MPI_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${JPEG_LIBRARIES} ${MKL_LIBRARIES} ${ZLIB_LIBRARIES} ${OCC_LIBRARIES} netgen_cgns ) target_link_libraries( nglib PRIVATE ${MPI_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${JPEG_LIBRARIES} ${MKL_LIBRARIES} ${ZLIB_LIBRARIES} ${OCC_LIBRARIES} netgen_cgns )
if(EMSCRIPTEN)
target_link_options(nglib PUBLIC -sALLOW_MEMORY_GROWTH)
target_link_options(nglib PUBLIC -pthread -sPTHREAD_POOL_SIZE=16)
target_compile_options(nglib PUBLIC -pthread)
endif(EMSCRIPTEN)
install(TARGETS nglib netgen_cgns ${NG_INSTALL_DIR}) install(TARGETS nglib netgen_cgns ${NG_INSTALL_DIR})
install(FILES nglib.h DESTINATION ${NG_INSTALL_DIR_INCLUDE} COMPONENT netgen_devel) install(FILES nglib.h DESTINATION ${NG_INSTALL_DIR_INCLUDE} COMPONENT netgen_devel)

View File

@ -1,7 +1,15 @@
# this file is included from the parent directory (otherwise generated source files are not recognized properly by cmake) # this file is included from the parent directory (otherwise generated source files are not recognized properly by cmake)
# generate .cpp files containing the string of the .rls meshing rule files # generate .cpp files containing the string of the .rls meshing rule files
add_executable(makerls rules/makerlsfile.cpp) if(EMSCRIPTEN)
add_custom_command(OUTPUT makerls
COMMAND g++ ${CMAKE_CURRENT_SOURCE_DIR}/rules/makerlsfile.cpp -o ${CMAKE_CURRENT_BINARY_DIR}/makerls
)
set(rules_command ${CMAKE_BINARY_DIR}/makerls)
else(EMSCRIPTEN)
add_executable(makerls rules/makerlsfile.cpp)
set(rules_command makerls)
endif()
set(rules set(rules
hexrules hexrules
@ -21,7 +29,7 @@ foreach(rule ${rules})
set(rule_cpp ${CMAKE_CURRENT_BINARY_DIR}/rules/rule_${rule}.cpp) set(rule_cpp ${CMAKE_CURRENT_BINARY_DIR}/rules/rule_${rule}.cpp)
add_custom_command(OUTPUT ${rule_cpp} add_custom_command(OUTPUT ${rule_cpp}
COMMAND makerls ${rule_file} ${rule_cpp} ${rule} COMMAND ${rules_command} ${rule_file} ${rule_cpp} ${rule}
DEPENDS makerls ${rule_file} DEPENDS makerls ${rule_file}
) )
endforeach() endforeach()