2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define FOLLY_GEN_PARALLEL_H_
22 #include <folly/gen/Base.h>
34 template <class Iterator>
35 class ChunkedRangeSource;
40 * chunked() - For producing values from a container in slices.
42 * Especially for use with 'parallel()', chunked can be used to process values
43 * from a persistent container in chunks larger than one value at a time. The
44 * values produced are generators for slices of the input container. */
47 class Iterator = typename Container::const_iterator,
48 class Chunked = detail::ChunkedRangeSource<Iterator>>
49 Chunked chunked(const Container& container, int chunkSize = 256) {
50 return Chunked(chunkSize, folly::range(container.begin(), container.end()));
55 class Iterator = typename Container::iterator,
56 class Chunked = detail::ChunkedRangeSource<Iterator>>
57 Chunked chunked(Container& container, int chunkSize = 256) {
58 return Chunked(chunkSize, folly::range(container.begin(), container.end()));
63 * parallel - A parallelization operator.
65 * 'parallel(ops)' can be used with any generator to process a segment
66 * of the pipeline in parallel. Multiple threads are used to apply the
67 * operations ('ops') to the input sequence, with the resulting sequence
68 * interleaved to be processed on the client thread.
72 * | parallel(map(fetchObj) | filter(isValid) | map(scoreObj))
75 * Operators specified for parallel execution must yield sequences, not just
76 * individual values. If a sink function such as 'count' is desired, it must be
77 * wrapped in 'sub' to produce a subcount, since any such aggregation must be
82 * | parallel(filter(expensiveTest) | sub(count))
85 * Here, each thread counts its portion of the result, then the sub-counts are
86 * summed up to produce the total count.
88 template <class Ops, class Parallel = detail::Parallel<Ops>>
89 Parallel parallel(Ops ops, size_t threads = 0) {
90 return Parallel(std::move(ops), threads);
94 * sub - For sub-summarization of a sequence.
96 * 'sub' can be used to apply a sink function to a generator, but wrap the
97 * single value in another generator. Note that the sink is eagerly evaluated on
100 * auto sum = from(list) | sub(count) | first;
102 * This is primarily used with 'parallel', as noted above.
104 template <class Sink, class Sub = detail::Sub<Sink>>
106 return Sub(std::move(sink));
109 } // !namespace folly
111 #include <folly/gen/Parallel-inl.h>