Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Isolate the device counter to fix the device_reset issue #810

Merged
merged 11 commits into from
Aug 2, 2021
6 changes: 3 additions & 3 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,17 @@ build/cuda91/clang/all/release/shared:

# cuda 9.2 and friends
build/cuda92/gcc/all/release/shared:
<<: *default_build
<<: *default_build_with_test
extends:
- .full_test_condition
- .quick_test_condition
- .use_gko-cuda92-gnu7-llvm50-intel2017
variables:
<<: *default_variables
BUILD_OMP: "ON"
BUILD_CUDA: "ON"
BUILD_HIP: "ON"
BUILD_TYPE: "Release"
CUDA_ARCH: 35
CUDA_ARCH: 61

# cuda 10.0 and friends
# Make sure that our jobs run when using self-installed
Expand Down
3 changes: 1 addition & 2 deletions .gitlab/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@
image: ginkgohub/cuda:92-gnu7-llvm50-intel2017
tags:
- private_ci
- controller
- cpu
- nvidia-gpu

.use_gko-cuda100-gnu7-llvm60-intel2018:
image: ginkgohub/cuda:100-gnu7-llvm60-intel2018
Expand Down
2 changes: 1 addition & 1 deletion cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ find_library(CURAND curand
HINT ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES})

add_library(ginkgo_cuda $<TARGET_OBJECTS:ginkgo_cuda_device> "")
set(GKO_CUDA_COMMON_SOURCES
set(GKO_CUDA_COMMON_SOURCES
../common/components/precision_conversion.cpp
../common/matrix/dense_kernels.cpp
../common/solver/bicg_kernels.cpp
Expand Down
7 changes: 6 additions & 1 deletion cuda/base/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <ginkgo/config.hpp>
#include <ginkgo/core/base/device.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>


Expand All @@ -64,8 +65,12 @@ std::shared_ptr<CudaExecutor> CudaExecutor::create(
alloc_mode),
[device_id](CudaExecutor *exec) {
auto device_reset = exec->get_device_reset();
std::lock_guard<std::mutex> guard(
device_class::get_mutex(device_id));
delete exec;
if (!CudaExecutor::get_num_execs(device_id) && device_reset) {
auto &num_execs = device_class::get_num_execs(device_id);
num_execs--;
if (!num_execs && device_reset) {
cuda::device_guard g(device_id);
cudaDeviceReset();
}
Expand Down
1 change: 1 addition & 0 deletions cuda/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
ginkgo_create_cuda_test(array)
ginkgo_create_cuda_test(cuda_executor)
ginkgo_create_thread_test(cuda_executor_reset)
if(GINKGO_HAVE_HWLOC)
find_package(NUMA REQUIRED)
ginkgo_create_cuda_test(cuda_executor_topology NUMA::NUMA)
Expand Down
87 changes: 87 additions & 0 deletions cuda/test/base/cuda_executor_reset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/executor.hpp>


#include <thread>


#include <gtest/gtest.h>


namespace {


#define GTEST_ASSERT_NO_EXIT(statement) \
ASSERT_EXIT({ {statement} exit(0); }, ::testing::ExitedWithCode(0), "")


TEST(DeviceReset, HipCuda)
{
GTEST_ASSERT_NO_EXIT({
auto ref = gko::ReferenceExecutor::create();
auto hip = gko::HipExecutor::create(0, ref, true);
auto cuda = gko::CudaExecutor::create(0, ref, true);
});
}


TEST(DeviceReset, CudaHip)
{
GTEST_ASSERT_NO_EXIT({
auto ref = gko::ReferenceExecutor::create();
auto cuda = gko::CudaExecutor::create(0, ref, true);
auto hip = gko::HipExecutor::create(0, ref, true);
});
}


void func()
{
auto ref = gko::ReferenceExecutor::create();
auto exec = gko::CudaExecutor::create(0, ref, true);
}


TEST(DeviceReset, CudaCuda)
{
GTEST_ASSERT_NO_EXIT({
std::thread t1(func);
std::thread t2(func);
t1.join();
t2.join();
});
}


} // namespace
2 changes: 1 addition & 1 deletion devices/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function(ginkgo_add_library name)
set_target_properties(${name} PROPERTIES POSITION_INDEPENDENT_CODE ON)
endfunction()

ginkgo_add_library(ginkgo_device machine_topology.cpp)
ginkgo_add_library(ginkgo_device machine_topology.cpp device.cpp)
ginkgo_install_library(ginkgo_device)

add_subdirectory(cuda)
Expand Down
6 changes: 0 additions & 6 deletions devices/cuda/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,4 @@ bool CudaExecutor::verify_memory_to(const HipExecutor *dest_exec) const
}


unsigned CudaExecutor::num_execs[max_devices];


std::mutex CudaExecutor::mutex[max_devices];


} // namespace gko
71 changes: 71 additions & 0 deletions devices/device.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <memory>
#include <mutex>


#include <ginkgo/core/base/device.hpp>


namespace gko {


std::mutex &NvidiaDevice::get_mutex(int i)
{
static std::mutex mutex[max_devices];
return mutex[i];
}


int &NvidiaDevice::get_num_execs(int i)
{
static int num_execs[max_devices];
return num_execs[i];
}


std::mutex &AmdDevice::get_mutex(int i)
{
static std::mutex mutex[max_devices];
return mutex[i];
}


int &AmdDevice::get_num_execs(int i)
{
static int num_execs[max_devices];
return num_execs[i];
}


} // namespace gko
6 changes: 0 additions & 6 deletions devices/hip/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,4 @@ bool HipExecutor::verify_memory_to(const CudaExecutor *dest_exec) const
}


int HipExecutor::num_execs[max_devices];


std::mutex HipExecutor::mutex[max_devices];


} // namespace gko
7 changes: 6 additions & 1 deletion hip/base/executor.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#include <ginkgo/config.hpp>
#include <ginkgo/core/base/device.hpp>
#include <ginkgo/core/base/exception_helpers.hpp>


