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
|
//go:build !windows && cgo
// +build !windows,cgo
package main
/*
#cgo CFLAGS: -I/usr/local/cuda/include -I.
#cgo LDFLAGS: -L/usr/local/cuda/lib64 -lcudart -L. -ltorus_kernel
#include <stdlib.h>
// Declare functions that may or may not be available
extern void updateTorusVerticesCUDA(float* vertices, int numVertices, float time, float majorRadius, float minorRadius);
extern void updateTorusColorsCUDA(float* colors, int numVertices, float time);
extern int initCUDA();
extern void cleanupCUDA();
*/
import "C"
import (
"fmt"
"unsafe"
)
var cudaAvailable bool
// Initialize CUDA subsystem
func initCUDASystem() bool {
// Try to initialize CUDA - if it fails, fall back to CPU
defer func() {
if r := recover(); r != nil {
fmt.Println("CUDA initialization failed, using CPU fallback:", r)
cudaAvailable = false
}
}()
result := C.initCUDA()
cudaAvailable = (result == 1)
if cudaAvailable {
fmt.Println("CUDA initialized successfully")
} else {
fmt.Println("CUDA not available, using CPU computations")
}
return cudaAvailable
}
// Cleanup CUDA resources
func cleanupCUDASystem() {
if cudaAvailable {
defer func() {
if r := recover(); r != nil {
fmt.Println("CUDA cleanup warning:", r)
}
}()
C.cleanupCUDA()
fmt.Println("CUDA cleanup completed")
}
}
// Update torus vertices using CUDA
func updateTorusVerticesCUDA(vertices []float32, time float32, majorRadius, minorRadius float32) {
if !cudaAvailable || len(vertices) == 0 {
return
}
defer func() {
if r := recover(); r != nil {
fmt.Println("CUDA vertex update failed:", r)
cudaAvailable = false
}
}()
numVertices := len(vertices) / 6 // 6 components per vertex (x,y,z,r,g,b)
C.updateTorusVerticesCUDA(
(*C.float)(unsafe.Pointer(&vertices[0])),
C.int(numVertices),
C.float(time),
C.float(majorRadius),
C.float(minorRadius),
)
}
// Update torus colors using CUDA
func updateTorusColorsCUDA(vertices []float32, time float32) {
if !cudaAvailable || len(vertices) == 0 {
return
}
defer func() {
if r := recover(); r != nil {
fmt.Println("CUDA color update failed:", r)
cudaAvailable = false
}
}()
numVertices := len(vertices) / 6 // 6 components per vertex (x,y,z,r,g,b)
C.updateTorusColorsCUDA(
(*C.float)(unsafe.Pointer(&vertices[0])),
C.int(numVertices),
C.float(time),
)
}
// Check if CUDA is available
func isCUDAAvailable() bool {
return cudaAvailable
}
|