|
12 | 12 |
|
13 | 13 | #include "platform.hpp"
|
14 | 14 |
|
| 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 | + |
15 | 81 | UR_APIEXPORT ur_result_t UR_APICALL urDeviceGet(ur_platform_handle_t hPlatform,
|
16 | 82 | ur_device_type_t DeviceType,
|
17 | 83 | uint32_t NumEntries,
|
@@ -223,8 +289,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
|
223 | 289 | // TODO : CHECK
|
224 | 290 | return ReturnValue(uint64_t{0});
|
225 | 291 | case UR_DEVICE_INFO_GLOBAL_MEM_SIZE:
|
226 |
| - // TODO : CHECK |
227 |
| - return ReturnValue(uint64_t{32768}); |
| 292 | + return ReturnValue(hDevice->mem_size); |
228 | 293 | case UR_DEVICE_INFO_LOCAL_MEM_SIZE:
|
229 | 294 | // TODO : CHECK
|
230 | 295 | return ReturnValue(uint64_t{32768});
|
@@ -252,9 +317,16 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice,
|
252 | 317 | return ReturnValue(bool{false});
|
253 | 318 | case UR_DEVICE_INFO_PARTITION_AFFINITY_DOMAIN:
|
254 | 319 | 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 | + } |
258 | 330 | case UR_DEVICE_INFO_EXECUTION_CAPABILITIES:
|
259 | 331 | // TODO : CHECK
|
260 | 332 | return ReturnValue(ur_device_exec_capability_flags_t{
|
@@ -420,3 +492,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceSelectBinary(
|
420 | 492 | // No image can be loaded for the given device
|
421 | 493 | return UR_RESULT_ERROR_INVALID_BINARY;
|
422 | 494 | }
|
| 495 | + |
| 496 | +ur_device_handle_t_::ur_device_handle_t_(ur_platform_handle_t ArgPlt) |
| 497 | + : mem_size(os_memory_bounded_size()), Platform(ArgPlt) {} |
0 commit comments