basic_dsp 0.2.1

Digital signal processing based on 1xN (one times N) or Nx1 vectors in real or complex number space. Vectors come with basic arithmetic, convolution, Fourier transformation and interpolation operations.
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
439
440
441
442
443
use num_cpus;
use std::slice::{Chunks, ChunksMut};
use num::traits::Float;
use crossbeam;
use std::ops::Range;
use std::sync::{Mutex, Arc};
use std::mem;
use super::RealNumber;
use std::iter::Iterator;

/// Indicates how complex an operation is and determines how many cores 
/// will be used since operations with smaller complexity are memory bus bound
/// and not CPU bound
#[derive(Copy)]
#[derive(Clone)]
#[derive(PartialEq)]
#[derive(Debug)]
pub enum Complexity {
	Small,
    Medium,
    Large
}

/// Holds parameters which specifiy how multiple cores are used
/// to execute an operation.
#[derive(Debug, Copy)] 
#[repr(C)]  
pub struct MultiCoreSettings {
    /// All operations will be limited to not create more threads than specified here
    pub core_limit: usize,
    
    /// Indicates whether the temp arrays of a vector should already be allocated during
    /// construction
    pub early_temp_allocation: bool
    // TODO: Specify and use options such as core/thread limits
}

impl MultiCoreSettings {
    /// Creates multi core settings with default values
    pub fn default() -> MultiCoreSettings {
        // Half because we assume hyper threading and that we will keep a core so busy
        // that hyper threading isn't of any use
        Self::new(num_cpus::get() / 2, false)
    }
    
    /// Creates multi core settings with the given values.
    pub fn new(core_limit: usize, early_temp_allocation: bool) -> MultiCoreSettings {
        MultiCoreSettings {
            core_limit: if core_limit >= 1 { core_limit } else { 1 }, 
            early_temp_allocation: early_temp_allocation
        }
    }
}

impl Clone for MultiCoreSettings {
    fn clone(&self) -> Self {
        MultiCoreSettings {
            core_limit: self.core_limit,
            early_temp_allocation: self.early_temp_allocation
        }
    }

    fn clone_from(&mut self, source: &Self) {
        self.core_limit = source.core_limit;
    }
}

/// Contains logic which helps to perform an operation
/// in parallel by dividing an array into chunks.
pub struct Chunk;
impl Chunk
{  
    /// Figures out how many threads make use for the an operation with the given complexity on 
    /// an array with the given size. 
    ///
    /// This method tries to balance the expected performance gain vs. CPU utilization since there is in most cases
    /// no point to keep all CPU cores busy only to get 5 to 10% performance gain.
    /// The expected performance gain is roughly estimated based on three factors:
    /// 1. More cores improves the calculation speed according to Amdahl's law (`https://en.wikipedia.org/wiki/Amdahl's_law`)
    /// 2. Spawning a thread consumes time and so the array length must be large enough to so that the expected performance
    ///    gain justifies the effort to spawn a thread/invoke the thread pool. 
    /// 3. The CPU is not the only resource and if one of the other resources is a bottleneck then Amdahl's law won't be applicable.
    ///    The memory bus speed is limited (20GB/s in case of a typical 2015 consumer laptop in the price range of $1000) and
    ///    for operations which only require a few CPU cycles already one or two cores will process data faster than the 
    ///    memory bus is able to provide and to transport back. Using more cores then only creates heat but no performance benefit.
	#[inline]
	fn determine_number_of_chunks(array_length: usize, complexity: Complexity, settings: &MultiCoreSettings) -> usize
	{
        let mut cores = num_cpus::get();
        if cores > settings.core_limit {
            cores = settings.core_limit;
        }
        if complexity == Complexity::Large || cores == 1 {
            cores
        }
        else if complexity == Complexity::Small  {
            if array_length < 500000 {
                1
            }
            else {
                if cores >= 2 {
                    2  
                } else {
                    1
                }
            }
        }
        else { // complexity == medium
            if array_length < 10000 {
                1
            }
            else if array_length < 50000 {
                if cores >= 2 {
                    2  
                } else {
                    1
                }
            }
            else {
                cores
            }
        }
	}
    
