Files
dex/pkg/lx/amm_gpu_cuda.go
zeekay 7d8c88cf8a dex/pkg/lx: AMM cross-platform + CLOB CUDA + ABI mirror asserts + parity
AMM unification (one Go entry point, per-platform impls):
- amm_gpu.go          (cgo) — BatchEvalConstantProduct dispatcher with
                                LUX_GPU_DISABLE kill switch + fallback metrics
- amm_gpu_metal.go    (cgo && darwin) — Metal driver via lux-dex-amm-metal
- amm_gpu_cuda.go     (cgo && linux)  — CUDA driver via lux-dex-amm-cuda
- amm_gpu_stub.go     (cgo && other)  — errAMMGPUUnsupported sentinel
- amm_nogpu.go        (!cgo)          — pure CPU path

CLOB CUDA wire (the LP-108 missing piece — Backend enum can come back):
- orderbook_cuda_types.go     — Go mirrors of DEXOrder (64B), DEXTrade (80B),
                                 DEXPrice; assertDEXABI() in init() panics
                                 on any size/offset drift vs the C header
- orderbook_match_cpu.go      — MatchOrderCPU canonical Go oracle
- orderbook_cuda.go           (cgo && linux) — gpuMatchOrder via
                                 lux-dex-clob-cuda → libdex_clob_cuda.a →
                                 luxcpp/cuda/kernels/gpu/dex_swap.cu
- orderbook_cuda_stub.go      (other) — errCLOBGPUUnsupported sentinel
- orderbook_gpu.go            — MatchOrderGPU dispatcher with strict fallback
                                 policy + backend.RecordFallback observability
- orderbook_cuda_parity_test.go — 8 randomized parity subtests using
                                  DEXTrade.EqualFields / DEXOrder.EqualFields
                                  (NOT struct ==) so padding bytes can't
                                  cause false negatives

Padding normalization: _ blank pads renamed to _pad (addressable);
ClearPadding() methods on DEXOrder/DEXTrade; CPU oracle + CUDA host
driver both zero pad bytes for deterministic structural comparison.

Branding: all `Lux*`/`LUX_*` types in the C ABI mirror dropped (DEXOrder,
DEXTrade, DEXPrice, AMMReservePair, DEX_SIDE_BID, etc.).

Temporary replace github.com/luxfi/crypto => ../../lux/crypto pending
publication of crypto with backend.GPUDisabled / RecordFallback /
FallbackReason. Remove the replace once crypto is tagged.
2026-05-24 14:19:56 -07:00

60 lines
1.5 KiB
Go

// Copyright (C) 2025-2026, Lux Industries Inc. All rights reserved.
// See the file LICENSE for licensing terms.
//go:build cgo && linux
package lx
// CUDA backend for batch xy=k AMM evaluation. Linked via
// lux-dex-amm-cuda pkg-config bundle
// -> libamm_xyk_cuda.a (luxcpp/dex/gpu/cuda/amm_xyk_driver.cu
// + luxcpp/cuda/kernels/dex/amm_xyk.cu)
/*
#cgo pkg-config: lux-dex-amm-cuda
#include <stdint.h>
#include "amm_xyk_driver.h"
*/
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
// gpuBatchEval is the CUDA implementation called by amm_gpu.go::BatchEvalConstantProduct.
// When no NVIDIA device is present at runtime, the driver returns -2 and we
// surface errAMMGPUUnsupported so the dispatcher falls back to CPU.
func gpuBatchEval(reserves []ReservePair, amounts []uint64) ([]uint64, error) {
n := len(reserves)
out := make([]uint64, n)
var pinner runtime.Pinner
defer pinner.Unpin()
pinner.Pin(&reserves[0])
pinner.Pin(&amounts[0])
pinner.Pin(&out[0])
rc := C.amm_xyk_batch_cuda_host(
(*C.AMMReservePair)(unsafe.Pointer(&reserves[0])),
(*C.uint64_t)(unsafe.Pointer(&amounts[0])),
(*C.uint64_t)(unsafe.Pointer(&out[0])),
C.uint32_t(n),
)
runtime.KeepAlive(reserves)
runtime.KeepAlive(amounts)
runtime.KeepAlive(out)
switch rc {
case 0:
return out, nil
case -2:
// No CUDA device / driver — fall back to CPU.
return nil, errAMMGPUUnsupported
default:
return nil, fmt.Errorf("lx: amm_xyk_batch_cuda_host rc=%d", int(rc))
}
}