Merge pull request #48 from drowe67/ms-uncouple-codec2

Disable AVX2 by default even on supported machines
ms-rpi-build
drowe67 2022-11-26 06:39:24 +10:30 committed by GitHub
commit 59f75b6fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 33 deletions

View File

@ -5,13 +5,19 @@
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.9" CACHE STRING "Minimum OS X deployment version")
cmake_minimum_required(VERSION 3.0)
# Avoid warning about DOWNLOAD_EXTRACT_TIMESTAMP in CMake 3.24:
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
cmake_policy(SET CMP0135 NEW)
endif()
project(LPCNet C)
option(DISABLE_CPU_OPTIMIZATION "Disable CPU optimization discovery." OFF)
option(AVX2 "Enable AVX2 CPU optimizations." OFF)
option(AVX "Enable AVX CPU optimizations." OFF)
option(SSE "Enable SSE CPU optimizations." OFF)
option(NEON "Enable NEON CPU optimizations for RPi." OFF)
option(AVX "Enable AVX CPU optimizations." ON)
option(SSE "Enable SSE CPU optimizations." ON)
option(NEON "Enable NEON CPU optimizations for RPi." ON)
include(GNUInstallDirs)
mark_as_advanced(CLEAR
@ -89,54 +95,73 @@ if(NOT DISABLE_CPU_OPTIMIZATION)
if(UNIX AND NOT APPLE)
message(STATUS "Looking for available CPU optimizations on Linux/BSD system...")
execute_process(COMMAND grep -c "avx2" /proc/cpuinfo
OUTPUT_VARIABLE AVX2)
OUTPUT_VARIABLE AVX2_PRESENT)
execute_process(COMMAND grep -c "avx " /proc/cpuinfo
OUTPUT_VARIABLE AVX)
OUTPUT_VARIABLE AVX_PRESENT)
execute_process(COMMAND grep -c "sse4_1 " /proc/cpuinfo
OUTPUT_VARIABLE SSE)
OUTPUT_VARIABLE SSE_PRESENT)
execute_process(COMMAND grep -c "neon" /proc/cpuinfo
OUTPUT_VARIABLE NEON)
OUTPUT_VARIABLE NEON_PRESENT)
elseif(APPLE)
if(BUILD_OSX_UNIVERSAL)
# Presume AVX/AVX2 are enabled on the x86 side. The ARM side will auto-enable
# NEON optimizations by virtue of being aarch64.
set(AVX TRUE)
set(AVX2 TRUE)
set(SSE TRUE)
# Presume AVX and SSE are enabled on the x86 side. (AVX2 is not guaranteed depending
# on model.) The ARM side will auto-enable NEON optimizations by virtue of being aarch64.
set(AVX_PRESENT TRUE)
set(SSE_PRESENT TRUE)
set(NEON_PRESENT TRUE)
else()
# Under OSX we need to look through a few sysctl entries to determine what our CPU supports.
message(STATUS "Looking for available CPU optimizations on an OSX system...")
execute_process(COMMAND sysctl -a COMMAND grep machdep.cpu.leaf7_features COMMAND grep -c AVX2
OUTPUT_VARIABLE AVX2)
OUTPUT_VARIABLE AVX2_PRESENT)
execute_process(COMMAND sysctl -a COMMAND grep machdep.cpu.features COMMAND grep -c AVX
OUTPUT_VARIABLE AVX)
OUTPUT_VARIABLE AVX_PRESENT)
execute_process(COMMAND sysctl -a COMMAND grep machdep.cpu.features COMMAND grep -c SSE4.1
OUTPUT_VARIABLE SSE_PRESENT)
# Unlike with the above, NEON *is* guaranteed if on ARM as there were never any ARM32 Macs
# available. We don't need any specific compiler flags for this, though.
set(NEON_PRESENT TRUE)
endif(BUILD_OSX_UNIVERSAL)
elseif(WIN32)
message(STATUS "No detection capability on Windows, assuming AVX is available.")
set(AVX TRUE)
set(AVX_PRESENT TRUE)
else()
message(STATUS "System is not *nix, processor specific optimizations cannot be determined.")
message(" You can try setting them manually, e.g.: -DAVX2=1 or -DAVX=1 or -DNEON=1")
# Presume all optimizations are available as the user will need to set them themselves.
set(AVX2_PRESENT TRUE)
set(AVX_PRESENT TRUE)
set(SSE_PRESENT TRUE)
set(NEON_PRESENT TRUE)
endif()
else()
# Presume all optimizations are available as the user is likely setting them themselves
# (e.g. cross-compiling)
set(AVX2_PRESENT TRUE)
set(AVX_PRESENT TRUE)
set(SSE_PRESENT TRUE)
set(NEON_PRESENT TRUE)
endif()
if(${AVX2} OR ${AVX2} GREATER 0)
set(LPCNET_C_PROC_FLAGS "")
if(${AVX2} AND (${AVX2_PRESENT} OR ${AVX2_PRESENT} GREATER 0))
message(STATUS "avx2 processor flags found or enabled.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx2 -mfma")
elseif(${AVX} OR ${AVX} GREATER 0)
set(LPCNET_C_PROC_FLAGS -mavx2 -mfma)
elseif(${AVX} AND (${AVX_PRESENT} OR ${AVX_PRESENT} GREATER 0))
# AVX2 machines will also match on AVX
message(STATUS "avx processor flags found or enabled.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mavx")
elseif(${SSE} OR ${SSE} GREATER 0)
set(LPCNET_C_PROC_FLAGS -mavx)
elseif(${SSE} AND (${SSE_PRESENT} OR ${SSE_PRESENT} GREATER 0))
# AVX and AVX2 machines will also match on SSE
message(STATUS "sse processor flags found or enabled.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.1")
endif()
# RPi / ARM 32bit
if(${NEON} OR ${NEON} GREATER 0)
set(LPCNET_C_PROC_FLAGS -msse4.1)
elseif(${NEON} AND (${NEON_PRESENT} OR ${NEON_PRESENT} GREATER 0))
# RPi / ARM 32bit
message(STATUS "neon processor flags found or enabled.")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mfpu=neon -march=armv8-a -mtune=cortex-a53")
set(LPCNET_C_PROC_FLAGS -mfpu=neon -march=armv8-a -mtune=cortex-a53)
endif()
# grab latest NN model (or substitute your own)

