Skip to content

[SYCL] MacOS support for os_util #763

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 1 commit into from
Nov 13, 2019
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
7 changes: 6 additions & 1 deletion sycl/include/CL/sycl/detail/os_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#elif __linux__
// Linux platform
#define SYCL_RT_OS_LINUX
#define SYCL_RT_OS_POSIX_SUPPORT
#elif defined(__APPLE__) && defined(__MACH__)
// Apple OSX
#define SYCL_RT_OS_DARWIN
#define SYCL_RT_OS_POSIX_SUPPORT
#else
#error "Unsupported compiler or OS"
#endif // _WIN32
Expand All @@ -41,7 +46,7 @@
#define __SYCL_EXPORTED __declspec(dllimport)
#endif

#elif defined(SYCL_RT_OS_LINUX)
#elif defined(SYCL_RT_OS_POSIX_SUPPORT)

#define DLL_LOCAL __attribute__((visibility("hidden")))
#define __SYCL_EXPORTED
Expand Down
43 changes: 35 additions & 8 deletions sycl/source/detail/os_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,32 @@
#include <CL/sycl/detail/os_util.hpp>
#include <CL/sycl/exception.hpp>

#ifdef SYCL_RT_OS_POSIX_SUPPORT
#include <cstdlib>
#endif

#if defined(SYCL_RT_OS_LINUX)

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif // _GNU_SOURCE

#include <cstdio>
#include <link.h>
#include <stdio.h>
#include <sys/sysinfo.h>

#elif defined(SYCL_RT_OS_WINDOWS)

#include <Windows.h>
#include <malloc.h>
#endif

#elif defined(SYCL_RT_OS_DARWIN)

#include <dlfcn.h>
#include <sys/sysctl.h>
#include <sys/types.h>

#endif // SYCL_RT_OS

namespace cl {
namespace sycl {
Expand Down Expand Up @@ -73,15 +84,23 @@ OSModuleHandle OSUtil::getOSModuleHandle(const void *VirtAddr) {
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT;
auto LpModuleAddr = reinterpret_cast<LPCSTR>(VirtAddr);
if (!GetModuleHandleExA(Flag, LpModuleAddr, &PhModule)) {
// Expect the caller to check for zero and take
// necessary action
return 0;
// Expect the caller to check for zero and take
// necessary action
return 0;
}
if (PhModule == GetModuleHandleA(nullptr))
return OSUtil::ExeModuleHandle;
return reinterpret_cast<OSModuleHandle>(PhModule);
}
#endif // SYCL_RT_OS_WINDOWS

#elif defined(SYCL_RT_OS_DARWIN)
OSModuleHandle OSUtil::getOSModuleHandle(const void *VirtAddr) {
Dl_info Res;
dladdr(VirtAddr, &Res);
return reinterpret_cast<OSModuleHandle>(Res.dli_fbase);
}

#endif // SYCL_RT_OS

size_t OSUtil::getOSMemSize() {
#if defined(SYCL_RT_OS_LINUX)
Expand All @@ -93,19 +112,27 @@ size_t OSUtil::getOSMemSize() {
MemInfo.dwLength = sizeof(MemInfo);
GlobalMemoryStatusEx(&MemInfo);
return static_cast<size_t>(MemInfo.ullTotalPhys);
#endif
#elif defined(SYCL_RT_OS_DARWIN)
int64_t Size = 0;
sysctlbyname("hw.memsize", &Size, nullptr, nullptr, 0);
return static_cast<size_t>(Size);
#endif // SYCL_RT_OS
}

void *OSUtil::alignedAlloc(size_t Alignment, size_t NumBytes) {
#if defined(SYCL_RT_OS_LINUX)
return aligned_alloc(Alignment, NumBytes);
#elif defined(SYCL_RT_OS_POSIX_SUPPORT)
void *Addr = nullptr;
int ReturnCode = posix_memalign(&Addr, Alignment, NumBytes);
return (ReturnCode == 0) ? Addr : nullptr;
#elif defined(SYCL_RT_OS_WINDOWS)
return _aligned_malloc(NumBytes, Alignment);
#endif
}

void OSUtil::alignedFree(void *Ptr) {
#if defined(SYCL_RT_OS_LINUX)
#if defined(SYCL_RT_OS_LINUX) || defined(SYCL_RT_OS_POSIX_SUPPORT)
free(Ptr);
#elif defined(SYCL_RT_OS_WINDOWS)
_aligned_free(Ptr);
Expand Down