Start by clarifying the problem constraints (input size, kernel size, thread count, hardware features) and then present a unified configurable routine that adapts partitioning, tiling, and vectorization based on the scenario. For each scenario, explain the trade-offs in work partitioning, cache locality, false sharing avoidance, synchronization, and SIMD usage, and justify your choices with performance reasoning.
Pro tip: Emphasize that you would measure and tune parameters like tile size and chunk size using profiling, and mention that you'd consider using non-temporal stores for large outputs to avoid cache pollution.
Ask about input size, kernel size, number of threads, CPU architecture (SIMD width, cache sizes), and whether the kernel is known at compile time. This determines the design space.
Propose an API that takes input, kernel, output, and a configuration struct with parameters like tile size, chunk size, and thread count. The routine internally selects a strategy based on the scenario.
For short kernel: partition output into contiguous chunks per thread, use SIMD across output elements, and avoid synchronization. For full-length kernel: use a tiled approach where each thread computes a tile of output, loading input tiles into shared memory or registers, and synchronize only if needed. For thread-capped: use a work-stealing or dynamic scheduling with atomic counters to balance load, and consider persistent threads.
Use tiling to keep input and kernel in cache, align data to cache lines, and pad per-thread accumulators to avoid false sharing. For thread-capped, ensure that work chunks are large enough to amortize synchronization but small enough for load balance.
Vectorize the inner loop over output elements using SIMD intrinsics or compiler auto-vectorization, ensuring alignment and no dependencies. Choose tile sizes to fit in L1/L2 cache and chunk sizes to minimize overhead; tune empirically.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.