Katkat

Implement Distributed Matrix Multiplication

xAIMachine Learning Engineer · Technical Screen

Last updated

This problem tests your ability to design a distributed matrix multiplication algorithm by partitioning large matrices across multiple worker nodes to minimize communication overhead.

Problem: Distributed Matrix Multiplication

You are tasked with designing a system to multiply two large matrices, A (size M x K) and B (size K x N), across a set of P computing nodes. Since these matrices are too large to fit into the memory of a single machine, they must be partitioned.

Assume you have a compute_node abstraction with the following methods:

  • send(target_id, data): Sends a chunk of data to another node.
  • receive(source_id): Blocks until data is received from a node.
  • local_matmul(block_A, block_B): Performs standard matrix multiplication on local memory.

Task

Implement a distributed multiplication strategy. You can assume that the matrices are already partitioned into sub-blocks and distributed across nodes. Propose a partitioning strategy (e.g., Cannon's algorithm or a simpler row-column distribution) and define the sequence of send/receive operations required for a single node to compute its assigned portion of the final matrix C.

Constraints

  • Assume P is a perfect square (e.g., P = 4, 9, 16).
  • You must account for data dependency and synchronization to avoid deadlocks.
  • The result matrix C = A * B will also be distributed across the nodes.

Example

If M=K=N=4 and P=4, you partition A and B into 2x2 blocks of size 2x2. Each node (i, j) in the grid will be responsible for computing block C_{i,j}.

def compute_distributed_block(node_id, my_block_A, my_block_B, neighbors):
    # Design the logic here to pass blocks between nodes 
    # to achieve local_matmul(A_chunk, B_chunk)
    pass

Edge Cases

  • What happens if the dimensions M, K, and N are not perfectly divisible by the square root of P?
  • How do you handle network latency during the communication phases?

Hints

Hint 1

Consider Cannon's algorithm, which uses a grid-based approach to shift blocks of A leftward and blocks of B upward in cyclic steps.

Hint 2

Map the matrix indices to the grid nodes such that node (i, j) is responsible for row-strip i of A and column-strip j of B.

Hint 3

Ensure that you implement a barrier or synchronization point after each shift phase to prevent race conditions during the multiplication step.

Follow-up questions

  • How would you modify your implementation if the network bandwidth between nodes was significantly lower than the local memory throughput?
  • If one node fails during the calculation, how could you implement a checkpointing or re-computation strategy to ensure fault tolerance?
  • How would you optimize the memory usage if the matrices are sparse? Does your distribution strategy change?