"...scripts/run-performance-benchmarks.sh" did not exist on "fb4f530bf5004a9afef1380cb0a84bfb98a89c63"
nixl.rs 7.34 KB
Newer Older
Ryan Olson's avatar
Ryan Olson committed
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! NixL backend configuration.
//!
//! Configures which NixL backends (UCX, GDS, etc.) are enabled for RDMA transfers,
//! along with optional parameters for each backend.

use dynamo_memory::nixl::NixlBackendConfig;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use validator::Validate;

/// NixL backend configuration.
///
/// Controls which NixL backends are enabled for RDMA memory transfers
/// and their optional parameters.
///
/// # Backends
///
/// Common backends include:
/// - `UCX` - Unified Communication X (default)
/// - `GDS` - GPUDirect Storage
/// - `GDS_MT` - GPUDirect Storage (multi-threaded)
///
/// All backend names are normalized to uppercase.
///
/// # Configuration
///
/// Each backend can have optional parameters as key-value pairs.
/// If a backend has no parameters, use an empty map.
///
/// ## TOML Example
///
/// ```toml
/// [nixl.backends.UCX]
/// # UCX with default params (empty map)
///
/// [nixl.backends.GDS]
/// threads = "4"
/// buffer_size = "1048576"
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct NixlConfig {
    /// Map of backend name (uppercase) -> optional parameters.
    ///
    /// If a backend is present in the map, it's enabled.
    /// The inner HashMap contains optional override parameters.
    /// An empty inner map means use default parameters.
    #[serde(default = "default_backends")]
    pub backends: HashMap<String, HashMap<String, String>>,
}

fn default_backends() -> HashMap<String, HashMap<String, String>> {
    let mut backends = HashMap::new();
    backends.insert("UCX".to_string(), HashMap::new());
    backends.insert("POSIX".to_string(), HashMap::new());
    backends
}

impl Default for NixlConfig {
    fn default() -> Self {
        Self {
            backends: default_backends(),
        }
    }
}

impl NixlConfig {
    pub fn new(backends: HashMap<String, HashMap<String, String>>) -> Self {
        Self { backends }
    }

    pub fn empty() -> Self {
        Self {
            backends: HashMap::new(),
        }
    }

    pub fn from_nixl_backend_config(config: NixlBackendConfig) -> Self {
        let backends: HashMap<String, HashMap<String, String>> = config
            .iter()
            .map(|(backend, params)| (backend.to_string(), params.clone()))
            .collect();

        Self { backends }
    }

    /// Add a backend with default parameters.
    /// Backend name is normalized to uppercase.
    pub fn with_backend(mut self, name: impl Into<String>) -> Self {
        self.backends
            .insert(name.into().to_uppercase(), HashMap::new());
        self
    }

    /// Add a backend with custom parameters.
    /// Backend name is normalized to uppercase.
    pub fn with_backend_params(
        mut self,
        name: impl Into<String>,
        params: HashMap<String, String>,
    ) -> Self {
        self.backends.insert(name.into().to_uppercase(), params);
        self
    }

    /// Get the list of enabled backend names (uppercase).
    pub fn enabled_backends(&self) -> Vec<&String> {
        self.backends.keys().collect()
    }

    /// Check if a specific backend is enabled.
    /// Backend name is normalized to uppercase for lookup.
    pub fn has_backend(&self, backend: &str) -> bool {
        self.backends.contains_key(&backend.to_uppercase())
    }

    /// Get parameters for a specific backend.
    /// Backend name is normalized to uppercase for lookup.
    ///
    /// Returns None if the backend is not enabled.
    pub fn backend_params(&self, backend: &str) -> Option<&HashMap<String, String>> {
        self.backends.get(&backend.to_uppercase())
    }

    /// Iterate over all enabled backends and their parameters.
    pub fn iter(&self) -> impl Iterator<Item = (&String, &HashMap<String, String>)> {
        self.backends.iter()
    }
}

impl From<NixlConfig> for NixlBackendConfig {
    fn from(config: NixlConfig) -> Self {
        NixlBackendConfig::new(config.backends)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let config = NixlConfig::default();
        assert!(config.has_backend("UCX"));
        assert!(!config.has_backend("GDS"));
    }

    #[test]
    fn test_new_default() {
        let config = NixlConfig::default();
        assert!(config.has_backend("UCX"));
        assert!(config.has_backend("POSIX"));
        assert!(!config.enabled_backends().is_empty());
    }

    #[test]
    fn test_with_backend() {
        let config = NixlConfig::empty().with_backend("ucx").with_backend("gds");

        assert!(config.has_backend("UCX"));
        assert!(config.has_backend("GDS"));
        assert!(!config.has_backend("POSIX"));

        // Keys are stored uppercase
        assert!(config.backends.contains_key("UCX"));
        assert!(config.backends.contains_key("GDS"));
    }

    #[test]
    fn test_with_backend_params() {
        let mut params = HashMap::new();
        params.insert("threads".to_string(), "4".to_string());
        params.insert("buffer_size".to_string(), "1048576".to_string());

        let config = NixlConfig::empty()
            .with_backend("UCX")
            .with_backend_params("GDS", params);

        // UCX should have empty params
        let ucx_params = config.backend_params("UCX").unwrap();
        assert!(ucx_params.is_empty());

        // GDS should have custom params
        let gds_params = config.backend_params("GDS").unwrap();
        assert_eq!(gds_params.get("threads"), Some(&"4".to_string()));
        assert_eq!(gds_params.get("buffer_size"), Some(&"1048576".to_string()));
    }

    #[test]
    fn test_lookup_normalizes_to_uppercase() {
        let config = NixlConfig::empty().with_backend("ucx");

        // All lookups normalize to uppercase
        assert!(config.has_backend("ucx"));
        assert!(config.has_backend("UCX"));
        assert!(config.has_backend("Ucx"));

        assert!(config.backend_params("ucx").is_some());
        assert!(config.backend_params("UCX").is_some());
    }

    #[test]
    fn test_enabled_backends() {
        let config = NixlConfig::empty().with_backend("ucx").with_backend("gds");

        let backends = config.enabled_backends();
        assert_eq!(backends.len(), 2);
        assert!(backends.contains(&&"UCX".to_string()));
        assert!(backends.contains(&&"GDS".to_string()));
    }

    #[test]
    fn test_iter() {
        let mut params = HashMap::new();
        params.insert("key".to_string(), "value".to_string());

        let config = NixlConfig::empty()
            .with_backend("UCX")
            .with_backend_params("GDS", params);

        let items: Vec<_> = config.iter().collect();
        assert_eq!(items.len(), 2);
    }

    #[test]
    fn test_serde_roundtrip() {
        let mut params = HashMap::new();
        params.insert("threads".to_string(), "4".to_string());

        let config = NixlConfig::empty()
            .with_backend("UCX")
            .with_backend_params("GDS", params);

        let json = serde_json::to_string(&config).unwrap();
        let parsed: NixlConfig = serde_json::from_str(&json).unwrap();

        assert!(parsed.has_backend("UCX"));
        assert!(parsed.has_backend("GDS"));
        assert_eq!(
            parsed.backend_params("GDS").unwrap().get("threads"),
            Some(&"4".to_string())
        );
    }
}