Skip to content

Commit 4fc2005

Browse files
authored
Utility functions to transform enum to string and back (#148)
Added functions: DDPL_StrToDeviceType and DPPL_DeviceTypeToStr.
1 parent edaebe6 commit 4fc2005

File tree

5 files changed

+125
-40
lines changed

5 files changed

+125
-40
lines changed

backends/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,15 @@ add_library(
7777
source/dppl_sycl_queue_manager.cpp
7878
source/dppl_sycl_usm_interface.cpp
7979
source/dppl_utils.cpp
80+
details/source/dppl_utils_details.cpp
8081
)
8182

8283
# Install DPPLSyclInterface
8384
target_include_directories(
8485
DPPLSyclInterface
8586
PRIVATE
8687
${CMAKE_SOURCE_DIR}/include/
88+
${CMAKE_SOURCE_DIR}/details/include/
8789
)
8890

8991
if(WIN32)
@@ -123,6 +125,12 @@ foreach(HEADER ${HEADERS})
123125
install(FILES "${HEADER}" DESTINATION include/Support)
124126
endforeach()
125127

128+
# Install all headers in details/include
129+
file(GLOB HEADERS "${CMAKE_SOURCE_DIR}/details/include/*.h*")
130+
foreach(HEADER ${HEADERS})
131+
install(FILES "${HEADER}" DESTINATION details/include)
132+
endforeach()
133+
126134
option(
127135
BUILD_CAPI_TESTS
128136
"Build dpctl C API google tests"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===------------------- dppl_utils.h - dpctl-C_API ---*--- C++ -----*-----===//
2+
//
3+
// Data Parallel Control Library (dpCtl)
4+
//
5+
// Copyright 2020 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file defines common helper functions used in other places in DPPL.
23+
//===----------------------------------------------------------------------===//
24+
25+
#pragma once
26+
27+
#include <CL/sycl.hpp>
28+
using namespace cl::sycl;
29+
30+
std::string DDPL_StrToDeviceType(info::device_type devTy);
31+
info::device_type DPPL_DeviceTypeToStr(std::string devTyStr);
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===------ dppl_utils_details.cpp - dpctl-C_API ----*---- C++ -----*-----===//
2+
//
3+
// Data Parallel Control Library (dpCtl)
4+
//
5+
// Copyright 2020 Intel Corporation
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
///
21+
/// \file
22+
/// This file implements the helper functions defined in dppl_utils_details.h.
23+
///
24+
//===----------------------------------------------------------------------===//
25+
26+
#include "dppl_utils_details.h"
27+
#include <string>
28+
#include <sstream>
29+
30+
using namespace cl::sycl;
31+
32+
/*!
33+
* Transforms enum info::device_type to string.
34+
*/
35+
std::string DDPL_StrToDeviceType(info::device_type devTy)
36+
{
37+
std::stringstream ss;
38+
switch (devTy)
39+
{
40+
case info::device_type::cpu:
41+
ss << "cpu" << '\n';
42+
break;
43+
case info::device_type::gpu:
44+
ss << "gpu" << '\n';
45+
break;
46+
case info::device_type::accelerator:
47+
ss << "accelerator" << '\n';
48+
break;
49+
case info::device_type::custom:
50+
ss << "custom" << '\n';
51+
break;
52+
case info::device_type::host:
53+
ss << "host" << '\n';
54+
break;
55+
default:
56+
ss << "unknown" << '\n';
57+
}
58+
return ss.str();
59+
}
60+
61+
/*!
62+
* Transforms string to enum info::device_type.
63+
*/
64+
info::device_type DPPL_DeviceTypeToStr(std::string devTyStr)
65+
{
66+
info::device_type devTy;
67+
if (devTyStr == "cpu") {
68+
devTy = info::device_type::cpu;
69+
} else if(devTyStr == "gpu") {
70+
devTy = info::device_type::gpu;
71+
} else if(devTyStr == "accelerator") {
72+
devTy = info::device_type::accelerator;
73+
} else if(devTyStr == "custom") {
74+
devTy = info::device_type::custom;
75+
} else if(devTyStr == "host") {
76+
devTy = info::device_type::host;
77+
} else {
78+
// \todo handle the error
79+
throw std::runtime_error("Unknown device type.");
80+
}
81+
return devTy;
82+
}

backends/source/dppl_sycl_device_interface.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <iostream>
3131
#include <cstring>
3232
#include <CL/sycl.hpp> /* SYCL headers */
33+
#include "../details/include/dppl_utils_details.h"
3334

3435
using namespace cl::sycl;
3536

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

6061
auto devTy = Device.get_info<info::device::device_type>();
61-
switch(devTy)
62-
{
63-
case info::device_type::cpu:
64-
ss << "cpu" << '\n';
65-
break;
66-
case info::device_type::gpu:
67-
ss << "gpu" << '\n';
68-
break;
69-
case info::device_type::accelerator:
70-
ss << "accelerator" << '\n';
71-
break;
72-
case info::device_type::custom:
73-
ss << "custom" << '\n';
74-
break;
75-
case info::device_type::host:
76-
ss << "host" << '\n';
77-
break;
78-
default:
79-
ss << "unknown" << '\n';
80-
}
62+
ss << DDPL_StrToDeviceType(devTy);
8163

8264
std::cout << ss.str();
8365
}

backends/source/dppl_sycl_platform_interface.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <iostream>
3030
#include <set>
3131
#include <sstream>
32+
#include "../details/include/dppl_utils_details.h"
3233

3334
#include <CL/sycl.hpp>
3435

@@ -124,26 +125,7 @@ void DPPLPlatform_DumpInfo ()
124125
<< "Device type";
125126

126127
auto devTy = devices[dn].get_info<info::device::device_type>();
127-
switch (devTy)
128-
{
129-
case info::device_type::cpu:
130-
ss << "cpu" << '\n';
131-
break;
132-
case info::device_type::gpu:
133-
ss << "gpu" << '\n';
134-
break;
135-
case info::device_type::accelerator:
136-
ss << "accelerator" << '\n';
137-
break;
138-
case info::device_type::custom:
139-
ss << "custom" << '\n';
140-
break;
141-
case info::device_type::host:
142-
ss << "host" << '\n';
143-
break;
144-
default:
145-
ss << "unknown" << '\n';
146-
}
128+
ss << DDPL_StrToDeviceType(devTy);
147129
}
148130
std::cout << ss.str();
149131
++i;

0 commit comments

Comments
 (0)