Skip to content

Utility functions to transform enum to string and back #148

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

Merged
merged 12 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backends/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ add_library(
source/dppl_sycl_queue_manager.cpp
source/dppl_sycl_usm_interface.cpp
source/dppl_utils.cpp
details/source/dppl_utils_details.cpp
)

# Install DPPLSyclInterface
target_include_directories(
DPPLSyclInterface
PRIVATE
${CMAKE_SOURCE_DIR}/include/
${CMAKE_SOURCE_DIR}/details/include/
)

if(WIN32)
Expand Down Expand Up @@ -123,6 +125,12 @@ foreach(HEADER ${HEADERS})
install(FILES "${HEADER}" DESTINATION include/Support)
endforeach()

# Install all headers in details/include
file(GLOB HEADERS "${CMAKE_SOURCE_DIR}/details/include/*.h*")
foreach(HEADER ${HEADERS})
install(FILES "${HEADER}" DESTINATION details/include)
endforeach()

option(
BUILD_CAPI_TESTS
"Build dpctl C API google tests"
Expand Down
31 changes: 31 additions & 0 deletions backends/details/include/dppl_utils_details.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//===------------------- dppl_utils.h - dpctl-C_API ---*--- C++ -----*-----===//
//
// Data Parallel Control Library (dpCtl)
//
// Copyright 2020 Intel Corporation
//
// Licensed 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.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file defines common helper functions used in other places in DPPL.
//===----------------------------------------------------------------------===//

#pragma once

#include <CL/sycl.hpp>
using namespace cl::sycl;

std::string DDPL_StrToDeviceType(info::device_type devTy);
info::device_type DPPL_DeviceTypeToStr(std::string devTyStr);
82 changes: 82 additions & 0 deletions backends/details/source/dppl_utils_details.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===------ dppl_utils_details.cpp - dpctl-C_API ----*---- C++ -----*-----===//
//
// Data Parallel Control Library (dpCtl)
//
// Copyright 2020 Intel Corporation
//
// Licensed 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.
//
//===----------------------------------------------------------------------===//
///
/// \file
/// This file implements the helper functions defined in dppl_utils_details.h.
///
//===----------------------------------------------------------------------===//

#include "dppl_utils_details.h"
#include <string>
#include <sstream>

using namespace cl::sycl;

/*!
* Transforms enum info::device_type to string.
*/
std::string DDPL_StrToDeviceType(info::device_type devTy)
{
std::stringstream ss;
switch (devTy)
{
case info::device_type::cpu:
ss << "cpu" << '\n';
break;
case info::device_type::gpu:
ss << "gpu" << '\n';
break;
case info::device_type::accelerator:
ss << "accelerator" << '\n';
break;
case info::device_type::custom:
ss << "custom" << '\n';
break;
case info::device_type::host:
ss << "host" << '\n';
break;
default:
ss << "unknown" << '\n';
}
return ss.str();
}

/*!
* Transforms string to enum info::device_type.
*/
info::device_type DPPL_DeviceTypeToStr(std::string devTyStr)
{
info::device_type devTy;
if (devTyStr == "cpu") {
devTy = info::device_type::cpu;
} else if(devTyStr == "gpu") {
devTy = info::device_type::gpu;
} else if(devTyStr == "accelerator") {
devTy = info::device_type::accelerator;
} else if(devTyStr == "custom") {
devTy = info::device_type::custom;
} else if(devTyStr == "host") {
devTy = info::device_type::host;
} else {
// \todo handle the error
throw std::runtime_error("Unknown device type.");
}
return devTy;
}
22 changes: 2 additions & 20 deletions backends/source/dppl_sycl_device_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <iostream>
#include <cstring>
#include <CL/sycl.hpp> /* SYCL headers */
#include "../details/include/dppl_utils_details.h"

using namespace cl::sycl;

Expand Down Expand Up @@ -58,26 +59,7 @@ void dump_device_info (const device & Device)
ss << std::setw(4) << " " << std::left << std::setw(16) << "Device type";

auto devTy = Device.get_info<info::device::device_type>();
switch(devTy)
{
case info::device_type::cpu:
ss << "cpu" << '\n';
break;
case info::device_type::gpu:
ss << "gpu" << '\n';
break;
case info::device_type::accelerator:
ss << "accelerator" << '\n';
break;
case info::device_type::custom:
ss << "custom" << '\n';
break;
case info::device_type::host:
ss << "host" << '\n';
break;
default:
ss << "unknown" << '\n';
}
ss << DDPL_StrToDeviceType(devTy);

std::cout << ss.str();
}
Expand Down
22 changes: 2 additions & 20 deletions backends/source/dppl_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <iostream>
#include <set>
#include <sstream>
#include "../details/include/dppl_utils_details.h"

#include <CL/sycl.hpp>

Expand Down Expand Up @@ -124,26 +125,7 @@ void DPPLPlatform_DumpInfo ()
<< "Device type";

auto devTy = devices[dn].get_info<info::device::device_type>();
switch (devTy)
{
case info::device_type::cpu:
ss << "cpu" << '\n';
break;
case info::device_type::gpu:
ss << "gpu" << '\n';
break;
case info::device_type::accelerator:
ss << "accelerator" << '\n';
break;
case info::device_type::custom:
ss << "custom" << '\n';
break;
case info::device_type::host:
ss << "host" << '\n';
break;
default:
ss << "unknown" << '\n';
}
ss << DDPL_StrToDeviceType(devTy);
}
std::cout << ss.str();
++i;
Expand Down