Turi Create  4.0
pylambda_function.hpp
1 /* Copyright © 2017 Apple Inc. All rights reserved.
2  *
3  * Use of this source code is governed by a BSD-3-clause license that can
4  * be found in the LICENSE.txt file or at https://opensource.org/licenses/BSD-3-Clause
5  */
6 #ifndef TURI_LAMBDA_PYLAMBDA_FUNCTION_HPP
7 #define TURI_LAMBDA_PYLAMBDA_FUNCTION_HPP
8 
9 #include<vector>
10 #include<string>
11 #include<core/data/flexible_type/flexible_type.hpp>
12 
13 namespace turi {
14 
15 class sframe_rows;
16 
17 namespace fileio {
18 struct file_ownership_handle;
19 }
20 
21 namespace lambda {
22 
23 /**
24  * Represents a python lambda function object which is evaluated in parallel.
25  *
26  * Usage
27  * -----
28  * \code
29  *
30  * // Constructed from a pickled lambda string.
31  * std::string lambda_string="some pickled str"
32  * pylambda_function f(lambda_string);
33  *
34  * // (optional) Set options such as random_seed or skip_undefined.
35  * f.set_skip_undefined(true);
36  * f.set_random_seed(0);
37  *
38  * // evaluate on a minibatch of values.
39  * std::vector<flexible_type> out;
40  * f.evaluate({1,2,3}, out);
41  *
42  * \endcode
43  *
44  * The pylambda_function can also contain a gl_pickle directory.
45  * The gl_pickle must contain one function.
46  * The function will then be unpickled from the directory and used.
47  * \code
48  * // Constructed from a pickled lambda string.
49  * std::string gl_pickle_directory = "./pickled_function"
50  * pylambda_function f(gl_pickle_directory);
51  * \endcode
52  *
53  * The evaluation is implemented using pylambda master and workers
54  * for parallelism:
55  * - each call to evaluate() will grab one avaiable worker.
56  * - if no worker is avaiable, block.
57  * - when evaluate returns, the corresponding worker is released.
58  */
60  public:
61  /**
62  * Constructs a lambda function from a either a series of pickled bytes
63  * or a gl_pickled directory which contains a function.
64  * \param delete_pickle_files_on_destruction Only meaningful if
65  * lambda_str contains a gl_pickled directory. If true (default), it
66  * the directory will be deleted when this pylambda_function instance is
67  * destroyed.
68  */
69  pylambda_function(const std::string& lambda_str,
70  bool delete_pickle_files_on_destruction = true);
71 
72  pylambda_function(const pylambda_function& other) = delete;
73  pylambda_function& operator=(const pylambda_function& other) = delete;
74 
76 
77  //// Options
78  void set_skip_undefined(bool value);
79  void set_random_seed(uint64_t value);
80 
81  //// Evaluating Interface
82 
83  /* One to one */
84  void eval(const sframe_rows& rows,
85  std::vector<flexible_type>& out);
86 
87  /* Many to one */
88  void eval(const std::vector<std::string>& keys,
89  const sframe_rows& rows,
90  std::vector<flexible_type>& out);
91 
92  private:
93  size_t lambda_hash = -1;
94  bool skip_undefined = false;
95  uint64_t random_seed = 0;
96  std::shared_ptr<fileio::file_ownership_handle> m_pickle_file_handle;
97 };
98 
99  } // end of lambda namespace
100 } // end of turi namespace
101 
102 #endif
std::set< Key > keys(const std::map< Key, T > &map)
Definition: stl_util.hpp:358