← 返回 ibm 的题目列表1-D Valid Convolution with Multithreading Follow-up
类型:qbank
Implement valid 1-D convolution over a numeric input and kernel with bias, then discuss CPU/hardware optimisations and how to parallelise under different input/kernel/thread sizes.
Requirements
Implement a "valid" 1-D convolution function: compute outputs only for complete input regions covered by the kernel.
Inputs include an input vector, a kernel vector, and a scalar bias.
For each valid offset, output the dot product between the input slice and kernel, plus bias.
Follow-ups ask for optimisation methods on CPU/hardware and how to code multithreading for:
input length around 1 million, kernel length 3;
input length around 1 million, kernel length 1 million;
a maximum of 100 threads.
Examples
input = [1, 2, 3, 4, 5]
kernel = [2, 1, 0.5]
bias = 0.5
output[0] = (1 * 2) + (2 * 1) + (3 * 0.5) + 0.5 = 6
output[1] = (2 * 2) + (3 * 1) + (4 * 0.5) + 0.5 = 9.5
output[2] = (3 * 2) + (4 * 1) + (5 * 0.5) + 0.5 = 13
Notes
Base output length is len(input) - len(kernel) + 1 when the kernel is no longer than the input.
A clean scalar implementation is two loops: output offset outside, kernel index inside.
For small kernels and large input, split output indices across worker threads.
For a kernel as large as the input, the number of valid output positions is tiny, so thread fan-out over output positions provides little benefit; discuss vectorisation, cache locality, and whether the problem shape justifies parallel overhead.
NumPy-style vector slicing is a useful baseline optimisation, but the follow-up explicitly probes hardware-level parallelism.
Preparation
Implement the scalar valid convolution, then benchmark or reason through the three shapes separately: tiny kernel, huge kernel, and bounded thread count.
Practise explaining chunk boundaries by output index, not input index; each output owns a complete kernel window, so threads do not need to write shared output cells.