1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
//go:build windows
// +build windows
package main
/*
#cgo LDFLAGS: -L. -lcuda_wrapper
#include <stdlib.h>
extern int initCUDA();
extern void cleanupCUDA();
extern void updateTorusVerticesCUDA(float* vertices, int numVertices, float time, float majorRadius, float minorRadius);
extern void updateTorusColorsCUDA(float* colors, int numVertices, float time);
*/
import "C"
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
"unsafe"
)
var cudaAvailable bool
var cudartDLL *syscall.DLL
// Initialize CUDA subsystem
func initCUDASystem() bool {
LogInfo("CUDA", "Starting CUDA initialization...")
LogInfo("System", "Running on Windows platform")
// Try to detect CUDA runtime first
detectionResult := detectCUDASoft()
LogInfo("CUDA", fmt.Sprintf("CUDA detection result: %v", detectionResult))
if !detectionResult {
LogWarning("CUDA", "CUDA detection failed, but attempting initialization anyway...")
}
// Try to initialize CUDA runtime
defer func() {
if r := recover(); r != nil {
LogError("CUDA", fmt.Sprintf("CUDA initialization failed: %v", r))
LogWarning("System", "Falling back to CPU mode")
cudaAvailable = false
}
}()
LogInfo("CUDA", "Attempting to call C.initCUDA()...")
result := C.initCUDA()
LogInfo("CUDA", fmt.Sprintf("C.initCUDA() returned: %d", result))
cudaAvailable = (result == 1)
if cudaAvailable {
LogSuccess("CUDA", "CUDA runtime initialized successfully")
LogInfo("CUDA", "NVIDIA GPU drivers: Available")
LogInfo("CUDA", "CUDA runtime library: Found")
LogInfo("CUDA", "GPU compute capability: Sufficient")
LogSuccess("System", "Current Mode: CUDA Acceleration")
return true
} else {
LogError("CUDA", "CUDA runtime initialization failed")
LogWarning("System", "Current Mode: CPU Fallback")
return false
}
}
// Detect CUDA using multiple methods
func detectCUDASoft() bool {
LogInfo("CUDA", "Detecting CUDA installation...")
// Method 1: Check nvidia-smi
if checkNvidiaSMI() {
LogInfo("CUDA", "✓ nvidia-smi detected - GPU drivers available")
} else {
LogWarning("CUDA", "✗ nvidia-smi not found - no GPU drivers")
return false
}
// Method 2: Check nvcc compiler
if checkNVCC() {
LogInfo("CUDA", "✓ nvcc compiler detected - CUDA toolkit available")
} else {
LogWarning("CUDA", "✗ nvcc not found - CUDA toolkit not installed")
return false
}
// Method 3: Check CUDA runtime DLL
if checkCUDARuntimeDLL() {
LogInfo("CUDA", "✓ CUDA runtime DLL found")
} else {
LogWarning("CUDA", "✗ CUDA runtime DLL not accessible")
return false
}
// Method 4: Check CUDA toolkit path
if checkCUDAPath() {
LogInfo("CUDA", "✓ CUDA toolkit path detected")
} else {
LogInfo("CUDA", "~ CUDA toolkit path not in environment (not critical)")
}
return true
}
// Check if nvidia-smi is available
func checkNvidiaSMI() bool {
cmd := exec.Command("nvidia-smi", "--version")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmd.Run()
return err == nil
}
// Check if nvcc compiler is available
func checkNVCC() bool {
cmd := exec.Command("nvcc", "--version")
cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
err := cmd.Run()
return err == nil
}
// Check if CUDA runtime DLL is accessible
func checkCUDARuntimeDLL() bool {
// Try to load CUDA runtime DLL
dllNames := []string{
"cudart64_12.dll",
"cudart64_11.dll",
"cudart64_10.dll",
"cudart.dll",
}
for _, dllName := range dllNames {
dll, err := syscall.LoadDLL(dllName)
if err == nil {
LogInfo("CUDA", fmt.Sprintf("✓ Found CUDA runtime: %s", dllName))
cudartDLL = dll
return true
}
}
LogDebug("CUDA", "No CUDA runtime DLL found in system")
return false
}
// Check CUDA toolkit path
func checkCUDAPath() bool {
cudaPath := os.Getenv("CUDA_PATH")
if cudaPath != "" {
LogInfo("CUDA", fmt.Sprintf("CUDA_PATH: %s", cudaPath))
return true
}
// Check common installation paths
commonPaths := []string{
"C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA",
"C:\\Program Files (x86)\\NVIDIA GPU Computing Toolkit\\CUDA",
}
for _, basePath := range commonPaths {
if _, err := os.Stat(basePath); err == nil {
// Find version subdirectories
entries, err := os.ReadDir(basePath)
if err == nil {
for _, entry := range entries {
if entry.IsDir() && strings.HasPrefix(entry.Name(), "v") {
fullPath := filepath.Join(basePath, entry.Name())
LogInfo("CUDA", fmt.Sprintf("Found CUDA installation: %s", fullPath))
return true
}
}
}
}
}
return false
}
// Cleanup CUDA resources
func cleanupCUDASystem() {
if cudaAvailable {
defer func() {
if r := recover(); r != nil {
LogWarning("CUDA", fmt.Sprintf("CUDA cleanup warning: %v", r))
}
}()
LogInfo("CUDA", "Cleaning up CUDA resources...")
C.cleanupCUDA()
LogInfo("CUDA", "CUDA cleanup completed")
} else {
LogInfo("System", "No CUDA resources to cleanup")
}
}
// Check if CUDA is available
func isCUDAAvailable() bool {
return cudaAvailable
}
// Update vertices using CUDA acceleration
func updateTorusVertices(vertices []float32, time float32, majorRadius, minorRadius float32) {
if !cudaAvailable || len(vertices) == 0 {
LogDebug("CPU", fmt.Sprintf("CUDA not available, skipping vertex update for %d vertices", len(vertices)/6))
return
}
defer func() {
if r := recover(); r != nil {
LogError("CUDA", fmt.Sprintf("CUDA vertex update failed: %v", r))
cudaAvailable = false
}
}()
numVertices := len(vertices) / 6 // 6 components per vertex (x,y,z,r,g,b)
// Quiet logging - only log occasionally to avoid spam
// LogDebug("CUDA", fmt.Sprintf("Updating %d vertices using CUDA acceleration", numVertices))
C.updateTorusVerticesCUDA(
(*C.float)(unsafe.Pointer(&vertices[0])),
C.int(numVertices),
C.float(time),
C.float(majorRadius),
C.float(minorRadius),
)
}
// Update colors using CUDA acceleration
func updateTorusColors(colors []float32, time float32) {
if !cudaAvailable || len(colors) == 0 {
LogDebug("CPU", fmt.Sprintf("CUDA not available, skipping color update for %d colors", len(colors)/3))
return
}
defer func() {
if r := recover(); r != nil {
LogError("CUDA", fmt.Sprintf("CUDA color update failed: %v", r))
cudaAvailable = false
}
}()
numVertices := len(colors) / 3 // 3 components per vertex (r,g,b)
// Quiet logging - only log occasionally to avoid spam
// LogDebug("CUDA", fmt.Sprintf("Updating %d colors using CUDA acceleration", numVertices))
C.updateTorusColorsCUDA(
(*C.float)(unsafe.Pointer(&colors[0])),
C.int(numVertices),
C.float(time),
)
}
|