summaryrefslogtreecommitdiff
path: root/runtimes/neurun/core/src/backend/JSONExecTime.cc
blob: e2404b2c814cb45bde472a311002452ef01be919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "backend/JSONExecTime.h"
#include "backend/IConfig.h"
#include <fstream>

namespace neurun
{
namespace backend
{
/**
 * @brief Helper function for reading string from stream
 *
 * @param str Output string
 * @param stream File stream
 */
void readString(std::string &str, std::ifstream &stream)
{
  str.clear();
  char buf;
  while (stream.good())
  {
    stream.get(buf);
    if (buf == '"')
      break;
    str.push_back(buf);
  }
}

/**
 * @brief Helper function for reading bool from stream
 *
 * @param quant Output bool
 * @param stream File stream
 */
void readBool(bool &quant, std::ifstream &stream)
{
  char buf;
  stream.get(buf);
  quant = (buf == '1');
  stream.get(buf);
}

void printString(const std::string &str, std::ofstream &stream) { stream << "\"" << str << "\""; }

void printBool(bool quant, std::ofstream &stream) { stream << "\"" << quant << "\""; }

void JSON::readOperation(const std::string &backend, const std::string &operation, bool quant,
                         std::ifstream &stream)
{
  uint32_t size = 0;
  int64_t time = 0;

  std::string int_buf;
  char buf;
  int number_of_closed_braces = 0;
  int number_of_commas = 0;

  while (stream.good())
  {
    stream.get(buf);

    switch (buf)
    {
      case ']':
      {
        number_of_closed_braces++;
        break;
      }
      case '[':
      {
        number_of_closed_braces--;
        break;
      }
      default:
      {
        if (std::isdigit(buf))
        {
          int_buf.push_back(buf);
        }
        break;
      }
    }

    if (number_of_closed_braces == 1)
      break;

    if ((buf == ']' && number_of_closed_braces == 0) ||
        (buf == ',' && number_of_closed_braces == -1))
    {
      switch (number_of_commas % 2)
      {
        case 0:
        {
          size = static_cast<uint32_t>(std::atoi(int_buf.c_str()));
          break;
        }
        case 1:
        {
          time = static_cast<int64_t>(std::atol(int_buf.c_str()));
          auto bf = _backends.find(backend);
          if (bf != _backends.end())
          {
            _measurements[bf->second][operation][quant][size] = time;
          } // we ignore the records for unsupported backends
          break;
        }
      }
      number_of_commas++;
      int_buf.clear();
    }
  }
}
void JSON::printOperation(const std::map<uint32_t, int64_t> &operation_info,
                          std::ofstream &stream) const
{
  for (const auto &items : operation_info)
  {
    stream << "[" << items.first << ", " << items.second << "], ";
  }
  stream.seekp(-2, std::ofstream::end);
}

void JSON::uploadOperationsExecTime() const
{
  std::ofstream stream(_measurement_file);
  if (!stream.is_open())
  {
    throw std::runtime_error("Failed to save backend config file");
  }
  else
  {
    stream << "{";
    for (const auto &backend : _measurements)
    {
      printString(backend.first->config()->id(), stream);
      stream << ": {";
      for (const auto &operation : backend.second)
      {
        printString(operation.first, stream);
        stream << ": {";
        for (const auto &type : operation.second)
        {
          printBool(type.first, stream);
          stream << ": [";
          printOperation(type.second, stream);
          stream << "], ";
        }
        stream.seekp(-2, std::ofstream::end);
        stream << "}, ";
      }
      stream.seekp(-2, std::ofstream::end);
      stream << "}, ";
    }
    stream.seekp(-2, std::ofstream::end);
    stream << "}";
    stream.close();
  }
}

void JSON::loadOperationsExecTime()
{
  std::ifstream stream(_measurement_file);
  if (stream.is_open())
  {
    std::string backend;
    std::string operation;
    bool quant = false;
    char buf;
    int number_of_open_braces = 0;

    while (stream.good())
    {
      stream.get(buf);
      switch (buf)
      {
        case '{':
          number_of_open_braces++;
          break;
        case '}':
          number_of_open_braces--;
          break;
        case '"':
        {
          if (number_of_open_braces == 1)
          {
            // read backend string
            readString(backend, stream);
          }
          if (number_of_open_braces == 2)
          {
            // read operation string
            readString(operation, stream);
          }
          if (number_of_open_braces == 3)
          {
            // read operation string
            readBool(quant, stream);
          }
          break;
        }
        case '[':
        {
          // reading and creating all info for operation
          readOperation(backend, operation, quant, stream);
          break;
        }
        default:
          break;
      }
    }
    stream.close();
  }
}

} // namespace backend
} // namespace neurun