YPC  0.2.0
enclave_iris_parser.h
1 #include "ypc/core_t/analyzer/data_source.h"
2 #include "ypc/stbox/ebyte.h"
3 #include "ypc/stbox/stx_common.h"
4 #ifdef YPC_SGX
5 #include "ypc/stbox/tsgx/log.h"
6 #else
7 #include <glog/logging.h>
8 #endif
9 #include "user_type.h"
10 
11 #include "ypc/corecommon/to_type.h"
12 #include <hpda/extractor/raw_data.h>
13 #include <hpda/output/memory_output.h>
14 #include <hpda/processor/processor_base.h>
15 #include <hpda/processor/transform/concat.h>
16 
17 namespace hpda {
18 template <typename T1, typename T2> struct euclidean;
19 
20 template <> struct euclidean<iris_data_t, double> {
21  static double distance_square(const iris_data_t &p1, const iris_data_t &p2) {
22  double ret = 0;
23  auto v1 = p1.get<sepal_len>() - p2.get<sepal_len>();
24  auto v2 = p1.get<sepal_wid>() - p2.get<sepal_wid>();
25  auto v3 = p1.get<petal_len>() - p2.get<petal_len>();
26  auto v4 = p1.get<petal_wid>() - p2.get<petal_wid>();
27 
28  ret += v1 * v1;
29  ret += v2 * v2;
30  ret += v3 * v3;
31  ret += v4 * v4;
32  return ret;
33  }
34 };
35 } // namespace hpda
36 
37 iris_data_t operator+(const iris_data_t &p1, const iris_data_t &p2) {
38  iris_data_t d;
39  d.set<sepal_len>(p1.get<sepal_len>() + p2.get<sepal_len>());
40  d.set<sepal_wid>(p1.get<sepal_wid>() + p2.get<sepal_wid>());
41  d.set<petal_len>(p1.get<petal_len>() + p2.get<petal_len>());
42  d.set<petal_wid>(p1.get<petal_wid>() + p2.get<petal_wid>());
43  return d;
44 }
45 
46 iris_data_t operator/(const iris_data_t &p1, size_t v) {
47  iris_data_t d;
48  d.set<sepal_len>(p1.get<sepal_len>() / v);
49  d.set<sepal_wid>(p1.get<sepal_wid>() / v);
50  d.set<petal_len>(p1.get<petal_len>() / v);
51  d.set<petal_wid>(p1.get<petal_wid>() / v);
52  return d;
53 }
54 #include <hpda/algorithm/kmeans.h>
55 define_nt(iid, int);
56 
57 typedef ff::util::ntobject<sepal_len, sepal_wid, petal_len, petal_wid, species>
58  extra_nt_t;
60  : public hpda::processor::processor_base_t<extra_nt_t, user_item_t> {
61 public:
64 
65  virtual bool process() {
66  if (!has_input_value()) {
67  return false;
68  }
69 
70  auto t = base::input_value();
71  m_data.get<iris_data>().set<sepal_len, sepal_wid, petal_len, petal_wid>(
72  t.get<sepal_len>(), t.get<sepal_wid>(), t.get<petal_len>(),
73  t.get<petal_wid>());
74  m_data.set<species>(t.get<species>());
75  base::consume_input_value();
76  return true;
77  }
78  virtual user_item_t output_value() { return m_data; }
79 
80 protected:
81  user_item_t m_data;
82 };
83 
85 public:
87  : m_source(source){};
88 
89  inline stbox::bytes do_parse(const stbox::bytes &param) {
90  ypc::to_type<stbox::bytes, extra_nt_t> converter(m_source);
91  transform_format trans(&converter);
92 
94  hpda::ntobject<iris_data, species>, iris_data, double, iid>
95  km(&trans, 3, 0.001);
96 
98  km.data_with_cluster_stream());
99 
100  mo.get_engine()->run();
101  stbox::bytes result;
102  int i = 0;
103  for (auto it : mo.values()) {
104  result += it.get<species>();
105  result += " - ";
106  result += std::to_string(it.get<iid>());
107  result += "\n";
108  i++;
109  // LOG(INFO) << i << ": " << it.get<species>() << " - "
110  //<< std::to_string(it.get<iid>());
111  }
112  return result;
113  }
114 
115 protected:
117 };
118 
120 public:
122  : m_source(source){};
123 
124  inline stbox::bytes do_parse(const stbox::bytes &param) {
125  ypc::to_type<stbox::bytes, extra_nt_t> converter(m_source);
126  transform_format trans(&converter);
127 
129  hpda::ntobject<iris_data, species>, iris_data, double, iid>
130  kmeans_t;
131  kmeans_t km(&trans, 3, 0.001);
132 
133  hpda::output::memory_output<iid, kmeans_t::mean_point,
134  kmeans_t::average_distance>
135  mo(km.means_stream());
136 
137  mo.get_engine()->run();
138  stbox::bytes result;
139  int i = 0;
140  for (auto it : mo.values()) {
141  result += std::to_string(it.get<iid>());
142  result += " - ";
143  auto id = it.get<kmeans_t::mean_point>();
144  result += " (" + std::to_string(id.template get<sepal_len>());
145  result += " ," + std::to_string(id.get<sepal_wid>());
146  result += " ," + std::to_string(id.get<petal_len>());
147  result += " ," + std::to_string(id.get<petal_wid>());
148  result += ") ";
149  result += "\n";
150  i++;
151  // LOG(INFO) << i << ": " << it.get<species>() << " - "
152  //<< std::to_string(it.get<iid>());
153  }
154  return result;
155  }
156 
157 protected:
159 };
hpda::processor::internal::processor_base
Definition: processor_base.h:9
transform_format
Definition: enclave_iris_parser.h:59
hpda::euclidean
Definition: enclave_iris_parser.h:18
hpda::algorithm::kmeans::internal::loyd_kmeans_impl
Definition: kmeans.h:54
enclave_iris_means_parser
Definition: enclave_iris_parser.h:119
ypc::data_source< stbox::bytes >
hpda::output::internal::memory_output_impl
Definition: memory_output.h:9
ypc::to_type
Definition: to_type.h:15
enclave_iris_parser
Definition: enclave_iris_parser.h:84
hpda::internal::processor_with_output
Definition: processor_with_output.h:9