common.h 2.36 KB
Newer Older
Nianchen Deng's avatar
sync    
Nianchen Deng committed
1
#pragma once
Nianchen Deng's avatar
Nianchen Deng committed
2
3
4
#ifdef WIN32
#include <Windows.h>
#endif
Nianchen Deng's avatar
sync    
Nianchen Deng committed
5
6
7
8
9
10
#include <memory>
#include <stdexcept>
#include <vector>
#include <string>
#include <sstream>
#include <GL/glew.h>
Nianchen Deng's avatar
sync    
Nianchen Deng committed
11
12
#include "common/logger.h"
#include "common/fmt.h"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
13

Nianchen Deng's avatar
Nianchen Deng committed
14
15
16
17
#ifdef WIN32
typedef unsigned int uint;
#endif

Nianchen Deng's avatar
sync    
Nianchen Deng committed
18
19
#ifndef COUNTOF
#define COUNTOF(__x__) (sizeof(__x__) / sizeof((__x__)[0]))
Nianchen Deng's avatar
sync    
Nianchen Deng committed
20
#endif
Nianchen Deng's avatar
sync    
Nianchen Deng committed
21
22
23
24
#ifndef CEILDIV
#define CEILDIV(__x__, __y__) (uint) ceil((__x__) / (float)(__y__))
#endif
#define INTERVAL(__start__, __end__) (((__end__) - (__start__)) / (float)CLOCKS_PER_SEC * 1000)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
25

Nianchen Deng's avatar
sync    
Nianchen Deng committed
26
inline uint getElementSize(nv::DataType t) {
Nianchen Deng's avatar
sync    
Nianchen Deng committed
27
28
    switch (t) {
    case nv::DataType::kINT32:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
29
    case nv::DataType::kFLOAT:
Nianchen Deng's avatar
sync    
Nianchen Deng committed
30
31
32
33
34
35
        return 4;
    case nv::DataType::kHALF:
        return 2;
    case nv::DataType::kBOOL:
    case nv::DataType::kINT8:
        return 1;
Nianchen Deng's avatar
sync    
Nianchen Deng committed
36
37
38
    default:
        throw std::runtime_error("Invalid DataType.");
    }
Nianchen Deng's avatar
sync    
Nianchen Deng committed
39
40
41
42
}

template <typename T> void dumpRow(std::ostream &os, T *buf, size_t n) {
    os << buf[0];
Nianchen Deng's avatar
Nianchen Deng committed
43
    for (size_t i = 1; i < n; ++i)
Nianchen Deng's avatar
sync    
Nianchen Deng committed
44
45
46
47
48
        os << " " << buf[i];
    os << std::endl;
}

template <typename T>
Nianchen Deng's avatar
Nianchen Deng committed
49
void dumpHostBuffer(std::ostream &os, T *buf, size_t bufSize, size_t rowCount,
Nianchen Deng's avatar
sync    
Nianchen Deng committed
50
51
52
53
54
55
56
57
58
59
60
61
                    size_t maxDumpRows = 0) {
    size_t numItems = bufSize / sizeof(T);
    size_t nInLastRow = numItems % rowCount;
    size_t rows;
    if (nInLastRow == 0) {
        rows = numItems / rowCount;
        nInLastRow = rowCount;
    } else {
        rows = numItems / rowCount + 1;
    }
    if (maxDumpRows == 0) {
        for (size_t i = 0; i < rows - 1; ++i) {
Nianchen Deng's avatar
Nianchen Deng committed
62
63
            dumpRow(os, buf, rowCount);
            buf += rowCount;
Nianchen Deng's avatar
sync    
Nianchen Deng committed
64
        }
Nianchen Deng's avatar
Nianchen Deng committed
65
        dumpRow(os, buf, nInLastRow);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
66
67
    } else {
        for (size_t i = 0; i < maxDumpRows / 2; ++i)
Nianchen Deng's avatar
Nianchen Deng committed
68
            dumpRow(os, buf + i * rowCount, rowCount);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
69
70
        os << "..." << std::endl;
        for (size_t i = rows - maxDumpRows + maxDumpRows / 2; i < rows - 1; ++i)
Nianchen Deng's avatar
Nianchen Deng committed
71
72
            dumpRow(os, buf + i * rowCount, rowCount);
        dumpRow(os, buf + (rows - 1) * rowCount, nInLastRow);
Nianchen Deng's avatar
sync    
Nianchen Deng committed
73
74
75
76
77
78
79
80
81
82
    }
}

template <typename T> struct Destroy {
    void operator()(T *t) {
        if (t != nullptr)
            t->destroy();
    }
};

Nianchen Deng's avatar
sync    
Nianchen Deng committed
83
#include "Formatter.h"
Nianchen Deng's avatar
sync    
Nianchen Deng committed
84
85
86
87

template <class T> using uptr = std::unique_ptr<T, ::Destroy<T>>;
template <class T> using sptr = std::shared_ptr<T>;

Nianchen Deng's avatar
sync    
Nianchen Deng committed
88
enum Eye { Eye_Left, Eye_Right };