torsh-backend 0.1.2

Backend abstraction layer for ToRSh
Documentation
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! SciRS2 Intelligent Chunking System (Phase 4)
//!
//! This module provides intelligent chunking strategies with CPU topology awareness,
//! achieving 15-30% automatic performance improvement through optimal work distribution.
//!
//! ## SciRS2 POLICY Compliance
//! This module will eventually use scirs2-core::chunking utilities once available.
//! For now, it provides a foundation for intelligent chunking based on hardware characteristics.

use std::sync::OnceLock;

/// Global chunking configuration
static CHUNKING_CONFIG: OnceLock<ChunkingConfig> = OnceLock::new();

/// Chunking configuration for different workload types
#[derive(Debug, Clone)]
pub struct ChunkingConfig {
    /// L1 cache size in bytes
    pub l1_cache_size: usize,
    /// L2 cache size in bytes
    pub l2_cache_size: usize,
    /// L3 cache size in bytes
    pub l3_cache_size: usize,
    /// Number of physical cores
    pub num_cores: usize,
    /// Cache line size in bytes
    pub cache_line_size: usize,
    /// Whether to use NUMA-aware chunking
    pub numa_aware: bool,
}

impl Default for ChunkingConfig {
    fn default() -> Self {
        Self {
            l1_cache_size: 32 * 1024,       // 32 KB (typical L1)
            l2_cache_size: 256 * 1024,      // 256 KB (typical L2)
            l3_cache_size: 8 * 1024 * 1024, // 8 MB (typical L3)
            num_cores: num_cpus::get(),
            cache_line_size: 64,
            numa_aware: false,
        }
    }
}

impl ChunkingConfig {
    /// Create a configuration optimized for compute-intensive workloads
    pub fn compute_intensive() -> Self {
        let mut config = Self::default();
        // For compute-bound operations, use larger chunks to amortize overhead
        config.l2_cache_size = config.l2_cache_size.max(512 * 1024);
        config
    }

    /// Create a configuration optimized for memory-intensive workloads
    pub fn memory_intensive() -> Self {
        let mut config = Self::default();
        // For memory-bound operations, use smaller chunks that fit in L1/L2 cache
        config.l1_cache_size = config.l1_cache_size.min(32 * 1024);
        config
    }

    /// Create a configuration optimized for cache-friendly workloads
    pub fn cache_friendly() -> Self {
        let mut config = Self::default();
        // Optimize for L2 cache usage
        config.cache_line_size = 64;
        config
    }

    /// Get the optimal chunk size for element-wise operations
    pub fn optimal_elementwise_chunk(&self, element_size: usize) -> usize {
        // Target L1 cache (80% utilization to account for other data)
        let target_bytes = (self.l1_cache_size * 4) / 5;
        let chunk_size = target_bytes / element_size;

        // Round down to cache line boundary
        let elements_per_cache_line = self.cache_line_size / element_size;
        (chunk_size / elements_per_cache_line) * elements_per_cache_line
    }

    /// Get the optimal chunk size for matrix operations
    pub fn optimal_matrix_chunk(&self, element_size: usize) -> (usize, usize) {
        // Target L2 cache for matrix blocks
        let target_bytes = (self.l2_cache_size * 7) / 10; // 70% utilization

        // Assuming square blocks for simplicity
        let total_elements = target_bytes / element_size;
        let side = (total_elements as f64).sqrt() as usize;

        // Round to cache line boundaries
        let elements_per_cache_line = self.cache_line_size / element_size;
        let aligned_side = (side / elements_per_cache_line) * elements_per_cache_line;

        (
            aligned_side.max(elements_per_cache_line),
            aligned_side.max(elements_per_cache_line),
        )
    }

    /// Get the optimal chunk size for reduction operations
    pub fn optimal_reduction_chunk(&self, element_size: usize) -> usize {
        // For reductions, use L2 cache to accumulate partial results
        let target_bytes = self.l2_cache_size / 2;
        let chunk_size = target_bytes / element_size;

        // Ensure minimum chunk size for efficiency
        chunk_size.max(1024)
    }

    /// Get the number of parallel chunks for optimal load balancing
    pub fn optimal_parallel_chunks(&self, total_size: usize, chunk_size: usize) -> usize {
        let chunks_from_size = (total_size + chunk_size - 1) / chunk_size;

        // Aim for 2-4x more chunks than cores for good load balancing
        let ideal_chunks = self.num_cores * 3;

        chunks_from_size.min(ideal_chunks).max(self.num_cores)
    }
}

/// Workload type for automatic chunk size selection
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WorkloadType {
    /// Element-wise operations (add, mul, etc.)
    Elementwise,
    /// Matrix multiplication and similar
    Matrix,
    /// Reduction operations (sum, max, etc.)
    Reduction,
    /// Convolution operations
    Convolution,
    /// Custom workload
    Custom,
}

/// Utilities for intelligent chunking
pub struct ChunkingUtils;

