use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GpuProfile {
pub gpu_model: String,
pub contributor: String,
pub timestamp: String,
pub kernel_configs: HashMap<String, KernelConfig>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KernelConfig {
pub name: String,
pub elem_type: String,
pub shape: Vec<i64>,
pub block_size: u32,
pub grid_size: u32,
pub shared_mem_bytes: u32,
pub execution_time_ms: f64,
pub uses_tensor_cores: bool,
}
impl GpuProfile {
pub fn new(gpu_model: &str, contributor: &str) -> Self {
GpuProfile {
gpu_model: gpu_model.to_string(),
contributor: contributor.to_string(),
timestamp: "2024-01-01T00:00:00Z".to_string(),
kernel_configs: HashMap::new(),
}
}
pub fn from_file(path: &Path) -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(path)?;
let profile: GpuProfile = serde_json::from_str(&content)?;
Ok(profile)
}
pub fn to_file(&self, path: &Path) -> Result<(), Box<dyn std::error::Error>> {
let json = serde_json::to_string_pretty(self)?;
std::fs::write(path, json)?;
Ok(())
}
pub fn add_kernel_config(&mut self, config: KernelConfig) {
let key = format!(
"{}_{}_{}",
config.name,
config.elem_type,
config
.shape
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.join("x")
);
self.kernel_configs.insert(key, config);
}
pub fn get_kernel_config(
&self,
name: &str,
elem_type: &str,
shape: &[i64],
) -> Option<&KernelConfig> {
let key = format!(
"{}_{}_{}",
name,
elem_type,
shape
.iter()
.map(|s| s.to_string())
.collect::<Vec<_>>()
.join("x")
);
self.kernel_configs.get(&key)
}
}
pub fn load_profiles_from_dir(dir: &Path) -> Result<Vec<GpuProfile>, Box<dyn std::error::Error>> {
let mut profiles = Vec::new();
if dir.exists() {
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|e| e.to_str()) == Some("json") {
match GpuProfile::from_file(&path) {
Ok(profile) => profiles.push(profile),
Err(e) => eprintln!("Warning: failed to load profile from {:?}: {}", path, e),
}
}
}
}
Ok(profiles)
}
pub fn save_profile_to_dir(
profile: &GpuProfile,
dir: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
if !dir.exists() {
std::fs::create_dir_all(dir)?;
}
let filename = format!("{}.json", profile.gpu_model);
let path = dir.join(filename);
profile.to_file(&path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gpu_profile_new() {
let profile = GpuProfile::new("RTX_3090", "test_user");
assert_eq!(profile.gpu_model, "RTX_3090");
assert_eq!(profile.contributor, "test_user");
assert!(profile.kernel_configs.is_empty());
}
#[test]
fn test_gpu_profile_add_config() {
let mut profile = GpuProfile::new("RTX_3090", "test_user");
profile.add_kernel_config(KernelConfig {
name: "vector_add".to_string(),
elem_type: "f32".to_string(),
shape: vec![1024],
block_size: 256,
grid_size: 4,
shared_mem_bytes: 0,
execution_time_ms: 0.5,
uses_tensor_cores: false,
});
assert_eq!(profile.kernel_configs.len(), 1);
}
}