|
| 1 | +####################################################################### |
| 2 | +# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org> |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# This source code is licensed under a BSD-style license (found in the |
| 6 | +# LICENSE file in the root directory of this source tree) |
| 7 | +####################################################################### |
| 8 | + |
| 9 | +import numpy as np |
| 10 | +import blosc2 |
| 11 | +import time |
| 12 | + |
| 13 | + |
| 14 | +def run_benchmark(num_arrays=10, size=500, aligned_chunks=False, axis=0): |
| 15 | + """ |
| 16 | + Benchmark blosc2.concatenate performance with different chunk alignments. |
| 17 | +
|
| 18 | + Parameters: |
| 19 | + - num_arrays: Number of arrays to concatenate |
| 20 | + - size: Base size for array dimensions |
| 21 | + - aligned_chunks: Whether to use aligned chunk shapes |
| 22 | + - axis: Axis along which to concatenate (0 or 1) |
| 23 | + """ |
| 24 | + if axis == 0: |
| 25 | + # For concatenating along axis 0, the second dimension must be consistent |
| 26 | + shapes = [(size // num_arrays, size) for _ in range(num_arrays)] |
| 27 | + elif axis == 1: |
| 28 | + # For concatenating along axis 1, the first dimension must be consistent |
| 29 | + shapes = [(size, size // num_arrays) for _ in range(num_arrays)] |
| 30 | + else: |
| 31 | + raise ValueError("Only axis 0 and 1 are supported") |
| 32 | + |
| 33 | + # Create appropriate chunk shapes |
| 34 | + if aligned_chunks: |
| 35 | + # Aligned chunks: divisors of the shape dimensions |
| 36 | + chunk_shapes = [(shape[0] // 4, shape[1] // 4) for shape in shapes] |
| 37 | + else: |
| 38 | + # Unaligned chunks: not divisors of shape dimensions |
| 39 | + chunk_shapes = [(shape[0] // 4 + 1, shape[1] // 4 - 1) for shape in shapes] |
| 40 | + |
| 41 | + # Create arrays |
| 42 | + arrays = [] |
| 43 | + for i, (shape, chunk_shape) in enumerate(zip(shapes, chunk_shapes)): |
| 44 | + arr = blosc2.arange( |
| 45 | + i * np.prod(shape), |
| 46 | + (i + 1) * np.prod(shape), |
| 47 | + 1, |
| 48 | + dtype="i4", |
| 49 | + shape=shape, |
| 50 | + chunks=chunk_shape |
| 51 | + ) |
| 52 | + arrays.append(arr) |
| 53 | + |
| 54 | + # Time the concatenation |
| 55 | + start_time = time.time() |
| 56 | + result = blosc2.concatenate(arrays, axis=axis) |
| 57 | + duration = time.time() - start_time |
| 58 | + |
| 59 | + # Force evaluation by accessing a value |
| 60 | + #_ = result[0, 0] |
| 61 | + |
| 62 | + return duration, result.shape |
| 63 | + |
| 64 | + |
| 65 | +def main(): |
| 66 | + print(f"{'=' * 50}") |
| 67 | + print(f"Blosc2 concatenation benchmark") |
| 68 | + print(f"{'=' * 50}") |
| 69 | + |
| 70 | + # Parameters |
| 71 | + sizes = [400, 800, 1600, 3200] # must be divisible by 4 for aligned chunks |
| 72 | + num_arrays = 10 |
| 73 | + |
| 74 | + for axis in [0, 1]: |
| 75 | + print(f"\nConcatenating {num_arrays} arrays along axis {axis}") |
| 76 | + print(f"{'Size':<10} {'Unaligned (s)':<15} {'Aligned (s)':<15} {'Speedup':<10}") |
| 77 | + print(f"{'-' * 50}") |
| 78 | + |
| 79 | + for size in sizes: |
| 80 | + # Run both benchmarks |
| 81 | + unaligned_time, shape1 = run_benchmark(num_arrays, size, aligned_chunks=False, axis=axis) |
| 82 | + aligned_time, shape2 = run_benchmark(num_arrays, size, aligned_chunks=True, axis=axis) |
| 83 | + |
| 84 | + # Calculate speedup |
| 85 | + speedup = unaligned_time / aligned_time if aligned_time > 0 else float('inf') |
| 86 | + |
| 87 | + # Print results |
| 88 | + print(f"{size:<10} {unaligned_time:<15.4f} {aligned_time:<15.4f} {speedup:<10.2f}x") |
| 89 | + |
| 90 | + # Quick verification of result shape |
| 91 | + if axis == 0: |
| 92 | + expected_shape = (size, size) # After concatenation along axis 0 |
| 93 | + else: |
| 94 | + expected_shape = (size, size) # After concatenation along axis 1 |
| 95 | + if shape1 != expected_shape: |
| 96 | + print(f"Warning: result shape unaligned {shape1} does not match expected shape {expected_shape}") |
| 97 | + if shape2 != expected_shape: |
| 98 | + print(f"Warning: result shape aligned {shape2} does not match expected shape {expected_shape}") |
| 99 | + |
| 100 | + print(f"{'=' * 50}") |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments