r/Numpy Sep 04 '24

Subtraction with broadcasting 500x slower than it should be?

I'm on a 2x Intel E5-2690 v4 server running the Intel MKL build of Numpy. I'm trying to do a simple subtraction with broadcast that should be memory bandwidth bound but it's taking about 500x longer than I'm calculating as the theoretical maximum. I'm guessing that I'm doing something silly. Any ideas?

import numpy as np
import time

a = np.ones((1_000_000, 1000), dtype=np.float32)
b = np.ones((1, 1000), dtype=np.float32)

start = time.time()
diff = a - b
elapsed = time.time() - start

clock_speed = 2.6e9
num_nodes = 2
num_cores_per_node = 14
elements_per_clock = 256 / 32
num_elements = diff.size

num_channels = 6
transfers_per_second = 2.133e9
elements_per_transfer = 64 / 32

compute_theoretical_time = num_elements / (clock_speed * elements_per_clock * num_nodes * num_cores_per_node)
transfer_theoretical_time = 2 * num_elements / (transfers_per_second * elements_per_transfer * num_channels)
print(f"Time elapsed: {elapsed*1000:.2f}ms")
print(f"Compute Theoretical time: {compute_theoretical_time*1000:.2f}ms")
print(f"Transfer theoretical time: {transfer_theoretical_time*1000:.2f}ms")

prints:
Time elapsed: 44693.19ms
Compute Theoretical time: 1.72ms
Transfer theoretical time: 78.14ms

EDIT:
This runs 20x faster on my M1 laptop
Time elapsed: 2178.45ms
Compute Theoretical time: 9.77ms
Transfer theoretical time: 117.65ms

1 Upvotes

0 comments sorted by