2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_TRAIN_MEASURE_H__
18 #define __ONERT_TRAIN_MEASURE_H__
29 clock_gettime(CLOCK_MONOTONIC, &ts);
30 return static_cast<uint64_t>(ts.tv_nsec) / 1e3 + static_cast<uint64_t>(ts.tv_sec) * 1e6;
40 // TODO Support memory usage
48 void set(const int epoch, const int step)
51 _results.resize(epoch);
52 std::for_each(_results.begin(), _results.end(), [step](auto &v) { v.resize(step); });
55 void run(const int epoch, const int step, const std::function<void()> &func)
57 if (_results.empty() || _results.size() <= epoch || _results[epoch].size() <= step)
59 throw std::runtime_error("Please set the number of epochs and steps first");
62 _results[epoch][step].time = nowMicros();
66 _results[epoch][step].time = nowMicros() - _results[epoch][step].time;
69 double timeMicros(const int epoch)
71 if (_results.empty() || _results.size() <= epoch)
73 throw std::runtime_error("Invalid epoch");
77 std::for_each(_results[epoch].begin(), _results[epoch].end(),
78 [&sum](auto &v) { sum += v.time; });
79 return sum / _results[epoch].size();
82 double timeMs(const int epoch) { return timeMicros(epoch) / 1e3; }
85 std::vector<std::vector<Step>> _results;
88 } // namespace onert_train
90 #endif // __ONERT_TRAIN_MEASURE_H__