View File

@ -1,6 +1,3 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${lpcnet_SOURCE_DIR})
# Files brought in from Codec2 that are mandatory for LPCNet.
set(codec2_import_srcs
from_codec2/sine.c
@ -10,6 +7,15 @@ set(codec2_import_srcs
from_codec2/kiss_fftr.c
)
# Set visibility of files brought in from Codec2 so that they're not visible to
# users (such as Codec2 itself).
foreach(CODEC2_FILE IN LISTS codec2_import_srcs)
set_source_files_properties(${CODEC2_FILE} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden -include ${CMAKE_CURRENT_SOURCE_DIR}/from_codec2/codec2_renames.h")
endforeach()
include_directories(${CMAKE_CURRENT_SOURCE_DIR}
${lpcnet_SOURCE_DIR})
set(lpcnet_freedv_srcs
freq.c
kiss_fft.c
@ -28,13 +34,12 @@ set(lpcnet_freedv_srcs
${lpcnet_SOURCE_DIR}/nnet_data.c
)
# Set visibility of files brought in from Codec2 so that they're not visible to
# users (such as Codec2 itself).
foreach(CODEC2_FILE IN LISTS codec2_import_srcs)
set_source_files_properties(${CODEC2_FILE} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden -include ${CMAKE_CURRENT_SOURCE_DIR}/from_codec2/codec2_renames.h")
foreach(LPCNET_FILE IN LISTS lpcnet_freedv_srcs)
set_source_files_properties(${LPCNET_FILE} PROPERTIES COMPILE_FLAGS ${LPCNET_C_PROC_FLAGS})
endforeach()
add_library(lpcnetfreedv SHARED ${lpcnet_freedv_srcs} ${codec2_import_srcs})
set_target_properties(lpcnetfreedv PROPERTIES
PUBLIC_HEADER lpcnet_freedv.h
VERSION ${LPCNET_VERSION}

View File

@ -49,4 +49,8 @@
#define synthesise __codec2__synthesise
#define codec2_rand __codec2__codec2_rand
#define hs_pitch_refinement __codec2__hs_pitch_refinement
#define post_process_sub_multiples __codec2__post_process_sub_multiples
#define nlp_fir __codec2__nlp_fir
#endif /* CODEC2_RENAMES_H */