• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 Huawei Technologies Co., Ltd
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 
17 #ifndef MINDSPORE_CCSRC_MINDDATA_DATASET_PERF_DATA_H
18 #define MINDSPORE_CCSRC_MINDDATA_DATASET_PERF_DATA_H
19 
20 #include <vector>
21 #include "minddata/dataset/include/dataset/constants.h"
22 
23 namespace mindspore {
24 namespace dataset {
25 
26 // PerfData is a convenience class to record and store the data produced by Monitor
27 // and represents a 2D column major table with every column storing samples
28 // for an operator. The number of rows equals to the number of samples,
29 // the number of columns equals to the number of operators.
30 // The capacity is determined on construction and cannot be changed.
31 // ColumnType can be std::vector or CyclicArray. In case of the latter data can be added
32 // indefinitely without the risk of overflowing otherwise the capacity must not be exceeded.
33 // Given PerfData pd(n_rows, n_cols) an element in the column i and row j can be accessed as
34 // pd[i][j]
35 
36 template <typename ColumnType>
37 class PerfData {
38  public:
39   PerfData() = default;
40   ~PerfData() = default;
PerfData(dsize_t max_rows,dsize_t n_cols)41   PerfData(dsize_t max_rows, dsize_t n_cols) : counter_(0), max_rows_(max_rows), n_cols_(n_cols) {
42     for (auto i = 0; i < n_cols_; i++) {
43       data_.push_back(ColumnType(max_rows_));
44     }
45   }
46   PerfData(const PerfData &rhs) = default;
47   PerfData(PerfData &&rhs) = default;
48 
49   // Adds a row of data
50   // T must be any container working with range based loops
51   template <typename T>
AddSample(const T & row)52   void AddSample(const T &row) {
53     auto i = 0;
54     for (const auto &e : row) {
55       if (data_.size() > i) {
56         data_[i++].push_back(e);
57       }
58     }
59     counter_++;
60   }
61 
62   // Fetches a row of data by copy
63   template <typename V = typename ColumnType::value_type>
Row(dsize_t idx)64   auto Row(dsize_t idx) {
65     std::vector<V> row(n_cols_);
66     for (auto i = 0; i < n_cols_; i++) {
67       if (data_.size() > i && data_[i].size() > idx) {
68         row[i] = data_[i][idx];
69       }
70     }
71     return row;
72   }
73 
74   // returns a column of data
75   ColumnType &operator[](size_t idx) { return data_[idx]; }
76 
77   const ColumnType &operator[](size_t idx) const { return data_[idx]; }
78 
size()79   dsize_t size() { return counter_ < max_rows_ ? counter_ : max_rows_; }
80 
capacity()81   dsize_t capacity() { return max_rows_; }
82 
83  private:
84   std::vector<ColumnType> data_;
85   dsize_t counter_;
86   dsize_t max_rows_;
87   int n_cols_;
88 };
89 
90 }  // namespace dataset
91 }  // namespace mindspore
92 #endif  // MINDSPORE_CCSRC_MINDDATA_DATASET_PERF_DATA_H
93