2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Created by raver119 on 07.10.2017.
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBND4J_OPREGISTRATOR_H
|
|
|
|
#define LIBND4J_OPREGISTRATOR_H
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/pointercast.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <vector>
|
2020-01-24 08:11:09 +01:00
|
|
|
#include <unordered_map>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <mutex>
|
|
|
|
#include <ops/declarable/DeclarableOp.h>
|
2019-09-11 20:50:28 +02:00
|
|
|
#include <ops/declarable/PlatformHelper.h>
|
2020-01-20 19:32:46 +01:00
|
|
|
#include <execution/Engine.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// handlers part
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
|
|
|
|
2020-02-24 05:51:01 +01:00
|
|
|
#ifndef __JAVACPP_HACK__
|
|
|
|
|
|
|
|
namespace std {
|
|
|
|
|
|
|
|
template <>
|
2020-03-09 06:22:49 +01:00
|
|
|
class hash<std::pair<Nd4jLong, samediff::Engine>> {
|
2020-02-24 05:51:01 +01:00
|
|
|
public:
|
2020-03-09 06:22:49 +01:00
|
|
|
size_t operator()(const std::pair<Nd4jLong, samediff::Engine>& k) const;
|
2020-02-24 05:51:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2020-03-09 06:22:49 +01:00
|
|
|
class hash<std::pair<std::string, samediff::Engine>> {
|
2020-02-24 05:51:01 +01:00
|
|
|
public:
|
2020-03-09 06:22:49 +01:00
|
|
|
size_t operator()(const std::pair<std::string, samediff::Engine>& k) const;
|
2020-02-24 05:51:01 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
/**
|
|
|
|
* This class provides runtime ops lookup, based on opName or opHash.
|
|
|
|
* To build lookup directory we use *_OP_IMPL macro, which puts static structs at compile time in .cpp files,
|
|
|
|
* so once binary is executed, static objects are initialized automatically, and we get list of all ops
|
|
|
|
* available at runtime via this singleton.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ND4J_EXPORT OpRegistrator {
|
|
|
|
private:
|
|
|
|
static OpRegistrator* _INSTANCE;
|
|
|
|
OpRegistrator() {
|
|
|
|
nd4j_debug("OpRegistrator started\n","");
|
|
|
|
|
|
|
|
#ifndef _RELEASE
|
|
|
|
std::signal(SIGSEGV, &OpRegistrator::sigSegVHandler);
|
|
|
|
std::signal(SIGINT, &OpRegistrator::sigIntHandler);
|
|
|
|
std::signal(SIGABRT, &OpRegistrator::sigIntHandler);
|
|
|
|
std::signal(SIGFPE, &OpRegistrator::sigIntHandler);
|
|
|
|
std::signal(SIGILL, &OpRegistrator::sigIntHandler);
|
|
|
|
std::signal(SIGTERM, &OpRegistrator::sigIntHandler);
|
|
|
|
atexit(&OpRegistrator::exitHandler);
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2020-02-24 05:51:01 +01:00
|
|
|
MAP_IMPL<Nd4jLong, std::string> _msvc;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-11 20:50:28 +02:00
|
|
|
// pointers to our operations
|
2020-03-02 10:49:41 +01:00
|
|
|
MAP_IMPL<Nd4jLong, sd::ops::DeclarableOp*> _declarablesLD;
|
|
|
|
MAP_IMPL<std::string, sd::ops::DeclarableOp*> _declarablesD;
|
|
|
|
std::vector<sd::ops::DeclarableOp *> _uniqueD;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-11 20:50:28 +02:00
|
|
|
// pointers to platform-specific helpers
|
2020-03-09 06:22:49 +01:00
|
|
|
MAP_IMPL<std::pair<Nd4jLong, samediff::Engine>, sd::ops::platforms::PlatformHelper*> _helpersLH;
|
|
|
|
MAP_IMPL<std::pair<std::string, samediff::Engine>, sd::ops::platforms::PlatformHelper*> _helpersH;
|
2020-03-02 10:49:41 +01:00
|
|
|
std::vector<sd::ops::platforms::PlatformHelper*> _uniqueH;
|
2019-09-11 20:50:28 +02:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
std::mutex _locker;
|
|
|
|
std::string _opsList;
|
|
|
|
bool isInit = false;
|
|
|
|
public:
|
|
|
|
~OpRegistrator();
|
|
|
|
|
|
|
|
static OpRegistrator* getInstance();
|
|
|
|
|
|
|
|
static void exitHandler();
|
|
|
|
static void sigIntHandler(int sig);
|
|
|
|
static void sigSegVHandler(int sig);
|
|
|
|
|
|
|
|
void updateMSVC(Nd4jLong newHash, std::string& oldName);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string local_to_string(T value);
|
|
|
|
const char * getAllCustomOperations();
|
|
|
|
|
|
|
|
/**
|
2019-09-11 20:50:28 +02:00
|
|
|
* This method registers operation in our registry, so we can use them later
|
2019-06-06 14:21:15 +02:00
|
|
|
*
|
|
|
|
* @param op
|
|
|
|
*/
|
2020-03-02 10:49:41 +01:00
|
|
|
bool registerOperation(const char* name, sd::ops::DeclarableOp* op);
|
|
|
|
bool registerOperation(sd::ops::DeclarableOp *op);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
void registerHelper(sd::ops::platforms::PlatformHelper* op);
|
2019-09-11 20:50:28 +02:00
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
bool hasHelper(Nd4jLong hash, samediff::Engine engine);
|
2019-09-11 20:50:28 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::ops::DeclarableOp* getOperation(const char *name);
|
|
|
|
sd::ops::DeclarableOp* getOperation(Nd4jLong hash);
|
|
|
|
sd::ops::DeclarableOp* getOperation(std::string &name);
|
2019-09-11 20:50:28 +02:00
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
sd::ops::platforms::PlatformHelper* getPlatformHelper(Nd4jLong hash, samediff::Engine engine);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
std::vector<Nd4jLong> getAllHashes();
|
|
|
|
|
|
|
|
int numberOfOperations();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These structs are used to "register" our ops in OpRegistrator.
|
|
|
|
*/
|
|
|
|
template <typename OpName>
|
|
|
|
struct __registrator{
|
|
|
|
__registrator();
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename OpName>
|
|
|
|
struct __registratorSynonym {
|
|
|
|
__registratorSynonym(const char *name, const char *oname);
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //LIBND4J_OPREGISTRATOR_H
|