Skip to content

Commit

Permalink
Py cpuflags (#106)
Browse files Browse the repository at this point in the history
* apple-capable cpu flags detection without pycpuinfo

* removed orphaned tmp file

* added missing detection script

* fixed ruff complaints for multiple imports on a single line
  • Loading branch information
alexKrauseTUD authored Aug 22, 2024
1 parent 7c53afa commit 5aacdfa
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 20 deletions.
85 changes: 85 additions & 0 deletions detect_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import platform
import os
import subprocess
import re

# https://stackoverflow.com/a/377028
def which(program):
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, _ = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ.get("PATH", "").split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None

def get_compile_info_from_compiler(compiler: str, arch_string: str):
info = ""
if "clang" in compiler:
ps = subprocess.Popen(("clang", "-E", "-", arch_string, "-###"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info = ps.stderr.read().decode("utf-8")
return(re.findall(r'"-target-feature" "\+([^\"]+)"', info))
elif "g++" in compiler:
ps = subprocess.Popen(("g++", "-E", "-", arch_string, "-###"), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info = ps.stderr.read().decode("utf-8")
return(re.findall(r' -m((?!no-)[^\s]+)', info))

def get_compile_info_from_lscpu():

if which("lscpu"):
my_env = os.environ.copy()
my_env["LANG"] = "en"
ps = subprocess.Popen(("lscpu"), env=my_env, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
info2 = ps.stderr.read()
info = ps.stdout.read()
lines = info.decode("utf-8").split("\n")

regex = re.compile(r"\s*Flags:\s*(?P<flags>(\w+\s)*(\w+))")
flags_set = set()
for line in lines:
if line.lstrip().lower().startswith("flags"):
for _, match in enumerate(regex.finditer(line)):
for flag in match.group("flags").split(" "):
flags_set.add(flag)
return flags_set
else:
print("No LSCPU")
return set()

def get_platform():
if "Darwin" in platform.system():
return "Apple"
else:
return platform.system()

arch = platform.uname()[4]
# if "x86" in arch:
# print(f"x86 CPU on {get_platform()}")
# arch_string = "-march=native"
# elif "aarch" in arch or "Apple" in get_platform():
# print("ARM CPU or Apple System")

flags = set()

if which("lscpu"):
flags = get_compile_info_from_lscpu()
else:

arch_string = ""
if "Apple" in get_platform() or "aarch" in arch:
arch_string = "-mcpu=native"
else:
arch_string = "-march=native"

print("Debug:", arch, get_platform())
gcc_flags = get_compile_info_from_compiler("g++", "-march=native") if which("g++") else set()
clang_flags = get_compile_info_from_compiler("clang", arch_string) if which("clang") else set()
flags = gcc_flags + clang_flags

print(' '.join(sorted(flags)))
20 changes: 8 additions & 12 deletions tsl.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,15 @@ function(create_tsl)
if (CREATE_TSL_ARGS_TARGETS_FLAGS STREQUAL "" OR NOT DEFINED CREATE_TSL_ARGS_TARGETS_FLAGS)
set(TARGETS_FLAGS "") # STRING "space separated lscpu flags for --targets, will attempt to call lscpu if empty"
execute_process(
COMMAND "${Python3_EXECUTABLE}" -c "import cpuinfo; print(*cpuinfo.get_cpu_info()['flags'])"
OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE TARGETS_FLAGS
COMMAND "${Python3_EXECUTABLE}" "${TSLGENERATOR_DIRECTORY}/detect_flags.py"
OUTPUT_VARIABLE TARGETS_FLAGS
RESULT_VARIABLE TSLHardwareReturnValue
)
if(TARGETS_FLAGS STREQUAL "")
execute_process(
COMMAND bash -c "LANG=en;lscpu|grep -i flags | tr ' ' '\n' | grep -v -E '^Flags:|^$' | sort -d | tr '\n' ' '"
OUTPUT_VARIABLE TARGETS_FLAGS
RESULT_VARIABLE TSLHardwareReturnValue
)
if(NOT TSLHardwareReturnValue EQUAL 0)
message(FATAL_ERROR "Could not determine hardware flags. Please specify them manually.")
endif()
# Remove trailing newline from the Python output
string(REGEX REPLACE "\n$" "" TARGETS_FLAGS "${TARGETS_FLAGS}")

if(NOT TSLHardwareReturnValue EQUAL 0)
message(FATAL_ERROR "Could not determine hardware flags. Please specify them manually.")
endif()
message(STATUS "lscpu flags: ${TARGETS_FLAGS}")
else()
Expand Down Expand Up @@ -168,7 +165,6 @@ function(create_tsl)
COMMAND "${Python3_EXECUTABLE}" "${TSLGENERATOR_DIRECTORY}/main.py"
${TSL_GENERATOR_OPTIONS} -o "${TSL_GENERATOR_DESTINATION}"
${TSL_TARGETS_SWITCH} ${TSL_PRIMITIVE_SWITCH} ${TSL_TYPES_SWITCH}
ANY
)
set(TSL_INCLUDE_DIRECTORY "${TSL_GENERATOR_DESTINATION}/include" CACHE STRING "include path of TSL")
set(TSL_INCLUDE_DIRECTORY_ROOT "${TSL_GENERATOR_DESTINATION}/" CACHE STRING "root path of TSL")
Expand Down
8 changes: 0 additions & 8 deletions xx.req.txt

This file was deleted.

0 comments on commit 5aacdfa

Please sign in to comment.