Skip to content

Commit 3b0defe

Browse files
v-klochkovbader
authored andcommitted
[SYCL] Add Windows support for device_info
Fixed few warnings caused by inconsistent usage of class and struct keywords. Added a macro to expose global symbols and generate sycl.lib on Windows. Signed-off-by: Vyacheslav N Klochkov <vyacheslav.n.klochkov@intel.com>
1 parent ed668e0 commit 3b0defe

13 files changed

+232
-115
lines changed

sycl/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project(sycl-solution)
55
set(CMAKE_CXX_STANDARD 11)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_CXX_EXTENSIONS OFF)
8+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
89

910
if(MSVC)
1011
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
@@ -135,6 +136,7 @@ add_library("${SYCLLibrary}" SHARED
135136
"${sourceRootPath}/detail/program_manager/program_manager.cpp"
136137
"${sourceRootPath}/detail/queue_impl.cpp"
137138
"${sourceRootPath}/detail/os_util.cpp"
139+
"${sourceRootPath}/detail/platform_util.cpp"
138140
"${sourceRootPath}/detail/sampler_impl.cpp"
139141
"${sourceRootPath}/detail/scheduler/commands.cpp"
140142
"${sourceRootPath}/detail/scheduler/commands2.cpp"

sycl/include/CL/sycl/detail/buffer_impl.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class accessor;
4242
template <typename T, int dimensions, typename AllocatorT> class buffer;
4343
class handler;
4444
class queue;
45-
template <int dimentions> class id;
45+
template <int dimentions> struct id;
4646
template <int dimentions> class range;
4747
using buffer_allocator = aligned_allocator<char, /*alignment*/ 64>;
4848
namespace detail {

sycl/include/CL/sycl/detail/helpers.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ namespace cl {
1919
namespace sycl {
2020
class context;
2121
class event;
22-
template <int dimensions, bool with_offset> class item;
22+
template <int dimensions, bool with_offset> struct item;
2323
template <int dimensions> class group;
2424
template <int dimensions> class range;
25-
template <int dimensions> class id;
25+
template <int dimensions> struct id;
2626
template <int dimensions> class nd_item;
2727
namespace detail {
2828
class context_impl;

sycl/include/CL/sycl/detail/os_util.hpp

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#pragma once
1212

13+
#include <stdlib.h>
14+
1315
#ifdef _WIN32
1416
#define SYCL_RT_OS_WINDOWS
1517
// Windows platform
@@ -48,6 +50,9 @@ class OSUtil {
4850
/// Module handle for the executable module - it is assumed there is always
4951
/// single one at most.
5052
static const OSModuleHandle ExeModuleHandle;
53+
54+
/// Returns the amount of RAM available for the operating system.
55+
static size_t getOSMemSize();
5156
};
5257

5358
} // namespace detail
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===-- platform_util.hpp - platform utilities ----------------*- C++ -*--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <cstdint>
12+
13+
namespace cl {
14+
namespace sycl {
15+
namespace detail {
16+
17+
struct PlatformUtil {
18+
enum class TypeIndex : unsigned int {
19+
Char = 0,
20+
Short = 1,
21+
Int = 2,
22+
Long = 3,
23+
Float = 4,
24+
Double = 5,
25+
Half = 6
26+
};
27+
28+
/// Returns the maximum vector width counted in elements of the given type.
29+
static uint32_t getNativeVectorWidth(TypeIndex Index);
30+
31+
static uint32_t getMaxClockFrequency();
32+
33+
static uint32_t getMemCacheLineSize();
34+
35+
static uint64_t getMemCacheSize();
36+
};
37+
38+
} // namespace detail
39+
} // namespace sycl
40+
} // namespace cl

sycl/include/CL/sycl/group.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
namespace cl {
2020
namespace sycl {
2121
namespace detail {
22-
class Builder;
22+
struct Builder;
2323
} // namespace detail
2424

2525
template <int dimensions = 1> class group {

sycl/include/CL/sycl/id.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ namespace cl {
1616
namespace sycl {
1717
template <int dimensions> class range;
1818
template <int dimensions = 1> struct id : public detail::array<dimensions> {
19-
public:
19+
private:
2020
using base = detail::array<dimensions>;
21+
public:
2122
id() = default;
2223

2324
/* The following constructor is only available in the id struct

sycl/include/CL/sycl/item.hpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
namespace cl {
1717
namespace sycl {
1818
namespace detail {
19-
class Builder;
19+
struct Builder;
2020
}
2121
template <int dimensions> struct id;
22-
template <int dimensions> struct range;
22+
template <int dimensions> class range;
2323
template <int dimensions = 1, bool with_offset = true> struct item {
2424

2525
item() = delete;
@@ -86,8 +86,9 @@ template <int dimensions = 1, bool with_offset = true> struct item {
8686

8787
protected:
8888
// For call constructor inside conversion operator
89-
friend class item<dimensions, false>;
90-
friend class detail::Builder;
89+
friend struct item<dimensions, false>;
90+
friend struct item<dimensions, true>;
91+
friend struct detail::Builder;
9192

9293
template <size_t W = with_offset>
9394
item(typename std::enable_if<(W == true), const range<dimensions>>::type &R,

sycl/include/CL/sycl/nd_item.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace cl {
2323
namespace sycl {
2424
namespace detail {
25-
class Builder;
25+
struct Builder;
2626
}
2727
template <int dimensions = 1> struct nd_item {
2828

sycl/include/CL/sycl/range.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace sycl {
1616
template <int dimensions> struct id;
1717
template <int dimensions = 1>
1818
class range : public detail::array<dimensions> {
19-
public:
2019
using base = detail::array<dimensions>;
20+
public:
2121
/* The following constructor is only available in the range class
2222
specialization where: dimensions==1 */
2323
template <int N = dimensions>

sycl/source/detail/device_info.cpp

+13-90
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
//===----------------------------------------------------------------------===//
88

99
#include <CL/sycl/detail/device_info.hpp>
10+
#include <CL/sycl/detail/os_util.hpp>
11+
#include <CL/sycl/detail/platform_util.hpp>
1012
#include <CL/sycl/device.hpp>
1113
#include <chrono>
12-
#include <sys/sysinfo.h>
1314
#include <thread>
1415

1516
#ifdef __GNUG__
@@ -21,22 +22,6 @@ namespace cl {
2122
namespace sycl {
2223
namespace detail {
2324

24-
// Used by methods that duplicate OpenCL behaviour in order to get CPU info
25-
// TODO add Windows support
26-
// TODO add support for x86-64 ABI selected using ifdef.
27-
static void cpuid(unsigned int cpuid_info[], unsigned int type) {
28-
unsigned int eax, ebx, ecx, edx;
29-
__asm__ __volatile__("mov %%ebx, %%edi\n\r"
30-
"cpuid\n\r"
31-
"xchg %%edi, %%ebx\n\r"
32-
: "=a"(eax), "=D"(ebx), "=c"(ecx), "=d"(edx)
33-
: "a"(type));
34-
cpuid_info[0] = eax;
35-
cpuid_info[1] = ebx;
36-
cpuid_info[2] = ecx;
37-
cpuid_info[3] = edx;
38-
}
39-
4025
vector_class<info::fp_config> read_fp_bitfield(cl_device_fp_config bits) {
4126
vector_class<info::fp_config> result;
4227
if (bits & CL_FP_DENORM)
@@ -156,109 +141,51 @@ cl_uint get_device_info_host<info::device::preferred_vector_width_half>() {
156141
return 0;
157142
}
158143

159-
// SSE4.2 has 16 byte (XMM) registers
160-
static const cl_uint NATIVE_VECTOR_WIDTH_SSE42[] = {16, 8, 4, 2, 4, 2, 0};
161-
// AVX supports 32 byte (YMM) registers only for floats and doubles
162-
static const cl_uint NATIVE_VECTOR_WIDTH_AVX[] = {16, 8, 4, 2, 8, 4, 0};
163-
// AVX2 has a full set of 32 byte (YMM) registers
164-
static const cl_uint NATIVE_VECTOR_WIDTH_AVX2[] = {32, 16, 8, 4, 8, 4, 0};
165-
// AVX512 has 64 byte (ZMM) registers
166-
static const cl_uint NATIVE_VECTOR_WIDTH_AVX512[] = {64, 32, 16, 8, 16, 8, 0};
167-
168-
cl_uint get_native_vector_width(size_t idx) {
169-
#if (__GNUG__ && GCC_VERSION > 40900)
170-
if (__builtin_cpu_supports("avx512f")) {
171-
return NATIVE_VECTOR_WIDTH_AVX512[idx];
172-
}
173-
#endif
174-
175-
if (__builtin_cpu_supports("avx2")) {
176-
return NATIVE_VECTOR_WIDTH_AVX2[idx];
177-
}
178-
if (__builtin_cpu_supports("avx")) {
179-
return NATIVE_VECTOR_WIDTH_AVX[idx];
180-
}
181-
return NATIVE_VECTOR_WIDTH_SSE42[idx];
182-
}
183-
184144
template <>
185145
cl_uint get_device_info_host<info::device::native_vector_width_char>() {
186-
return get_native_vector_width(0);
146+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Char);
187147
}
188148

189149
template <>
190150
cl_uint get_device_info_host<info::device::native_vector_width_short>() {
191-
return get_native_vector_width(1);
151+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Short);
192152
}
193153

194154
template <>
195155
cl_uint get_device_info_host<info::device::native_vector_width_int>() {
196-
return get_native_vector_width(2);
156+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Int);
197157
}
198158

199159
template <>
200160
cl_uint get_device_info_host<info::device::native_vector_width_long>() {
201-
return get_native_vector_width(3);
161+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Long);
202162
}
203163

204164
template <>
205165
cl_uint get_device_info_host<info::device::native_vector_width_float>() {
206-
return get_native_vector_width(4);
166+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Float);
207167
}
208168

209169
template <>
210170
cl_uint get_device_info_host<info::device::native_vector_width_double>() {
211-
return get_native_vector_width(5);
171+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Double);
212172
}
213173

214174
template <>
215175
cl_uint get_device_info_host<info::device::native_vector_width_half>() {
216-
return get_native_vector_width(6);
176+
return PlatformUtil::getNativeVectorWidth(PlatformUtil::TypeIndex::Half);
217177
}
218178

219179
template <> cl_uint get_device_info_host<info::device::max_clock_frequency>() {
220-
throw runtime_error(
221-
"max_clock_frequency parameter is not supported for host device");
222-
unsigned int cpuInfo[4] = {0 - 1u};
223-
string_class buff(sizeof(cpuInfo) * 3 + 1, 0);
224-
size_t offset = 0;
225-
226-
for (unsigned int i = 0x80000002; i <= 0x80000004; i++) {
227-
cpuid(cpuInfo, i);
228-
std::copy(reinterpret_cast<char *>(cpuInfo),
229-
reinterpret_cast<char *>(cpuInfo) + sizeof(cpuInfo),
230-
buff.begin() + offset);
231-
offset += sizeof(cpuInfo);
232-
}
233-
std::size_t found = buff.rfind("Hz");
234-
// Bail out if frequency is not found in CPUID string
235-
if (found == std::string::npos)
236-
return 0;
237-
238-
buff = buff.substr(0, found);
239-
240-
cl_uint freq = 0;
241-
switch (buff[buff.size() - 1]) {
242-
case 'M':
243-
freq = 1;
244-
break;
245-
case 'G':
246-
freq = 1000;
247-
break;
248-
}
249-
buff = buff.substr(buff.rfind(' '), buff.length());
250-
freq *= std::stod(buff);
251-
return freq;
180+
return PlatformUtil::getMaxClockFrequency();
252181
}
253182

254183
template <> cl_uint get_device_info_host<info::device::address_bits>() {
255184
return sizeof(void *) * 8;
256185
}
257186

258187
template <> cl_ulong get_device_info_host<info::device::global_mem_size>() {
259-
struct sysinfo meminfo;
260-
sysinfo(&meminfo);
261-
return meminfo.totalram * meminfo.mem_unit;
188+
return static_cast<cl_ulong>(OSUtil::getOSMemSize());
262189
}
263190

264191
template <> cl_ulong get_device_info_host<info::device::max_mem_alloc_size>() {
@@ -362,16 +289,12 @@ get_device_info_host<info::device::global_mem_cache_type>() {
362289

363290
template <>
364291
cl_uint get_device_info_host<info::device::global_mem_cache_line_size>() {
365-
unsigned int viCPUInfo[4] = {(unsigned int)-1};
366-
cpuid(viCPUInfo, 0x80000006);
367-
return viCPUInfo[2] & 0xff;
292+
return PlatformUtil::getMemCacheLineSize();
368293
}
369294

370295
template <>
371296
cl_ulong get_device_info_host<info::device::global_mem_cache_size>() {
372-
unsigned int viCPUInfo[4] = {(unsigned int)-1};
373-
cpuid(viCPUInfo, 0x80000006);
374-
return ((viCPUInfo[2] >> 16) & 0xffff) * 1024;
297+
return PlatformUtil::getMemCacheSize();
375298
}
376299

377300
template <>

0 commit comments

Comments
 (0)