Parallel.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2014-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. #pragma once
  17. #define FOLLY_GEN_PARALLEL_H_
  18. #include <mutex>
  19. #include <folly/gen/Base.h>
  20. namespace folly {
  21. namespace gen {
  22. namespace detail {
  23. template <class Ops>
  24. class Parallel;
  25. template <class Sink>
  26. class Sub;
  27. template <class Iterator>
  28. class ChunkedRangeSource;
  29. } // namespace detail
  30. /**
  31. * chunked() - For producing values from a container in slices.
  32. *
  33. * Especially for use with 'parallel()', chunked can be used to process values
  34. * from a persistent container in chunks larger than one value at a time. The
  35. * values produced are generators for slices of the input container. */
  36. template <
  37. class Container,
  38. class Iterator = typename Container::const_iterator,
  39. class Chunked = detail::ChunkedRangeSource<Iterator>>
  40. Chunked chunked(const Container& container, int chunkSize = 256) {
  41. return Chunked(chunkSize, folly::range(container.begin(), container.end()));
  42. }
  43. template <
  44. class Container,
  45. class Iterator = typename Container::iterator,
  46. class Chunked = detail::ChunkedRangeSource<Iterator>>
  47. Chunked chunked(Container& container, int chunkSize = 256) {
  48. return Chunked(chunkSize, folly::range(container.begin(), container.end()));
  49. }
  50. /**
  51. * parallel - A parallelization operator.
  52. *
  53. * 'parallel(ops)' can be used with any generator to process a segment
  54. * of the pipeline in parallel. Multiple threads are used to apply the
  55. * operations ('ops') to the input sequence, with the resulting sequence
  56. * interleaved to be processed on the client thread.
  57. *
  58. * auto scoredResults
  59. * = from(ids)
  60. * | parallel(map(fetchObj) | filter(isValid) | map(scoreObj))
  61. * | as<vector>();
  62. *
  63. * Operators specified for parallel execution must yield sequences, not just
  64. * individual values. If a sink function such as 'count' is desired, it must be
  65. * wrapped in 'sub' to produce a subcount, since any such aggregation must be
  66. * re-aggregated.
  67. *
  68. * auto matches
  69. * = from(docs)
  70. * | parallel(filter(expensiveTest) | sub(count))
  71. * | sum;
  72. *
  73. * Here, each thread counts its portion of the result, then the sub-counts are
  74. * summed up to produce the total count.
  75. */
  76. template <class Ops, class Parallel = detail::Parallel<Ops>>
  77. Parallel parallel(Ops ops, size_t threads = 0) {
  78. return Parallel(std::move(ops), threads);
  79. }
  80. /**
  81. * sub - For sub-summarization of a sequence.
  82. *
  83. * 'sub' can be used to apply a sink function to a generator, but wrap the
  84. * single value in another generator. Note that the sink is eagerly evaluated on
  85. * the input sequence.
  86. *
  87. * auto sum = from(list) | sub(count) | first;
  88. *
  89. * This is primarily used with 'parallel', as noted above.
  90. */
  91. template <class Sink, class Sub = detail::Sub<Sink>>
  92. Sub sub(Sink sink) {
  93. return Sub(std::move(sink));
  94. }
  95. } // namespace gen
  96. } // namespace folly
  97. #include <folly/gen/Parallel-inl.h>