Repository: incubator-singa Updated Branches: refs/heads/master 364c88562 -> b2cfa17b8
SINGA-104 Add Context Class Project: http://git-wip-us.apache.org/repos/asf/incubator-singa/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-singa/commit/771ff328 Tree: http://git-wip-us.apache.org/repos/asf/incubator-singa/tree/771ff328 Diff: http://git-wip-us.apache.org/repos/asf/incubator-singa/diff/771ff328 Branch: refs/heads/master Commit: 771ff328098fed89177a0f8e0d41a44c4c1c7e0c Parents: 364c885 Author: seaok <[email protected]> Authored: Wed Nov 25 13:19:42 2015 +0800 Committer: seaok <[email protected]> Committed: Wed Nov 25 13:19:42 2015 +0800 ---------------------------------------------------------------------- include/singa/utils/context.h | 87 +++++++++++++++++++++++++++++++++++++ src/test/test_context.cc | 66 ++++++++++++++++++++++++++++ src/utils/context.cc | 89 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/771ff328/include/singa/utils/context.h ---------------------------------------------------------------------- diff --git a/include/singa/utils/context.h b/include/singa/utils/context.h new file mode 100644 index 0000000..762ae75 --- /dev/null +++ b/include/singa/utils/context.h @@ -0,0 +1,87 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://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. +* +*************************************************************/ + +#ifndef SINGA_UTILS_CONTEXT_H_ +#define SINGA_UTILS_CONTEXT_H_ + +#include <vector> + +#ifdef USE_GPU +#include <cublas_v2.h> +#include <cuda_runtime.h> +#include <curand.h> +#endif + + +namespace singa { + +const int kDefaultDevice = 20; + +class Context { + public: + + ~Context(); + + void Setup(); + +#ifdef USE_GPU + int DeviceID(const int index) { + return device_ids_[index]; + } + + void SetDeviceID(const int index, const int id) { + device_ids_[index] = id; + } + + void SetDevice(const int index) { + cudaSetDevice(device_ids_[index]); + } + + cublasHandle_t Handle(const int index) { + return handles_[index]; + } + + void CreateHandle(const int index); + + void DestoryHandle(const int index); + + curandGenerator_t GpuRandGenerator(const int index) { + return gpu_rand_generators_[index]; + } + + void CreateGpuRandGenerator(const int index); + + void DestoryGpuRandGenerator(const int index); + +#endif + + protected: + std::vector<int> device_ids_; +#ifdef USE_GPU + std::vector<cublasHandle_t> handles_; + std::vector<curandGenerator_t> gpu_rand_generators_; +#endif + +}; + +} // namespace singa + +#endif // SINGA_UTILS_MATH_ADDR_H_ http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/771ff328/src/test/test_context.cc ---------------------------------------------------------------------- diff --git a/src/test/test_context.cc b/src/test/test_context.cc new file mode 100644 index 0000000..3a23b23 --- /dev/null +++ b/src/test/test_context.cc @@ -0,0 +1,66 @@ +#include "gtest/gtest.h" +#include "singa/utils/singleton.h" +#include "singa/utils/context.h" + +//#include <cuda_runtime.h> +//#include "cublas_v2.h" + +using namespace singa; +using namespace std; + +TEST(ContextTest, TestDevice) { + auto context = Singleton<Context>::Instance(); + context->Setup(); + + int index = 4; + int device_id = context->DeviceID(index); + ASSERT_EQ(4,device_id); + + context->SetDeviceID(index,6); + device_id = context->DeviceID(index); + ASSERT_EQ(6,device_id); +} + +TEST(ContextTest, TestHandle) { + auto context = Singleton<Context>::Instance(); + context->Setup(); + + int index = 2; + context->CreateHandle(index); + + float cpu_ret = 0.0f; + float gpu_ret = 0.0f; + + float A[12]; + float B[12]; + + for(int i = 0; i < 12; i++) { + A[i]=i-1; + B[i]=i+1; + } + + float* A_gpu = NULL; + float* B_gpu = NULL; + + cudaMalloc((void**)&A_gpu, 12*sizeof(float)); + cudaMalloc((void**)&B_gpu, 12*sizeof(float)); + + cudaMemcpy(A_gpu,A,12*sizeof(float),cudaMemcpyHostToDevice); + cudaMemcpy(B_gpu,B,12*sizeof(float),cudaMemcpyHostToDevice); + + cublasHandle_t handle = context->Handle(index); + /*cublasHandle_t handle; + cudaSetDevice(0); + cublasCreate(&handle);*/ + + cublasSdot(handle, 12, A_gpu, 1, B_gpu, 1, &gpu_ret); + + for(int i = 0; i < 12;++i) { + cpu_ret += A[i] * B[i]; + } + + ASSERT_EQ(gpu_ret,cpu_ret); + + cudaFree(A_gpu); + cudaFree(B_gpu); +} http://git-wip-us.apache.org/repos/asf/incubator-singa/blob/771ff328/src/utils/context.cc ---------------------------------------------------------------------- diff --git a/src/utils/context.cc b/src/utils/context.cc new file mode 100644 index 0000000..671bec0 --- /dev/null +++ b/src/utils/context.cc @@ -0,0 +1,89 @@ +/************************************************************ +* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://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. +* +*************************************************************/ +#include "singa/utils/context.h" +#include "singa/utils/factory.h" +#include "singa/utils/singleton.h" + +namespace singa { + +Context::~Context() { +#ifdef USE_GPU + for(int i = 0; i < kDefaultDevice; ++i) { + SetDevice(i); + + if(handles_[i] != NULL) { + cublasDestroy(handles_[i]); + } + + if(gpu_rand_generators_[i] != NULL) { + curandDestroyGenerator(gpu_rand_generators_[i]); + } + } +#endif +} + +void Context::Setup() { + + for(int i = 0; i < kDefaultDevice; ++i) { + //init device index + device_ids_.push_back(i); + } + +#ifdef USE_GPU + for(int i = 0; i < kDefaultDevice; ++i) { + //init handle + cublasHandle_t handle = NULL; + handles_.push_back(handle); + + curandGenerator_t gpu_rand_generator = NULL; + gpu_rand_generators_.push_back(gpu_rand_generator); + } +#endif +} + +#ifdef USE_GPU +void Context::CreateHandle(const int index) { + SetDevice(device_ids_[index]); + cublasCreate(&handles_[index]); +} + +void Context::DestoryHandle(const int index) { + SetDevice(device_ids_[index]); + cublasDestroy(handles_[index]); + handles_[index] = NULL; +} + +void Context::CreateGpuRandGenerator(const int index) { + SetDevice(device_ids_[index]); + curandCreateGenerator(&gpu_rand_generators_[index], CURAND_RNG_PSEUDO_DEFAULT); +} + +void Context::DestoryGpuRandGenerator(const int index) { + SetDevice(device_ids_[index]); + curandDestroyGenerator(gpu_rand_generators_[index]); + gpu_rand_generators_[index] = NULL; +} + +#endif + + +} // namespace singa +
