BarrierStat.cpp 11.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

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 <sys/types.h>
#include <iomanip>
#include <algorithm>
#include <string.h>
#include "paddle/utils/Stat.h"
#include "paddle/utils/BarrierStat.h"
#include "paddle/utils/Flags.h"

23 24
P_DEFINE_bool(log_barrier_abstract,
              true,
Z
zhangjinchao01 已提交
25
              "if true, show abstract of barrier performance");
26 27
P_DEFINE_int32(log_barrier_lowest_nodes,
               5,
Z
zhangjinchao01 已提交
28
               "how many lowest node will be logged");
29 30
P_DEFINE_bool(log_barrier_show_log,
              false,  // for performance tuning insight
Z
zhangjinchao01 已提交
31 32 33 34
              "if true, always show barrier abstract even with little gap");

namespace paddle {

35
std::ostream &operator<<(std::ostream &output, const BarrierStatBase &stat) {
Z
zhangjinchao01 已提交
36
  if (FLAGS_log_barrier_abstract) {
Y
Yu Yang 已提交
37
    std::lock_guard<std::mutex> guard(stat.lock_);
Z
zhangjinchao01 已提交
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
    stat.showAbstract(output);
  }
  return output;
}

BarrierStatBase::BarrierStatBase(uint16_t numConnThreads,
                                 const std::string &name)
    : totSamples_(0), numConnThreads_(numConnThreads), name_(name) {
  abstract_.resize(numConnThreads_);
  if (FLAGS_log_barrier_show_log) {
    rateThreshold_ = 0.0;
  } else {
    /* probablity of abnormal node
     * p = 1/n + (n/8)/(n+1), n = nodes, n > 1
     * if the freq of lowest trainerId larger than p,
     * output FLAGS_log_barrier_lowest_nodes lastTrainerId.
     * numConnThreads_ indicates nodes
     */
    float n = (float)numConnThreads;
    rateThreshold_ = 1.0 / n + (n / 8.0) / (n + 1.0);
  }
}

BarrierEndStat::BarrierEndStat(uint16_t numConnThreads, const std::string &name)
    : BarrierStatBase(numConnThreads, name) {
  timeVector_.reset(new TimeVectorEnd(numConnThreads_));
  reset(true);
  LOG(INFO) << " create barrierEndStat: " << name
            << " endBarrier warning rate: " << rateThreshold_;
}

/*
 * Note:
 * the design different pserver entity owns different statSet to obey
 * the background that different pserver runs separately.
 */
void BarrierEndStat::updateStat(struct timeval &cur, int32_t trainerId) {
  CHECK_LT(trainerId, numConnThreads_) << "trainerId is invalid in barrier";

  std::lock_guard<std::mutex> guard(lock_);
  timeVector_->addTimeval(cur, trainerId);

  if (timeVector_->full()) {
    std::lock_guard<std::mutex> abstractGuard(abstractLock_);
    auto id = timeVector_->getLastTrainerId();
    auto delta = timeToMicroSecond(timeVector_->getDelta());
    auto secondDelta = timeToMicroSecond(timeVector_->get1NDelta());
    auto lastTwoDelta = timeToMicroSecond(timeVector_->getMinus1NDelta());
    auto midDelta = timeToMicroSecond(timeVector_->getMidNDelta());
    // discard first sample, since first sample probably is abnormal.
    if (totSamples_) {
      abstract_[id].freq++;

      if (delta < abstract_[id].minDelta) {
        abstract_[id].minDelta = delta;
      }
      if (delta > abstract_[id].maxDelta) {
        abstract_[id].maxDelta = delta;
      }
      abstract_[id].totDelta += delta;
      abstract_[id].totSecondDelta += secondDelta;
      abstract_[id].totLastTwoDelta += lastTwoDelta;
      abstract_[id].totMidDelta += midDelta;

      // update totAbstract_
      totAbstract_.freq++;
      if (delta < totAbstract_.minDelta) {
        totAbstract_.minDelta = delta;
      }
      if (delta > totAbstract_.maxDelta) {
        totAbstract_.maxDelta = delta;
      }
      totAbstract_.totDelta += delta;
      totAbstract_.totSecondDelta += secondDelta;
      totAbstract_.totLastTwoDelta += lastTwoDelta;
      totAbstract_.totMidDelta += midDelta;
    }

    totSamples_++;
    timeVector_->reset();
  }
}

void BarrierEndStat::reset(bool clearRawData) {
  int32_t i = 0;

  totSamples_ = 0;

  std::lock_guard<std::mutex> guard(abstractLock_);

  if (clearRawData) {
    timeVector_->reset();
  }

  for (auto &abstract : abstract_) {
    memset((void *)&abstract, 0, sizeof(abstract));
    abstract.minDelta = UINT64_MAX;
    abstract.trainerId = i++;
  }
  memset((void *)&totAbstract_, 0, sizeof(Abstract));
  totAbstract_.minDelta = UINT64_MAX;
}

Y
Yu Yang 已提交
141
void BarrierEndStat::showAbstract(std::ostream &output) const {
Z
zhangjinchao01 已提交
142 143 144 145 146 147 148
  // do not support the case "<=2 pserver"
  if (numConnThreads_ <= 2 || !totSamples_) {
    return;
  }

  // duplicate freq info
  std::vector<struct Abstract> outputAbstract = abstract_;
149 150
  std::sort(outputAbstract.begin(),
            outputAbstract.end(),
Z
zhangjinchao01 已提交
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
            [](const struct Abstract &a, const struct Abstract &b) {
              return a.freq > b.freq;
            });

  auto rate = (float)outputAbstract[0].freq / (float)totSamples_;
  if (rate < rateThreshold_) {
    return;
  }

  output << std::setw(20) << name_ << std::endl;

  /*
   * Note:
   * avgGap:        the average delta between 1 -- n arriving trainers
   * avgSecondGap:  the average delta between 2 -- n arriving trainers
   * avgLastTwoGap: the average delta between n-1 -- n  arriving trainers
   * avgMidGap:     the average delta between n/2 -- n  arriving trainers
   * rato: samples / totSamples
   *
   * the stat is based on per trainer if trainer_id is set, totAbstract is
   * stat based on all trainers scope.
   */
  output << std::setw(42) << " " << std::setw(15) << "trainerId"
         << std::setw(15) << "avgGap" << std::setw(15) << "avgSecondGap"
         << std::setw(15) << "avgLastTwoGap" << std::setw(15) << "avgMidGap"
         << std::setw(10) << "rate" << std::setw(10) << "samples"
         << std::setw(10) << "totSamples" << std::endl;
  // show totAbstract, it's valuable when lastTrainerId is even-distributed'
  if (!totAbstract_.freq) return;
  output << std::setw(42) << " " << std::setw(15) << "totAbstract"
         << std::setw(15) << (totAbstract_.totDelta / totAbstract_.freq) * 0.001
         << std::setw(15)
         << (totAbstract_.totSecondDelta / totAbstract_.freq) * 0.001
         << std::setw(15)
         << (totAbstract_.totLastTwoDelta / totAbstract_.freq) * 0.001
         << std::setw(15)
         << (totAbstract_.totMidDelta / totAbstract_.freq) * 0.001
         << std::setw(10) << (float)totAbstract_.freq / (float)totSamples_
         << std::setw(10) << (float)totAbstract_.freq << std::setw(10)
         << (float)totSamples_ << std::endl;

  // show lastTrainerId abstract
  int count = 0;
  for (auto &abstract : outputAbstract) {
    if (!abstract.freq || count++ >= FLAGS_log_barrier_lowest_nodes) {
      break;
    }
    // output format control
    output << std::setw(42) << " " << std::setw(15) << abstract.trainerId
           << std::setw(15) << (abstract.totDelta / abstract.freq) * 0.001
           << std::setw(15) << (abstract.totSecondDelta / abstract.freq) * 0.001
           << std::setw(15)
           << (abstract.totLastTwoDelta / abstract.freq) * 0.001
           << std::setw(15) << (abstract.totMidDelta / abstract.freq) * 0.001
           << std::setw(10) << (float)abstract.freq / (float)totSamples_
           << std::setw(10) << (float)abstract.freq << std::setw(10)
           << (float)totSamples_ << std::endl;
  }
}

BarrierDeltaStat::BarrierDeltaStat(uint16_t numConnThreads,
                                   const std::string &name)
    : BarrierStatBase(numConnThreads, name) {
  timeVector_.reset(new TimeVectorDelta(numConnThreads_));
  reset(true);
  LOG(INFO) << " create barrierDeltaStat: " << name
            << " barrierDelta warning rate: " << rateThreshold_;
}

void BarrierDeltaStat::updateStat(uint64_t delta, int32_t trainerId) {
  CHECK_LT(trainerId, numConnThreads_) << "trainerId is invalid in barrier";

  std::lock_guard<std::mutex> guard(lock_);
  timeVector_->addTimeval(delta, trainerId);

  if (timeVector_->full()) {
    std::lock_guard<std::mutex> abstractGuard(abstractLock_);
    auto id = timeVector_->getMaxTrainerId();
    auto delta = timeVector_->getDelta();
    // discard first sample, since first sample probably is abnormal.
    if (totSamples_) {
      abstract_[id].freq++;

      if (delta < abstract_[id].minDelta) {
        abstract_[id].minDelta = delta;
      }
      if (delta > abstract_[id].maxDelta) {
        abstract_[id].maxDelta = delta;
      }
      abstract_[id].totDelta += delta;

      // update totAbstract_
      totAbstract_.freq++;
      if (delta < totAbstract_.minDelta) {
        totAbstract_.minDelta = delta;
      }
      if (delta > totAbstract_.maxDelta) {
        totAbstract_.maxDelta = delta;
      }
      totAbstract_.totDelta += delta;
    }

    totSamples_++;
    timeVector_->reset();
  }
}

void BarrierDeltaStat::reset(bool clearRawData) {
  int32_t i = 0;

  totSamples_ = 0;

  std::lock_guard<std::mutex> guard(abstractLock_);

  if (clearRawData) {
    timeVector_->reset();
  }

  for (auto &abstract : abstract_) {
    memset((void *)&abstract, 0, sizeof(abstract));
    abstract.minDelta = UINT64_MAX;
    abstract.trainerId = i++;
  }
  memset((void *)&totAbstract_, 0, sizeof(Abstract));
  totAbstract_.minDelta = UINT64_MAX;
}

Y
Yu Yang 已提交
278
void BarrierDeltaStat::showAbstract(std::ostream &output) const {
Z
zhangjinchao01 已提交
279 280 281 282 283 284 285
  // do not support the case "<=2 pserver"
  if (numConnThreads_ <= 2 || !totSamples_) {
    return;
  }

  // duplicate freq info
  std::vector<struct Abstract> outputAbstract = abstract_;
286 287
  std::sort(outputAbstract.begin(),
            outputAbstract.end(),
Z
zhangjinchao01 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
            [](const struct Abstract &a, const struct Abstract &b) {
              return a.freq > b.freq;
            });

  auto rate = (float)outputAbstract[0].freq / (float)totSamples_;
  if (rate < rateThreshold_) {
    return;
  }

  output << std::setw(20) << name_ << std::endl;

  /* Note:
   * Gap means the delta from all trainers' forwardbackward
   * avgGap: average Gap in log_period batches
   * minGap: min Gap in log_period batches
   * maxGap: max Gap in log_period batches
   * trainerId: the slowest trainer_id
   *
   * the stat is based on per trainer if trainer_id is set, totAbstract is
   * stat based on all trainers scope.
   */
  output << std::setw(42) << " " << std::setw(15) << "trainerId"
         << std::setw(15) << "avgGap" << std::setw(10) << "minGap"
         << std::setw(10) << "maxGap" << std::setw(10) << "rate"
         << std::setw(10) << "samples" << std::setw(10) << "totSamples"
         << std::endl;
  // show totAbstract, it's valuable when lastTrainerId is even-distributed'
  if (!totAbstract_.freq) return;
  output << std::setw(42) << " " << std::setw(15) << "totAbstract"
         << std::setw(15) << (totAbstract_.totDelta / totAbstract_.freq) * 0.001
         << std::setw(10) << totAbstract_.minDelta * 0.001 << std::setw(10)
         << totAbstract_.maxDelta * 0.001 << std::setw(10)
         << (float)totAbstract_.freq / (float)totSamples_ << std::setw(10)
         << (float)totAbstract_.freq << std::setw(10) << (float)totSamples_
         << std::endl;

  // show lastTrainerId abstract
  int count = 0;
  for (auto &abstract : outputAbstract) {
    if (!abstract.freq || count++ >= FLAGS_log_barrier_lowest_nodes) {
      break;
    }
    // output format control
    output << std::setw(42) << " " << std::setw(15) << abstract.trainerId
           << std::setw(15) << (abstract.totDelta / abstract.freq) * 0.001
           << std::setw(10) << abstract.minDelta * 0.001 << std::setw(10)
           << abstract.maxDelta * 0.001 << std::setw(10)
           << (float)abstract.freq / (float)totSamples_ << std::setw(10)
           << (float)abstract.freq << std::setw(10) << (float)totSamples_
           << std::endl;
  }
}
}  // namespace paddle