1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
#![allow(non_snake_case)] use super::maybe_done::{maybe_done, MaybeDone}; use super::{ContextId, GpuTask, Progress}; macro_rules! generate_join { ($( $(#[$doc:meta])* ($Join:ident, <A, $($B:ident),*>), )*) => ($( $(#[$doc])* pub struct $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { id: ContextId, a: MaybeDone<A, A::Output, Ec>, $($B: MaybeDone<$B, $B::Output, Ec>),* } impl<A, $($B),*, Ec> $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { pub(crate) fn new(a: A, $($B: $B),*) -> Self { let mut id = a.context_id(); $( id = id.combine($B.context_id()).unwrap(); )* $Join { id, a: maybe_done(a), $($B: maybe_done($B)),* } } } unsafe impl<A, $($B),*, Ec> GpuTask<Ec> for $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { type Output = (A::Output, $($B::Output),*); fn context_id(&self) -> ContextId { self.id } fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output> { let mut all_done = self.a.progress(execution_context); $( all_done = all_done && self.$B.progress(execution_context); )* if all_done { Progress::Finished((self.a.take(), $(self.$B.take()),*)) } else { Progress::ContinueFenced } } } impl<A, $($B),*, Ec> Clone for $Join<A, $($B),*, Ec> where A: GpuTask<Ec> + Clone, A::Output: Clone, $( $B: GpuTask<Ec> + Clone, $B::Output: Clone, )* { fn clone(&self) -> Self { $Join { id: self.id.clone(), a: self.a.clone(), $($B: self.$B.clone()),* } } } )*) } generate_join! { /// Task for the `join` combinator, waiting for two tasks to complete in no particular order. /// /// See [join] and [GpuTaskExt::join]. (Join, <A, B>), /// Task for the `join3` combinator, waiting for three tasks to complete in no particular order. /// /// See [join3] and [GpuTaskExt::join3]. (Join3, <A, B, C>), /// Task for the `join4` combinator, waiting for four tasks to complete in no particular order. /// /// See [join4] and [GpuTaskExt::join4]. (Join4, <A, B, C, D>), /// Task for the `join5` combinator, waiting for five tasks to complete in no particular order. /// /// See [join5] and [GpuTaskExt::join5]. (Join5, <A, B, C, D, E>), } macro_rules! generate_join_left { ($( $(#[$doc:meta])* ($Join:ident, <A, $($B:ident),*>), )*) => ($( $(#[$doc])* pub struct $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { id: ContextId, a: MaybeDone<A, A::Output, Ec>, $($B: MaybeDone<$B, $B::Output, Ec>),* } impl<A, $($B),*, Ec> $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { pub(crate) fn new(a: A, $($B: $B),*) -> Self { let mut id = a.context_id(); $( id = id.combine($B.context_id()).unwrap(); )* $Join { id, a: maybe_done(a), $($B: maybe_done($B)),* } } } unsafe impl<A, $($B),*, Ec> GpuTask<Ec> for $Join<A, $($B),*, Ec> where A: GpuTask<Ec>, $($B: GpuTask<Ec>),* { type Output = A::Output; fn context_id(&self) -> ContextId { self.id } fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output> { let mut all_done = self.a.progress(execution_context); $( all_done = all_done && self.$B.progress(execution_context); )* if all_done { Progress::Finished(self.a.take()) } else { Progress::ContinueFenced } } } impl<A, $($B),*, Ec> Clone for $Join<A, $($B),*, Ec> where A: GpuTask<Ec> + Clone, A::Output: Clone, $( $B: GpuTask<Ec> + Clone, $B::Output: Clone, )* { fn clone(&self) -> Self { $Join { id: self.id.clone(), a: self.a.clone(), $($B: self.$B.clone()),* } } } )*) } generate_join_left! { /// Task for the `join_left` combinator, waiting for two tasks to complete in no particular /// order and only outputting the output of the left-most task. /// /// See [join_left] and [GpuTaskExt::join_left]. (JoinLeft, <A, B>), /// Task for the `join3_left` combinator, waiting for three tasks to complete in no particular /// order and only outputting the output of the left-most task. /// /// See [join3_left] and [GpuTaskExt::join3_left]. (Join3Left, <A, B, C>), /// Task for the `join4_left` combinator, waiting for four tasks to complete in no particular /// order and only outputting the output of the left-most task. /// /// See [join4_left] and [GpuTaskExt::join4_left]. (Join4Left, <A, B, C, D>), /// Task for the `join5_left` combinator, waiting for five tasks to complete in no particular /// order and only outputting the output of the left-most task. /// /// See [join5_left] and [GpuTaskExt::join5_left]. (Join5Left, <A, B, C, D, E>), } macro_rules! generate_join_right { ($( $(#[$doc:meta])* ($Join:ident, <$($A:ident),*> $B:ident), )*) => ($( $(#[$doc])* pub struct $Join<$($A,)* $B, Ec> where $($A: GpuTask<Ec>,)* $B: GpuTask<Ec> { id: ContextId, $($A: MaybeDone<$A, $A::Output, Ec>,)* $B: MaybeDone<$B, $B::Output, Ec>, } impl<$($A,)* $B, Ec> $Join<$($A,)* $B, Ec> where $($A: GpuTask<Ec>,)* $B: GpuTask<Ec> { pub(crate) fn new($($A: $A,)* $B: $B) -> Self { let mut id = ContextId::Any; $( id = id.combine($A.context_id()).unwrap(); )* id = id.combine($B.context_id()).unwrap(); $Join { id, $($A: maybe_done($A),)* $B: maybe_done($B) } } } unsafe impl<$($A,)* $B, Ec> GpuTask<Ec> for $Join<$($A,)* $B, Ec> where $($A: GpuTask<Ec>,)* $B: GpuTask<Ec> { type Output = $B::Output; fn context_id(&self) -> ContextId { self.id } fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output> { let mut all_done = true; $( all_done = all_done && self.$A.progress(execution_context); )* all_done = all_done && self.$B.progress(execution_context); if all_done { Progress::Finished(self.$B.take()) } else { Progress::ContinueFenced } } } impl<$($A,)* $B, Ec> Clone for $Join<$($A,)* $B, Ec> where $( $A: GpuTask<Ec> + Clone, $A::Output: Clone, )* $B: GpuTask<Ec> + Clone, $B::Output: Clone, { fn clone(&self) -> Self { $Join { id: self.id.clone(), $($A: self.$A.clone(),)* $B: self.$B.clone(), } } } )*) } generate_join_right! { /// Task for the `join_right` combinator, waiting for two tasks to complete in no particular /// order and only outputting the output of the right-most task. /// /// See [join_right] and [GpuTaskExt::join_right]. (JoinRight, <A> B), /// Task for the `join3_right` combinator, waiting for three tasks to complete in no particular /// order and only outputting the output of the right-most task. /// /// See [join3_right] and [GpuTaskExt::join3_right]. (Join3Right, <A, B> C), /// Task for the `join4_right` combinator, waiting for four tasks to complete in no particular /// order and only outputting the output of the right-most task. /// /// See [join4_right] and [GpuTaskExt::join4_right]. (Join4Right, <A, B, C> D), /// Task for the `join5_right` combinator, waiting for five tasks to complete in no particular /// order and only outputting the output of the right-most task. /// /// See [join5_right] and [GpuTaskExt::join5_right]. (Join5Right, <A, B, C, D> E), } #[doc(hidden)] #[macro_export] macro_rules! join_all { ($e0:expr, $e1:expr) => (join_all!($e0, $e1,)); ($e0:expr, $e1:expr, $($e:expr,)+) => (join_all!($e0, $e1, $($e),*)); ($e0:expr, $e1:expr, $($e:expr),*) => { { let joined = $crate::task::join($e0, $e1); $( let joined = $crate::task::join(joined, $e); )* joined } } } #[doc(hidden)] #[macro_export] macro_rules! join_all_left { ($e0:expr, $e1:expr) => (join_all_left!($e0, $e1,)); ($e0:expr, $e1:expr, $($e:expr,)+) => (join_all_left!($e0, $e1, $($e),*)); ($e0:expr, $e1:expr, $($e:expr),*) => { { let joined = $crate::task::join_all_left($e0, $e1); $( let joined = $crate::task::join_all_left(joined, $e); )* joined } } } #[doc(hidden)] #[macro_export] macro_rules! join_all_right { ($e0:expr, $e1:expr) => (join_all_right!($e0, $e1,)); ($e0:expr, $e1:expr, $($e:expr,)+) => (join_all_right!($e0, $e1, $($e),*)); ($e0:expr, $e1:expr, $($e:expr),*) => { { let joined = $crate::task::join_all_right($e0, $e1); $( let joined = $crate::task::join_all_right(joined, $e); )* joined } } } /// Task for the [join_iter] combinator, waiting for all tasks in the iterator to complete in no /// particular order, outputting `()`. /// /// See [join_iter]. pub struct JoinIter<T, Ec> { id: ContextId, vec: Vec<MaybeDone<T, (), Ec>>, } impl<T, Ec> JoinIter<T, Ec> where T: GpuTask<Ec, Output = ()>, { fn new<I>(tasks: I) -> Self where I: IntoIterator<Item = T>, { let mut id = ContextId::Any; let vec: Vec<MaybeDone<T, (), Ec>> = tasks .into_iter() .inspect(|t| { id = id.combine(t.context_id()).unwrap(); }) .map(|t| maybe_done(t)) .collect(); JoinIter { id, vec } } } unsafe impl<T, Ec> GpuTask<Ec> for JoinIter<T, Ec> where T: GpuTask<Ec, Output = ()>, { type Output = (); fn context_id(&self) -> ContextId { self.id } fn progress(&mut self, execution_context: &mut Ec) -> Progress<Self::Output> { if self.vec.is_empty() { return Progress::Finished(()); } let mut all_done = true; for task in &mut self.vec { all_done = all_done && task.progress(execution_context); } if all_done { Progress::Finished(()) } else { Progress::ContinueFenced } } } impl<T, Ec> Clone for JoinIter<T, Ec> where T: Clone, { fn clone(&self) -> Self { JoinIter { id: self.id.clone(), vec: self.vec.clone(), } } } /// Combines task `a` with another task `b`, waiting for both tasks to complete in no particular /// order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any order. /// The joined task will finish when both sub-tasks have finished. When it finishes, it will output /// a tuple `(A, B)` where `A` is this task's output and `B` is task `b`'s output. /// /// # Panics /// /// Panics if the [ContextId]s of `a` and `b` are not compatible. pub fn join<A, B, Ec>(a: A, b: B) -> Join<A, B, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, { Join::new(a, b) } /// Combines task `a` with another task `b`, waiting for both tasks to complete in no particular /// order, with the output of task `a`. /// /// Similar to [join], except that instead of returning a tuple of the outputs of `a` and `b`, it /// only returns the output of `a`. /// /// See also [join_right]. /// /// # Panics /// /// Panics if the [ContextId]s of `a` and `b` are not compatible. pub fn join_left<A, B, Ec>(a: A, b: B) -> JoinLeft<A, B, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, { JoinLeft::new(a, b) } /// Combines task `a` with another task `b`, waiting for both tasks to complete in no particular /// order, with the output of task `a`. /// /// Similar to [join], except that instead of returning a tuple of the outputs of `a` and `b`, it /// only returns the output of `b`. /// /// See also [join_left]. /// /// # Panics /// /// Panics if the [ContextId]s of `a` and `b` are not compatible. pub fn join_right<A, B, Ec>(a: A, b: B) -> JoinRight<A, B, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, { JoinRight::new(a, b) } /// Combines task `a`, `b` and `c`, waiting for all tasks to complete in no particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any order. /// The joined task will finish when all sub-tasks have finished. When it finishes, it will output a /// tuple `(A, B, C)` where `A` is this task's output, `B` is task `b`'s output and `C` is task /// `c`'s output. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b` and `c` are not compatible. pub fn join3<A, B, C, Ec>(a: A, b: B, c: C) -> Join3<A, B, C, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, { Join3::new(a, b, c) } /// Combines task `a`, `b` and `c`, waiting for all tasks to complete in no particular order, with /// the output of task `a`. /// /// Similar to [join3], except that instead of returning a tuple of the outputs of `a`, `b` and `c`, /// it only returns the output of `a`. /// /// See also [join3_right]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b` and `c` are not compatible. pub fn join3_left<A, B, C, Ec>(a: A, b: B, c: C) -> Join3Left<A, B, C, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, { Join3Left::new(a, b, c) } /// Combines task `a`, `b` and `c`, waiting for all tasks to complete in no particular order, with /// the output of task `c`. /// /// Similar to [join3], except that instead of returning a tuple of the outputs of `a`, `b` and `c`, /// it only returns the output of `c`. /// /// See also [join3_left]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b` and `c` are not compatible. pub fn join3_right<A, B, C, Ec>(a: A, b: B, c: C) -> Join3Right<A, B, C, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, { Join3Right::new(a, b, c) } /// Combines task `a`, `b`, `c` and `d`, waiting for all tasks to complete in no particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any order. /// The joined task will finish when all sub-tasks have finished. When it finishes, it will output a /// tuple `(A, B, C, D)` where `A` is this task's output, `B` is task `b`'s output, `C` is task /// `c`'s output and `D` is task `d`'s output. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c` and `d` are not compatible. pub fn join4<A, B, C, D, Ec>(a: A, b: B, c: C, d: D) -> Join4<A, B, C, D, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, { Join4::new(a, b, c, d) } /// Combines task `a`, `b`, `c` and `d`, waiting for all tasks to complete in no particular order, /// with the output of task `a`. /// /// Similar to [join4], except that instead of returning a tuple of the outputs of `a`, `b`, `c` and /// `d`, it only returns the output of `a`. /// /// See also [join4_right]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c` and `d` are not compatible. pub fn join4_left<A, B, C, D, Ec>(a: A, b: B, c: C, d: D) -> Join4Left<A, B, C, D, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, { Join4Left::new(a, b, c, d) } /// Combines task `a`, `b`, `c` and `d`, waiting for all tasks to complete in no particular order, /// with the output of task `d`. /// /// Similar to [join4], except that instead of returning a tuple of the outputs of `a`, `b`, `c` and /// `d`, it only returns the output of `d`. /// /// See also [join4_left]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c` and `d` are not compatible. pub fn join4_right<A, B, C, D, Ec>(a: A, b: B, c: C, d: D) -> Join4Right<A, B, C, D, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, { Join4Right::new(a, b, c, d) } /// Combines task `a`, `b`, `c`, `d` and `e`, waiting for all tasks to complete in no particular /// order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any order. /// The joined task will finish when all sub-tasks have finished. When it finishes, it will output a /// tuple `(A, B, C, D, E)` where `A` is this task's output, `B` is task `b`'s output, `C` is task /// `c`'s output, `D` is task `d`'s output and `E` is task `e`'s output. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c`, `d` and `e` are not compatible. pub fn join5<A, B, C, D, E, Ec>(a: A, b: B, c: C, d: D, e: E) -> Join5<A, B, C, D, E, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, { Join5::new(a, b, c, d, e) } /// Combines task `a`, `b`, `c`, `d` and `e`, waiting for all tasks to complete in no particular /// order, with the output of task `a`. /// /// Similar to [join5], except that instead of returning a tuple of the outputs of `a`, `b`, `c`, /// `d` and `e`, it only returns the output of `a`. /// /// See also [join5_right]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c`, `d` and `e` are not compatible. pub fn join5_left<A, B, C, D, E, Ec>(a: A, b: B, c: C, d: D, e: E) -> Join5Left<A, B, C, D, E, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, { Join5Left::new(a, b, c, d, e) } /// Combines task `a`, `b`, `c`, `d` and `e`, waiting for all tasks to complete in no particular /// order, with the output of task `e`. /// /// Similar to [join5], except that instead of returning a tuple of the outputs of `a`, `b`, `c`, /// `d` and `e`, it only returns the output of `e`. /// /// See also [join5_left]. /// /// # Panics /// /// Panics if the [ContextId]s `a`, `b`, `c`, `d` and `e` are not compatible. pub fn join5_right<A, B, C, D, E, Ec>(a: A, b: B, c: C, d: D, e: E) -> Join5Right<A, B, C, D, E, Ec> where A: GpuTask<Ec>, B: GpuTask<Ec>, C: GpuTask<Ec>, D: GpuTask<Ec>, E: GpuTask<Ec>, { Join5Right::new(a, b, c, d, e) } /// Combines all tasks in an iterator, waiting for all tasks to complete in no particular order. /// /// This returns a new "joined" task. This joined task may progress the its sub-tasks in any order. /// The joined task will finish when both sub-tasks have finished. When it finishes, it will output /// `()`. All tasks in the iterator must also output `()`. /// /// This combinator allocates. See also the [join_all] macro for an alternative that does not /// allocate if the set of tasks that are to be sequenced is statically known. /// /// # Panics /// /// Panics if the [ContextId]s of any of the tasks in the iterator are not compatible. pub fn join_iter<I, Ec>(iterator: I) -> JoinIter<I::Item, Ec> where I: IntoIterator, I::Item: GpuTask<Ec, Output = ()>, { JoinIter::new(iterator) }