From 80199bc96c9c913300c6692955939ff4d6ad7fd3 Mon Sep 17 00:00:00 2001 From: baominghelly <1508269885@qq.com> Date: Fri, 22 May 2026 09:38:22 +0000 Subject: [PATCH] feat: Add metax build for cuda script --- .../cuda-memory-benchmark/CMakeLists.txt | 70 ++++++++------ .../hardware/cuda-memory-benchmark/build.sh | 96 +++++++++++++++---- infinimetrics/hardware/hardware_adapter.py | 21 +++- 3 files changed, 137 insertions(+), 50 deletions(-) diff --git a/infinimetrics/hardware/cuda-memory-benchmark/CMakeLists.txt b/infinimetrics/hardware/cuda-memory-benchmark/CMakeLists.txt index 1326088c..ae4703c1 100644 --- a/infinimetrics/hardware/cuda-memory-benchmark/CMakeLists.txt +++ b/infinimetrics/hardware/cuda-memory-benchmark/CMakeLists.txt @@ -1,31 +1,29 @@ cmake_minimum_required(VERSION 3.18) -project(CudaPerfSuite VERSION 1.0.0 LANGUAGES CXX CUDA) + +# Platform detection +if(NOT DEFINED PLATFORM) + message(FATAL_ERROR "PLATFORM is not set. Use -DPLATFORM=cuda or -DPLATFORM=metax") +endif() + +if(PLATFORM STREQUAL "cuda") + project(CudaPerfSuite LANGUAGES CXX CUDA VERSION 1.0.0) +elseif(PLATFORM STREQUAL "metax") + project(CudaPerfSuite LANGUAGES CXX VERSION 1.0.0) +else() + message(FATAL_ERROR "Unsupported PLATFORM: ${PLATFORM}. Use 'cuda' or 'metax'") +endif() # Set C++ standard set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CUDA_STANDARD 17) -set(CMAKE_CUDA_STANDARD_REQUIRED ON) - -# Set CUDA architecture (adjust based on your GPU) -set(CMAKE_CUDA_ARCHITECTURES "80;86;89;90" CACHE STRING "CUDA architectures") # Build type if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -# Compiler flags -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -Wall") -set(CMAKE_CUDA_FLAGS_RELEASE "${CMAKE_CUDA_FLAGS_RELEASE} -O3 -lineinfo") -set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Wno-deprecated-gpu-targets") - -# Find CUDA -find_package(CUDAToolkit REQUIRED) - # Include directories -include_directories(${CUDA_INCLUDE_DIRS} - ${CMAKE_CURRENT_SOURCE_DIR}/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) # Source files set(CUDA_SOURCES @@ -36,29 +34,43 @@ set(CXX_SOURCES # Add any .cpp files here if needed ) -# Create executable -add_executable(cuda_perf_suite - ${CUDA_SOURCES} - ${CXX_SOURCES} -) +# ---- Platform-specific settings ---- + +if(PLATFORM STREQUAL "cuda") + set(CMAKE_CUDA_STANDARD 17) + set(CMAKE_CUDA_STANDARD_REQUIRED ON) + set(CMAKE_CUDA_ARCHITECTURES "80;86;89;90" CACHE STRING "CUDA architectures") + set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler -Wall") + set(CMAKE_CUDA_FLAGS_RELEASE "${CMAKE_CUDA_FLAGS_RELEASE} -O3 -lineinfo") + + add_executable(cuda_perf_suite ${CUDA_SOURCES} ${CXX_SOURCES}) + +elseif(PLATFORM STREQUAL "metax") + # Use cu-bridge compiler + set(CMAKE_CXX_COMPILER "$ENV{CUDA_PATH}/bin/nvcc") + set(CMAKE_C_COMPILER "$ENV{CUDA_PATH}/bin/nvcc") + + # Treat .cu as CXX for cu-bridge + set_source_files_properties(src/main.cu PROPERTIES LANGUAGE CXX) + + add_executable(cuda_perf_suite ${CUDA_SOURCES} ${CXX_SOURCES}) +endif() # Link libraries -target_link_libraries(cuda_perf_suite - ${CUDA_LIBRARIES} - ${CMAKE_THREADS_LIB_INIT} -) +target_link_libraries(cuda_perf_suite ${CMAKE_THREADS_LIB_INIT}) # Installation -install(TARGETS cuda_perf_suite - RUNTIME DESTINATION bin -) +install(TARGETS cuda_perf_suite RUNTIME DESTINATION bin) # Print configuration message(STATUS "") message(STATUS "Configuration Summary:") message(STATUS " Project: ${PROJECT_NAME}") message(STATUS " Version: ${PROJECT_VERSION}") +message(STATUS " Platform: ${PLATFORM}") message(STATUS " Build type: ${CMAKE_BUILD_TYPE}") message(STATUS " C++ standard: ${CMAKE_CXX_STANDARD}") -message(STATUS " CUDA architectures: ${CMAKE_CUDA_ARCHITECTURES}") +if(PLATFORM STREQUAL "cuda") + message(STATUS " CUDA architectures: ${CMAKE_CUDA_ARCHITECTURES}") +endif() message(STATUS "") diff --git a/infinimetrics/hardware/cuda-memory-benchmark/build.sh b/infinimetrics/hardware/cuda-memory-benchmark/build.sh index 37a9ff3a..fedce824 100644 --- a/infinimetrics/hardware/cuda-memory-benchmark/build.sh +++ b/infinimetrics/hardware/cuda-memory-benchmark/build.sh @@ -1,6 +1,9 @@ #!/bin/bash # Build script for CUDA Performance Suite +# Usage: +# bash build.sh --platform cuda # Build with native CUDA (NVIDIA GPU) +# bash build.sh --platform metax # Build with cu-bridge (MetaX GPU) set -e # Exit on error @@ -10,34 +13,92 @@ GREEN='\033[0;32m' YELLOW='\033[1;33m' NC='\033[0m' # No Color +# Parse arguments +PLATFORM="" +while [[ $# -gt 0 ]]; do + case $1 in + --platform) + PLATFORM="$2" + shift 2 + ;; + *) + echo -e "${RED}Unknown option: $1${NC}" + echo "Usage: bash build.sh --platform " + exit 1 + ;; + esac +done + +if [[ -z "$PLATFORM" ]]; then + echo -e "${RED}ERROR: --platform is required.${NC}" + echo "Usage: bash build.sh --platform " + exit 1 +fi + echo "==========================================" echo " CUDA Performance Suite - Build Script" +echo " Platform: ${PLATFORM}" echo "==========================================" echo "" -# Check if CUDA is available -if ! command -v nvcc &> /dev/null; then - echo -e "${RED}ERROR: nvcc not found. Please install CUDA toolkit.${NC}" - exit 1 -fi +if [[ "$PLATFORM" == "metax" ]]; then + # ---- MetaX platform: using cu-bridge ---- + + export MACA_PATH=${MACA_PATH:-/opt/maca} + export CUCC_PATH=${CUCC_PATH:-${MACA_PATH}/tools/cu-bridge} + export PATH=$PATH:${CUCC_PATH}/tools:${CUCC_PATH}/bin + export CUCC_CMAKE_ENTRY=2 + export CUDA_PATH=${CUCC_PATH} + + # Create nvcc symlink (if it doesn't exist) + if [ ! -e ${CUCC_PATH}/bin/nvcc ]; then + ln -s ${CUCC_PATH}/bin/cucc ${CUCC_PATH}/bin/nvcc + fi + + if ! command -v cucc &> /dev/null; then + echo -e "${RED}ERROR: cucc not found. Please check cu-bridge installation at ${CUCC_PATH}${NC}" + exit 1 + fi + + echo -e "${YELLOW}[MetaX] Using cu-bridge: ${CUCC_PATH}${NC}" + + mkdir -p build + cd build -# Create build directory -echo -e "${YELLOW}Creating build directory...${NC}" -mkdir -p build -cd build + echo -e "${YELLOW}Configuring with cmake_maca...${NC}" + cmake_maca .. -DCMAKE_BUILD_TYPE=Release -DPLATFORM=metax -# Configure with CMake -echo -e "${YELLOW}Configuring with CMake...${NC}" -cmake .. -DCMAKE_BUILD_TYPE=Release + echo -e "${YELLOW}Building with make_maca...${NC}" + make_maca -j$(nproc) -# Build -echo -e "${YELLOW}Building...${NC}" -make -j$(nproc) +elif [[ "$PLATFORM" == "cuda" ]]; then + # ---- NVIDIA CUDA platform ---- + + if ! command -v nvcc &> /dev/null; then + echo -e "${RED}ERROR: nvcc not found. Please install CUDA toolkit.${NC}" + exit 1 + fi + + echo -e "${YELLOW}[CUDA] Using native nvcc: $(which nvcc)${NC}" + + mkdir -p build + cd build + + echo -e "${YELLOW}Configuring with CMake...${NC}" + cmake .. -DCMAKE_BUILD_TYPE=Release -DPLATFORM=cuda + + echo -e "${YELLOW}Building...${NC}" + make -j$(nproc) + +else + echo -e "${RED}ERROR: Unsupported platform '${PLATFORM}'. Use 'cuda' or 'metax'.${NC}" + exit 1 +fi # Check if build was successful if [ $? -eq 0 ]; then echo "" - echo -e "${GREEN}✓ Build completed successfully!${NC}" + echo -e "${GREEN}Build completed successfully!${NC}" echo "" echo "Executable: build/cuda_perf_suite" echo "" @@ -50,8 +111,7 @@ if [ $? -eq 0 ]; then echo "" else echo "" - echo -e "${RED}✗ Build failed!${NC}" + echo -e "${RED}Build failed!${NC}" echo "Please check the error messages above." exit 1 fi - diff --git a/infinimetrics/hardware/hardware_adapter.py b/infinimetrics/hardware/hardware_adapter.py index 426c60a7..e1392df9 100644 --- a/infinimetrics/hardware/hardware_adapter.py +++ b/infinimetrics/hardware/hardware_adapter.py @@ -4,6 +4,7 @@ import logging import subprocess import re +import shutil from pathlib import Path from typing import Any, Dict, Optional, List @@ -29,6 +30,15 @@ logger = logging.getLogger(__name__) +def detect_platform() -> str: + """Detect GPU platform: 'metax' if MACA/cu-bridge is available, else 'cuda'.""" + maca_path = Path("/opt/maca") + cucc_path = maca_path / "tools" / "cu-bridge" / "bin" / "cucc" + if cucc_path.exists() or shutil.which("cucc") or shutil.which("mxcc"): + return "metax" + return "cuda" + + class HardwareTestAdapter(BaseAdapter): """Adapter for CUDA Unified hardware performance tests.""" @@ -126,10 +136,13 @@ def _build_cuda_project(self) -> None: ) if not self.build_script.exists(): raise FileNotFoundError(f"Build script not found: {self.build_script}") - logger.info("Building CUDA project in: %s", self.build_dir) + platform = detect_platform() + logger.info( + "Building CUDA project in: %s (platform: %s)", self.build_dir, platform + ) try: result = subprocess.run( - ["bash", str(self.build_script)], + ["bash", str(self.build_script), "--platform", platform], cwd=str(self.build_dir), capture_output=True, text=True, @@ -137,7 +150,9 @@ def _build_cuda_project(self) -> None: ) if result.returncode != 0: raise RuntimeError(f"Failed to build CUDA project:\n{result.stderr}") - logger.info("CUDA project build completed successfully") + logger.info( + "CUDA project build completed successfully (platform: %s)", platform + ) except subprocess.TimeoutExpired: raise RuntimeError("CUDA project build timed out after 5 minutes")