Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
common.h
1 /************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21 
22 #ifndef SINGA_UTILS_COMMON_H_
23 #define SINGA_UTILS_COMMON_H_
24 
25 #include <google/protobuf/message.h>
26 #include <unordered_map>
27 #include <sstream>
28 #include <string>
29 #include <vector>
30 #include <utility>
31 #include "proto/common.pb.h"
32 
33 namespace singa {
34 
35 std::string IntVecToString(const std::vector<int>& vec);
36 std::string VStringPrintf(std::string fmt, va_list l);
37 std::string StringPrintf(std::string fmt, ...);
38 
47 int ArgPos(int argc, char** arglist, const char* arg);
48 void CreateFolder(const std::string name);
57 const std::vector<std::vector<int>> Slice(int num,
58  const std::vector<int>& sizes);
66 const std::vector<int> PartitionSlices(int num, const std::vector<int>& slices);
67 /*
68 inline void Sleep(int millisec=1){
69  std::this_thread::sleep_for(std::chrono::milliseconds(millisec));
70 }
71 */
72 int gcd(int a, int b);
73 int LeastCommonMultiple(int a, int b);
74 /*
75 inline float rand_real() {
76  return static_cast<float>(rand_r())/(RAND_MAX+1.0f);
77 }
78 */
79 std::string GetHostIP();
80 void SetupLog(const std::string& workspace, const std::string& model);
81 
85 class Metric {
86  public:
87  Metric() {}
88  explicit Metric(const std::string& str);
97  void Add(const std::string& name, float value);
98  void Add(const std::string& name, float value, int count);
102  void Reset();
106  std::string ToLogString() const;
110  std::string ToString() const;
114  void ParseFrom(const std::string& msg);
115 
116  private:
117  std::unordered_map<std::string, std::pair<int, float>> entry_;
118 };
119 
120 using google::protobuf::Message;
121 void Im2col(const float* data_im, const int channels,
122  const int height, const int width, const int kernel_h, const int kernel_w,
123  const int pad_h, const int pad_w, const int stride_h, const int stride_w,
124  float* data_col);
125 void Col2im(const float* data_col, const int channels,
126  const int height, const int width, const int patch_h, const int patch_w,
127  const int pad_h, const int pad_w, const int stride_h, const int stride_w,
128  float* data_im);
129 void ForwardMaxPooling(const float* bottom, const int num, const int channels,
130  const int height, const int width, const int kernel_h, const int kernel_w,
131  const int pad_h, const int pad_w, const int stride_h, const int stride_w,
132  float* top, float* mask);
133 void BackwardMaxPooling(const float* top, const float* mask, const int num,
134  const int channels, const int height, const int width,
135  const int kernel_h, const int kernel_w, const int pad_h, const int pad_w,
136  const int stride_h, const int stride_w,
137  float* bottom);
138 void ForwardAvgPooling(const float* bottom, const int num, const int channels,
139  const int height, const int width, const int kernel_h, const int kernel_w,
140  const int pad_h, const int pad_w, const int stride_h, const int stride_w,
141  float* top);
142 void BackwardAvgPooling(const float* top, const int num, const int channels,
143  const int height, const int width, const int kernel_h, const int kernel_w,
144  const int pad_h, const int pad_w, const int stride_h, const int stride_w,
145  float* bottom);
146 
147 void ReadProtoFromTextFile(const char* filename, Message* proto);
148 void WriteProtoToTextFile(const Message& proto, const char* filename);
149 void ReadProtoFromBinaryFile(const char* filename, Message* proto);
150 void WriteProtoToBinaryFile(const Message& proto, const char* filename);
151 
152 
153 } // namespace singa
154 
155 #endif // SINGA_UTILS_COMMON_H_
void Add(const std::string &name, float value)
Add one metric.
const std::vector< std::vector< int > > Slice(int num, const std::vector< int > &sizes)
Slice a set of large Params into small pieces such that they can be roughtly equally partitioned into...
std::string ToString() const
Serialize the object into a string.
const std::vector< int > PartitionSlices(int num, const std::vector< int > &slices)
Partition slices into boxes.
void Reset()
reset all metric counter and value to 0
void ParseFrom(const std::string &msg)
Parse the metric from a string.
int ArgPos(int argc, char **arglist, const char *arg)
Locate the position of the arg in arglist.
std::string ToLogString() const
Generate a one-line string for logging.
Performance mtrics.
Definition: common.h:85