handle.cpp 6.0 KB
Newer Older
1 2 3 4
#include "megdnn/basic_types.h"

#include "src/common/handle_impl.h"
#include "src/common/utils.h"
5
#if !MGE_BUILD_CL_ONLY
6
#include "src/fallback/handle.h"
7
#endif
8 9 10 11 12 13 14 15
#include "src/naive/handle.h"

#include "midout.h"

#if MEGDNN_X86
#include "src/x86/handle.h"
#endif

16 17 18 19 20 21 22 23
#if MEGDNN_ARMV7
#include "src/armv7/handle.h"
#endif

#if MEGDNN_AARCH64
#include "src/aarch64/handle.h"
#endif

24 25 26 27
#if MEGDNN_WITH_CUDA
#include "src/cuda/handle.h"
#endif

28 29 30 31 32 33 34 35
#if MEGDNN_WITH_CAMBRICON
#include "src/cambricon/handle.h"
#endif

#ifdef MEGDNN_WITH_ATLAS
#include "src/atlas/handle.h"
#endif

36 37 38 39 40 41 42 43
using namespace megdnn;

MIDOUT_DECL(HandlePlatform);
MIDOUT_DECL(HandleOpr);

Handle::Handle(megcoreComputingHandle_t computing_handle, HandleType type)
        : m_computing_handle(computing_handle), m_handle_type(type) {}

M
Megvii Engine Team 已提交
44 45
std::unique_ptr<Handle> Handle::make(
        megcoreComputingHandle_t computing_handle, int debug_level) {
46 47 48 49 50 51 52 53 54 55
    (void)debug_level;
    megcoreDeviceHandle_t device_handle;
    megcorePlatform_t platform;
    megcoreGetDeviceHandle(computing_handle, &device_handle);

    megcoreGetPlatform(device_handle, &platform);
    if (platform == megcorePlatformCPU) {
        // only enable midout for CPU, becuase CPU might be unused when some
        // other platforms are used
        MIDOUT_BEGIN(HandlePlatform, midout_iv(megcorePlatformCPU)) {
M
Megvii Engine Team 已提交
56
            // CPU
57 58 59 60 61 62 63 64 65 66
#if MEGDNN_NAIVE
            return make_unique<naive::HandleImpl>(computing_handle);
#else
            if (debug_level == 0) {
#if MEGDNN_X86
                // Because of ICC bug, we cannot use make_unique here. It will
                // trigger an internal compiler error.
                return std::unique_ptr<x86::HandleImpl>(
                        new x86::HandleImpl(computing_handle));
                // return make_unique<x86::HandleImpl>(computing_handle);
67 68 69 70
#elif MEGDNN_ARMV7
                return make_unique<armv7::HandleImpl>(computing_handle);
#elif MEGDNN_AARCH64
                return make_unique<aarch64::HandleImpl>(computing_handle);
71 72 73 74 75 76 77 78
#else
                return make_unique<fallback::HandleImpl>(computing_handle);
#endif
            } else if (debug_level == 1) {
                return make_unique<fallback::HandleImpl>(computing_handle);
            } else if (debug_level == 2) {
                return make_unique<naive::HandleImpl>(computing_handle);
            } else {
M
Megvii Engine Team 已提交
79
                megdnn_throw("Debug level must be 0/1/2.");
80
            }
81 82
#endif
        }
M
Megvii Engine Team 已提交
83 84 85 86
        MIDOUT_END();

    }
    else if (platform == megcorePlatformROCM) {
87
#if MEGDNN_WITH_ROCM
M
Megvii Engine Team 已提交
88
        return make_rocm_handle(computing_handle);
89
#else
M
Megvii Engine Team 已提交
90
        return nullptr;
91
#endif
M
Megvii Engine Team 已提交
92
    } else if (platform == megcorePlatformCambricon) {
93
#if MEGDNN_WITH_CAMBRICON
M
Megvii Engine Team 已提交
94
        return make_unique<cambricon::HandleImpl>(computing_handle);
95
#else
M
Megvii Engine Team 已提交
96
        return nullptr;
97
#endif
M
Megvii Engine Team 已提交
98
    } else if (platform == megcorePlatformAtlas) {
99
#if MEGDNN_WITH_ATLAS
M
Megvii Engine Team 已提交
100
        return make_unique<atlas::HandleImpl>(computing_handle);
101
#else
M
Megvii Engine Team 已提交
102
        return nullptr;
103
#endif
M
Megvii Engine Team 已提交
104 105 106
    }
    else {
        // CUDA
M
Megvii Engine Team 已提交
107 108 109
        megdnn_throw_if(
                platform != megcorePlatformCUDA, megdnn_error,
                "platform should be CUDA Platform");
110
#if MEGDNN_WITH_CUDA
M
Megvii Engine Team 已提交
111
        return make_unique<cuda::HandleImpl>(computing_handle);
112
#else
113
        return nullptr;
M
Megvii Engine Team 已提交
114
#endif
115
    }
M
Megvii Engine Team 已提交
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
    return nullptr;
}

