You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

perf_data.h 2.8 kB

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