HeadlinesBriefing favicon HeadlinesBriefing.com

Parallel Parentheses Matching Algorithms in Futhark

Hacker News •
×

A professor at the PLTC section introduced the author to Oleg Kiselyov's 'More Fun with Monoids' paper, sparking an investigation into map-reduce patterns for parentheses matching. The classic sequential stack approach processes opening and closing parentheses in linear time, tracking balance with push and pop operations. This technique translates naturally to functional languages like Haskell.

For parallel execution, the author maps parentheses to +1/-1 values and applies parallel prefix sums to track stack sizes. The initial Futhark implementation achieves O(n) work with O(log n) span, but requires two kernel launches. A single monoid reduce combines total sum and minimum prefix values, reducing memory traffic to approximately n bytes while maintaining the same complexity bounds.

However, this approach only works for single parenthesis types. Multiple bracket variants like {[]} require tracking nesting depths and stable sorting to pair matching elements correctly. The author implements this using radix sort in Futhark, achieving O(n) work with O(log n) span, though finding the sorting overhead unsatisfying.

The piece connects to the broader 'all previous smaller or equal' problem studied by Berkman, Schieber & Vishkin, suggesting more efficient parallel solutions exist through reduction tree techniques rather than sorting-based approaches.