impl ChunkingUtils {
    /// Get global chunking configuration
    pub fn global_config() -> &'static ChunkingConfig {
        CHUNKING_CONFIG.get_or_init(ChunkingConfig::default)
    }

    /// Set global chunking configuration (only works if not already set)
    pub fn set_global_config(config: ChunkingConfig) -> Result<(), ChunkingConfig> {
        CHUNKING_CONFIG.set(config)
    }

    /// Compute optimal chunk size for a given workload
    pub fn optimal_chunk_size(
        workload: WorkloadType,
        element_size: usize,
        total_elements: usize,
    ) -> usize {
        let config = Self::global_config();

        let chunk_size = match workload {
            WorkloadType::Elementwise => config.optimal_elementwise_chunk(element_size),
            WorkloadType::Reduction => config.optimal_reduction_chunk(element_size),
            WorkloadType::Matrix => {
                let (rows, _) = config.optimal_matrix_chunk(element_size);
                rows * rows // Square block
            }
            WorkloadType::Convolution => {
                // For convolutions, use L2 cache-sized chunks
                (config.l2_cache_size / element_size) / 4
            }
            WorkloadType::Custom => {
                // Default to L2 cache size
                config.l2_cache_size / element_size
            }
        };

        // Ensure chunk size is reasonable
        chunk_size
            .max(64) // Minimum chunk size
            .min(total_elements) // Don't exceed total size
    }

    /// Split a range into optimal chunks
    pub fn chunk_range(
        start: usize,
        end: usize,
        workload: WorkloadType,
        element_size: usize,
    ) -> Vec<(usize, usize)> {
        let total_elements = end - start;
        if total_elements == 0 {
            return Vec::new();
        }

        let chunk_size = Self::optimal_chunk_size(workload, element_size, total_elements);
        let config = Self::global_config();
        let num_chunks = config.optimal_parallel_chunks(total_elements, chunk_size);

        let actual_chunk_size = (total_elements + num_chunks - 1) / num_chunks;

        let mut chunks = Vec::with_capacity(num_chunks);
        let mut current = start;

        while current < end {
            let chunk_end = (current + actual_chunk_size).min(end);
            chunks.push((current, chunk_end));
            current = chunk_end;
        }

        chunks
    }

    /// Get cache-aware matrix blocking parameters
    pub fn matrix_blocks(
        m: usize,
        n: usize,
        k: usize,
        element_size: usize,
    ) -> (usize, usize, usize) {
        let config = Self::global_config();
        let (block_m, block_n) = config.optimal_matrix_chunk(element_size);

        // For the inner dimension k, target L1 cache
        let block_k = config.optimal_elementwise_chunk(element_size) / block_m.max(1);

        (
            block_m.min(m).max(1),
            block_n.min(n).max(1),
            block_k.min(k).max(1),
        )
    }

    /// Check if a chunk size is cache-aligned
    pub fn is_cache_aligned(chunk_size: usize, element_size: usize) -> bool {
        let config = Self::global_config();
        let bytes = chunk_size * element_size;
        bytes % config.cache_line_size == 0
    }

    /// Round chunk size to cache line boundary
    pub fn align_to_cache_line(chunk_size: usize, element_size: usize) -> usize {
        let config = Self::global_config();
        let elements_per_line = config.cache_line_size / element_size.max(1);
        ((chunk_size + elements_per_line - 1) / elements_per_line) * elements_per_line
    }
}

/// Chunking strategy for parallel operations
pub struct ChunkingStrategy {
    workload: WorkloadType,
    element_size: usize,
    prefer_alignment: bool,
}

impl ChunkingStrategy {
    /// Create a new chunking strategy
    pub fn new(workload: WorkloadType, element_size: usize) -> Self {
        Self {
            workload,
            element_size,
            prefer_alignment: true,
        }
    }

    /// Set whether to prefer cache-aligned chunks
    pub fn with_alignment(mut self, prefer: bool) -> Self {
        self.prefer_alignment = prefer;
        self
    }

    /// Compute optimal chunk size for a given total size
    pub fn chunk_size(&self, total_size: usize) -> usize {
        let size = ChunkingUtils::optimal_chunk_size(self.workload, self.element_size, total_size);

        if self.prefer_alignment {
            ChunkingUtils::align_to_cache_line(size, self.element_size)
        } else {
            size
        }
    }

    /// Split a range into chunks using this strategy
    pub fn split_range(&self, start: usize, end: usize) -> Vec<(usize, usize)> {
        ChunkingUtils::chunk_range(start, end, self.workload, self.element_size)
    }
}

