### Get pkg-config Flags (Standard Prefix) Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Demonstrates how to use pkg-config to retrieve compiler and linker flags for the libcpuinfo library when installed in a standard system prefix. ```console $ pkg-config --cflags --libs libcpuinfo -I/usr/include/x86_64-linux-gnu/ -L/lib/x86_64-linux-gnu/ -lcpuinfo ``` -------------------------------- ### Verify Installation and Get Flags with pkg-config Source: https://context7.com/pytorch/cpuinfo/llms.txt Verifies the installation of libcpuinfo and retrieves the necessary compiler and linker flags using pkg-config. Useful for manual build configurations. ```bash # Verify installation and get compiler/linker flags $ pkg-config --cflags --libs libcpuinfo ``` ```bash # For custom prefix installations $ PKG_CONFIG_PATH="/path/to/cpuinfo/lib/pkgconfig" pkg-config --cflags --libs libcpuinfo ``` -------------------------------- ### Generate and Install pkg-config Files with CMake Source: https://github.com/pytorch/cpuinfo/blob/main/CMakeLists.txt Uses a custom JOIN_PATHS function to resolve installation directories and configures the libcpuinfo.pc file. ```cmake IF(CPUINFO_BUILD_PKG_CONFIG) FUNCTION(JOIN_PATHS joined_path first_path_segment) SET(temp_path "${first_path_segment}") FOREACH(current_segment IN LISTS ARGN) IF(NOT ("${current_segment}" STREQUAL "")) IF(IS_ABSOLUTE "${current_segment}") SET(temp_path "${current_segment}") ELSE() SET(temp_path "${temp_path}/${current_segment}") ENDIF() ENDIF() ENDFOREACH() SET(${joined_path} "${temp_path}" PARENT_SCOPE) ENDFUNCTION() JOIN_PATHS(libdir_for_pc_file "\${exec_prefix}" "${CMAKE_INSTALL_LIBDIR}") JOIN_PATHS(includedir_for_pc_file "\${prefix}" "${CMAKE_INSTALL_INCLUDEDIR}") CONFIGURE_FILE( "libcpuinfo.pc.in" "libcpuinfo.pc" @ONLY) INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/libcpuinfo.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") ENDIF() ``` -------------------------------- ### Get pkg-config Flags (Non-Standard Prefix) Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Shows how to use pkg-config to find libcpuinfo when installed in a non-standard directory by setting the PKG_CONFIG_PATH environment variable. ```console $ PKG_CONFIG_PATH="/home/me/projects/cpuinfo/prefix/lib/pkgconfig/:$PKG_CONFIG_PATH" pkg-config --cflags --libs libcpuinfo -I/home/me/projects/cpuinfo/prefix/include -L/home/me/projects/cpuinfo/prefix/lib -lcpuinfo ``` -------------------------------- ### Initialize CPUInfo and Get Basic Information Source: https://context7.com/pytorch/cpuinfo/llms.txt Initializes the CPUInfo library and retrieves basic processor details. Ensure cpuinfo_initialize() is called before any other cpuinfo functions. Deinitialization is optional for short-lived programs. ```c #include #include #include int main(int argc, char** argv) { // Initialize cpuinfo - must be called before any other cpuinfo functions if (!cpuinfo_initialize()) { fprintf(stderr, "failed to initialize CPU information\n"); exit(EXIT_FAILURE); } // Get basic processor information printf("Running on %s CPU\n", cpuinfo_get_package(0)->name); printf("Number of processors: %u\n", cpuinfo_get_processors_count()); printf("Number of cores: %u\n", cpuinfo_get_cores_count()); printf("Number of packages: %u\n", cpuinfo_get_packages_count()); // Optional: clean up when done (not required for short-lived programs) cpuinfo_deinitialize(); return 0; } ``` -------------------------------- ### Configure Autotools for libcpuinfo Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Example snippet for a project's configure.ac file to check for libcpuinfo using PKG_CHECK_MODULES and set CXXFLAGS and LIBS accordingly. ```makefile # CPU INFOrmation library... PKG_CHECK_MODULES( [libcpuinfo], [libcpuinfo], [], [AC_MSG_ERROR([libcpuinfo missing...])]) YOURPROJECT_CXXFLAGS="$YOURPROJECT_CXXFLAGS $libcpuinfo_CFLAGS" YOURPROJECT_LIBS="$YOURPROJECT_LIBS $libcpuinfo_LIBS" ``` -------------------------------- ### Get L1 Data Cache Size Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library and retrieves the size of the L1 data cache on the fastest core. ```c cpuinfo_initialize(); const size_t l1_size = cpuinfo_get_processor(0)->cache.l1d->size; ``` -------------------------------- ### Makefile Integration with pkg-config Source: https://context7.com/pytorch/cpuinfo/llms.txt Integrates CPUInfo into a Makefile using pkg-config to automatically fetch compiler and linker flags. Assumes pkg-config is installed and libcpuinfo is available. ```makefile # Makefile integration using pkg-config CFLAGS += $(shell pkg-config --cflags libcpuinfo) LDFLAGS += $(shell pkg-config --libs libcpuinfo) my_app: main.c $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) ``` -------------------------------- ### Get Current Core Microarchitecture Index (Linux) Source: https://context7.com/pytorch/cpuinfo/llms.txt Retrieves the microarchitecture index for the current core on Linux systems. Returns 0 if unknown. ```c // Get current core's microarchitecture index for heterogeneous systems uint32_t get_current_core_type(void) { cpuinfo_initialize(); // Returns 0 if unknown, otherwise the uarch index return cpuinfo_get_current_uarch_index(); } ``` -------------------------------- ### Get Optimal Cache Size for Fastest Core Source: https://context7.com/pytorch/cpuinfo/llms.txt Retrieves the L1 data cache size for the fastest core (typically processor 0) on the system. This is useful for optimizing performance on heterogeneous CPU architectures. Requires cpuinfo_initialize(). ```c // Get cache size for the fastest core (useful for big.LITTLE systems) size_t get_optimal_cache_size(void) { cpuinfo_initialize(); // Processor 0 is typically the fastest core const struct cpuinfo_processor* proc = cpuinfo_get_processor(0); if (proc && proc->cache.l1d) { return proc->cache.l1d->size; } return 0; } ``` -------------------------------- ### Get Current Core Microarchitecture Index with Default (Linux) Source: https://context7.com/pytorch/cpuinfo/llms.txt Retrieves the microarchitecture index for the current core, providing a default value if detection fails. Useful for unsupported systems. ```c // With custom default for unsupported systems uint32_t get_current_core_type_safe(void) { cpuinfo_initialize(); // Returns provided default (e.g., 0 for performance core) if detection fails return cpuinfo_get_current_uarch_index_with_default(0); } #endif ``` -------------------------------- ### Log Processor Name Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library and prints the name of the first package. ```c cpuinfo_initialize(); printf("Running on %s CPU\n", cpuinfo_get_package(0)->name); ``` -------------------------------- ### Retrieve and print processor topology in C Source: https://context7.com/pytorch/cpuinfo/llms.txt Initializes the library and iterates through packages, microarchitectures, cores, and logical processors to display system topology. ```c #include #include #include void print_processor_topology(void) { cpuinfo_initialize(); // Iterate over physical packages (sockets) printf("Packages: %" PRIu32 "\n", cpuinfo_get_packages_count()); for (uint32_t i = 0; i < cpuinfo_get_packages_count(); i++) { const struct cpuinfo_package* package = cpuinfo_get_package(i); printf(" Package %u: %s\n", i, package->name); printf(" Cores: %" PRIu32 " (% ``` -------------------------------- ### Detect Microarchitecture and Vendor Information in C Source: https://context7.com/pytorch/cpuinfo/llms.txt Uses cpuinfo_initialize to prepare the library, then dispatches logic based on the detected microarchitecture or vendor. Ensure cpuinfo_initialize is called before accessing core information to avoid undefined behavior. ```c #include #include void optimize_for_microarchitecture(void* args) { cpuinfo_initialize(); // Get the current core's microarchitecture const struct cpuinfo_core* current_core = cpuinfo_get_current_core(); if (current_core == NULL) { // Fallback if current core detection fails generic_implementation(args); return; } // Dispatch based on microarchitecture switch (current_core->uarch) { // ARM Cortex cores case cpuinfo_uarch_cortex_a53: case cpuinfo_uarch_cortex_a55: cortex_a5x_implementation(args); // Optimized for little cores break; case cpuinfo_uarch_cortex_a72: case cpuinfo_uarch_cortex_a73: case cpuinfo_uarch_cortex_a75: case cpuinfo_uarch_cortex_a76: cortex_a7x_implementation(args); // Optimized for big cores break; // Apple Silicon case cpuinfo_uarch_firestorm: // M1/A14 performance cores case cpuinfo_uarch_avalanche: // M2/A15 performance cores apple_pcore_implementation(args); break; case cpuinfo_uarch_icestorm: // M1/A14 efficiency cores case cpuinfo_uarch_blizzard: // M2/A15 efficiency cores apple_ecore_implementation(args); break; // Intel cores case cpuinfo_uarch_sky_lake: case cpuinfo_uarch_golden_cove: case cpuinfo_uarch_raptor_cove: intel_pcore_implementation(args); break; case cpuinfo_uarch_gracemont: case cpuinfo_uarch_crestmont: intel_ecore_implementation(args); break; // AMD cores case cpuinfo_uarch_zen: case cpuinfo_uarch_zen2: case cpuinfo_uarch_zen3: case cpuinfo_uarch_zen4: case cpuinfo_uarch_zen5: amd_zen_implementation(args); break; default: generic_implementation(args); break; } } // Get vendor information void print_vendor_info(void) { cpuinfo_initialize(); const struct cpuinfo_core* core = cpuinfo_get_core(0); switch (core->vendor) { case cpuinfo_vendor_intel: printf("Intel processor\n"); break; case cpuinfo_vendor_amd: printf("AMD processor\n"); break; case cpuinfo_vendor_arm: printf("ARM processor\n"); break; case cpuinfo_vendor_apple: printf("Apple processor\n"); break; case cpuinfo_vendor_qualcomm: printf("Qualcomm processor\n"); break; default: printf("Unknown vendor\n"); break; } } ``` -------------------------------- ### Configure Dependencies Source: https://github.com/pytorch/cpuinfo/blob/main/CMakeLists.txt Sets up directory paths for dependencies and manages the automated download of Google Test and Google Benchmark if they are not provided by the system. ```cmake SET(CONFU_DEPENDENCIES_SOURCE_DIR ${CMAKE_SOURCE_DIR}/deps CACHE PATH "Confu-style dependencies source directory") SET(CONFU_DEPENDENCIES_BINARY_DIR ${CMAKE_BINARY_DIR}/deps CACHE PATH "Confu-style dependencies binary directory") IF(CPUINFO_SUPPORTED_PLATFORM AND (CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS)) IF(USE_SYSTEM_GOOGLETEST) FIND_PACKAGE(GTest REQUIRED) ELSEIF(NOT DEFINED GOOGLETEST_SOURCE_DIR) MESSAGE(STATUS "Downloading Google Test to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest (define GOOGLETEST_SOURCE_DIR to avoid it)") CONFIGURE_FILE(cmake/DownloadGoogleTest.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download/CMakeLists.txt") EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download") EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googletest-download") SET(GOOGLETEST_SOURCE_DIR "${CONFU_DEPENDENCIES_SOURCE_DIR}/googletest" CACHE STRING "Google Test source directory") ENDIF() ENDIF() IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_BENCHMARKS) IF(USE_SYSTEM_GOOGLEBENCHMARK) FIND_PACKAGE(benchmark REQUIRED) ELSEIF(NOT DEFINED GOOGLEBENCHMARK_SOURCE_DIR) MESSAGE(STATUS "Downloading Google Benchmark to ${CONFU_DEPENDENCIES_SOURCE_DIR}/googlebenchmark (define GOOGLEBENCHMARK_SOURCE_DIR to avoid it)") CONFIGURE_FILE(cmake/DownloadGoogleBenchmark.cmake "${CONFU_DEPENDENCIES_BINARY_DIR}/googlebenchmark-download/CMakeLists.txt") EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googlebenchmark-download") EXECUTE_PROCESS(COMMAND "${CMAKE_COMMAND}" --build . WORKING_DIRECTORY "${CONFU_DEPENDENCIES_BINARY_DIR}/googlebenchmark-download") SET(GOOGLEBENCHMARK_SOURCE_DIR "${CONFU_DEPENDENCIES_SOURCE_DIR}/googlebenchmark" CACHE STRING "Google Benchmark source directory") ENDIF() ENDIF() ``` -------------------------------- ### Check for ARM NEON Support Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library and checks if the host CPU supports ARM NEON instructions. ```c cpuinfo_initialize(); if (cpuinfo_has_arm_neon()) { neon_implementation(arguments); } ``` -------------------------------- ### Integrate with Makefile Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Use shell substitution with pkg-config to supply necessary compiler and linker flags. ```makefile CFLAGS=-g3 -Wall -Wextra -Werror ... LDFLAGS=-lfoo ... ... CFLAGS+= $(pkg-config --cflags libcpuinfo) LDFLAGS+= $(pkg-config --libs libcpuinfo) ``` -------------------------------- ### Check for x86 AVX Support Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library and checks if the host CPU supports x86 AVX instructions. ```c cpuinfo_initialize(); if (cpuinfo_has_x86_avx()) { avx_implementation(arguments); } ``` -------------------------------- ### Select Optimal NEON Implementation Source: https://context7.com/pytorch/cpuinfo/llms.txt Conditionally calls a NEON-optimized implementation if NEON is available, otherwise falls back to a scalar implementation. Requires cpuinfo initialization. ```c // Example: Select optimal NEON implementation void process_data_arm(float* data, size_t size) { cpuinfo_initialize(); if (cpuinfo_has_arm_neon()) { neon_implementation(data, size); } else { scalar_implementation(data, size); } } ``` -------------------------------- ### Meson Build Integration Source: https://context7.com/pytorch/cpuinfo/llms.txt Integrates CPUInfo into a Meson build system. Requires Meson version 0.55.0 or higher and the libcpuinfo dependency to be available. ```meson # Meson build integration project('MyCpuInfoProject', 'cpp', meson_version: '>=0.55.0') executable( 'MyCpuInfoExecutable', sources: 'main.cpp', dependencies: dependency('libcpuinfo') ) ``` -------------------------------- ### Select Optimal Computation Path Source: https://context7.com/pytorch/cpuinfo/llms.txt Demonstrates selecting an optimized computation function based on available x86 instruction sets, prioritizing newer and more capable instruction sets. Requires corresponding compute functions (e.g., avx512_compute) to be defined elsewhere. ```c // Example: Select optimal implementation based on available instructions void compute_with_best_path(float* data, size_t size) { cpuinfo_initialize(); if (cpuinfo_has_x86_avx512f()) { // Use AVX-512 optimized implementation avx512_compute(data, size); } else if (cpuinfo_has_x86_avx2() && cpuinfo_has_x86_fma3()) { // Use AVX2+FMA3 optimized implementation avx2_fma_compute(data, size); } else if (cpuinfo_has_x86_avx()) { // Use AVX optimized implementation avx_compute(data, size); } else if (cpuinfo_has_x86_sse4_1()) { // Use SSE4.1 optimized implementation sse41_compute(data, size); } else { // Fallback to scalar implementation scalar_compute(data, size); } } ``` -------------------------------- ### Print Current Core Information Source: https://context7.com/pytorch/cpuinfo/llms.txt Prints the vendor and microarchitecture of the current core. Works on all platforms but may return NULL if not supported. ```c // Get current core information (works on all platforms, but may return NULL) void print_current_core_info(void) { cpuinfo_initialize(); const struct cpuinfo_core* core = cpuinfo_get_current_core(); if (core != NULL) { printf("Current core: vendor=%u, uarch=0x%08x\n", core->vendor, (unsigned)core->uarch); } else { printf("Current core detection not supported\n"); } } ``` -------------------------------- ### Configure cpuinfo helper and debug tools Source: https://github.com/pytorch/cpuinfo/blob/main/CMakeLists.txt Defines build rules for diagnostic tools like isa-info, cpu-info, and cache-info, with platform-specific dumps for ARM and x86. ```cmake IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_TOOLS) ADD_EXECUTABLE(isa-info tools/isa-info.c) CPUINFO_TARGET_ENABLE_C99(isa-info) CPUINFO_TARGET_RUNTIME_LIBRARY(isa-info) TARGET_LINK_LIBRARIES(isa-info PRIVATE cpuinfo) INSTALL(TARGETS isa-info RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ADD_EXECUTABLE(cpu-info tools/cpu-info.c) CPUINFO_TARGET_ENABLE_C99(cpu-info) CPUINFO_TARGET_RUNTIME_LIBRARY(cpu-info) TARGET_LINK_LIBRARIES(cpu-info PRIVATE cpuinfo) INSTALL(TARGETS cpu-info RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ADD_EXECUTABLE(cache-info tools/cache-info.c) CPUINFO_TARGET_ENABLE_C99(cache-info) CPUINFO_TARGET_RUNTIME_LIBRARY(cache-info) TARGET_LINK_LIBRARIES(cache-info PRIVATE cpuinfo) INSTALL(TARGETS cache-info RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) IF(CMAKE_SYSTEM_NAME MATCHES "^(Android|Linux)$" AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(armv[5-8].*|aarch64)$") ADD_EXECUTABLE(auxv-dump tools/auxv-dump.c) CPUINFO_TARGET_ENABLE_C99(auxv-dump) CPUINFO_TARGET_RUNTIME_LIBRARY(auxv-dump) TARGET_LINK_LIBRARIES(auxv-dump PRIVATE ${CMAKE_DL_LIBS} cpuinfo) ADD_EXECUTABLE(cpuinfo-dump tools/cpuinfo-dump.c) CPUINFO_TARGET_ENABLE_C99(cpuinfo-dump) CPUINFO_TARGET_RUNTIME_LIBRARY(cpuinfo-dump) ENDIF() IF(CPUINFO_TARGET_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?)$") ADD_EXECUTABLE(cpuid-dump tools/cpuid-dump.c) CPUINFO_TARGET_ENABLE_C99(cpuid-dump) CPUINFO_TARGET_RUNTIME_LIBRARY(cpuid-dump) TARGET_INCLUDE_DIRECTORIES(cpuid-dump BEFORE PRIVATE src) TARGET_INCLUDE_DIRECTORIES(cpuid-dump BEFORE PRIVATE include) INSTALL(TARGETS cpuid-dump RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ENDIF() ENDIF() ``` -------------------------------- ### Print CPU Cache Hierarchy Source: https://context7.com/pytorch/cpuinfo/llms.txt Initializes cpuinfo and prints details for L1 instruction, L1 data, L2, and L3 caches. Includes cache size, associativity, line size, and inclusivity for L2. Requires including cpuinfo.h and stdio.h. ```c #include #include #include void print_cache_hierarchy(void) { cpuinfo_initialize(); printf("Max cache size (upper bound): %" PRIu32 " bytes\n", cpuinfo_get_max_cache_size()); // L1 Instruction Cache if (cpuinfo_get_l1i_caches_count() > 0) { const struct cpuinfo_cache* l1i = cpuinfo_get_l1i_cache(0); printf("L1i: %" PRIu32 " x %" PRIu32 " KB, %" PRIu32 "-way, " "%" PRIu32 "-byte lines\n", cpuinfo_get_l1i_caches_count(), l1i->size / 1024, l1i->associativity, l1i->line_size); } // L1 Data Cache if (cpuinfo_get_l1d_caches_count() > 0) { const struct cpuinfo_cache* l1d = cpuinfo_get_l1d_cache(0); printf("L1d: %" PRIu32 " x %" PRIu32 " KB, %" PRIu32 "-way, " "%" PRIu32 "-byte lines\n", cpuinfo_get_l1d_caches_count(), l1d->size / 1024, l1d->associativity, l1d->line_size); } // L2 Cache if (cpuinfo_get_l2_caches_count() > 0) { const struct cpuinfo_cache* l2 = cpuinfo_get_l2_cache(0); printf("L2: %" PRIu32 " x %" PRIu32 " KB, %" PRIu32 "-way, " "%" PRIu32 "-byte lines", cpuinfo_get_l2_caches_count(), l2->size / 1024, l2->associativity, l2->line_size); printf(", %s\n", (l2->flags & CPUINFO_CACHE_INCLUSIVE) ? "inclusive" : "exclusive"); } // L3 Cache if (cpuinfo_get_l3_caches_count() > 0) { const struct cpuinfo_cache* l3 = cpuinfo_get_l3_cache(0); printf("L3: %" PRIu32 " x %" PRIu32 " MB, %" PRIu32 "-way, " "shared by %" PRIu32 " processors\n", cpuinfo_get_l3_caches_count(), l3->size / (1024 * 1024), l3->associativity, l3->processor_count); } } ``` -------------------------------- ### Detect All x86 Instruction Sets Source: https://context7.com/pytorch/cpuinfo/llms.txt Initializes CPUInfo and prints the detection status for a comprehensive list of x86 instruction sets. Ensure cpuinfo_initialize() is called before using any detection functions. ```c #include #include void detect_x86_features(void) { cpuinfo_initialize(); // SSE family detection printf("SSE support: %s\n", cpuinfo_has_x86_sse() ? "yes" : "no"); printf("SSE2 support: %s\n", cpuinfo_has_x86_sse2() ? "yes" : "no"); printf("SSE3 support: %s\n", cpuinfo_has_x86_sse3() ? "yes" : "no"); printf("SSSE3 support: %s\n", cpuinfo_has_x86_ssse3() ? "yes" : "no"); printf("SSE4.1 support: %s\n", cpuinfo_has_x86_sse4_1() ? "yes" : "no"); printf("SSE4.2 support: %s\n", cpuinfo_has_x86_sse4_2() ? "yes" : "no"); // AVX family detection printf("AVX support: %s\n", cpuinfo_has_x86_avx() ? "yes" : "no"); printf("AVX2 support: %s\n", cpuinfo_has_x86_avx2() ? "yes" : "no"); printf("FMA3 support: %s\n", cpuinfo_has_x86_fma3() ? "yes" : "no"); printf("F16C support: %s\n", cpuinfo_has_x86_f16c() ? "yes" : "no"); // AVX-512 family detection printf("AVX512F support: %s\n", cpuinfo_has_x86_avx512f() ? "yes" : "no"); printf("AVX512BW support: %s\n", cpuinfo_has_x86_avx512bw() ? "yes" : "no"); printf("AVX512DQ support: %s\n", cpuinfo_has_x86_avx512dq() ? "yes" : "no"); printf("AVX512VL support: %s\n", cpuinfo_has_x86_avx512vl() ? "yes" : "no"); printf("AVX512VNNI support: %s\n", cpuinfo_has_x86_avx512vnni() ? "yes" : "no"); printf("AVX512BF16 support: %s\n", cpuinfo_has_x86_avx512bf16() ? "yes" : "no"); // Intel AMX (Advanced Matrix Extensions) detection printf("AMX-TILE support: %s\n", cpuinfo_has_x86_amx_tile() ? "yes" : "no"); printf("AMX-BF16 support: %s\n", cpuinfo_has_x86_amx_bf16() ? "yes" : "no"); printf("AMX-INT8 support: %s\n", cpuinfo_has_x86_amx_int8() ? "yes" : "no"); printf("AMX-FP16 support: %s\n", cpuinfo_has_x86_amx_fp16() ? "yes" : "no"); // Cryptography extensions printf("AES-NI support: %s\n", cpuinfo_has_x86_aes() ? "yes" : "no"); printf("SHA support: %s\n", cpuinfo_has_x86_sha() ? "yes" : "no"); printf("RDRAND support: %s\n", cpuinfo_has_x86_rdrand() ? "yes" : "no"); // Bit manipulation printf("BMI support: %s\n", cpuinfo_has_x86_bmi() ? "yes" : "no"); printf("BMI2 support: %s\n", cpuinfo_has_x86_bmi2() ? "yes" : "no"); printf("POPCNT support: %s\n", cpuinfo_has_x86_popcnt() ? "yes" : "no"); printf("LZCNT support: %s\n", cpuinfo_has_x86_lzcnt() ? "yes" : "no"); } ``` -------------------------------- ### Integrate with CMake Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Use FindPkgConfig to locate and link the libcpuinfo library. ```cmake cmake_minimum_required(VERSION 3.6) project("MyCpuInfoProject") find_package(PkgConfig) pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PkgConfig::CpuInfo) ``` -------------------------------- ### Integrate with Bazel Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Add the dependency to MODULE.bazel and link it in the BUILD file. ```python # fetch cpuinfo from Bazel Central Registry: https://registry.bazel.build/modules/cpuinfo bazel_dep(name = "cpuinfo", version = "0.0.0-20250925-877328f") # Optional: Override it with some specific commit hash git_override( module_name = "cpuinfo", commit = "" remote = "https://github.com/pytorch/cpuinfo.git", ) ``` ```python cc_binary( name = "cpuinfo_test", srcs = [ # ... ], deps = [ "@cpuinfo", ], ) ``` -------------------------------- ### Integrate with Meson Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Use the dependency function to link libcpuinfo in a Meson project. ```meson project( 'MyCpuInfoProject', 'cpp', meson_version: '>=0.55.0' ) executable( 'MyCpuInfoExecutable', sources: 'main.cpp', dependencies: dependency('libcpuinfo') ) ``` -------------------------------- ### Validate Platform and Architecture Source: https://github.com/pytorch/cpuinfo/blob/main/CMakeLists.txt Checks if the target system and processor architecture are supported by cpuinfo. If validation fails, cpuinfo_initialize() will not function correctly. ```cmake SET(CPUINFO_SUPPORTED_PLATFORM TRUE) IF(NOT CMAKE_SYSTEM_PROCESSOR) IF(NOT IOS) MESSAGE(WARNING "Target processor architecture is not specified. " "cpuinfo will compile, but cpuinfo_initialize() will always fail.") SET(CPUINFO_SUPPORTED_PLATFORM FALSE) ENDIF() ELSEIF(NOT CPUINFO_TARGET_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?|armv[5-8].*|aarch64|arm64.*|ARM64.*|riscv(32|64))$") MESSAGE(WARNING "Target processor architecture \"${CPUINFO_TARGET_PROCESSOR}\" is not supported in cpuinfo. " "cpuinfo will compile, but cpuinfo_initialize() will always fail.") SET(CPUINFO_SUPPORTED_PLATFORM FALSE) ENDIF() IF(NOT CMAKE_SYSTEM_NAME) MESSAGE(WARNING "Target operating system is not specified. " "cpuinfo will compile, but cpuinfo_initialize() will always fail.") SET(CPUINFO_SUPPORTED_PLATFORM FALSE) ELSEIF(NOT CMAKE_SYSTEM_NAME MATCHES "^(Windows|WindowsStore|CYGWIN|MSYS|Darwin|Linux|Android|FreeBSD|Emscripten)$") IF(${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.14" AND NOT IS_APPLE_OS) MESSAGE(WARNING "Target operating system \"${CMAKE_SYSTEM_NAME}\" is not supported in cpuinfo. " "cpuinfo will compile, but cpuinfo_initialize() will always fail.") SET(CPUINFO_SUPPORTED_PLATFORM FALSE) ENDIF() ENDIF() IF(CPUINFO_SUPPORTED_PLATFORM) IF(CPUINFO_BUILD_MOCK_TESTS OR CPUINFO_BUILD_UNIT_TESTS OR CPUINFO_BUILD_BENCHMARKS) ENABLE_LANGUAGE(CXX) ENDIF() ENDIF() ``` -------------------------------- ### Bazel Module Integration Source: https://context7.com/pytorch/cpuinfo/llms.txt Integrates CPUInfo into a Bazel project using the MODULE.bazel file. Allows specifying a version or overriding with a specific commit. ```python # Bazel MODULE.bazel bazel_dep(name = "cpuinfo", version = "0.0.0-20250925-877328f") # Optional: Pin to specific commit git_override( module_name = "cpuinfo", commit = "", remote = "https://github.com/pytorch/cpuinfo.git", ) ``` -------------------------------- ### Bazel Build File Integration Source: https://context7.com/pytorch/cpuinfo/llms.txt Defines a C++ binary target in Bazel that depends on the cpuinfo library. Assumes cpuinfo is correctly declared in WORKSPACE or MODULE.bazel. ```python # Bazel BUILD file cc_binary( name = "my_app", srcs = ["main.cc"], deps = ["@cpuinfo"], ) ``` -------------------------------- ### Configure cpuinfo unit tests Source: https://github.com/pytorch/cpuinfo/blob/main/CMakeLists.txt Defines executable targets and test suites for various platforms and architectures, including Linux, Android, and x86 systems. ```cmake IF(CPUINFO_SUPPORTED_PLATFORM AND CPUINFO_BUILD_UNIT_TESTS) ADD_EXECUTABLE(init-test test/init.cc) CPUINFO_TARGET_ENABLE_CXX11(init-test) CPUINFO_TARGET_RUNTIME_LIBRARY(init-test) TARGET_LINK_LIBRARIES(init-test PRIVATE cpuinfo gtest gtest_main) ADD_TEST(NAME init-test COMMAND init-test) IF(CMAKE_SYSTEM_NAME STREQUAL "Linux" OR CMAKE_SYSTEM_NAME STREQUAL "Android") ADD_EXECUTABLE(get-current-test test/get-current.cc) CPUINFO_TARGET_ENABLE_CXX11(get-current-test) CPUINFO_TARGET_RUNTIME_LIBRARY(get-current-test) TARGET_LINK_LIBRARIES(get-current-test PRIVATE cpuinfo gtest gtest_main) ADD_TEST(NAME get-current-test COMMAND get-current-test) ENDIF() IF(CPUINFO_TARGET_PROCESSOR MATCHES "^(i[3-6]86|AMD64|x86(_64)?)$") ADD_EXECUTABLE(brand-string-test test/name/brand-string.cc) CPUINFO_TARGET_ENABLE_CXX11(brand-string-test) CPUINFO_TARGET_RUNTIME_LIBRARY(brand-string-test) TARGET_LINK_LIBRARIES(brand-string-test PRIVATE cpuinfo_internals gtest gtest_main) ADD_TEST(NAME brand-string-test COMMAND brand-string-test) ENDIF() IF(CMAKE_SYSTEM_NAME STREQUAL "Android" AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(armv[5-8].*|aarch64)$") ADD_LIBRARY(android_properties_interface STATIC test/name/android-properties-interface.c) CPUINFO_TARGET_ENABLE_C99(android_properties_interface) CPUINFO_TARGET_RUNTIME_LIBRARY(android_properties_interface) TARGET_LINK_LIBRARIES(android_properties_interface PRIVATE cpuinfo_internals) ADD_EXECUTABLE(chipset-test test/name/proc-cpuinfo-hardware.cc test/name/ro-product-board.cc test/name/ro-board-platform.cc test/name/ro-mediatek-platform.cc test/name/ro-arch.cc test/name/ro-chipname.cc test/name/android-properties.cc) CPUINFO_TARGET_ENABLE_CXX11(chipset-test) CPUINFO_TARGET_RUNTIME_LIBRARY(chipset-test) TARGET_LINK_LIBRARIES(chipset-test PRIVATE android_properties_interface gtest gtest_main) ADD_TEST(NAME chipset-test COMMAND chipset-test) ADD_EXECUTABLE(cache-test test/arm-cache.cc) CPUINFO_TARGET_ENABLE_CXX11(cache-test) CPUINFO_TARGET_RUNTIME_LIBRARY(cache-test) TARGET_COMPILE_DEFINITIONS(cache-test PRIVATE __STDC_LIMIT_MACROS=1 __STDC_CONSTANT_MACROS=1) TARGET_LINK_LIBRARIES(cache-test PRIVATE cpuinfo_internals gtest gtest_main) ADD_TEST(NAME cache-test COMMAND cache-test) ENDIF() ENDIF() ``` -------------------------------- ### Pin Thread to L2 Cache Sharing Cores (Linux/Android) Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library, identifies cores sharing L2 cache with the current core, and sets the thread affinity using pthread functions. Requires Linux or Android. ```c cpuinfo_initialize(); cpu_set_t cpu_set; CPU_ZERO(&cpu_set); const struct cpuinfo_cache* current_l2 = cpuinfo_get_current_processor()->cache.l2; for (uint32_t i = 0; i < current_l2->processor_count; i++) { CPU_SET(cpuinfo_get_processor(current_l2->processor_start + i)->linux_id, &cpu_set); } pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set); ``` -------------------------------- ### Define and Use Module Logging in C Source: https://github.com/pytorch/cpuinfo/blob/main/deps/clog/README.md Define logging macros for a specific module and use them to log messages at different levels. Ensure the module's log level is defined before including clog.h. ```c #include #ifndef MYMODULE_LOG_LEVEL #define MYMODULE_LOG_LEVEL CLOG_DEBUG #endif CLOG_DEFINE_LOG_DEBUG(mymodule_, "My Module", MYMODULE_LOG_LEVEL); CLOG_DEFINE_LOG_INFO(mymodule_, "My Module", MYMODULE_LOG_LEVEL); CLOG_DEFINE_LOG_WARNING(mymodule_, "My Module", MYMODULE_LOG_LEVEL); CLOG_DEFINE_LOG_ERROR(mymodule_, "My Module", MYMODULE_LOG_LEVEL); ... void some_function(...) { int status = ... if (status != 0) { mymodule_log_error( "something really bad happened: " "operation failed with status %d", status); } uint32_t expected_zero = ... if (expected_zero != 0) { mymodule_log_warning( "something suspicious happened (var = %"PRIu32"), " "fall back to generic implementation", expected_zero); } void* usually_non_null = ... if (usually_non_null == NULL) { mymodule_log_info( "something unusual, but common, happened: " "enabling work-around"); } float a = ... mymodule_log_debug("computed a = %.7f", a); } ``` -------------------------------- ### CMake Integration with pkg-config Source: https://context7.com/pytorch/cpuinfo/llms.txt Integrates CPUInfo into a CMake project using pkg-config to find and link the library. Requires CMake version 3.6 or higher. ```cmake # CMake integration using pkg-config cmake_minimum_required(VERSION 3.6) project("MyCpuInfoProject") find_package(PkgConfig) pkg_check_modules(CpuInfo REQUIRED IMPORTED_TARGET libcpuinfo) add_executable(${PROJECT_NAME} main.cpp) target_link_libraries(${PROJECT_NAME} PkgConfig::CpuInfo) ``` -------------------------------- ### Check for Cortex-A53 Core Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Initializes the library and checks if the current thread is running on a Cortex-A53 core using a switch statement. ```c cpuinfo_initialize(); switch (cpuinfo_get_current_core()->uarch) { case cpuinfo_uarch_cortex_a53: cortex_a53_implementation(arguments); break; default: generic_implementation(arguments); break; } ``` -------------------------------- ### Detect ARM Architecture Source: https://github.com/pytorch/cpuinfo/blob/main/README.md Checks if the target system is a 32-bit or 64-bit ARM system using preprocessor directives. ```c #if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 /* 32-bit ARM-specific code here */ #endif ``` -------------------------------- ### Access Cache Levels for a Specific Processor Source: https://context7.com/pytorch/cpuinfo/llms.txt Prints the sizes of L1 instruction, L1 data, L2, and L3 caches for a given processor index. Initializes cpuinfo and checks for the existence of each cache level before printing. Returns early if the processor index is invalid. ```c // Access all cache levels for a specific processor void print_processor_caches(uint32_t proc_index) { cpuinfo_initialize(); const struct cpuinfo_processor* proc = cpuinfo_get_processor(proc_index); if (proc == NULL) return; if (proc->cache.l1i) { printf(" L1i: %" PRIu32 " bytes\n", proc->cache.l1i->size); } if (proc->cache.l1d) { printf(" L1d: %" PRIu32 " bytes\n", proc->cache.l1d->size); } if (proc->cache.l2) { printf(" L2: %" PRIu32 " bytes\n", proc->cache.l2->size); } if (proc->cache.l3) { printf(" L3: %" PRIu32 " bytes\n", proc->cache.l3->size); } } ``` -------------------------------- ### Pin Thread to L2 Sharing Cores (Linux) Source: https://context7.com/pytorch/cpuinfo/llms.txt Pins the current thread to cores that share an L2 cache with the current core. Requires Linux/Android and cpuinfo initialization. ```c #include #include #if defined(__linux__) #include #include // Pin current thread to cores sharing L2 cache with current core void pin_to_l2_sharing_cores(void) { cpuinfo_initialize(); // Get current processor's L2 cache const struct cpuinfo_processor* current = cpuinfo_get_current_processor(); if (current == NULL || current->cache.l2 == NULL) { return; // Cannot determine current processor } const struct cpuinfo_cache* l2 = current->cache.l2; // Build CPU set with all processors sharing this L2 cpu_set_t cpu_set; CPU_ZERO(&cpu_set); for (uint32_t i = 0; i < l2->processor_count; i++) { const struct cpuinfo_processor* proc = cpuinfo_get_processor(l2->processor_start + i); if (proc != NULL) { CPU_SET(proc->linux_id, &cpu_set); } } // Apply affinity mask pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpu_set); } #endif ``` -------------------------------- ### Detect ARM Instruction Set Extensions Source: https://context7.com/pytorch/cpuinfo/llms.txt Use this function to detect various ARM instruction set extensions like NEON, SVE, SME, and cryptography features. Ensure cpuinfo is initialized before calling. ```c #include #include void detect_arm_features(void) { cpuinfo_initialize(); // ARM NEON SIMD detection printf("NEON support: %s\n", cpuinfo_has_arm_neon() ? "yes" : "no"); printf("NEON-FP16 support: %s\n", cpuinfo_has_arm_neon_fp16() ? "yes" : "no"); printf("NEON-FMA support: %s\n", cpuinfo_has_arm_neon_fma() ? "yes" : "no"); printf("NEON dot product: %s\n", cpuinfo_has_arm_neon_dot() ? "yes" : "no"); printf("FP16 arithmetic: %s\n", cpuinfo_has_arm_fp16_arith() ? "yes" : "no"); printf("BF16 support: %s\n", cpuinfo_has_arm_bf16() ? "yes" : "no"); printf("I8MM support: %s\n", cpuinfo_has_arm_i8mm() ? "yes" : "no"); // ARMv8.1+ atomic operations printf("Atomics (LSE): %s\n", cpuinfo_has_arm_atomics() ? "yes" : "no"); // ARM SVE (Scalable Vector Extension) detection printf("SVE support: %s\n", cpuinfo_has_arm_sve() ? "yes" : "no"); printf("SVE2 support: %s\n", cpuinfo_has_arm_sve2() ? "yes" : "no"); if (cpuinfo_has_arm_sve()) { printf("SVE max vector length: %u bits\n", cpuinfo_get_max_arm_sve_length()); } // ARM SME (Scalable Matrix Extension) detection printf("SME support: %s\n", cpuinfo_has_arm_sme() ? "yes" : "no"); printf("SME2 support: %s\n", cpuinfo_has_arm_sme2() ? "yes" : "no"); if (cpuinfo_has_arm_sme()) { printf("SME max vector length: %u bits\n", cpuinfo_get_max_arm_sme_length()); } // Cryptography extensions printf("AES support: %s\n", cpuinfo_has_arm_aes() ? "yes" : "no"); printf("SHA1 support: %s\n", cpuinfo_has_arm_sha1() ? "yes" : "no"); printf("SHA2 support: %s\n", cpuinfo_has_arm_sha2() ? "yes" : "no"); printf("PMULL support: %s\n", cpuinfo_has_arm_pmull() ? "yes" : "no"); printf("CRC32 support: %s\n", cpuinfo_has_arm_crc32() ? "yes" : "no"); } ``` -------------------------------- ### Detect RISC-V Instruction Set Extensions Source: https://context7.com/pytorch/cpuinfo/llms.txt Detects RISC-V base extensions (I, M, A, F, D, C, G) and the Vector extension (V). Requires cpuinfo initialization. ```c #include #include void detect_riscv_features(void) { cpuinfo_initialize(); // RISC-V base and standard extensions printf("Base Integer (I): %s\n", cpuinfo_has_riscv_i() ? "yes" : "no"); printf("Integer Multiply/Divide (M): %s\n", cpuinfo_has_riscv_m() ? "yes" : "no"); printf("Atomics (A): %s\n", cpuinfo_has_riscv_a() ? "yes" : "no"); printf("Single-Precision FP (F): %s\n", cpuinfo_has_riscv_f() ? "yes" : "no"); printf("Double-Precision FP (D): %s\n", cpuinfo_has_riscv_d() ? "yes" : "no"); printf("Compressed (C): %s\n", cpuinfo_has_riscv_c() ? "yes" : "no"); printf("General (G = IMAFD): %s\n", cpuinfo_has_riscv_g() ? "yes" : "no"); // RISC-V Vector extension printf("Vector (V): %s\n", cpuinfo_has_riscv_v() ? "yes" : "no"); printf("Half-Precision FP (Zfh): %s\n", cpuinfo_has_riscv_zfh() ? "yes" : "no"); printf("Vector Half-Precision (Zvfh): %s\n", cpuinfo_has_riscv_zvfh() ? "yes" : "no"); } ``` -------------------------------- ### Detect Target Architecture with Compile-Time Macros Source: https://context7.com/pytorch/cpuinfo/llms.txt Uses compile-time macros provided by CPUInfo to determine the target architecture. This allows for conditional compilation of architecture-specific code paths. ```c #include #include void print_architecture_info(void) { // Compile-time architecture detection macros #if CPUINFO_ARCH_X86 printf("Target: 32-bit x86\n"); #endif #if CPUINFO_ARCH_X86_64 printf("Target: 64-bit x86-64\n"); #endif #if CPUINFO_ARCH_ARM printf("Target: 32-bit ARM\n"); #endif #if CPUINFO_ARCH_ARM64 printf("Target: 64-bit ARM64/AArch64\n"); #endif #if CPUINFO_ARCH_RISCV32 printf("Target: 32-bit RISC-V\n"); #endif #if CPUINFO_ARCH_RISCV64 printf("Target: 64-bit RISC-V\n"); #endif // Combined check for any ARM architecture #if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64 printf("Running on ARM-based system\n"); #endif // Combined check for any x86 architecture #if CPUINFO_ARCH_X86 || CPUINFO_ARCH_X86_64 printf("Running on x86-based system\n"); #endif } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.