progressreporter.h 2.5 KB
Newer Older
M
Matt Pharr 已提交
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
// pbrt is Copyright(c) 1998-2020 Matt Pharr, Wenzel Jakob, and Greg Humphreys.
// The pbrt source code is licensed under the Apache License, Version 2.0.
// SPDX: Apache-2.0

#ifndef PBRT_UTIL_PROGRESSREPORTER_H
#define PBRT_UTIL_PROGRESSREPORTER_H

#include <pbrt/pbrt.h>

#include <atomic>
#include <chrono>
#include <cstdint>
#include <string>
#include <thread>

#ifdef PBRT_BUILD_GPU_RENDERER
#include <cuda_runtime.h>
#include <pbrt/util/pstd.h>
#include <vector>
#endif

namespace pbrt {

// Timer Definition
class Timer {
  public:
    Timer() { start = clock::now(); }
    double ElapsedSeconds() const {
        clock::time_point now = clock::now();
        int64_t elapseduS =
            std::chrono::duration_cast<std::chrono::microseconds>(now - start).count();
        return elapseduS / 1000000.;
    }

    std::string ToString() const;

  private:
    using clock = std::chrono::steady_clock;
    clock::time_point start;
};

// ProgressReporter Definition
class ProgressReporter {
  public:
    // ProgressReporter Public Methods
    ProgressReporter() : quiet(true) {}
    ProgressReporter(int64_t totalWork, const std::string &title, bool quiet,
                     bool gpu = false);
49

M
Matt Pharr 已提交
50 51
    ~ProgressReporter();

52
    void Update(int64_t num = 1);
M
Matt Pharr 已提交
53
    void Done();
54
    double ElapsedSeconds() const;
M
Matt Pharr 已提交
55 56 57 58 59 60 61 62

    std::string ToString() const;

  private:
    // ProgressReporter Private Methods
    void launchThread();
    void printBar();

63
    // ProgressReporter Private Members
M
Matt Pharr 已提交
64 65 66 67 68 69 70 71 72 73
    int64_t totalWork;
    std::string title;
    bool quiet;
    Timer timer;
    std::atomic<int64_t> workDone;
    std::atomic<bool> exitThread;
    std::thread updateThread;

#ifdef PBRT_BUILD_GPU_RENDERER
    std::vector<cudaEvent_t> gpuEvents;
M
Matt Pharr 已提交
74
    std::atomic<size_t> gpuEventsLaunchedOffset;
M
Matt Pharr 已提交
75 76 77
    int gpuEventsFinishedOffset;
#endif
};
78 79 80 81 82 83 84 85
// ProgressReporter Inline Method Definitions
inline double ProgressReporter::ElapsedSeconds() const {
    return timer.ElapsedSeconds();
}

inline void ProgressReporter::Update(int64_t num) {
#ifdef PBRT_BUILD_GPU_RENDERER
    if (gpuEvents.size() > 0) {
86 87 88 89 90 91
        if (gpuEventsLaunchedOffset + num <= gpuEvents.size()) {
            while (num-- > 0) {
                CHECK_EQ(cudaEventRecord(gpuEvents[gpuEventsLaunchedOffset]),
                         cudaSuccess);
                ++gpuEventsLaunchedOffset;
            }
92 93 94 95 96 97 98 99
        }
        return;
    }
#endif
    if (num == 0 || quiet)
        return;
    workDone += num;
}
M
Matt Pharr 已提交
100 101 102 103

}  // namespace pbrt

#endif  // PBRT_UTIL_PROGRESSREPORTER_H