    /// Partitions an array into the given number of chunks. It makes sure that all chunks have the same size
    /// and so it will happen that some elements at the end of the array are not part of any chunk. 
	#[inline]
	fn partition<T>(array: &[T], array_length: usize, step_size: usize, number_of_chunks: usize) -> Chunks<T>
		where T : Float + Copy + Clone + Send
	{
		let chunk_size = Chunk::calc_chunk_size(array_length, step_size, number_of_chunks);
		array[0 .. array_length].chunks(chunk_size)
	}
	
    /// Partitions an array into the given number of chunks. It makes sure that all chunks have the same size
    /// and so it will happen that some elements at the end of the array are not part of any chunk. 
	#[inline]
	fn partition_mut<T>(array: &mut [T], array_length: usize, step_size: usize, number_of_chunks: usize) -> ChunksMut<T>
		where T : Copy + Clone + Send
	{
		let chunk_size = Chunk::calc_chunk_size(array_length, step_size, number_of_chunks);
		array[0 .. array_length].chunks_mut(chunk_size)
	}
	
	#[inline]
	fn calc_chunk_size(array_length: usize, step_size: usize, number_of_chunks: usize) -> usize
	{
		let mut chunk_size = (array_length as f64/ number_of_chunks as f64).ceil() as usize;
		let remainder = chunk_size % step_size;
		if remainder > 0
		{
			chunk_size += step_size - chunk_size % step_size;
		}
		
		chunk_size
	}
	
    /// This function returns the ranges which correspond to the chunks generated by `partition_in_number`. 
	#[inline]
	fn partition_in_ranges(array_length: usize, step_size: usize, number_of_chunks: usize) -> Vec<Range<usize>>
	{
		let chunk_size = Chunk::calc_chunk_size(array_length, step_size, number_of_chunks);
		let mut ranges = Vec::with_capacity(number_of_chunks);
		let mut sum = 0;
		for i in 0..number_of_chunks {
			let new_sum = if i < number_of_chunks - 1 { sum + chunk_size } else { array_length };
			ranges.push(Range { start: sum, end: new_sum });
			sum = new_sum;
		} 
		
		ranges
	}
	
