composition_2.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2018-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <iostream>
  19. #include <vector>
  20. #include <folly/experimental/pushmi/examples/pool.h>
  21. #include <folly/experimental/pushmi/o/transform.h>
  22. #include <folly/experimental/pushmi/o/via.h>
  23. #include <folly/experimental/pushmi/strand.h>
  24. using namespace pushmi::aliases;
  25. struct f_t {};
  26. f_t f() {
  27. return {};
  28. }
  29. struct g_t {};
  30. g_t g(f_t) {
  31. return {};
  32. }
  33. // these expressions are read backward, bottom-right to top-left
  34. template <class CPUExecutor, class IOExecutor>
  35. void lisp(CPUExecutor cpu, IOExecutor io) {
  36. // f on cpu - g on cpu (implicit: a single task on the cpu executor runs all
  37. // the functions)
  38. op::submit([](g_t) {})(op::transform([](f_t ft) { return g(ft); })(
  39. op::transform([](auto) { return f(); })(cpu)));
  40. // f on cpu - g on cpu (explicit: the first cpu task runs f and a second cpu
  41. // task runs g)
  42. op::submit([](g_t) {})(op::transform([](f_t ft) { return g(ft); })(
  43. op::via(mi::strands(cpu))(op::transform([](auto) { return f(); })(cpu))));
  44. // f on io - g on cpu
  45. op::submit([](g_t) {})(op::transform([](f_t ft) { return g(ft); })(
  46. op::via(mi::strands(cpu))(op::transform([](auto) { return f(); })(io))));
  47. }
  48. template <class CPUExecutor, class IOExecutor>
  49. void sugar(CPUExecutor cpu, IOExecutor io) {
  50. // f on cpu - g on cpu (implicit: a single task on the cpu executor runs all
  51. // the functions)
  52. cpu | op::transform([](auto) { return f(); }) |
  53. op::transform([](f_t ft) { return g(ft); }) | op::submit([](g_t) {});
  54. // f on cpu - g on cpu (explicit: the first cpu task runs f and a second cpu
  55. // task runs g)
  56. cpu | op::transform([](auto) { return f(); }) | op::via(mi::strands(cpu)) |
  57. op::transform([](f_t ft) { return g(ft); }) | op::submit([](g_t) {});
  58. // f on io - g on cpu
  59. io | op::transform([](auto) { return f(); }) | op::via(mi::strands(cpu)) |
  60. op::transform([](f_t ft) { return g(ft); }) | op::submit([](g_t) {});
  61. }
  62. template <class CPUExecutor, class IOExecutor>
  63. void pipe(CPUExecutor cpu, IOExecutor io) {
  64. // f on cpu - g on cpu (implicit: a single task on the cpu executor runs all
  65. // the functions)
  66. mi::pipe(
  67. cpu,
  68. op::transform([](auto) { return f(); }),
  69. op::transform([](f_t ft) { return g(ft); }),
  70. op::submit([](g_t) {}));
  71. // f on cpu - g on cpu (explicit: the first cpu task runs f and a second cpu
  72. // task runs g)
  73. mi::pipe(
  74. cpu,
  75. op::transform([](auto) { return f(); }),
  76. op::via(mi::strands(cpu)),
  77. op::transform([](f_t ft) { return g(ft); }),
  78. op::submit([](g_t) {}));
  79. // f on io - g on cpu
  80. mi::pipe(
  81. io,
  82. op::transform([](auto) { return f(); }),
  83. op::via(mi::strands(cpu)),
  84. op::transform([](f_t ft) { return g(ft); }),
  85. op::submit([](g_t) {}));
  86. }
  87. int main() {
  88. mi::pool cpuPool{std::max(1u, std::thread::hardware_concurrency())};
  89. mi::pool ioPool{std::max(1u, std::thread::hardware_concurrency())};
  90. lisp(cpuPool.executor(), ioPool.executor());
  91. sugar(cpuPool.executor(), ioPool.executor());
  92. pipe(cpuPool.executor(), ioPool.executor());
  93. ioPool.wait();
  94. cpuPool.wait();
  95. std::cout << "OK" << std::endl;
  96. }