Qiskit / rustworkx

A high performance Python graph library implemented in Rust.
https://www.rustworkx.org
Apache License 2.0
1.11k stars 151 forks source link

Use rayon-cond to use threads conditionally without manual code duplication. #448

Open jlapeyre opened 3 years ago

jlapeyre commented 3 years ago

See this comment:

https://github.com/Qiskit/retworkx/pull/428#issuecomment-918391894

Sometimes one wants to choose whether to use multiple threads in a loop based on some runtime criteria. rayon-cond does the code duplication with a macro hidden behind a nice interface.

@mtreinish pointed to these sections that could be cleaned up this way.

https://github.com/Qiskit/retworkx/blob/main/src/shortest_path/floyd_warshall.rs#L189-L216 https://github.com/Qiskit/retworkx/blob/main/src/shortest_path/floyd_warshall.rs#L117-L140 https://github.com/Qiskit/retworkx/blob/main/src/shortest_path/average_length.rs#L65-L72 https://github.com/Qiskit/retworkx/blob/main/src/shortest_path/distance_matrix.rs#L73-L84

jlapeyre commented 3 years ago

Unfortunately, rayon's par_iter iterates over items in a collection, but rayon-cond's CondIterator iterates over references to items. So code has to be changed a bit when modifying to use rayon-cond.

jlapeyre commented 1 year ago

Finished examples of making this work are here https://github.com/Qiskit/rustworkx/pull/815 and reference therein. It looks like at least some of the items in the opening comment above are still relevant.