lalmodel-code / src /build.ps1
gasschina's picture
upload build.ps1 (commit 9ef903f)
6a34ddd verified
Raw
History Blame Contribute Delete
3.34 kB
# build.ps1 - Windows build script
#
# On Windows (MSYS2/MinGW) usually only gcc is available - no make, no bash -
# so the Makefile cannot run here. Use this script on Windows and the Makefile
# on Linux/macOS. Compiler flags MUST stay identical between the two.
#
# NOTE: keep this file pure ASCII. Windows PowerShell 5.1 reads .ps1 as the
# system ANSI codepage (GBK on zh-CN), so non-ASCII characters here get
# mis-decoded and break string parsing.
#
# Usage:
# .\build.ps1 build -> ste_train.exe
# .\build.ps1 -Run build, then start training (all defaults hardcoded)
# .\build.ps1 -Clean remove build output
#
# -fopenmp is REQUIRED: ste_train.c calls omp_get_max_threads().
param(
[switch]$Run,
[switch]$Clean
)
$ErrorActionPreference = 'Stop'
$root = $PSScriptRoot
Set-Location $root
$out = Join-Path $root 'ste_train.exe'
if ($Clean) {
Remove-Item $out -Force -ErrorAction SilentlyContinue
Write-Host "cleaned $out"
exit 0
}
$gcc = Get-Command gcc -ErrorAction SilentlyContinue
if (-not $gcc) {
Write-Host "ERROR: gcc not found. Install MSYS2 and add mingw64\bin to PATH." -ForegroundColor Red
Write-Host " https://www.msys2.org/" -ForegroundColor Red
exit 1
}
# Must match CFLAGS in the Makefile
$cflags = @('-O1', '-march=native', '-fno-strict-aliasing', '-fopenmp', '-funroll-loops', '-Wall', '-Wno-unused-function', '-Wno-unused-variable')
$src = @('models/ste_train.c', 'runtime/lal_runtime.c')
$ldlibs = @('-lm')
# [加速] OpenBLAS 检测: 有则定义 HAVE_OPENBLAS + 链接 -lopenblas (CORE 路径用 cblas_sgemm)
# MSYS2 安装: pacman -S mingw-w64-x86_64-openblas
# 注意: Windows MSYS2 下 OpenBLAS 与 OpenMP 链接有冲突 (segfault), 默认关闭.
# 设环境变量 LAL_USE_OPENBLAS=1 强制开启 (Linux/WSL 下推荐).
# cblas.h 在 mingw64/include/openblas/ 子目录, 需要显式 -I
$useOpenBLAS = $env:LAL_USE_OPENBLAS -eq '1'
if ($useOpenBLAS) {
$openblasInc = "C:\msys64\mingw64\include\openblas"
$openblasHeader = @'
#include <cblas.h>
int main(){ return 0; }
'@
$tmpC = Join-Path $env:TEMP "test_openblas.c"
$tmpExe = Join-Path $env:TEMP "test_openblas.exe"
Set-Content -Path $tmpC -Value $openblasHeader -Encoding ascii
$incFlags = @()
if (Test-Path $openblasInc) { $incFlags += "-I$openblasInc" }
& gcc @incFlags $tmpC -o $tmpExe -lopenblas 2>$null
if ($LASTEXITCODE -eq 0) {
$cflags += '-DHAVE_OPENBLAS'
$cflags += $incFlags
$ldlibs += '-lopenblas'
Write-Host "[*] OpenBLAS detected — CORE matmul will use cblas_sgemv (2-3x faster)" -ForegroundColor Cyan
Remove-Item $tmpExe -ErrorAction SilentlyContinue
} else {
Write-Host "[*] OpenBLAS not found — using OpenMP fallback" -ForegroundColor Yellow
}
Remove-Item $tmpC -ErrorAction SilentlyContinue
} else {
Write-Host "[*] OpenBLAS disabled (set LAL_USE_OPENBLAS=1 to enable) — using OpenMP fallback" -ForegroundColor Yellow
}
Write-Host "compiling -> ste_train.exe ..."
& gcc @cflags -o $out @src @ldlibs
if ($LASTEXITCODE -ne 0) {
Write-Host "BUILD FAILED" -ForegroundColor Red
exit $LASTEXITCODE
}
Write-Host "[*] built ste_train.exe - just run .\ste_train.exe, no arguments needed" -ForegroundColor Green
if ($Run) {
Write-Host ""
& $out
}