|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import os |
| 3 | +import sys |
| 4 | +import shutil |
| 5 | +import subprocess |
| 6 | +import urllib.request |
| 7 | +import ssl |
| 8 | + |
| 9 | +def main(): |
| 10 | + # Set variables |
| 11 | + MAGMA_VERSION = "2.5.4" |
| 12 | + |
| 13 | + # Get environment variables |
| 14 | + CUDA_VERSION = os.environ.get('CUDA_VERSION', '') |
| 15 | + CONFIG = os.environ.get('CONFIG', '') |
| 16 | + NUMBER_OF_PROCESSORS = os.environ.get('NUMBER_OF_PROCESSORS', '1') |
| 17 | + |
| 18 | + if not CUDA_VERSION or not CONFIG: |
| 19 | + print("Error: CUDA_VERSION and CONFIG environment variables must be set") |
| 20 | + sys.exit(1) |
| 21 | + |
| 22 | + CUVER_NODOT = CUDA_VERSION |
| 23 | + CUVER = f"{CUVER_NODOT[:-1]}.{CUVER_NODOT[-1]}" |
| 24 | + |
| 25 | + # Convert config to lowercase |
| 26 | + CONFIG_LOWERCASE = CONFIG.replace('D', 'd').replace('R', 'r').replace('M', 'm') |
| 27 | + |
| 28 | + print(f"Building for configuration: {CONFIG_LOWERCASE}, {CUVER}") |
| 29 | + |
| 30 | + # Download Ninja |
| 31 | + print("Downloading Ninja...") |
| 32 | + ninja_url = "https://s3.amazonaws.com/ossci-windows/ninja_1.8.2.exe" |
| 33 | + ninja_path = r"C:\Tools\ninja.exe" |
| 34 | + |
| 35 | + # Create Tools directory if it doesn't exist |
| 36 | + os.makedirs(r"C:\Tools", exist_ok=True) |
| 37 | + |
| 38 | + # Download with SSL verification disabled (equivalent to curl -k) |
| 39 | + ssl_context = ssl.create_default_context() |
| 40 | + ssl_context.check_hostname = False |
| 41 | + ssl_context.verify_mode = ssl.CERT_NONE |
| 42 | + |
| 43 | + try: |
| 44 | + with urllib.request.urlopen(ninja_url, context=ssl_context) as response: |
| 45 | + with open(ninja_path, 'wb') as out_file: |
| 46 | + out_file.write(response.read()) |
| 47 | + except Exception as e: |
| 48 | + print(f"Error downloading Ninja: {e}") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + # Set environment variables |
| 52 | + cuda_base = rf"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v{CUVER}" |
| 53 | + os.environ['PATH'] = rf"C:\Tools;{cuda_base}\bin;{cuda_base}\libnvvp;" + os.environ['PATH'] |
| 54 | + os.environ['CUDA_PATH'] = cuda_base |
| 55 | + os.environ['NVTOOLSEXT_PATH'] = r"C:\Program Files\NVIDIA Corporation\NvToolsExt" |
| 56 | + |
| 57 | + # Create and navigate to directory |
| 58 | + magma_dir = f"magma_cuda{CUVER_NODOT}" |
| 59 | + os.makedirs(magma_dir, exist_ok=True) |
| 60 | + os.chdir(magma_dir) |
| 61 | + |
| 62 | + # Clone or clean magma repository |
| 63 | + if not os.path.exists("magma"): |
| 64 | + print("Cloning magma repository...") |
| 65 | + result = subprocess.run( |
| 66 | + ["git", "clone", "https://github.com/ptrblck/magma_win.git", "magma"], |
| 67 | + capture_output=True |
| 68 | + ) |
| 69 | + if result.returncode != 0: |
| 70 | + print(f"Error cloning repository: {result.stderr.decode()}") |
| 71 | + sys.exit(1) |
| 72 | + else: |
| 73 | + print("Cleaning existing build directories...") |
| 74 | + shutil.rmtree("magma/build", ignore_errors=True) |
| 75 | + shutil.rmtree("magma/install", ignore_errors=True) |
| 76 | + |
| 77 | + # Navigate to build directory |
| 78 | + os.chdir("magma") |
| 79 | + os.makedirs("build", exist_ok=True) |
| 80 | + os.chdir("build") |
| 81 | + |
| 82 | + # Set GPU target and CUDA architecture list |
| 83 | + GPU_TARGET = "All" |
| 84 | + CUDA_ARCH_LIST = "" |
| 85 | + |
| 86 | + if CUVER_NODOT == "128": |
| 87 | + CUDA_ARCH_LIST = "-gencode arch=compute_50,code=sm_50 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_90,code=sm_90 -gencode arch=compute_100,code=sm_100 -gencode arch=compute_120,code=sm_120" |
| 88 | + elif CUVER_NODOT[:2] == "12" and CUVER_NODOT != "128": |
| 89 | + CUDA_ARCH_LIST = "-gencode arch=compute_50,code=sm_50 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_90,code=sm_90" |
| 90 | + elif CUVER_NODOT == "118": |
| 91 | + CUDA_ARCH_LIST = "-gencode arch=compute_37,code=sm_37 -gencode arch=compute_50,code=sm_50 -gencode arch=compute_60,code=sm_60 -gencode arch=compute_70,code=sm_70 -gencode arch=compute_80,code=sm_80 -gencode arch=compute_86,code=sm_86 -gencode arch=compute_90,code=sm_90" |
| 92 | + |
| 93 | + # Set compiler environment variables |
| 94 | + os.environ['CC'] = "cl.exe" |
| 95 | + os.environ['CXX'] = "cl.exe" |
| 96 | + |
| 97 | + # Run cmake configure |
| 98 | + print("Configuring with cmake...") |
| 99 | + cmake_cmd = [ |
| 100 | + "cmake", "..", |
| 101 | + f"-DGPU_TARGET={GPU_TARGET}", |
| 102 | + "-DUSE_FORTRAN=0", |
| 103 | + "-DCMAKE_CXX_FLAGS=/FS /Zf", |
| 104 | + f"-DCMAKE_BUILD_TYPE={CONFIG}", |
| 105 | + "-DCMAKE_GENERATOR=Ninja", |
| 106 | + "-DCMAKE_INSTALL_PREFIX=..\\install\\", |
| 107 | + f"-DCUDA_ARCH_LIST={CUDA_ARCH_LIST}", |
| 108 | + "-DCMAKE_POLICY_VERSION_MINIMUM=3.5" |
| 109 | + ] |
| 110 | + |
| 111 | + result = subprocess.run(cmake_cmd, capture_output=True, text=True) |
| 112 | + if result.returncode != 0: |
| 113 | + print(f"CMake configure failed: {result.stderr}") |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + # Build and install |
| 117 | + print("Building and installing...") |
| 118 | + build_cmd = [ |
| 119 | + "cmake", "--build", ".", "--target", "install", |
| 120 | + "--config", CONFIG, "--", f"-j{NUMBER_OF_PROCESSORS}" |
| 121 | + ] |
| 122 | + |
| 123 | + result = subprocess.run(build_cmd, capture_output=True, text=True) |
| 124 | + if result.returncode != 0: |
| 125 | + print(f"Build failed: {result.stderr}") |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + # Navigate back to original directory |
| 129 | + os.chdir("../../..") |
| 130 | + |
| 131 | + # Create archive |
| 132 | + print("Creating archive...") |
| 133 | + archive_name = f"magma_{MAGMA_VERSION}_cuda{CUVER_NODOT}_{CONFIG_LOWERCASE}.7z" |
| 134 | + source_path = os.path.join(os.getcwd(), f"magma_cuda{CUVER_NODOT}", "magma", "install", "*") |
| 135 | + |
| 136 | + archive_cmd = ["7z", "a", archive_name, source_path] |
| 137 | + subprocess.run(archive_cmd) |
| 138 | + |
| 139 | + # Clean up |
| 140 | + print("Cleaning up...") |
| 141 | + shutil.rmtree(f"magma_cuda{CUVER_NODOT}", ignore_errors=True) |
| 142 | + |
| 143 | + print("Build completed successfully!") |
| 144 | + |
| 145 | +if __name__ == "__main__": |
| 146 | + main() |
0 commit comments