├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── appveyor.yml └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | **/*.rs.bk 3 | Cargo.lock 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: rust 2 | cache: cargo 3 | rust: 4 | - stable 5 | - beta 6 | - nightly 7 | script: 8 | - cargo build --verbose 9 | - cargo test --verbose -- --test-threads=1 --nocapture 10 | matrix: 11 | allow_failures: 12 | - rust: nightly 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # changelog for polyester 2 | 3 | ## Pending 4 | ### Changed 5 | - `coco` has been swapped for `crossbeam` 6 | - `par_fold` no longer requires `'static` for the iterator, its items, or any closure 7 | 8 | ## 0.1.0 - 2017-11-04 9 | 10 | Initial version: 11 | 12 | - `par_fold` 13 | - `par_map` (and `ParMap`) 14 | - `set_thread_count` 15 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "polyester" 3 | version = "0.1.0" 4 | authors = ["QuietMisdreavus "] 5 | description = "Parallel iterator adaptors that accept arbitrary iterators" 6 | documentation = "https://docs.rs/polyester" 7 | repository = "https://github.com/QuietMisdreavus/polyester" 8 | categories = ["concurrency", "rust-patterns"] 9 | keywords = ["iterators", "parallel"] 10 | license = "MPL-2.0" 11 | readme = "README.md" 12 | 13 | [badges] 14 | travis-ci = { repository = "QuietMisdreavus/polyester" } 15 | appveyor = { repository = "QuietMisdreavus/polyester" } 16 | 17 | [dependencies] 18 | num_cpus = "1" 19 | synchronoise = "0.4.0" 20 | # crossbeam-deque 0.2.0 is the version used by rayon-core 1.4.0 21 | crossbeam-deque = "0.2.0" 22 | rayon-core = "1" 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Mozilla Public License Version 2.0 2 | ================================== 3 | 4 | 1. Definitions 5 | -------------- 6 | 7 | 1.1. "Contributor" 8 | means each individual or legal entity that creates, contributes to 9 | the creation of, or owns Covered Software. 10 | 11 | 1.2. "Contributor Version" 12 | means the combination of the Contributions of others (if any) used 13 | by a Contributor and that particular Contributor's Contribution. 14 | 15 | 1.3. "Contribution" 16 | means Covered Software of a particular Contributor. 17 | 18 | 1.4. "Covered Software" 19 | means Source Code Form to which the initial Contributor has attached 20 | the notice in Exhibit A, the Executable Form of such Source Code 21 | Form, and Modifications of such Source Code Form, in each case 22 | including portions thereof. 23 | 24 | 1.5. "Incompatible With Secondary Licenses" 25 | means 26 | 27 | (a) that the initial Contributor has attached the notice described 28 | in Exhibit B to the Covered Software; or 29 | 30 | (b) that the Covered Software was made available under the terms of 31 | version 1.1 or earlier of the License, but not also under the 32 | terms of a Secondary License. 33 | 34 | 1.6. "Executable Form" 35 | means any form of the work other than Source Code Form. 36 | 37 | 1.7. "Larger Work" 38 | means a work that combines Covered Software with other material, in 39 | a separate file or files, that is not Covered Software. 40 | 41 | 1.8. "License" 42 | means this document. 43 | 44 | 1.9. "Licensable" 45 | means having the right to grant, to the maximum extent possible, 46 | whether at the time of the initial grant or subsequently, any and 47 | all of the rights conveyed by this License. 48 | 49 | 1.10. "Modifications" 50 | means any of the following: 51 | 52 | (a) any file in Source Code Form that results from an addition to, 53 | deletion from, or modification of the contents of Covered 54 | Software; or 55 | 56 | (b) any new file in Source Code Form that contains any Covered 57 | Software. 58 | 59 | 1.11. "Patent Claims" of a Contributor 60 | means any patent claim(s), including without limitation, method, 61 | process, and apparatus claims, in any patent Licensable by such 62 | Contributor that would be infringed, but for the grant of the 63 | License, by the making, using, selling, offering for sale, having 64 | made, import, or transfer of either its Contributions or its 65 | Contributor Version. 66 | 67 | 1.12. "Secondary License" 68 | means either the GNU General Public License, Version 2.0, the GNU 69 | Lesser General Public License, Version 2.1, the GNU Affero General 70 | Public License, Version 3.0, or any later versions of those 71 | licenses. 72 | 73 | 1.13. "Source Code Form" 74 | means the form of the work preferred for making modifications. 75 | 76 | 1.14. "You" (or "Your") 77 | means an individual or a legal entity exercising rights under this 78 | License. For legal entities, "You" includes any entity that 79 | controls, is controlled by, or is under common control with You. For 80 | purposes of this definition, "control" means (a) the power, direct 81 | or indirect, to cause the direction or management of such entity, 82 | whether by contract or otherwise, or (b) ownership of more than 83 | fifty percent (50%) of the outstanding shares or beneficial 84 | ownership of such entity. 85 | 86 | 2. License Grants and Conditions 87 | -------------------------------- 88 | 89 | 2.1. Grants 90 | 91 | Each Contributor hereby grants You a world-wide, royalty-free, 92 | non-exclusive license: 93 | 94 | (a) under intellectual property rights (other than patent or trademark) 95 | Licensable by such Contributor to use, reproduce, make available, 96 | modify, display, perform, distribute, and otherwise exploit its 97 | Contributions, either on an unmodified basis, with Modifications, or 98 | as part of a Larger Work; and 99 | 100 | (b) under Patent Claims of such Contributor to make, use, sell, offer 101 | for sale, have made, import, and otherwise transfer either its 102 | Contributions or its Contributor Version. 103 | 104 | 2.2. Effective Date 105 | 106 | The licenses granted in Section 2.1 with respect to any Contribution 107 | become effective for each Contribution on the date the Contributor first 108 | distributes such Contribution. 109 | 110 | 2.3. Limitations on Grant Scope 111 | 112 | The licenses granted in this Section 2 are the only rights granted under 113 | this License. No additional rights or licenses will be implied from the 114 | distribution or licensing of Covered Software under this License. 115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a 116 | Contributor: 117 | 118 | (a) for any code that a Contributor has removed from Covered Software; 119 | or 120 | 121 | (b) for infringements caused by: (i) Your and any other third party's 122 | modifications of Covered Software, or (ii) the combination of its 123 | Contributions with other software (except as part of its Contributor 124 | Version); or 125 | 126 | (c) under Patent Claims infringed by Covered Software in the absence of 127 | its Contributions. 128 | 129 | This License does not grant any rights in the trademarks, service marks, 130 | or logos of any Contributor (except as may be necessary to comply with 131 | the notice requirements in Section 3.4). 132 | 133 | 2.4. Subsequent Licenses 134 | 135 | No Contributor makes additional grants as a result of Your choice to 136 | distribute the Covered Software under a subsequent version of this 137 | License (see Section 10.2) or under the terms of a Secondary License (if 138 | permitted under the terms of Section 3.3). 139 | 140 | 2.5. Representation 141 | 142 | Each Contributor represents that the Contributor believes its 143 | Contributions are its original creation(s) or it has sufficient rights 144 | to grant the rights to its Contributions conveyed by this License. 145 | 146 | 2.6. Fair Use 147 | 148 | This License is not intended to limit any rights You have under 149 | applicable copyright doctrines of fair use, fair dealing, or other 150 | equivalents. 151 | 152 | 2.7. Conditions 153 | 154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted 155 | in Section 2.1. 156 | 157 | 3. Responsibilities 158 | ------------------- 159 | 160 | 3.1. Distribution of Source Form 161 | 162 | All distribution of Covered Software in Source Code Form, including any 163 | Modifications that You create or to which You contribute, must be under 164 | the terms of this License. You must inform recipients that the Source 165 | Code Form of the Covered Software is governed by the terms of this 166 | License, and how they can obtain a copy of this License. You may not 167 | attempt to alter or restrict the recipients' rights in the Source Code 168 | Form. 169 | 170 | 3.2. Distribution of Executable Form 171 | 172 | If You distribute Covered Software in Executable Form then: 173 | 174 | (a) such Covered Software must also be made available in Source Code 175 | Form, as described in Section 3.1, and You must inform recipients of 176 | the Executable Form how they can obtain a copy of such Source Code 177 | Form by reasonable means in a timely manner, at a charge no more 178 | than the cost of distribution to the recipient; and 179 | 180 | (b) You may distribute such Executable Form under the terms of this 181 | License, or sublicense it under different terms, provided that the 182 | license for the Executable Form does not attempt to limit or alter 183 | the recipients' rights in the Source Code Form under this License. 184 | 185 | 3.3. Distribution of a Larger Work 186 | 187 | You may create and distribute a Larger Work under terms of Your choice, 188 | provided that You also comply with the requirements of this License for 189 | the Covered Software. If the Larger Work is a combination of Covered 190 | Software with a work governed by one or more Secondary Licenses, and the 191 | Covered Software is not Incompatible With Secondary Licenses, this 192 | License permits You to additionally distribute such Covered Software 193 | under the terms of such Secondary License(s), so that the recipient of 194 | the Larger Work may, at their option, further distribute the Covered 195 | Software under the terms of either this License or such Secondary 196 | License(s). 197 | 198 | 3.4. Notices 199 | 200 | You may not remove or alter the substance of any license notices 201 | (including copyright notices, patent notices, disclaimers of warranty, 202 | or limitations of liability) contained within the Source Code Form of 203 | the Covered Software, except that You may alter any license notices to 204 | the extent required to remedy known factual inaccuracies. 205 | 206 | 3.5. Application of Additional Terms 207 | 208 | You may choose to offer, and to charge a fee for, warranty, support, 209 | indemnity or liability obligations to one or more recipients of Covered 210 | Software. However, You may do so only on Your own behalf, and not on 211 | behalf of any Contributor. You must make it absolutely clear that any 212 | such warranty, support, indemnity, or liability obligation is offered by 213 | You alone, and You hereby agree to indemnify every Contributor for any 214 | liability incurred by such Contributor as a result of warranty, support, 215 | indemnity or liability terms You offer. You may include additional 216 | disclaimers of warranty and limitations of liability specific to any 217 | jurisdiction. 218 | 219 | 4. Inability to Comply Due to Statute or Regulation 220 | --------------------------------------------------- 221 | 222 | If it is impossible for You to comply with any of the terms of this 223 | License with respect to some or all of the Covered Software due to 224 | statute, judicial order, or regulation then You must: (a) comply with 225 | the terms of this License to the maximum extent possible; and (b) 226 | describe the limitations and the code they affect. Such description must 227 | be placed in a text file included with all distributions of the Covered 228 | Software under this License. Except to the extent prohibited by statute 229 | or regulation, such description must be sufficiently detailed for a 230 | recipient of ordinary skill to be able to understand it. 231 | 232 | 5. Termination 233 | -------------- 234 | 235 | 5.1. The rights granted under this License will terminate automatically 236 | if You fail to comply with any of its terms. However, if You become 237 | compliant, then the rights granted under this License from a particular 238 | Contributor are reinstated (a) provisionally, unless and until such 239 | Contributor explicitly and finally terminates Your grants, and (b) on an 240 | ongoing basis, if such Contributor fails to notify You of the 241 | non-compliance by some reasonable means prior to 60 days after You have 242 | come back into compliance. Moreover, Your grants from a particular 243 | Contributor are reinstated on an ongoing basis if such Contributor 244 | notifies You of the non-compliance by some reasonable means, this is the 245 | first time You have received notice of non-compliance with this License 246 | from such Contributor, and You become compliant prior to 30 days after 247 | Your receipt of the notice. 248 | 249 | 5.2. If You initiate litigation against any entity by asserting a patent 250 | infringement claim (excluding declaratory judgment actions, 251 | counter-claims, and cross-claims) alleging that a Contributor Version 252 | directly or indirectly infringes any patent, then the rights granted to 253 | You by any and all Contributors for the Covered Software under Section 254 | 2.1 of this License shall terminate. 255 | 256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all 257 | end user license agreements (excluding distributors and resellers) which 258 | have been validly granted by You or Your distributors under this License 259 | prior to termination shall survive termination. 260 | 261 | ************************************************************************ 262 | * * 263 | * 6. Disclaimer of Warranty * 264 | * ------------------------- * 265 | * * 266 | * Covered Software is provided under this License on an "as is" * 267 | * basis, without warranty of any kind, either expressed, implied, or * 268 | * statutory, including, without limitation, warranties that the * 269 | * Covered Software is free of defects, merchantable, fit for a * 270 | * particular purpose or non-infringing. The entire risk as to the * 271 | * quality and performance of the Covered Software is with You. * 272 | * Should any Covered Software prove defective in any respect, You * 273 | * (not any Contributor) assume the cost of any necessary servicing, * 274 | * repair, or correction. This disclaimer of warranty constitutes an * 275 | * essential part of this License. No use of any Covered Software is * 276 | * authorized under this License except under this disclaimer. * 277 | * * 278 | ************************************************************************ 279 | 280 | ************************************************************************ 281 | * * 282 | * 7. Limitation of Liability * 283 | * -------------------------- * 284 | * * 285 | * Under no circumstances and under no legal theory, whether tort * 286 | * (including negligence), contract, or otherwise, shall any * 287 | * Contributor, or anyone who distributes Covered Software as * 288 | * permitted above, be liable to You for any direct, indirect, * 289 | * special, incidental, or consequential damages of any character * 290 | * including, without limitation, damages for lost profits, loss of * 291 | * goodwill, work stoppage, computer failure or malfunction, or any * 292 | * and all other commercial damages or losses, even if such party * 293 | * shall have been informed of the possibility of such damages. This * 294 | * limitation of liability shall not apply to liability for death or * 295 | * personal injury resulting from such party's negligence to the * 296 | * extent applicable law prohibits such limitation. Some * 297 | * jurisdictions do not allow the exclusion or limitation of * 298 | * incidental or consequential damages, so this exclusion and * 299 | * limitation may not apply to You. * 300 | * * 301 | ************************************************************************ 302 | 303 | 8. Litigation 304 | ------------- 305 | 306 | Any litigation relating to this License may be brought only in the 307 | courts of a jurisdiction where the defendant maintains its principal 308 | place of business and such litigation shall be governed by laws of that 309 | jurisdiction, without reference to its conflict-of-law provisions. 310 | Nothing in this Section shall prevent a party's ability to bring 311 | cross-claims or counter-claims. 312 | 313 | 9. Miscellaneous 314 | ---------------- 315 | 316 | This License represents the complete agreement concerning the subject 317 | matter hereof. If any provision of this License is held to be 318 | unenforceable, such provision shall be reformed only to the extent 319 | necessary to make it enforceable. Any law or regulation which provides 320 | that the language of a contract shall be construed against the drafter 321 | shall not be used to construe this License against a Contributor. 322 | 323 | 10. Versions of the License 324 | --------------------------- 325 | 326 | 10.1. New Versions 327 | 328 | Mozilla Foundation is the license steward. Except as provided in Section 329 | 10.3, no one other than the license steward has the right to modify or 330 | publish new versions of this License. Each version will be given a 331 | distinguishing version number. 332 | 333 | 10.2. Effect of New Versions 334 | 335 | You may distribute the Covered Software under the terms of the version 336 | of the License under which You originally received the Covered Software, 337 | or under the terms of any subsequent version published by the license 338 | steward. 339 | 340 | 10.3. Modified Versions 341 | 342 | If you create software not governed by this License, and you want to 343 | create a new license for such software, you may create and use a 344 | modified version of this License if you rename the license and remove 345 | any references to the name of the license steward (except to note that 346 | such modified license differs from this License). 347 | 348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary 349 | Licenses 350 | 351 | If You choose to distribute Source Code Form that is Incompatible With 352 | Secondary Licenses under the terms of this version of the License, the 353 | notice described in Exhibit B of this License must be attached. 354 | 355 | Exhibit A - Source Code Form License Notice 356 | ------------------------------------------- 357 | 358 | This Source Code Form is subject to the terms of the Mozilla Public 359 | License, v. 2.0. If a copy of the MPL was not distributed with this 360 | file, You can obtain one at http://mozilla.org/MPL/2.0/. 361 | 362 | If it is not possible or desirable to put the notice in a particular 363 | file, then You may include the notice in a location (such as a LICENSE 364 | file in a relevant directory) where a recipient would be likely to look 365 | for such a notice. 366 | 367 | You may add additional accurate notices of copyright ownership. 368 | 369 | Exhibit B - "Incompatible With Secondary Licenses" Notice 370 | --------------------------------------------------------- 371 | 372 | This Source Code Form is "Incompatible With Secondary Licenses", as 373 | defined by the Mozilla Public License, v. 2.0. 374 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # polyester 2 | 3 | some parallel iterator adapters for rust 4 | 5 | **Please note:** This library has been largely superceded by the introduction of `rayon`'s 6 | [`ParallelBridge`][] trait. The implementation of that trait is based on this library, and use of it 7 | allows you to use all of rayon's parallel adaptors, not just `map` and `fold`. 8 | 9 | [`ParallelBridge`]: https://docs.rs/rayon/1.0.3/rayon/iter/trait.ParallelBridge.html 10 | 11 | [Documentation] | [(manually-updated docs for master)][doc-dev] 12 | 13 | [Documentation]: https://docs.rs/polyester 14 | [doc-dev]: https://tonberry.quietmisdreavus.net/polyester-dev/polyester/ 15 | 16 | This work-in-progress library features an extension trait, `Polyester`, that extends `Iterator` and 17 | provides some parallel operations that allow you to spread consumption of the iterator across 18 | multiple threads. 19 | 20 | To use this crate in your own project, add the following to your Cargo.toml: 21 | 22 | ```toml 23 | [dependencies] 24 | polyester = "0.1.0" 25 | ``` 26 | 27 | ...and the following to your crate root: 28 | 29 | ```rust 30 | extern crate polyester; 31 | 32 | use polyester::Polyester; 33 | ``` 34 | 35 | ## but wait, isn't that what `rayon` does? 36 | 37 | Not quite. `rayon` is built on creating adaptors that fit its own `ParallelIterator` trait, which 38 | can only be created from fixed-length collections. `polyester` wants to work from arbitrary 39 | `Iterator`s and provide arbitrary `Iterator`s back (or consume them and provide some appropriate 40 | output). Basically, it's purely an extension trait for anything that already implements `Iterator`. 41 | This means that there are distinct design goals for `polyester` that give it different performance 42 | characteristics from `rayon`. 43 | 44 | ## architecture overview 45 | 46 | The internal design of this library changes pretty rapidly as i consider the ramifications of 47 | various design decisions, but you're welcome to take a look and offer suggestions to improve 48 | performance or suggest additional adaptors. For the moment, this is the basic idea, as of this 49 | writing (2017-10-30): 50 | 51 | The major problem with wanting to spread an iterator's values across threads is that the iterator 52 | itself becomes a bottleneck. There is only one source of iteration state, and it requires a `&mut 53 | self` borrow to get the next item. The way that `polyester` currently deals with this is placing the 54 | iterator into a background thread so it can be loaded into per-thread queues, which the worker 55 | threads pick up at the same time. 56 | 57 | There's a major drawback to this approach, though: If the worker threads are not expected to be 58 | performing a lot of per-item work, this will always be slower than just doing it sequentially. 59 | Therefore, the current version of `polyester` is only recommended if you need to perform intensive 60 | (or erratically intensive) work per item, or cannot afford to work sequentially or collect the 61 | iterator beforehand (to hand off to `rayon` instead). Note that if you have an expensive 62 | *sequential* operation in the iterator before you hand it off to `polyester`, that will impact how 63 | fast the cache-filler can generate items. 64 | 65 | Anyway, once this "hopper" is prepared, handles to it are given to a number of worker threads, so 66 | that they can run user-supplied closures on the items. Each thread has its own queue to load items 67 | from, and if its own queue is empty it will begin walking forward through other thread's queues to 68 | attempt to load more items before waiting. Each queue has an associated `SignalEvent` (from 69 | `synchronoise`) which the cache-loader worker will signal periodically while filling items or once 70 | the iterator has been exhausted. 71 | 72 | From here, each adaptor has its own processing: 73 | 74 | ### `par_fold` 75 | 76 | `par_fold` performs a basic folding operation with its items: Start from a `seed` accumulator, and 77 | iteratively add items to it until you're done. However, since there are multiple folds happening at 78 | the same time, there needs to be an additional step where all the "intermediate" accumulators are 79 | brought together. This is done by offering each worker thread a channel to hand their finished 80 | accumulator back to the calling thread, which performs the "outer fold" to merge all the 81 | accumulators together. 82 | 83 | ### `par_map` 84 | 85 | `par_map` has a simpler goal with a slightly more complicated implementation: Since it wants to turn 86 | the parallel iterator back into a sequential one, it performs the given map in each thread before 87 | handing each item back to another channel, whose `Receiver` is used in the corresponding `ParMap` 88 | `Iterator` implementation. 89 | 90 | Note that due the nature of the hopper creation and processing, **order is not guaranteed** for the 91 | items that come out of either adapter. (In fact, for `par_fold` each thread is basically getting 92 | every Nth item, and the `outer_fold` will need to reconstruct the ordering if it wants it.) 93 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | RUSTUP_INIT_SKIP_MSVC_CHECK: '1' 3 | matrix: 4 | - TARGET: stable-x86_64-pc-windows-msvc 5 | - TARGET: beta-x86_64-pc-windows-msvc 6 | - TARGET: nightly-x86_64-pc-windows-msvc 7 | - TARGET: stable-x86_64-pc-windows-gnu 8 | MSYS_BITS: 64 9 | - TARGET: beta-x86_64-pc-windows-gnu 10 | MSYS_BITS: 64 11 | - TARGET: nightly-x86_64-pc-windows-gnu 12 | MSYS_BITS: 64 13 | 14 | matrix: 15 | allow_failures: 16 | - TARGET: nightly-x86_64-pc-windows-msvc 17 | - TARGET: nightly-i686-pc-windows-msvc 18 | - TARGET: nightly-x86_64-pc-windows-gnu 19 | - TARGET: nightly-i686-pc-windows-gnu 20 | 21 | cache: 22 | - '%USERPROFILE%\.cargo -> Cargo.toml' 23 | - '%USERPROFILE%\.rustup' 24 | 25 | install: 26 | - curl -sSf -o rustup-init.exe https://win.rustup.rs/ 27 | - rustup-init.exe -y --default-toolchain %TARGET% 28 | - set PATH=%PATH%;C:\Users\appveyor\.cargo\bin 29 | - if defined MSYS_BITS set PATH=C:\msys64\mingw%MSYS_BITS%\bin;C:\msys64\usr\bin;%PATH% 30 | - rustc -vV 31 | - cargo -vV 32 | 33 | build_script: 34 | - cargo build --verbose 35 | 36 | test_script: 37 | - cargo test --verbose -- --test-threads=1 --nocapture 38 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // This Source Code Form is subject to the terms of the Mozilla Public 2 | // License, v. 2.0. If a copy of the MPL was not distributed with this 3 | // file, You can obtain one at http://mozilla.org/MPL/2.0/. 4 | 5 | //! An extension trait to provide `Iterator` adapters that process items in parallel. 6 | //! 7 | //! The heart of this crate (and what should be considered its entry point) is the [`Polyester`] 8 | //! trait, which is applied to any `Iterator` where it and its items are `Send`. 9 | //! 10 | //! [`Polyester`]: trait.Polyester.html 11 | 12 | #![doc(test(attr(allow(unused_variables))))] 13 | 14 | extern crate num_cpus; 15 | extern crate crossbeam_deque; 16 | extern crate synchronoise; 17 | extern crate rayon_core; 18 | 19 | use std::sync::{Arc, mpsc}; 20 | use std::sync::atomic::AtomicBool; 21 | use std::sync::atomic::Ordering::SeqCst; 22 | use std::sync::mpsc::channel; 23 | 24 | use crossbeam_deque::{Deque, Stealer, Steal}; 25 | use synchronoise::{SignalEvent, SignalKind}; 26 | 27 | /// A trait to extend `Iterator`s with consumers that work in parallel. 28 | /// 29 | /// This trait is applied to any iterator where it and its items are `Send`, allowing them to be 30 | /// processed by multiple threads. Importing the trait into your code allows these adaptors to be 31 | /// used like any other iterator adaptor - the only difference is that between the time they're 32 | /// started and when they finish, they'll have spawned a number of threads to perform their work. 33 | /// 34 | /// # Implementation Note 35 | /// 36 | /// It's worth noting that even though this promises parallel processing, that's no guarantee that 37 | /// it will be faster than just doing it sequentially. The iterator itself is a bottleneck for the 38 | /// processing, since it needs an exclusive `&mut self` borrow to get each item. This library 39 | /// attempts to get around that by draining the items in a separate thread into a cache that the 40 | /// workers load from, but the synchronization costs for this mean that switching `map` for 41 | /// `par_map` (for example) is not a universal win. Because of this, these adapters are only 42 | /// expected to speed up processing if your source iterator is rather quick, and the closures you 43 | /// hand to the adapters are not. 44 | pub trait Polyester 45 | { 46 | /// Fold the iterator in parallel. 47 | /// 48 | /// This method works in two parts: 49 | /// 50 | /// 1. Use a set of threads to fold items individually into a per-thread "sub-accumulator" 51 | /// using `inner_fold`. Each per-thread sub-accumulator begins with a clone of `seed`. 52 | /// 2. Once the source iterator is exhausted and has no more items, collect each intermediate 53 | /// sub-accumulator into a final accumulator, starting with the first thread's personal 54 | /// sub-accumulator and folding additional sub-accumulators using `outer_fold`. 55 | /// 56 | /// If there are no items in the iterator, `seed` is returned untouched. 57 | /// 58 | /// # Example 59 | /// 60 | /// ``` 61 | /// use polyester::Polyester; 62 | /// # fn some_expensive_computation(it: usize) -> usize { 63 | /// # if it == 7 { std::thread::sleep(std::time::Duration::from_secs(1)); } 64 | /// # it 65 | /// # } 66 | /// 67 | /// let my_results = (0..1_000_000).par_fold( 68 | /// vec![], 69 | /// |mut acc, it| { 70 | /// acc.push(some_expensive_computation(it)); 71 | /// acc 72 | /// }, 73 | /// |mut left, right| { 74 | /// left.extend(right); 75 | /// left 76 | /// } 77 | /// ); 78 | /// ``` 79 | fn par_fold( 80 | self, 81 | seed: Acc, 82 | inner_fold: InnerFold, 83 | outer_fold: OuterFold, 84 | ) -> Acc 85 | where 86 | Acc: Clone + Send, 87 | InnerFold: Fn(Acc, T) -> Acc + Send + Sync, 88 | OuterFold: Fn(Acc, Acc) -> Acc; 89 | 90 | /// Maps the given closure onto each element in the iterator, in parallel. 91 | /// 92 | /// The `ParMap` adaptor returned by this function starts up a thread pool to run `map` on each 93 | /// item of the iterator. The result of each `map` is then passed back to the calling thread, 94 | /// where it can then be returned by `ParMap`'s `Iterator` implementation. Note that `ParMap` 95 | /// will yield items in the order the *threads* return them, which may not be the same as the 96 | /// order the *source iterator* does. 97 | /// 98 | /// The `ParMap` adaptor does not start its thread pool until it is first polled, after which 99 | /// it will block for the next item until the iterator is exhausted. 100 | /// 101 | /// # Example 102 | /// 103 | /// ``` 104 | /// use polyester::Polyester; 105 | /// # fn some_expensive_computation(it: usize) -> usize { 106 | /// # if it == 7 { std::thread::sleep(std::time::Duration::from_secs(1)); } 107 | /// # it 108 | /// # } 109 | /// 110 | /// let my_results = (0..1_000_000).par_map(|it| some_expensive_computation(it)) 111 | /// .collect::>(); 112 | /// ``` 113 | fn par_map(self, map: Map) -> ParMap 114 | where 115 | Self: Sized, 116 | T: Send + 'static, 117 | Map: Fn(T) -> Out + Send + Sync + 'static, 118 | Out: Send + 'static; 119 | } 120 | 121 | impl Polyester for I 122 | where 123 | I: Iterator + Send, 124 | T: Send, 125 | { 126 | fn par_fold( 127 | self, 128 | seed: Acc, 129 | inner_fold: InnerFold, 130 | outer_fold: OuterFold, 131 | ) -> Acc 132 | where 133 | Acc: Clone + Send, 134 | InnerFold: Fn(Acc, T) -> Acc + Send + Sync, 135 | OuterFold: Fn(Acc, Acc) -> Acc 136 | { 137 | let res = rayon_core::scope(|scope| { 138 | let num_jobs = rayon_core::current_num_threads(); 139 | 140 | if num_jobs == 1 { 141 | //it's not worth collecting the items into the hopper and spawning a thread if it's 142 | //still going to be serial, just fold it inline 143 | return Err(self.fold(seed, inner_fold)); 144 | } 145 | 146 | let hopper = Hopper::new_scoped(self, num_jobs, scope); 147 | let inner_fold = Arc::new(inner_fold); 148 | let (report, recv) = channel::(); 149 | 150 | //launch the workers 151 | for id in 0..num_jobs { 152 | let hopper = hopper.clone(); 153 | let acc = seed.clone(); 154 | let inner_fold = inner_fold.clone(); 155 | let report = report.clone(); 156 | scope.spawn(move |_| { 157 | let mut acc = acc; 158 | 159 | loop { 160 | let item = hopper.get_item(id); 161 | 162 | if let Some(item) = item { 163 | acc = inner_fold(acc, item); 164 | } else { 165 | break; 166 | } 167 | } 168 | 169 | report.send(acc).unwrap(); 170 | }); 171 | } 172 | 173 | Ok((seed, recv)) 174 | }); 175 | 176 | let mut acc: Option = None; 177 | match res { 178 | Ok((seed, recv)) => { 179 | //collect and fold the workers' results 180 | for res in recv.iter() { 181 | if acc.is_none() { 182 | acc = Some(res); 183 | } else { 184 | acc = acc.map(|acc| outer_fold(acc, res)); 185 | } 186 | } 187 | 188 | acc.unwrap_or(seed) 189 | } 190 | Err(acc) => acc, 191 | } 192 | } 193 | 194 | fn par_map(self, map: Map) -> ParMap 195 | where 196 | Self: Sized, 197 | T: Send + 'static, 198 | Map: Fn(T) -> Out + Send + Sync + 'static, 199 | Out: Send + 'static 200 | { 201 | ParMap { 202 | iter: Some(self), 203 | map: Some(map), 204 | recv: None, 205 | } 206 | } 207 | } 208 | 209 | /// A parallel `map` adapter, which uses multiple threads to process items. 210 | /// 211 | /// This struct is returned by [`Polyester::par_map`]. See that function's documentation for more 212 | /// details. 213 | /// 214 | /// [`Polyester::par_map`]: trait.Polyester.html#method.par_map 215 | pub struct ParMap 216 | { 217 | iter: Option, 218 | map: Option, 219 | recv: Option>, 220 | } 221 | 222 | impl Iterator for ParMap 223 | where 224 | Iter: Iterator + Send + 'static, 225 | Iter::Item: Send + 'static, 226 | Map: Fn(Iter::Item) -> T + Send + Sync + 'static, 227 | T: Send + 'static, 228 | { 229 | type Item = T; 230 | 231 | fn next(&mut self) -> Option { 232 | if let (Some(iter), Some(map)) = (self.iter.take(), self.map.take()) { 233 | let num_jobs = rayon_core::current_num_threads(); 234 | 235 | let hopper = Hopper::new(iter, num_jobs); 236 | let map = Arc::new(map); 237 | let (report, recv) = channel(); 238 | 239 | //launch the workers 240 | for id in 0..num_jobs { 241 | let hopper = hopper.clone(); 242 | let report = report.clone(); 243 | let map = map.clone(); 244 | rayon_core::spawn(move || { 245 | loop { 246 | let item = hopper.get_item(id); 247 | 248 | if let Some(item) = item { 249 | report.send(map(item)).unwrap(); 250 | } else { 251 | break; 252 | } 253 | } 254 | }); 255 | } 256 | 257 | self.recv = Some(recv.into_iter()); 258 | } 259 | 260 | self.recv.as_mut().and_then(|r| r.next()) 261 | } 262 | } 263 | 264 | /// A distributed cache of an iterator's items, meant to be filled by a background thread so that 265 | /// worker threads can pull from a per-thread cache. 266 | struct Hopper { 267 | /// The cache of items, broken down into per-thread sub-caches. 268 | cache: Vec>, 269 | /// A set of associated `Condvar`s for worker threads to wait on while the background thread 270 | /// adds more items to its cache. 271 | signals: Vec, 272 | /// A signal that tells whether or not the source iterator has been exhausted. 273 | done: AtomicBool, 274 | } 275 | 276 | impl Hopper { 277 | /// Creates a new `Hopper` from the given iterator, with the given number of slots, and begins 278 | /// the background cache-filler worker. 279 | fn new_scoped<'a, I>(iter: I, slots: usize, scope: &rayon_core::Scope<'a>) -> Arc> 280 | where 281 | I: Iterator + Send + 'a, 282 | T: Send + 'a, 283 | { 284 | //the fillers go into the filler thread, the cache and signals go into the final hopper 285 | let mut fillers = Vec::with_capacity(slots); 286 | let mut cache = Vec::with_capacity(slots); 287 | let mut signals = Vec::::with_capacity(slots); 288 | 289 | for _ in 0..slots { 290 | let deque = Deque::new(); 291 | let stealer = deque.stealer(); 292 | fillers.push(deque); 293 | cache.push(stealer); 294 | //start the SignalEvents as "ready" in case the filler thread gets a heard start on the 295 | //workers 296 | signals.push(SignalEvent::new(true, SignalKind::Auto)); 297 | } 298 | 299 | let ret = Arc::new(Hopper { 300 | cache: cache, 301 | signals: signals, 302 | done: AtomicBool::new(false), 303 | }); 304 | 305 | let hopper = ret.clone(); 306 | 307 | scope.spawn(move |_| { 308 | let hopper = hopper; 309 | let fillers = fillers; 310 | let mut current_slot = 0; 311 | let mut rounds = 0usize; 312 | 313 | for item in iter { 314 | fillers[current_slot].push(item); 315 | 316 | current_slot = (current_slot + 1) % slots; 317 | if current_slot == 0 { 318 | rounds += 1; 319 | } 320 | 321 | //every time we've added (slots * 2) items to each slot, wake up all the threads if 322 | //they're waiting on more items 323 | if (rounds % (slots * 2)) == 0 { 324 | hopper.signals[current_slot].signal(); 325 | } 326 | } 327 | 328 | //we're out of items, so tell all the workers that we're done 329 | hopper.done.store(true, SeqCst); 330 | 331 | //...and wake them up so they can react to the "done" signal 332 | for signal in &hopper.signals { 333 | signal.signal(); 334 | } 335 | }); 336 | 337 | ret 338 | } 339 | 340 | /// Creates a new `Hopper` from the given iterator, with the given number of slots, and begins 341 | /// the background cache-filler worker. 342 | fn new(iter: I, slots: usize) -> Arc> 343 | where 344 | I: Iterator + Send + 'static, 345 | T: Send + 'static, 346 | { 347 | //the fillers go into the filler thread, the cache and signals go into the final hopper 348 | let mut fillers = Vec::with_capacity(slots); 349 | let mut cache = Vec::with_capacity(slots); 350 | let mut signals = Vec::::with_capacity(slots); 351 | 352 | for _ in 0..slots { 353 | let deque = Deque::new(); 354 | let stealer = deque.stealer(); 355 | fillers.push(deque); 356 | cache.push(stealer); 357 | //start the SignalEvents as "ready" in case the filler thread gets a heard start on the 358 | //workers 359 | signals.push(SignalEvent::new(true, SignalKind::Auto)); 360 | } 361 | 362 | let ret = Arc::new(Hopper { 363 | cache: cache, 364 | signals: signals, 365 | done: AtomicBool::new(false), 366 | }); 367 | 368 | let hopper = ret.clone(); 369 | 370 | rayon_core::spawn(move || { 371 | let hopper = hopper; 372 | let fillers = fillers; 373 | let mut current_slot = 0; 374 | let mut rounds = 0usize; 375 | 376 | for item in iter { 377 | fillers[current_slot].push(item); 378 | 379 | current_slot = (current_slot + 1) % slots; 380 | if current_slot == 0 { 381 | rounds += 1; 382 | } 383 | 384 | //every time we've added (slots * 2) items to each slot, wake up all the threads if 385 | //they're waiting on more items 386 | if (rounds % (slots * 2)) == 0 { 387 | hopper.signals[current_slot].signal(); 388 | } 389 | } 390 | 391 | //we're out of items, so tell all the workers that we're done 392 | hopper.done.store(true, SeqCst); 393 | 394 | //...and wake them up so they can react to the "done" signal 395 | for signal in &hopper.signals { 396 | signal.signal(); 397 | } 398 | }); 399 | 400 | ret 401 | } 402 | 403 | /// Attempts to steal an item from a single queue. 404 | fn steal(&self, id: usize) -> Option { 405 | loop { 406 | match self.cache[id].steal() { 407 | Steal::Empty => return None, 408 | Steal::Data(it) => return Some(it), 409 | Steal::Retry => (), 410 | } 411 | } 412 | } 413 | 414 | /// Loads an item from the given cache slot, potentially blocking while the cache-filler worker 415 | /// adds more items. 416 | fn get_item(&self, id: usize) -> Option { 417 | loop { 418 | //go pull from our cache to see if we have anything 419 | if let Some(item) = self.steal(id) { 420 | return Some(item); 421 | } 422 | 423 | //...but before we sleep, go check the other caches to see if they still have anything 424 | let mut current_id = id; 425 | loop { 426 | current_id = (current_id + 1) % self.cache.len(); 427 | if current_id == id { break; } 428 | 429 | if let Some(item) = self.steal(current_id) { 430 | return Some(item); 431 | } 432 | } 433 | 434 | //as a final check, see whether the filler thread is finished 435 | if self.done.load(SeqCst) { 436 | return None; 437 | } 438 | 439 | //otherwise, wait for the cache-filler to get more items 440 | self.signals[id].wait(); 441 | } 442 | } 443 | } 444 | 445 | #[cfg(test)] 446 | mod tests { 447 | use super::Polyester; 448 | use std::time::{Instant, Duration}; 449 | 450 | fn secs_millis(dur: Duration) -> (u64, u32) { 451 | (dur.as_secs(), dur.subsec_nanos() / 1_000_000) 452 | } 453 | 454 | #[test] 455 | fn basic_fold() { 456 | let before = Instant::now(); 457 | let par = (0..1_000_000).par_fold(0usize, |l,r| l+r, |l,r| l+r); 458 | let after_par = Instant::now(); 459 | let seq = (0..1_000_000).fold(0usize, |l,r| l+r); 460 | let after_seq = Instant::now(); 461 | 462 | let par_dur = secs_millis(after_par.duration_since(before)); 463 | let seq_dur = secs_millis(after_seq.duration_since(after_par)); 464 | println!(""); 465 | println!(" parallel fold: {}.{:03}s", par_dur.0, par_dur.1); 466 | println!(" sequential fold: {}.{:03}s", seq_dur.0, seq_dur.1); 467 | 468 | assert_eq!(par, seq); 469 | } 470 | 471 | #[test] 472 | fn basic_map() { 473 | let before = Instant::now(); 474 | let mut par = (0..1_000_000).par_map(|x| x*x).collect::>(); 475 | let after_par = Instant::now(); 476 | let mut seq = (0..1_000_000).map(|x| x*x).collect::>(); 477 | let after_seq = Instant::now(); 478 | 479 | par.sort(); 480 | seq.sort(); 481 | 482 | let par_dur = secs_millis(after_par.duration_since(before)); 483 | let seq_dur = secs_millis(after_seq.duration_since(after_par)); 484 | println!(""); 485 | println!(" parallel map: {}.{:03}s", par_dur.0, par_dur.1); 486 | println!(" sequential map: {}.{:03}s", seq_dur.0, seq_dur.1); 487 | 488 | assert_eq!(par, seq); 489 | } 490 | } 491 | --------------------------------------------------------------------------------