void Handle::set_destructor(const thin_function<void()>& d) {
    megdnn_assert(!m_destructor, "destructor can be set only once");
    m_destructor = d;
}

Handle::~Handle() {
    if (m_destructor)
        m_destructor();
    m_alive_magic = 0;
}

size_t Handle::alignment_requirement() const {
    // default to 32
    return 32;
}

size_t Handle::image2d_pitch_alignment() const {
    megdnn_throw("image2d tensor format not supported on this handle");
}

megdnn::HandleImplHelper::HandleVendorType Handle::vendor_type() const {
    return HandleVendorType::NOT_SPEC;
}

bool Handle::check_cross_dev_copy_constraint(const TensorLayout& src) {
    return src.is_contiguous();
}

void Handle::on_opr_destructed(OperatorBase* opr) {
    if (m_alive_magic != ALIVE_MAGIC) {
        megdnn_log_error(
                "Handle is destructed before opr gets destructed. "
                "Please fix the destruction order as this would cause "
                "undefined memory access. "
                "Abort now to avoid further problems.");
        abort();
155
    }
M
Megvii Engine Team 已提交
156 157
    if (m_on_opr_destructed) {
        m_on_opr_destructed(opr);
158
    }
M
Megvii Engine Team 已提交
159
}
160

M
Megvii Engine Team 已提交
161 162 163
OperatorBase::~OperatorBase() {
    m_handle->on_opr_destructed(this);
}
164

M
Megvii Engine Team 已提交
165 166
template <typename Opr>
std::unique_ptr<Opr> Handle::create_operator() {
167 168 169 170 171 172 173 174
#define CASE(etype, nm)                                                        \
    case HandleType::etype: {                                                  \
        MIDOUT_BEGIN(HandleOpr, Opr, midout_iv(HandleType::etype)) {           \
            return static_cast<nm::HandleImpl*>(this)->create_operator<Opr>(); \
        }                                                                      \
        MIDOUT_END();                                                          \
    }

M
Megvii Engine Team 已提交
175 176
    switch (m_handle_type) {
        CASE(NAIVE, naive);
177
#if !MEGDNN_NAIVE
M
Megvii Engine Team 已提交
178
        CASE(FALLBACK, fallback);
179
#if MEGDNN_X86
M
Megvii Engine Team 已提交
180
        CASE(X86, x86);
181
#endif
182
#if MEGDNN_ARMV7
M
Megvii Engine Team 已提交
183
        CASE(ARMV7, armv7);
184 185
#endif
#if MEGDNN_AARCH64
M
Megvii Engine Team 已提交
186
        CASE(AARCH64, aarch64);
187 188
#endif
#if MEGDNN_ARMV7 || MEGDNN_AARCH64
M
Megvii Engine Team 已提交
189
        CASE(ARM_COMMON, arm_common);
190
#endif
191 192
#endif  // !MEGDNN_NAIVE
#if MEGDNN_WITH_CUDA
M
Megvii Engine Team 已提交
193
        CASE(CUDA, cuda);
194 195
#endif
#if MEGDNN_WITH_ATLAS
M
Megvii Engine Team 已提交
196
        CASE(ATLAS, atlas);
197
#endif
198
#if MEGDNN_WITH_ROCM
M
Megvii Engine Team 已提交
199 200 201
        case HandleType::ROCM: {
            MIDOUT_BEGIN(HandleOpr, Opr, midout_iv(HandleType::ROCM)) {
                return create_rocm_operator<Opr>();
202
            }
M
Megvii Engine Team 已提交
203 204
            MIDOUT_END();
        }
205
#endif
206 207
#if MEGDNN_WITH_CAMBRICON
            CASE(CAMBRICON, cambricon);
208
#endif
M
Megvii Engine Team 已提交
209 210
        default:
            megdnn_throw("bad handle type");
211
    }
M
Megvii Engine Team 已提交
212 213
#undef CASE
}
214 215

#define INST(opr) template std::unique_ptr<opr> Handle::create_operator();
M
Megvii Engine Team 已提交
216
MEGDNN_FOREACH_OPR_CLASS(INST)
217

218 219
#undef INST
// vim: syntax=cpp.doxygen