Skip to content

Commit

Permalink
print device used in test environment
Browse files Browse the repository at this point in the history
Co-authored-by: Tobias Ribizel <[email protected]>
  • Loading branch information
MarcelKoch and upsj committed Aug 3, 2023
1 parent 978fd4e commit 946a6e9
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 28 deletions.
112 changes: 90 additions & 22 deletions core/test/gtest/environments.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************<GINKGO LICENSE>*******************************/

#ifndef GINKGO_ENVIRONMENTS_HPP
#define GINKGO_ENVIRONMENTS_HPP
#ifndef GKO_CORE_TEST_GTEST_ENVIRONMENTS_HPP_
#define GKO_CORE_TEST_GTEST_ENVIRONMENTS_HPP_

#include <algorithm>
#include <regex>
Expand All @@ -55,6 +55,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endif


#if GKO_COMPILING_DPCPP
#include "dpcpp/base/device.hpp"
#endif


#include <ginkgo/core/base/exception_helpers.hpp>
#include <ginkgo/core/base/executor.hpp>
#include <ginkgo/core/base/mpi.hpp>
Expand Down Expand Up @@ -111,8 +116,11 @@ class ResourceEnvironment : public ::testing::Environment {
auto rs_count_env = std::getenv("CTEST_RESOURCE_GROUP_COUNT");
auto rs_count = rs_count_env ? std::stoi(rs_count_env) : 0;
if (rs_count == 0) {
std::cerr << "Running without CTest ctest_resource configuration"
<< std::endl;
if (rank == 0) {
std::cerr
<< "Running without CTest ctest_resource configuration"
<< std::endl;
}
return;
}
if (rs_count != size) {
Expand All @@ -121,38 +129,25 @@ class ResourceEnvironment : public ::testing::Environment {
}

// parse CTest ctest_resource group descriptions
if (rank == 0) {
std::cerr << "Running with CTest ctest_resource configuration:"
<< std::endl;
}
// OpenMP CPU threads
if (auto rs_omp_env = get_ctest_group("cpu", rank)) {
auto resource = parse_ctest_resources(rs_omp_env);
omp_threads = resource.slots;
if (rank == 0) {
std::cerr << omp_threads << " CPU threads" << std::endl;
}
}
// CUDA GPUs
if (auto rs_cuda_env = get_ctest_group("cudagpu", rank)) {
auto resource = parse_ctest_resources(rs_cuda_env);
cuda_device_id = resource.id;
std::cerr << "Rank " << rank << ": CUDA device " << cuda_device_id
<< std::endl;
}
// HIP GPUs
if (auto rs_hip_env = get_ctest_group("hipgpu", rank)) {
auto resource = parse_ctest_resources(rs_hip_env);
hip_device_id = resource.id;
std::cerr << "Rank " << rank << ": HIP device " << cuda_device_id
<< std::endl;
}
// SYCL GPUs (no other devices!)
if (auto rs_sycl_env = get_ctest_group("syclgpu", rank)) {
auto resource = parse_ctest_resources(rs_sycl_env);
sycl_device_id = resource.id;
std::cerr << "Rank " << rank << ": SYCL device " << cuda_device_id
<< std::endl;
}
}

Expand All @@ -167,18 +162,31 @@ class ResourceEnvironment : public ::testing::Environment {

class OmpEnvironment : public ::testing::Environment {
public:
explicit OmpEnvironment(int rank) : rank_(rank) {}

void SetUp() override
{
if (ResourceEnvironment::omp_threads > 0) {
omp_set_num_threads(ResourceEnvironment::omp_threads);
omp_set_num_threads(num_threads);
}
#pragma omp parallel
#pragma single
std::cerr << "Rank " << rank_ << ": OMP threads "
<< omp_get_num_threads();
<< std::endl;
}

private:
int rank_;
};

#else


class OmpEnvironment : public ::testing::Environment {};
class OmpEnvironment : public ::testing::Environment {
public:
explicit OmpEnvironment(int){};
};

#endif

Expand All @@ -187,15 +195,31 @@ class OmpEnvironment : public ::testing::Environment {};

class CudaEnvironment : public ::testing::Environment {
public:
explicit CudaEnvironment(int rank) : rank_(rank) {}

void SetUp() override
{
auto device_id = ResourceEnvironment::cuda_device_id;
std::cerr << "Rank " << rank_ << ": CUDA device "
<< gko::kernels::cuda::get_device_name(device_id) << " ID "
<< device_id << std::endl;
}

void TearDown() override
{
gko::kernels::cuda::reset_device(ResourceEnvironment::cuda_device_id);
}

private:
int rank_;
};

#else

class CudaEnvironment : public ::testing::Environment {};
class CudaEnvironment : public ::testing::Environment {
public:
explicit CudaEnvironment(int){};
};

#endif

Expand All @@ -204,17 +228,61 @@ class CudaEnvironment : public ::testing::Environment {};

class HipEnvironment : public ::testing::Environment {
public:
explicit HipEnvironment(int rank) : rank_(rank) {}

void SetUp() override
{
auto device_id = ResourceEnvironment::hip_device_id;
std::cerr << "Rank " << rank_ << ": HIP device "
<< gko::kernels::hip::get_device_name(device_id) << " ID "
<< device_id << std::endl;
}

void TearDown() override
{
gko::kernels::hip::reset_device(ResourceEnvironment::hip_device_id);
}

private:
int rank_;
};

#else

class HipEnvironment : public ::testing::Environment {
public:
explicit HipEnvironment(int){};
};

#endif


#ifdef GKO_COMPILING_DPCPP

class SyclEnvironment : public ::testing::Environment {
public:
explicit SyclEnvironment(int rank) : rank_(rank) {}

void SetUp() override
{
auto device_id = ResourceEnvironment::sycl_device_id;
std::cerr << "Rank " << rank_ << ": SYCL device "
<< gko::kernels::dpcpp::get_device_name(device_id) << " ID "
<< device_id << std::endl;
}

private:
int rank_;
};

#else

class HipEnvironment : public ::testing::Environment {};
class SyclEnvironment : public ::testing::Environment {
public:
explicit SyclEnvironment(int){};
};

#endif


#endif // GINKGO_ENVIRONMENTS_HPP
#endif // GKO_CORE_TEST_GTEST_ENVIRONMENTS_HPP_
7 changes: 4 additions & 3 deletions core/test/gtest/ginkgo_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ int main(int argc, char** argv)
::testing::InitGoogleTest(&argc, argv);

::testing::AddGlobalTestEnvironment(new ResourceEnvironment);
::testing::AddGlobalTestEnvironment(new CudaEnvironment);
::testing::AddGlobalTestEnvironment(new HipEnvironment);
::testing::AddGlobalTestEnvironment(new OmpEnvironment);
::testing::AddGlobalTestEnvironment(new CudaEnvironment(0));
::testing::AddGlobalTestEnvironment(new HipEnvironment(0));
::testing::AddGlobalTestEnvironment(new SyclEnvironment(0));
::testing::AddGlobalTestEnvironment(new OmpEnvironment(0));
int result = RUN_ALL_TESTS();
return result;
}
7 changes: 4 additions & 3 deletions core/test/gtest/ginkgo_mpi_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,10 @@ int main(int argc, char** argv)

testing::AddGlobalTestEnvironment(new GTestMPIListener::MPIEnvironment);
::testing::AddGlobalTestEnvironment(new ResourceEnvironment(rank, size));
::testing::AddGlobalTestEnvironment(new CudaEnvironment);
::testing::AddGlobalTestEnvironment(new HipEnvironment);
::testing::AddGlobalTestEnvironment(new OmpEnvironment);
::testing::AddGlobalTestEnvironment(new CudaEnvironment(rank));
::testing::AddGlobalTestEnvironment(new HipEnvironment(rank));
::testing::AddGlobalTestEnvironment(new SyclEnvironment(rank));
::testing::AddGlobalTestEnvironment(new OmpEnvironment(rank));

::testing::TestEventListeners& listeners =
::testing::UnitTest::GetInstance()->listeners();
Expand Down
8 changes: 8 additions & 0 deletions cuda/base/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ void destroy_event(CUevent_st* event)
}


