2019-04-17 10:49:11 +08:00
|
|
|
//
|
|
|
|
// Backend.cpp
|
|
|
|
// MNN
|
|
|
|
//
|
|
|
|
// Created by MNN on 2018/07/06.
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <mutex>
|
|
|
|
#include "MNN_generated.h"
|
2019-12-27 22:16:57 +08:00
|
|
|
#include "core/Macro.h"
|
|
|
|
#include "core/Backend.hpp"
|
2019-04-17 10:49:11 +08:00
|
|
|
|
|
|
|
namespace MNN {
|
|
|
|
|
2019-05-14 19:54:21 +08:00
|
|
|
void registerBackend();
|
2019-05-09 19:39:33 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
static std::map<MNNForwardType, std::pair<const BackendCreator*, bool>>& GetExtraCreator() {
|
|
|
|
static std::once_flag flag;
|
|
|
|
static std::map<MNNForwardType, std::pair<const BackendCreator*, bool>>* gExtraCreator;
|
|
|
|
std::call_once(flag,
|
|
|
|
[&]() { gExtraCreator = new std::map<MNNForwardType, std::pair<const BackendCreator*, bool>>; });
|
|
|
|
return *gExtraCreator;
|
|
|
|
}
|
|
|
|
|
|
|
|
const BackendCreator* MNNGetExtraBackendCreator(MNNForwardType type) {
|
2019-05-14 19:54:21 +08:00
|
|
|
registerBackend();
|
2019-05-08 15:44:57 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
auto& gExtraCreator = GetExtraCreator();
|
|
|
|
auto iter = gExtraCreator.find(type);
|
|
|
|
if (iter == gExtraCreator.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!iter->second.second) {
|
|
|
|
return iter->second.first;
|
|
|
|
}
|
|
|
|
Backend::Info info;
|
|
|
|
info.type = type;
|
|
|
|
std::shared_ptr<Backend> bn(iter->second.first->onCreate(info));
|
|
|
|
if (nullptr != bn.get()) {
|
|
|
|
return iter->second.first;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MNNInsertExtraBackendCreator(MNNForwardType type, const BackendCreator* creator, bool needCheck) {
|
|
|
|
auto& gExtraCreator = GetExtraCreator();
|
|
|
|
if (gExtraCreator.find(type) != gExtraCreator.end()) {
|
|
|
|
MNN_ASSERT(false && "duplicate type");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gExtraCreator.insert(std::make_pair(type, std::make_pair(creator, needCheck)));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace MNN
|