gasschina commited on
Commit
6a34ddd
·
verified ·
1 Parent(s): 16beaf4

upload build.ps1 (commit 9ef903f)

Browse files
Files changed (1) hide show
  1. src/build.ps1 +90 -0
src/build.ps1 ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # build.ps1 - Windows build script
2
+ #
3
+ # On Windows (MSYS2/MinGW) usually only gcc is available - no make, no bash -
4
+ # so the Makefile cannot run here. Use this script on Windows and the Makefile
5
+ # on Linux/macOS. Compiler flags MUST stay identical between the two.
6
+ #
7
+ # NOTE: keep this file pure ASCII. Windows PowerShell 5.1 reads .ps1 as the
8
+ # system ANSI codepage (GBK on zh-CN), so non-ASCII characters here get
9
+ # mis-decoded and break string parsing.
10
+ #
11
+ # Usage:
12
+ # .\build.ps1 build -> ste_train.exe
13
+ # .\build.ps1 -Run build, then start training (all defaults hardcoded)
14
+ # .\build.ps1 -Clean remove build output
15
+ #
16
+ # -fopenmp is REQUIRED: ste_train.c calls omp_get_max_threads().
17
+
18
+ param(
19
+ [switch]$Run,
20
+ [switch]$Clean
21
+ )
22
+
23
+ $ErrorActionPreference = 'Stop'
24
+ $root = $PSScriptRoot
25
+ Set-Location $root
26
+
27
+ $out = Join-Path $root 'ste_train.exe'
28
+
29
+ if ($Clean) {
30
+ Remove-Item $out -Force -ErrorAction SilentlyContinue
31
+ Write-Host "cleaned $out"
32
+ exit 0
33
+ }
34
+
35
+ $gcc = Get-Command gcc -ErrorAction SilentlyContinue
36
+ if (-not $gcc) {
37
+ Write-Host "ERROR: gcc not found. Install MSYS2 and add mingw64\bin to PATH." -ForegroundColor Red
38
+ Write-Host " https://www.msys2.org/" -ForegroundColor Red
39
+ exit 1
40
+ }
41
+
42
+ # Must match CFLAGS in the Makefile
43
+ $cflags = @('-O1', '-march=native', '-fno-strict-aliasing', '-fopenmp', '-funroll-loops', '-Wall', '-Wno-unused-function', '-Wno-unused-variable')
44
+ $src = @('models/ste_train.c', 'runtime/lal_runtime.c')
45
+ $ldlibs = @('-lm')
46
+
47
+ # [加速] OpenBLAS 检测: 有则定义 HAVE_OPENBLAS + 链接 -lopenblas (CORE 路径用 cblas_sgemm)
48
+ # MSYS2 安装: pacman -S mingw-w64-x86_64-openblas
49
+ # 注意: Windows MSYS2 下 OpenBLAS 与 OpenMP 链接有冲突 (segfault), 默认关闭.
50
+ # 设环境变量 LAL_USE_OPENBLAS=1 强制开启 (Linux/WSL 下推荐).
51
+ # cblas.h 在 mingw64/include/openblas/ 子目录, 需要显式 -I
52
+ $useOpenBLAS = $env:LAL_USE_OPENBLAS -eq '1'
53
+ if ($useOpenBLAS) {
54
+ $openblasInc = "C:\msys64\mingw64\include\openblas"
55
+ $openblasHeader = @'
56
+ #include <cblas.h>
57
+ int main(){ return 0; }
58
+ '@
59
+ $tmpC = Join-Path $env:TEMP "test_openblas.c"
60
+ $tmpExe = Join-Path $env:TEMP "test_openblas.exe"
61
+ Set-Content -Path $tmpC -Value $openblasHeader -Encoding ascii
62
+ $incFlags = @()
63
+ if (Test-Path $openblasInc) { $incFlags += "-I$openblasInc" }
64
+ & gcc @incFlags $tmpC -o $tmpExe -lopenblas 2>$null
65
+ if ($LASTEXITCODE -eq 0) {
66
+ $cflags += '-DHAVE_OPENBLAS'
67
+ $cflags += $incFlags
68
+ $ldlibs += '-lopenblas'
69
+ Write-Host "[*] OpenBLAS detected — CORE matmul will use cblas_sgemv (2-3x faster)" -ForegroundColor Cyan
70
+ Remove-Item $tmpExe -ErrorAction SilentlyContinue
71
+ } else {
72
+ Write-Host "[*] OpenBLAS not found — using OpenMP fallback" -ForegroundColor Yellow
73
+ }
74
+ Remove-Item $tmpC -ErrorAction SilentlyContinue
75
+ } else {
76
+ Write-Host "[*] OpenBLAS disabled (set LAL_USE_OPENBLAS=1 to enable) — using OpenMP fallback" -ForegroundColor Yellow
77
+ }
78
+
79
+ Write-Host "compiling -> ste_train.exe ..."
80
+ & gcc @cflags -o $out @src @ldlibs
81
+ if ($LASTEXITCODE -ne 0) {
82
+ Write-Host "BUILD FAILED" -ForegroundColor Red
83
+ exit $LASTEXITCODE
84
+ }
85
+ Write-Host "[*] built ste_train.exe - just run .\ste_train.exe, no arguments needed" -ForegroundColor Green
86
+
87
+ if ($Run) {
88
+ Write-Host ""
89
+ & $out
90
+ }