| Crates.io | memkit-co |
| lib.rs | memkit-co |
| version | 0.1.0-alpha.1 |
| created_at | 2025-12-27 00:39:58.746014+00 |
| updated_at | 2025-12-27 00:39:58.746014+00 |
| description | CPU-GPU memory coordination for the memkit ecosystem |
| homepage | |
| repository | https://github.com/YelenaTor/memkit |
| max_upload_size | |
| id | 2006495 |
| size | 68,577 |
CPU-GPU memory coordination for the memkit ecosystem.
Version: 0.1.0-alpha.1
memkit-co is the coordination layer that bridges CPU memory management (memkit) with GPU memory management (memkit-gpu). It provides unified APIs for:
| Use Case | Crate |
|---|---|
| CPU-only allocation | memkit |
| GPU-only memory | memkit-gpu |
| CPU ↔ GPU coordination | memkit-co |
use memkit_co::{MkCoordinator, CoordinatorConfig};
use memkit_gpu::DummyBackend;
// Create coordinator with default config
let config = CoordinatorConfig::default();
let mut coordinator = MkCoordinator::new(DummyBackend::new(), config);
// Allocate data on CPU, upload to GPU in one call
let vertices: &[f32] = &[0.0, 0.5, 1.0, -0.5, -0.5, 1.0, 0.5, -0.5, 1.0];
let gpu_buffer = coordinator.upload_slice(vertices)?;
// Frame-based workflow
coordinator.begin_frame();
{
// All allocations this frame go to the frame arena
let frame_data = coordinator.frame_alloc::<[f32; 4]>();
*frame_data = [1.0, 0.0, 0.0, 1.0];
// Stage for upload (batched at end of frame)
coordinator.stage_for_upload(frame_data);
}
coordinator.end_frame()?; // Commits all staged transfers
┌─────────────────────────────────────────────────────────────┐
│ memkit-co │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────────────┐ │
│ │ Frame │ │ Staging │ │ Transfer │ │
│ │ Coordinator │ │ Pool │ │ Scheduler │ │
│ └──────┬──────┘ └──────┬──────┘ └──────────┬──────────┘ │
└─────────┼────────────────┼─────────────────────┼────────────┘
│ │ │
▼ ▼ ▼
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ memkit │ │ memkit-gpu │ │ memkit-gpu │
│ (CPU arenas) │ │ (staging bufs) │ │ (device bufs) │
└─────────────────┘ └─────────────────┘ └─────────────────┘
Manages per-frame CPU allocations that automatically sync to GPU:
// Frame-scoped allocation with automatic GPU upload
coordinator.begin_frame();
// Allocate transform matrices for this frame
let transforms = coordinator.frame_alloc_slice::<Mat4>(entity_count);
for (i, transform) in transforms.iter_mut().enumerate() {
*transform = compute_transform(i);
}
// Mark for GPU upload
let gpu_transforms = coordinator.stage_transforms(transforms)?;
// End frame commits all pending transfers
coordinator.end_frame()?;
Reuses staging buffers to minimize allocation overhead:
// Acquire staging buffer from pool
let staging = coordinator.acquire_staging(size)?;
// Write data
staging.write_data(&my_data);
// Transfer and release back to pool
coordinator.transfer_and_release(staging, &device_buffer)?;
Batches transfers for optimal GPU utilization:
// Queue multiple transfers
coordinator.queue_transfer(src1, dst1);
coordinator.queue_transfer(src2, dst2);
coordinator.queue_transfer(src3, dst3);
// Submit all as single batch
coordinator.flush_transfers()?;
Built-in support for frame-pipelined uploads:
let coordinator = MkCoordinator::with_buffering(
backend,
BufferingMode::Triple, // Double or Triple
);
// Coordinator automatically rotates buffers each frame
for frame in 0..1000 {
coordinator.begin_frame();
// ... frame work ...
coordinator.end_frame()?;
}
If you're using memkit-gpu without coordination needs:
use memkit_gpu::{MkGpu, DummyBackend};
let gpu = MkGpu::new(DummyBackend::new());
// Direct GPU operations, no coordinator needed
If you're using memkit without GPU:
use memkit::MkFastArena;
let arena = MkFastArena::new(1024 * 1024);
// CPU-only allocations
When you need coordinated CPU-GPU memory:
use memkit_co::MkCoordinator;
let coordinator = MkCoordinator::new(backend, config);
// Coordinator internally uses both memkit and memkit-gpu
use memkit_co::{CoordinatorConfig, BufferingMode};
let config = CoordinatorConfig {
// Frame arena size for per-frame CPU allocations
frame_arena_size: 16 * 1024 * 1024, // 16 MB
// Staging buffer pool settings
staging_pool_size: 32,
staging_buffer_size: 64 * 1024, // 64 KB each
// Buffering mode
buffering: BufferingMode::Double,
// Max pending transfers before auto-flush
max_pending_transfers: 64,
};
let coordinator = MkCoordinator::new(backend, config);
Licensed under the Mozilla Public License 2.0.