QuantumLibrary
quantum_util.h
1 /*
2 ** Copyright 2018 Bloomberg Finance L.P.
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 #ifndef BLOOMBERG_QUANTUM_UTIL_H
17 #define BLOOMBERG_QUANTUM_UTIL_H
18 
19 #include <tuple>
20 #include <functional>
21 #include <utility>
22 #include <iostream>
23 #include <map>
24 #include <vector>
25 #include <iterator>
26 #include <quantum/quantum_traits.h>
27 #include <quantum/interface/quantum_itask.h>
28 #include <quantum/interface/quantum_icontext.h>
29 #include <quantum/interface/quantum_ipromise.h>
30 #include <quantum/quantum_capture.h>
31 
32 namespace Bloomberg {
33 namespace quantum {
34 
35 //fwd declarations
36 template <class RET> class Context;
37 template <class RET> class Promise;
38 
39 //==============================================================================================
40 // struct Util
41 //==============================================================================================
45 struct Util
46 {
47  template<class RET, class FUNC, class ...ARGS>
48  static Function<int(Traits::Yield&)>
49  bindCaller(std::shared_ptr<Context<RET>> ctx, FUNC&& func0, ARGS&& ...args0);
50 
51  template<class RET, class FUNC, class ...ARGS>
52  static Function<int()>
53  bindIoCaller(std::shared_ptr<Promise<RET>> promise, FUNC&& func0, ARGS&& ...args0);
54 
55  //------------------------------------------------------------------------------------------
56  // ForEach
57  //------------------------------------------------------------------------------------------
58  template <class RET, class INPUT_IT>
59  static int forEachCoro(CoroContextPtr<std::vector<RET>> ctx,
60  INPUT_IT inputIt,
61  size_t num,
63 
64  template <class RET, class INPUT_IT>
65  static int forEachBatchCoro(CoroContextPtr<std::vector<std::vector<RET>>> ctx,
66  INPUT_IT inputIt,
67  size_t num,
69  size_t numCoroutineThreads);
70 
71  //------------------------------------------------------------------------------------------
72  // MapReduce
73  //------------------------------------------------------------------------------------------
74  template <class KEY,
75  class MAPPED_TYPE,
76  class REDUCED_TYPE,
77  class INPUT_IT>
78  static int mapReduceCoro(CoroContextPtr<std::map<KEY, REDUCED_TYPE>> ctx,
79  INPUT_IT inputIt,
80  size_t num,
83 
84  template <class KEY,
85  class MAPPED_TYPE,
86  class REDUCED_TYPE,
87  class INPUT_IT>
88  static int mapReduceBatchCoro(CoroContextPtr<std::map<KEY, REDUCED_TYPE>> ctx,
89  INPUT_IT inputIt,
90  size_t num,
93 
94 #ifdef __QUANTUM_PRINT_DEBUG
95  //Synchronize logging
96  static std::mutex& LogMutex();
97 #endif
98 };
99 
100 }}
101 
102 #include <quantum/util/impl/quantum_util_impl.h>
103 
104 #endif //BLOOMBERG_QUANTUM_UTIL_H
Definition: quantum_buffer_impl.h:22
static int mapReduceCoro(CoroContextPtr< std::map< KEY, REDUCED_TYPE >> ctx, INPUT_IT inputIt, size_t num, const Functions::MapFunc< KEY, MAPPED_TYPE, INPUT_IT > &mapper, const Functions::ReduceFunc< KEY, MAPPED_TYPE, REDUCED_TYPE > &reducer)
Definition: quantum_util_impl.h:162
typename ICoroContext< RET >::Ptr CoroContextPtr
Definition: quantum_icoro_context.h:479
Utility to bind a user callable function unto a coroutine or an IO task.
Definition: quantum_util.h:45
Class representing a promised value.
Definition: quantum_icoro_promise.h:77
std::function< RET(const typename std::iterator_traits< INPUT_IT >::value_type &)> ForEachFunc
Definition: quantum_functions.h:34
std::function< std::vector< std::pair< KEY, MAPPED_TYPE > >(const typename std::iterator_traits< INPUT_IT >::value_type &)> MapFunc
Definition: quantum_functions.h:37
Concrete class representing a coroutine or a thread context.
Definition: quantum_icoro_context.h:29
static Function< int(Traits::Yield &)> bindCaller(std::shared_ptr< Context< RET >> ctx, FUNC &&func0, ARGS &&...args0)
Definition: quantum_util_impl.h:88
static int forEachBatchCoro(CoroContextPtr< std::vector< std::vector< RET >>> ctx, INPUT_IT inputIt, size_t num, const Functions::ForEachFunc< RET, INPUT_IT > &func, size_t numCoroutineThreads)
Definition: quantum_util_impl.h:122
typename BoostCoro::pull_type Yield
Definition: quantum_traits.h:58
std::function< std::pair< KEY, REDUCED_TYPE >(std::pair< KEY, std::vector< MAPPED_TYPE > > &&)> ReduceFunc
Definition: quantum_functions.h:40
Similar implementation to std::function except that it allows capture of non-copyable types.
Definition: quantum_capture.h:62
static int mapReduceBatchCoro(CoroContextPtr< std::map< KEY, REDUCED_TYPE >> ctx, INPUT_IT inputIt, size_t num, const Functions::MapFunc< KEY, MAPPED_TYPE, INPUT_IT > &mapper, const Functions::ReduceFunc< KEY, MAPPED_TYPE, REDUCED_TYPE > &reducer)
Definition: quantum_util_impl.h:206
static int forEachCoro(CoroContextPtr< std::vector< RET >> ctx, INPUT_IT inputIt, size_t num, const Functions::ForEachFunc< RET, INPUT_IT > &func)
Definition: quantum_util_impl.h:103
static Function< int()> bindIoCaller(std::shared_ptr< Promise< RET >> promise, FUNC &&func0, ARGS &&...args0)
Definition: quantum_util_impl.h:96