Expand All @@ -63,8 +64,12 @@ std::shared_ptr<HipExecutor> HipExecutor::create(
new HipExecutor(device_id, std::move(master), device_reset, alloc_mode),
[device_id](HipExecutor *exec) {
auto device_reset = exec->get_device_reset();
std::lock_guard<std::mutex> guard(
device_class::get_mutex(device_id));
delete exec;
if (!HipExecutor::get_num_execs(device_id) && device_reset) {
auto &num_execs = device_class::get_num_execs(device_id);
num_execs--;
if (!num_execs && device_reset) {
hip::device_guard g(device_id);
hipDeviceReset();
}
Expand Down
1 change: 1 addition & 0 deletions hip/test/base/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ginkgo_create_hip_test(hip_executor)
ginkgo_create_thread_test(hip_executor_reset)
if(GINKGO_HAVE_HWLOC)
find_package(NUMA REQUIRED)
ginkgo_create_hip_test(hip_executor_topology NUMA::NUMA)
Expand Down
87 changes: 87 additions & 0 deletions hip/test/base/hip_executor_reset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*******************************<GINKGO LICENSE>******************************
Copyright (c) 2017-2021, the Ginkgo authors
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#include <ginkgo/core/base/executor.hpp>


#include <thread>


#include <gtest/gtest.h>


namespace {


#define GTEST_ASSERT_NO_EXIT(statement) \
ASSERT_EXIT({ {statement} exit(0); }, ::testing::ExitedWithCode(0), "")


TEST(DeviceReset, HipCuda)
{
GTEST_ASSERT_NO_EXIT({
auto ref = gko::ReferenceExecutor::create();
auto hip = gko::HipExecutor::create(0, ref, true);
auto cuda = gko::CudaExecutor::create(0, ref, true);
});
}


TEST(DeviceReset, CudaHip)
{
GTEST_ASSERT_NO_EXIT({
auto ref = gko::ReferenceExecutor::create();
auto cuda = gko::CudaExecutor::create(0, ref, true);
auto hip = gko::HipExecutor::create(0, ref, true);
});
}


void func()
{
auto ref = gko::ReferenceExecutor::create();
auto exec = gko::HipExecutor::create(0, ref, true);
}


TEST(DeviceReset, HipHip)
{
GTEST_ASSERT_NO_EXIT({
std::thread t1(func);
std::thread t2(func);
t1.join();
t2.join();
});
}


} // namespace
8 changes: 8 additions & 0 deletions include/ginkgo/config.hpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#cmakedefine GINKGO_MIXED_PRECISION


/* Should we compile cuda kenrels for Ginkgo? */
yhmtsai marked this conversation as resolved.
Show resolved Hide resolved
#cmakedefine GINKGO_BUILD_CUDA


/* Should we compile hip kenrels for Ginkgo? */
yhmtsai marked this conversation as resolved.
Show resolved Hide resolved
#cmakedefine GINKGO_BUILD_HIP


/* What is HIP compiled for, hcc or nvcc? */
// clang-format off
#define GINKGO_HIP_PLATFORM_HCC @GINKGO_HIP_PLATFORM_HCC@
Expand Down
Loading