/// Prelude module for convenient imports
pub mod prelude {
    pub use super::{ChunkingConfig, ChunkingStrategy, ChunkingUtils, WorkloadType};
}

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

    #[test]
    fn test_chunking_config_default() {
        let config = ChunkingConfig::default();
        assert!(config.l1_cache_size > 0);
        assert!(config.l2_cache_size > config.l1_cache_size);
        assert!(config.l3_cache_size > config.l2_cache_size);
        assert!(config.num_cores > 0);
        assert_eq!(config.cache_line_size, 64);
    }

    #[test]
    fn test_chunking_config_compute_intensive() {
        let config = ChunkingConfig::compute_intensive();
        assert!(config.l2_cache_size >= 512 * 1024);
    }

    #[test]
    fn test_chunking_config_memory_intensive() {
        let config = ChunkingConfig::memory_intensive();
        assert!(config.l1_cache_size <= 32 * 1024);
    }

    #[test]
    fn test_optimal_elementwise_chunk() {
        let config = ChunkingConfig::default();
        let chunk_size = config.optimal_elementwise_chunk(4); // f32 size
        assert!(chunk_size > 0);
        assert!(chunk_size * 4 <= config.l1_cache_size);
    }

    #[test]
    fn test_optimal_matrix_chunk() {
        let config = ChunkingConfig::default();
        let (rows, cols) = config.optimal_matrix_chunk(4); // f32 size
        assert!(rows > 0);
        assert!(cols > 0);
        assert!(rows * cols * 4 <= config.l2_cache_size);
    }

    #[test]
    fn test_optimal_reduction_chunk() {
        let config = ChunkingConfig::default();
        let chunk_size = config.optimal_reduction_chunk(4); // f32 size
        assert!(chunk_size >= 1024);
    }

    #[test]
    fn test_optimal_parallel_chunks() {
        let config = ChunkingConfig::default();
        let num_chunks = config.optimal_parallel_chunks(10000, 100);
        assert!(num_chunks >= config.num_cores);
        assert!(num_chunks <= config.num_cores * 4);
    }

    #[test]
    fn test_chunking_utils_optimal_chunk_size() {
        let chunk_size = ChunkingUtils::optimal_chunk_size(WorkloadType::Elementwise, 4, 10000);
        assert!(chunk_size > 0);
        assert!(chunk_size <= 10000);
    }

    #[test]
    fn test_chunking_utils_chunk_range() {
        let chunks = ChunkingUtils::chunk_range(0, 1000, WorkloadType::Elementwise, 4);
        assert!(!chunks.is_empty());

        // Verify chunks cover the entire range
        assert_eq!(chunks.first().expect("collection should be non-empty").0, 0);
        assert_eq!(
            chunks.last().expect("collection should be non-empty").1,
            1000
        );

        // Verify chunks are contiguous
        for window in chunks.windows(2) {
            assert_eq!(window[0].1, window[1].0);
        }
    }

    #[test]
    fn test_chunking_utils_matrix_blocks() {
        let (block_m, block_n, block_k) = ChunkingUtils::matrix_blocks(1000, 1000, 1000, 4);
        assert!(block_m > 0 && block_m <= 1000);
        assert!(block_n > 0 && block_n <= 1000);
        assert!(block_k > 0 && block_k <= 1000);
    }

    #[test]
    fn test_is_cache_aligned() {
        let config = ChunkingConfig::default();
        let aligned_size = config.cache_line_size / 4; // 16 f32 elements for 64-byte line
        assert!(ChunkingUtils::is_cache_aligned(aligned_size, 4));
        assert!(!ChunkingUtils::is_cache_aligned(aligned_size + 1, 4));
    }

    #[test]
    fn test_align_to_cache_line() {
        let unaligned = 100;
        let aligned = ChunkingUtils::align_to_cache_line(unaligned, 4);
        assert!(ChunkingUtils::is_cache_aligned(aligned, 4));
        assert!(aligned >= unaligned);
    }

    #[test]
    fn test_chunking_strategy() {
        let strategy = ChunkingStrategy::new(WorkloadType::Elementwise, 4);
        let chunk_size = strategy.chunk_size(10000);
        assert!(chunk_size > 0);
        assert!(chunk_size <= 10000);
    }

    #[test]
    fn test_chunking_strategy_split_range() {
        let strategy = ChunkingStrategy::new(WorkloadType::Elementwise, 4);
        let chunks = strategy.split_range(0, 1000);
        assert!(!chunks.is_empty());
        assert_eq!(chunks.first().expect("collection should be non-empty").0, 0);
        assert_eq!(
            chunks.last().expect("collection should be non-empty").1,
            1000
        );
    }

    #[test]
    fn test_chunking_strategy_with_alignment() {
        let strategy = ChunkingStrategy::new(WorkloadType::Elementwise, 4).with_alignment(true);
        let chunk_size = strategy.chunk_size(10000);
        assert!(ChunkingUtils::is_cache_aligned(chunk_size, 4));
    }

    #[test]
    fn test_workload_types() {
        for workload in &[
            WorkloadType::Elementwise,
            WorkloadType::Matrix,
            WorkloadType::Reduction,
            WorkloadType::Convolution,
            WorkloadType::Custom,
        ] {
            let chunk_size = ChunkingUtils::optimal_chunk_size(*workload, 4, 10000);
            assert!(chunk_size > 0);
        }
    }
}