    /// Executes the given function on the first `array_length` elements of the given array in parallel and passes
    /// the argument to all function calls.
	#[inline]
	pub fn execute_partial<T,S,F>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            array: &mut [T], array_length: usize, step_size: usize, 
            arguments:S, ref function: F)
		where F: Fn(&mut [T], S) + 'static + Sync, 
			  T: RealNumber,
			  S: Sync + Copy + Send
	{
		let number_of_chunks = Chunk::determine_number_of_chunks(array_length, complexity, settings);
		if number_of_chunks > 1
		{
            let chunks = Chunk::partition_mut(array, array_length, step_size, number_of_chunks);
            crossbeam::scope(|scope| {
                for chunk in chunks {
                    scope.spawn(move|| {
                        function(chunk, arguments);
                    });
                }
            });
		}
		else
		{
			function(&mut array[0..array_length], arguments);
		}
	}
    
    /// Executes the given function on the all elements of the array and also tells the function on which range/chunk
    /// it operates on.
	#[inline]
	pub fn execute_with_range<T,S,F>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            array: &mut [T], array_length: usize, step_size: usize, 
            arguments: S, ref function: F)
		where F: Fn(&mut [T], Range<usize>, S) + 'static + Sync,
			  T : Copy + Clone + Send + Sync,
			  S: Sync + Copy + Send
	{
		let number_of_chunks = Chunk::determine_number_of_chunks(array_length, complexity, settings);
		if number_of_chunks > 1
		{
			let chunks = Chunk::partition_mut(array, array_length, step_size, number_of_chunks);
			let ranges = Chunk::partition_in_ranges(array_length, step_size, chunks.len());
            crossbeam::scope(|scope| {
                for chunk in chunks.zip(ranges) {
                    scope.spawn(move|| {
                        function(chunk.0, chunk.1, arguments);
                    });
                }
            });
		}
		else
		{
			function(&mut array[0..array_length], Range { start: 0, end: array_length }, arguments);
		}
	}
    
    /// Executes the given function on the all elements of the array and also tells the function on which range/chunk
    /// it operates on.
    ///
    /// This function will chunk the array into an even number and pass every
    /// call to `function` two chunks. The two chunks will always be symmetric 
    /// around 0. This allos `function` to make use of symmetry properties of the
    /// underlying data or the argument.
	#[inline]
	pub fn execute_sym_pairs_with_range<T,S,F>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            array: &mut [T], array_length: usize, step_size: usize, 
            arguments: S, ref function: F)
		where F: Fn(&mut &mut [T], &Range<usize>, &mut &mut [T], &Range<usize>, S) + 'static + Sync,
			  T: Copy + Clone + Send + Sync,
			  S: Sync + Copy + Send
	{
		let number_of_chunks = 2 * Chunk::determine_number_of_chunks(array_length, complexity, settings);
		if number_of_chunks > 2
		{
			let chunks = Chunk::partition_mut(array, array_length, step_size, number_of_chunks);
			let ranges = Chunk::partition_in_ranges(array_length, step_size, chunks.len());
            let mut i = 0;
            let (mut chunks1, mut chunks2): (Vec<_>, Vec<_>) = 
                chunks.partition(|_c| { i += 1; i <= number_of_chunks / 2 });
            i = 0;
            let (ranges1, ranges2): (Vec<_>, Vec<_>)  = 
                ranges.iter().partition(|_r| { i += 1; i <= number_of_chunks / 2 });
            let chunks2 = chunks2.iter_mut().rev();
            let ranges2 = ranges2.iter().rev();
            let zipped1 = chunks1.iter_mut().zip(ranges1);
            let zipped2 = chunks2.zip(ranges2);
            crossbeam::scope(|scope| {
                for chunk in zipped1.zip(zipped2) {
                    scope.spawn(move|| {
                        let (pair1, pair2) = chunk;
                        function(pair1.0, pair1.1, pair2.0, pair2.1, arguments);
                    });
                }
            });
		}
		else
		{
            let mut chunks = Chunk::partition_mut(array, array_length, step_size, number_of_chunks);
            let mut chunks1 = chunks.next().unwrap();
            let len1 = chunks1.len();
            let mut chunks2 = chunks.next().unwrap();
			function(
                &mut chunks1, 
                &Range { start: 0, end: len1 }, 
                &mut chunks2, 
                &Range { start: len1, end: array_length }, 
                arguments);
		}
	}
    
    /// Executes the given function on the all elements of the array in parallel. A result is
    /// returned for each chunk.
	#[inline]
	pub fn get_a_fold_b<F, T, R>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            a: &[T], a_len: usize, a_step: usize, 
            b: &[T], b_len: usize, b_step: usize, 
            ref function: F) -> Vec<R>
		where F: Fn(&[T], Range<usize>, &[T]) -> R + 'static + Sync,
			  T: Float + Copy + Clone + Send + Sync,
              R: Send
	{
		let number_of_chunks = Chunk::determine_number_of_chunks(a_len, complexity, settings);
		if number_of_chunks > 1
		{
			let chunks = Chunk::partition(b, b_len, b_step, number_of_chunks);
			let ranges = Chunk::partition_in_ranges(a_len, a_step, chunks.len());
            let result = Vec::with_capacity(chunks.len());
            let stack_array = Arc::new(Mutex::new(result));
            crossbeam::scope(|scope| {
                for chunk in chunks.zip(ranges) {
                    let stack_array = stack_array.clone();
                    scope.spawn(move|| {
                        let r = function(a, chunk.1, chunk.0);
                        stack_array.lock().unwrap().push(r);
                    });
                }
            });
            let mut guard = stack_array.lock().unwrap();
            mem::replace(&mut guard, Vec::new())
		}
		else
		{
			let result = function(a, Range { start: 0, end: a_len }, &b[0..b_len]);
            vec![result]
		}
	}
    
    /// Executes the given function on the all elements of the array in parallel. A result is
    /// returned for each chunk.
	#[inline]
	pub fn get_chunked_results<F, S, T, R>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            a: &[T], a_len: usize, a_step: usize, 
            arguments:S, ref function: F) -> Vec<R>
		where F: Fn(&[T], Range<usize>, S) -> R + 'static + Sync,
			  T: Float + Copy + Clone + Send + Sync,
              R: Send,
              S: Sync + Copy + Send
	{
		let number_of_chunks = Chunk::determine_number_of_chunks(a_len, complexity, settings);
		if number_of_chunks > 1
		{
			let chunks = Chunk::partition(a, a_len, a_step, number_of_chunks);
            let ranges = Chunk::partition_in_ranges(a_len, a_step, chunks.len());
            let result = Vec::with_capacity(chunks.len());
            let stack_array = Arc::new(Mutex::new(result));
            crossbeam::scope(|scope| {
                for chunk in chunks.zip(ranges) {
                    let stack_array = stack_array.clone();
                    scope.spawn(move|| {
                        let r = function(chunk.0, chunk.1, arguments);
                        stack_array.lock().unwrap().push(r);
                    });
                }
            });
            let mut guard = stack_array.lock().unwrap();
            mem::replace(&mut guard, Vec::new())
		}
		else
		{
			let result = function(&a[0..a_len], Range { start: 0, end: a_len }, arguments);
            vec![result]
		}
	}
    
    /// Executes the given function on the all elements of the array in parallel and passes
    /// the argument to all function calls.. Results are intended to be stored in the target array.
	#[inline]
	pub fn from_src_to_dest<T,S,F>(
            complexity: Complexity, 
            settings: &MultiCoreSettings, 
            original: &[T], original_length: usize, original_step: usize, 
            target: &mut [T], target_length: usize, target_step: usize, 
            arguments: S, ref function: F)
		where F: Fn(&[T], Range<usize>, &mut [T], S) + 'static + Sync,
			  T: Float + Copy + Clone + Send + Sync,
			  S: Sync + Copy + Send
	{
		let number_of_chunks = Chunk::determine_number_of_chunks(original_length, complexity, settings);
		if number_of_chunks > 1
		{
			let chunks = Chunk::partition_mut(target, target_length, target_step, number_of_chunks);
			let ranges = Chunk::partition_in_ranges(original_length, original_step, chunks.len());
            
            crossbeam::scope(|scope| {
                for chunk in chunks.zip(ranges) {
                    scope.spawn(move|| {
                        function(original, chunk.1, chunk.0, arguments);
                    });
                }
            });
		}
		else
		{
			function(original, Range { start: 0, end: original_length }, &mut target[0..target_length], arguments);
		}
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use std::ops::Range;
	
	#[test]
	fn partition_array()
	{
		let mut array = [0.0; 256];
		let chunks = Chunk::partition_mut(&mut array, 256, 4, 2);
		assert_eq!(chunks.len(), 2);
		for chunk in chunks
		{
			assert_eq!(chunk.len(), 128);
		}
	}
	
	#[test]
	fn partition_array_8_cores()
	{
		let mut array = [0.0; 1023];
		let chunks = Chunk::partition_mut(&mut array, 1023, 4, 8);
		assert_eq!(chunks.len(), 8);
		let mut i = 0;
		for chunk in chunks
		{
			let expected = if i >= 7 { 127 } else { 128 };
			assert_eq!(chunk.len(), expected);
			i += 1;
		}
	}
	
	#[test]
	fn partitionin_ranges()
	{
		let ranges = Chunk::partition_in_ranges(1023, 4, 2);
		assert_eq!(ranges.len(), 2);
		assert_eq!(ranges[0], Range { start: 0, end: 512 });
		assert_eq!(ranges[1], Range { start: 512, end: 1023 });
	}
}