* initial commit Signed-off-by: raver119 <raver119@gmail.com> * one file Signed-off-by: raver119 <raver119@gmail.com> * few more includes Signed-off-by: raver119 <raver119@gmail.com> * m? Signed-off-by: raver119 <raver119@gmail.com> * const Signed-off-by: raver119 <raver119@gmail.com> * cudnn linkage in tests Signed-off-by: raver119 <raver119@gmail.com> * culibos Signed-off-by: raver119 <raver119@gmail.com> * static reminder Signed-off-by: raver119 <raver119@gmail.com> * platform engine tag Signed-off-by: raver119 <raver119@gmail.com> * HAVE_CUDNN moved to config.h.in Signed-off-by: raver119 <raver119@gmail.com> * include Signed-off-by: raver119 <raver119@gmail.com> * include Signed-off-by: raver119 <raver119@gmail.com> * skip cudnn handle creation if there's not cudnn Signed-off-by: raver119 <raver119@gmail.com> * meh Signed-off-by: raver119 <raver119@gmail.com> * target device in context Signed-off-by: raver119 <raver119@gmail.com> * platform engines Signed-off-by: raver119 <raver119@gmail.com> * platform engines Signed-off-by: raver119 <raver119@gmail.com> * allow multiple -h args Signed-off-by: raver119 <raver119@gmail.com> * allow multiple -h args Signed-off-by: raver119 <raver119@gmail.com> * move mkldnn out of CPU block Signed-off-by: raver119 <raver119@gmail.com> * link to mkldnn on cuda Signed-off-by: raver119 <raver119@gmail.com> * less prints Signed-off-by: raver119 <raver119@gmail.com> * minor tweaks Signed-off-by: raver119 <raver119@gmail.com> * next step Signed-off-by: raver119 <raver119@gmail.com> * conv2d NCHW draft Signed-off-by: raver119 <raver119@gmail.com> * conv2d biasAdd Signed-off-by: raver119 <raver119@gmail.com> * test for MKL/CUDNN combined use Signed-off-by: raver119 <raver119@gmail.com> * - provide additional code for conv2d ff based on cudnn api, not tested yet Signed-off-by: Yurii <iuriish@yahoo.com> * - further work on conv2d helper based on using cudnn api Signed-off-by: Yurii <iuriish@yahoo.com> * - fixing several cuda bugs which appeared after cudnn lib had been started to use Signed-off-by: Yurii <iuriish@yahoo.com> * - implementation of conv2d backprop op based on cudnn api Signed-off-by: Yurii <iuriish@yahoo.com> * - implementaion of conv3d and conv3d_bp ops based on cudnn api Signed-off-by: Yurii <iuriish@yahoo.com> * - bugs fixing in conv3d/conv3d_bp ops (cudnn in use) Signed-off-by: Yurii <iuriish@yahoo.com> * - implementation of depthwiseConv2d (ff/bp) op based on cudnn api Signed-off-by: Yurii <iuriish@yahoo.com> * - implementation of batchnorm ff op based on cudnn api Signed-off-by: Yurii <iuriish@yahoo.com> * - disable cudnn batchnorm temporary Signed-off-by: Yurii <iuriish@yahoo.com> * - add minor change in cmake Signed-off-by: Yurii <iuriish@yahoo.com> * engine for depthwise mkldnn Signed-off-by: raver119 <raver119@gmail.com> * couple of includes Signed-off-by: raver119 <raver119@gmail.com> * - provide permutation to cudnn batchnorm ff when format is NHWC Signed-off-by: Yurii <iuriish@yahoo.com> * lgamma fix Signed-off-by: raver119 <raver119@gmail.com> * - eliminate memory leak in two tests Signed-off-by: Yurii <iuriish@yahoo.com> Co-authored-by: Yurii Shyrma <iuriish@yahoo.com>
144 lines
4.4 KiB
Plaintext
144 lines
4.4 KiB
Plaintext
/*******************************************************************************
|
|
* 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
|
|
******************************************************************************/
|
|
|
|
//
|
|
// @author raver119@gmail.com
|
|
//
|
|
|
|
|
|
#include <cublas_v2.h>
|
|
#include <cusolverDn.h>
|
|
#include "../cublasHelper.h"
|
|
#include <exceptions/cuda_exception.h>
|
|
#include <helpers/logger.h>
|
|
#include <execution/AffinityManager.h>
|
|
#include "config.h"
|
|
|
|
#ifdef HAVE_CUDNN
|
|
|
|
#include <cudnn.h>
|
|
|
|
#endif
|
|
|
|
namespace nd4j {
|
|
std::mutex CublasHelper::_mutex;
|
|
|
|
static void* handle_() {
|
|
auto _handle = new cublasHandle_t();
|
|
auto status = cublasCreate_v2(_handle); // initialize CUBLAS context
|
|
if (status != CUBLAS_STATUS_SUCCESS)
|
|
throw cuda_exception::build("cuBLAS handle creation failed !", status);
|
|
|
|
return reinterpret_cast<void *>(_handle);
|
|
}
|
|
|
|
static void* solver_() {
|
|
auto cusolverH = new cusolverDnHandle_t();
|
|
auto status = cusolverDnCreate(cusolverH);
|
|
if (status != CUSOLVER_STATUS_SUCCESS)
|
|
throw cuda_exception::build("cuSolver handle creation failed !", status);
|
|
|
|
return cusolverH;
|
|
}
|
|
|
|
static void* cudnn_() {
|
|
#ifdef HAVE_CUDNN
|
|
auto cudnnH = new cudnnHandle_t();
|
|
auto status = cudnnCreate(cudnnH);
|
|
if (status != CUDNN_STATUS_SUCCESS)
|
|
throw cuda_exception::build("cuDNN handle creation failed !", status);
|
|
|
|
return cudnnH;
|
|
#endif
|
|
return nullptr;
|
|
}
|
|
|
|
static void destroyHandle_(void* handle) {
|
|
auto ch = reinterpret_cast<cublasHandle_t *>(handle);
|
|
auto status = cublasDestroy_v2(*ch);
|
|
if (status != CUBLAS_STATUS_SUCCESS)
|
|
throw cuda_exception::build("cuBLAS handle destruction failed !", status);
|
|
|
|
delete ch;
|
|
}
|
|
|
|
CublasHelper::CublasHelper() {
|
|
//nd4j_printf("Initializing cuBLAS\n","");
|
|
auto numDevices = AffinityManager::numberOfDevices();
|
|
auto currentDevice = AffinityManager::currentDeviceId();
|
|
_cache.resize(numDevices);
|
|
_solvers.resize(numDevices);
|
|
_cudnn.resize(numDevices);
|
|
for (int e = 0; e < numDevices; e++) {
|
|
AffinityManager::setCurrentNativeDevice(e);
|
|
|
|
_cache[e] = handle_();
|
|
_solvers[e] = solver_();
|
|
_cudnn[e] = cudnn_();
|
|
}
|
|
|
|
// don't forget to restore back original device
|
|
AffinityManager::setCurrentNativeDevice(currentDevice);
|
|
}
|
|
|
|
CublasHelper::~CublasHelper() {
|
|
nd4j_printf("Releasing cuBLAS\n","");
|
|
auto numDevices = AffinityManager::numberOfDevices();
|
|
|
|
for (int e = 0; e < numDevices; e++)
|
|
destroyHandle_(_cache[e]);
|
|
}
|
|
|
|
CublasHelper* CublasHelper::getInstance() {
|
|
_mutex.lock();
|
|
if (!_INSTANCE)
|
|
_INSTANCE = new nd4j::CublasHelper();
|
|
_mutex.unlock();
|
|
|
|
return _INSTANCE;
|
|
}
|
|
|
|
void* CublasHelper::cudnn() {
|
|
auto deviceId = AffinityManager::currentDeviceId();
|
|
if (deviceId < 0 || deviceId > _cudnn.size())
|
|
throw cuda_exception::build("requested deviceId doesn't look valid", deviceId);
|
|
|
|
return _cudnn[deviceId];
|
|
}
|
|
|
|
void* CublasHelper::handle() {
|
|
auto deviceId = AffinityManager::currentDeviceId();
|
|
return handle(deviceId);
|
|
}
|
|
|
|
void* CublasHelper::solver() {
|
|
auto deviceId = AffinityManager::currentDeviceId();
|
|
if (deviceId < 0 || deviceId > _solvers.size())
|
|
throw cuda_exception::build("requested deviceId doesn't look valid", deviceId);
|
|
|
|
return _solvers[deviceId];
|
|
}
|
|
|
|
void* CublasHelper::handle(int deviceId) {
|
|
if (deviceId < 0 || deviceId > _cache.size())
|
|
throw cuda_exception::build("requested deviceId doesn't look valid", deviceId);
|
|
|
|
return _cache[deviceId];
|
|
}
|
|
|
|
|
|
nd4j::CublasHelper* nd4j::CublasHelper::_INSTANCE = 0;
|
|
} |