std::string get_device_name(int device_id)
{
cudaDeviceProp prop;
GKO_ASSERT_NO_CUDA_ERRORS(cudaGetDeviceProperties(&prop, device_id));
return {prop.name};
}


} // namespace cuda
} // namespace kernels
} // namespace gko
4 changes: 4 additions & 0 deletions cuda/base/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ void reset_device(int device_id);
void destroy_event(CUevent_st* event);


/** returns cudaDeviceProp.name for the given device */
std::string get_device_name(int device_id);


} // namespace cuda
} // namespace kernels
} // namespace gko
Expand Down
3 changes: 3 additions & 0 deletions dpcpp/base/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace dpcpp {
void destroy_event(sycl::event* event);


std::string get_device_name(int device_id);


} // namespace dpcpp
} // namespace kernels
} // namespace gko
Expand Down
11 changes: 11 additions & 0 deletions dpcpp/base/executor.dp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ namespace dpcpp {
void destroy_event(sycl::event* event) { delete event; }


std::string get_device_name(int device_id)
{
auto devices = ::gko::detail::get_devices("gpu");
if (devices.empty()) {
return "CPU";
}

return devices[device_id].get_info<sycl::info::device::name>();
}


} // namespace dpcpp
} // namespace kernels
} // namespace gko
8 changes: 8 additions & 0 deletions hip/base/device.hip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ void destroy_event(GKO_HIP_EVENT_STRUCT* event)
}


std::string get_device_name(int device_id)
{
hipDeviceProp_t prop;
GKO_ASSERT_NO_HIP_ERRORS(hipGetDeviceProperties(&prop, device_id));
return {prop.name};
}


} // namespace hip
} // namespace kernels
} // namespace gko
4 changes: 4 additions & 0 deletions hip/base/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void reset_device(int device_id);
void destroy_event(GKO_HIP_EVENT_STRUCT* event);


/** returns hipDeviceProp.name for the given device */
std::string get_device_name(int device_id);


} // namespace hip
} // namespace kernels
} // namespace gko
Expand Down

0 comments on commit 946a6e9

Please sign in to comment.