Skip to content

Commit 85c2c04

Browse files
committed
Support max mem alloc query
1 parent 6c2329e commit 85c2c04

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

source/adapters/native_cpu/device.cpp

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,72 @@
1212

1313
#include "platform.hpp"
1414

15+
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
16+
#ifndef NOMINMAX
17+
#define NOMINMAX
18+
#endif
19+
#include <windows.h>
20+
#endif
21+
22+
#ifdef __linux__
23+
#include <sys/sysinfo.h>
24+
#include <unistd.h>
25+
26+
#include <cstdio>
27+
#include <cstdlib>
28+
#endif
29+
30+
#ifdef __APPLE__
31+
#include <sys/sysctl.h>
32+
#include <unistd.h>
33+
#endif
34+
35+
#ifdef __MCOS_POSIX__
36+
#include <emcos/emcos_device_info.h>
37+
#endif
38+
39+
uint64_t os_memory_total_size() {
40+
#if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
41+
MEMORYSTATUSEX status;
42+
status.dwLength = sizeof(status);
43+
if (GlobalMemoryStatusEx(&status)) {
44+
return static_cast<uint64_t>(status.ullTotalPhys);
45+
} else {
46+
return 0;
47+
}
48+
#elif defined(__APPLE__)
49+
// query the physical memory size by name, name documented in
50+
// https://opensource.apple.com/source/xnu/xnu-792.12.6/libkern/libkern/sysctl.h
51+
uint64_t memsize;
52+
size_t size = sizeof(uint64_t);
53+
if (sysctlbyname("hw.memsize", &memsize, &size, nullptr, 0)) {
54+
return 0;
55+
}
56+
return memsize;
57+
#elif defined(__linux__)
58+
struct sysinfo info;
59+
if (0 == sysinfo(&info)) {
60+
return static_cast<uint64_t>(info.totalram) *
61+
static_cast<uint64_t>(info.mem_unit);
62+
} else {
63+
return 0;
64+
}
65+
#elif defined(__MCOS_POSIX__)
66+
return emcos::get_device_total_memory_size();
67+
#else
68+
#error Unknown platform!
69+
#endif
70+
}
71+
72+
static uint64_t os_memory_bounded_size() {
73+
const uint64_t size = os_memory_total_size();
74+
// Limit the memory size to what fits in a size_t, this is necessary when
75+
// compiling for 32 bits on a 64 bits host
76+
return std::numeric_limits<size_t>::max() >= size
77+
? size
78+
: std::numeric_limits<size_t>::max();
79+
}
80+
1581
UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet(ur_platform_handle_t hPlatform,
1682
ur_device_type_t DeviceType,
1783
uint32_t NumEntries,
@@ -223,8 +289,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
223289
// TODO : CHECK
224290
return ReturnValue(uint64_t{0});
225291
case UR_DEVICE_INFO_GLOBAL_MEM_SIZE:
226-
// TODO : CHECK
227-
return ReturnValue(uint64_t{32768});
292+
return ReturnValue(hDevice->mem_size);
228293
case UR_DEVICE_INFO_LOCAL_MEM_SIZE:
229294
// TODO : CHECK
230295
return ReturnValue(uint64_t{32768});
@@ -252,9 +317,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
252317
return ReturnValue(bool{false});
253318
case UR_DEVICE_INFO_PARTITION_AFFINITY_DOMAIN:
254319
return ReturnValue(ur_device_affinity_domain_flags_t{0});
255-
case UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE:
256-
// TODO : CHECK
257-
return ReturnValue(uint64_t{0});
320+
case UR_DEVICE_INFO_MAX_MEM_ALLOC_SIZE: {
321+
size_t Global = hDevice->mem_size;
322+
323+
auto QuarterGlobal = static_cast<uint32_t>(Global / 4u);
324+
325+
auto MaxAlloc = std::max(std::min(1024u * 1024u * 1024u, QuarterGlobal),
326+
32u * 1024u * 1024u);
327+
328+
return ReturnValue(uint64_t{MaxAlloc});
329+
}
258330
case UR_DEVICE_INFO_EXECUTION_CAPABILITIES:
259331
// TODO : CHECK
260332
return ReturnValue(ur_device_exec_capability_flags_t{
@@ -420,3 +492,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceSelectBinary(
420492
// No image can be loaded for the given device
421493
return UR_RESULT_ERROR_INVALID_BINARY;
422494
}
495+
496+
ur_device_handle_t_::ur_device_handle_t_(ur_platform_handle_t ArgPlt)
497+
: mem_size(os_memory_bounded_size()), Platform(ArgPlt) {}

source/adapters/native_cpu/device.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515

1616
struct ur_device_handle_t_ {
1717
native_cpu::threadpool_t tp;
18-
ur_device_handle_t_(ur_platform_handle_t ArgPlt) : Platform(ArgPlt) {}
18+
ur_device_handle_t_(ur_platform_handle_t ArgPlt);
1919

20+
const uint64_t mem_size;
2021
ur_platform_handle_t Platform;
2122
};

0 commit comments

Comments
 (0)