1+ # !/usr/bin/env julia
2+ # Broadcast operation benchmark for Issue #78
3+
4+ using Pkg
5+ Pkg. activate (dirname (@__DIR__ ) * " /GreenFunc.jl" )
6+
7+ using GreenFunc
8+ using GreenFunc. MeshArrays
9+ using CompositeGrids
10+
11+ println (" Issue #78 - Broadcast Operation Benchmark" )
12+ println (" =" ^ 60 )
13+
14+ # Create grids for test
15+ k1 = CompositeGrids. SimpleG. Uniform ([0.0 , 1.0 ], 10 )
16+ k2 = CompositeGrids. SimpleG. Uniform ([0.0 , 1.0 ], 10 )
17+
18+ # Create MeshArrays
19+ ma1 = MeshArray (k1, k2; dtype= Float64)
20+ ma2 = MeshArray (k1, k2; dtype= Float64)
21+
22+ # Fill with data
23+ ma1. data .= rand (size (ma1. data)... )
24+ ma2. data .= rand (size (ma2. data)... )
25+
26+ println (" \n Testing broadcast operations on 10x10 MeshArray:" )
27+ println (" -" ^ 40 )
28+
29+ # Warmup
30+ ma1 .+ = ma2
31+ ma1 .*= 2.0
32+
33+ # Reset
34+ ma1. data .= rand (size (ma1. data)... )
35+
36+ # Measure broadcast allocations
37+ println (" \n In-place broadcast operations:" )
38+ alloc_add = @allocated ma1 .+ = ma2
39+ println (" ma1 .+= ma2: $(alloc_add) bytes" )
40+
41+ alloc_mul = @allocated ma1 .*= 2.0
42+ println (" ma1 .*= 2.0: $(alloc_mul) bytes" )
43+
44+ alloc_scalar = @allocated ma1 .+ = 1.0
45+ println (" ma1 .+= 1.0: $(alloc_scalar) bytes" )
46+
47+ # Test with larger arrays
48+ println (" \n Testing with 100x100 MeshArray:" )
49+ println (" -" ^ 40 )
50+
51+ k1_large = CompositeGrids. SimpleG. Uniform ([0.0 , 1.0 ], 100 )
52+ k2_large = CompositeGrids. SimpleG. Uniform ([0.0 , 1.0 ], 100 )
53+
54+ ma1_large = MeshArray (k1_large, k2_large; dtype= Float64)
55+ ma2_large = MeshArray (k1_large, k2_large; dtype= Float64)
56+
57+ ma1_large. data .= rand (size (ma1_large. data)... )
58+ ma2_large. data .= rand (size (ma2_large. data)... )
59+
60+ # Warmup
61+ ma1_large .+ = ma2_large
62+
63+ # Reset and measure
64+ ma1_large. data .= rand (size (ma1_large. data)... )
65+
66+ alloc_add_large = @allocated ma1_large .+ = ma2_large
67+ println (" ma1 .+= ma2 (100x100): $(alloc_add_large) bytes" )
68+
69+ alloc_mul_large = @allocated ma1_large .*= 2.0
70+ println (" ma1 .*= 2.0 (100x100): $(alloc_mul_large) bytes" )
71+
72+ println (" \n Conclusion:" )
73+ if alloc_add == 0 && alloc_mul == 0
74+ println (" ✓ Broadcast operations are allocation-free for small arrays" )
75+ else
76+ println (" ⚠ Broadcast operations still allocate for small arrays" )
77+ end
78+
79+ if alloc_add_large < 1000
80+ println (" ✓ Large array broadcast allocations minimal: $(alloc_add_large) bytes" )
81+ else
82+ println (" ⚠ Large array broadcast allocations: $(round (alloc_add_large / 1024 , digits= 2 )) KB" )
83+ end
0 commit comments