Skip to content

Commit f7df7e4

Browse files
committed
surface gpu vram and cores details
1 parent 4c6cd7a commit f7df7e4

5 files changed

Lines changed: 67 additions & 13 deletions

File tree

cgo/proofs.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,26 @@ func GenerateWindowPoSt(randomness *ByteArray32, replicas SliceRefPrivateReplica
278278
return proofs, []uint64{}, nil
279279
}
280280

281-
func GetGpuDevices() ([]string, error) {
282-
resp := (*resultSliceBoxedSliceBoxedUint8)(C.get_gpu_devices())
283-
defer resp.destroy()
284-
if err := CheckErr(resp); err != nil {
285-
return nil, err
281+
func GetGpuDevices() ([]GpuDeviceInfoGo, error) {
282+
resp := (SliceBoxedGpuDeviceInfo)(C.get_gpu_devices())
283+
defer C.destroy_gpu_devices(resp)
284+
285+
if resp.ptr == nil || resp.len == 0 {
286+
return nil, nil
287+
}
288+
289+
ref := resp.slice()
290+
out := make([]GpuDeviceInfoGo, 0, len(ref))
291+
for i := range ref {
292+
name := string((*SliceBoxedUint8)(&ref[i].name).copy())
293+
out = append(out, GpuDeviceInfoGo{
294+
Name: name,
295+
VRAMBytes: uint64(ref[i].vram_bytes),
296+
Cores: uint32(ref[i].cores),
297+
})
286298
}
287299

288-
return (SliceBoxedSliceBoxedUint8)(resp.value).copyAsStrings(), nil
300+
return out, nil
289301
}
290302

291303
func GetSealVersion(registeredProof RegisteredSealProof) (string, error) {

cgo/types.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ type resultEmptySectorUpdateEncodeInto C.Result_EmptySectorUpdateEncodeInto_t
6868
type resultGenerateFallbackSectorChallenges C.Result_GenerateFallbackSectorChallenges_t
6969
type resultGenerateSingleWindowPoStWithVanilla C.Result_GenerateSingleWindowPoStWithVanilla_t
7070
type resultPoStProof C.Result_PoStProof_t
71+
type GpuDeviceInfo C.GpuDeviceInfo_t
72+
type SliceBoxedGpuDeviceInfo C.slice_boxed_GpuDeviceInfo_t
7173

7274
// FVM types moved to types_fvm.go behind build tag
7375

@@ -604,6 +606,30 @@ func (ptr *PoStProof) Destroy() {
604606
}
605607
}
606608

609+
func (ptr SliceBoxedGpuDeviceInfo) slice() []GpuDeviceInfo {
610+
if ptr.ptr == nil || ptr.len == 0 {
611+
return nil
612+
}
613+
return unsafe.Slice((*GpuDeviceInfo)(unsafe.Pointer(ptr.ptr)), int(ptr.len))
614+
}
615+
616+
type GpuDeviceInfoGo struct {
617+
Name string
618+
VRAMBytes uint64
619+
Cores uint32
620+
}
621+
622+
func (info GpuDeviceInfo) copy() GpuDeviceInfoGo {
623+
// name is slice_boxed_uint8_t, which matches your SliceBoxedUint8 wrapper.
624+
name := (*SliceBoxedUint8)(&info.name).copy()
625+
626+
return GpuDeviceInfoGo{
627+
Name: string(name),
628+
VRAMBytes: uint64(info.vram_bytes),
629+
Cores: uint32(info.cores),
630+
}
631+
}
632+
607633
// FVM helpers moved to types_fvm.go
608634

609635
// FVM helpers moved to types_fvm.go

proofs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,9 @@ func GenerateWindowPoSt(
628628
return proofs, nil, nil
629629
}
630630

631-
// GetGPUDevices produces a slice of strings, each representing the name of a
631+
// GetGPUDevices produces a slice of struct, each representing the name, vRAM, and GPU cores of a
632632
// detected GPU device.
633-
func GetGPUDevices() ([]string, error) {
633+
func GetGPUDevices() ([]cgo.GpuDeviceInfoGo, error) {
634634
return cgo.GetGpuDevices()
635635
}
636636

rust/src/util/api.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ use std::sync::Once;
44

55
use anyhow::anyhow;
66
use safer_ffi::prelude::*;
7-
use safer_ffi::slice::slice_boxed;
87

98
use super::types::{
10-
catch_panic_response, catch_panic_response_no_log, GpuDeviceResponse, InitLogFdResponse,
9+
catch_panic_response, catch_panic_response_no_log, GpuDeviceResponse, InitLogFdResponse, GpuDeviceInfo
1110
};
1211

1312
/// Protects the init off the logger.
@@ -35,18 +34,22 @@ pub fn init_log_with_file(file: File) -> Option<()> {
3534

3635
/// Serialize the GPU device names into a vector
3736
#[cfg(any(feature = "opencl", feature = "cuda", feature = "cuda-supraseal"))]
38-
fn get_gpu_devices_internal() -> Vec<slice_boxed<u8>> {
37+
fn get_gpu_devices_internal() -> Vec<GpuDeviceInfo> {
3938
let devices = rust_gpu_tools::Device::all();
4039

4140
devices
4241
.into_iter()
43-
.map(|d| d.name().into_bytes().into_boxed_slice().into())
42+
.map(|d| GpuDeviceInfo{
43+
name: d.name().into_bytes().into_boxed_slice().into(),
44+
vram_bytes: d.memory().into(),
45+
cores: d.compute_units().into()
46+
})
4447
.collect()
4548
}
4649

4750
// Return empty vector for GPU devices if cuda and opencl are disabled
4851
#[cfg(not(any(feature = "opencl", feature = "cuda", feature = "cuda-supraseal")))]
49-
fn get_gpu_devices_internal() -> Vec<slice_boxed<u8>> {
52+
fn get_gpu_devices_internal() -> Vec<GpuDeviceInfo> {
5053
Vec::new()
5154
}
5255

rust/src/util/types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{fmt::Display, mem::MaybeUninit, ops::Deref, panic, path::PathBuf, str::Utf8Error};
22

33
use safer_ffi::prelude::*;
4+
use safer_ffi::slice::slice_boxed;
45

56
use super::api::init_log;
67

@@ -250,3 +251,15 @@ macro_rules! destructor {
250251
}
251252
};
252253
}
254+
255+
#[derive_ReprC]
256+
#[repr(C)]
257+
#[derive(Clone)]
258+
pub struct GpuDeviceInfo<> {
259+
pub name: slice_boxed<u8>,
260+
pub vram_bytes: u64,
261+
pub cores: u32,
262+
}
263+
264+
pub type GpuDeviceResponse = Result<c_slice::Box<GpuDeviceInfo>>;
265+

0 commit comments

Comments
 (0)