forked from oneapi-src/unified-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023 Intel Corporation | ||
* | ||
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. | ||
* See LICENSE.TXT | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
#ifndef UMF_IPC_H | ||
#define UMF_IPC_H 1 | ||
|
||
#include <umf/base.h> | ||
#include <umf/memory_pool.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
typedef struct umf_ipc_data_t *umf_ipc_handle_t; | ||
|
||
/// | ||
/// \brief Creates an IPC handle for the specified UMF allocation. | ||
/// \param ptr pointer to the allocated memory. | ||
/// \param ipcHandle [out] returned IPC handle. | ||
/// \param size [out] size of IPC handle in bytes. | ||
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
enum umf_result_t umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *ipcHandle, | ||
size_t *size); | ||
|
||
/// | ||
/// \brief Release IPC handle retrieved by umfGetIPCHandle. | ||
/// \param ipcHandle IPC handle. | ||
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
enum umf_result_t umfPutIPCHandle(umf_ipc_handle_t ipcHandle); | ||
|
||
/// | ||
/// \brief Open IPC handle retrieved by umfGetIPCHandle. | ||
/// \param hPool [in] Pool handle where to open the the IPC handle. | ||
/// \param ipcHandle [in] IPC handle. | ||
/// \param ptr [out] pointer to the memory in the current process. | ||
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
enum umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool, | ||
umf_ipc_handle_t ipcHandle, void **ptr); | ||
|
||
/// | ||
/// \brief Close IPC handle. | ||
/// \param ptr [in] pointer to the memory. | ||
/// \return UMF_RESULT_SUCCESS on success or appropriate error code on failure. | ||
enum umf_result_t umfCloseIPCHandle(void *ptr); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* UMF_IPC_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* | ||
* Copyright (C) 2023 Intel Corporation | ||
* | ||
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. | ||
* See LICENSE.TXT | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
* | ||
*/ | ||
|
||
#include "umf/ipc.h" | ||
|
||
#include "memory_pool_internal.h" | ||
#include "memory_tracker.h" | ||
|
||
#include <assert.h> | ||
#include <stdlib.h> | ||
|
||
struct umf_ipc_data_t { | ||
uint32_t size; | ||
uint64_t offset; | ||
char providerData[]; | ||
}; | ||
|
||
enum umf_result_t | ||
umfGetIPCHandle(const void *ptr, umf_ipc_handle_t *umfIPCHandle, size_t *size) { | ||
size_t ipcHandleSize = 0; | ||
struct umf_alloc_info_t allocInfo; | ||
enum umf_result_t res = | ||
umfMemoryTrackerGetAllocInfo(umfMemoryTrackerGet(), ptr, &allocInfo); | ||
if (res != UMF_RESULT_SUCCESS) { | ||
return res; | ||
} | ||
|
||
// TODO: we have no cases with multiple memory providers | ||
assert(allocInfo.pool->numProviders == 1); | ||
umf_memory_provider_handle_t provider = allocInfo.pool->providers[0]; | ||
|
||
size_t providerIPCHandleSize; | ||
res = umfMemoryProviderGetIPCHandleSize(provider, &providerIPCHandleSize); | ||
if (res != UMF_RESULT_SUCCESS) { | ||
return res; | ||
} | ||
|
||
ipcHandleSize = sizeof(struct umf_ipc_data_t) + providerIPCHandleSize; | ||
struct umf_ipc_data_t *ipcData = malloc(ipcHandleSize); | ||
if (!ipcData) { | ||
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; | ||
} | ||
|
||
res = | ||
umfMemoryProviderGetIPCHandle(provider, allocInfo.base, allocInfo.size, | ||
(void *)ipcData->providerData); | ||
if (res != UMF_RESULT_SUCCESS) { | ||
free(ipcData); | ||
return res; | ||
} | ||
|
||
ipcData->size = (uint32_t)ipcHandleSize; | ||
// assert below protect against accidental overflow | ||
assert(ipcData->size == ipcHandleSize); | ||
ipcData->offset = (uintptr_t)ptr - (uintptr_t)allocInfo.base; | ||
|
||
*umfIPCHandle = ipcData; | ||
*size = ipcHandleSize; | ||
|
||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
enum umf_result_t umfPutIPCHandle(umf_ipc_handle_t umfIPCHandle) { | ||
return UMF_RESULT_ERROR_NOT_SUPPORTED; | ||
} | ||
|
||
enum umf_result_t umfOpenIPCHandle(umf_memory_pool_handle_t hPool, | ||
umf_ipc_handle_t umfIPCHandle, void **ptr) { | ||
umf_memory_provider_handle_t hProvider; | ||
size_t numProviders; | ||
void *base = NULL; | ||
|
||
// TODO: So far we have no pools that support more then 1 memory providers. | ||
enum umf_result_t res = | ||
umfPoolGetMemoryProviders(hPool, 1, &hProvider, &numProviders); | ||
if (res != UMF_RESULT_SUCCESS) { | ||
return res; | ||
} | ||
|
||
umfMemoryProviderOpenIPCHandle(hProvider, | ||
(void *)umfIPCHandle->providerData, &base); | ||
|
||
*ptr = (void *)((uintptr_t)base + umfIPCHandle->offset); | ||
|
||
return UMF_RESULT_SUCCESS; | ||
} | ||
|
||
enum umf_result_t umfCloseIPCHandle(void *ptr) { | ||
return UMF_RESULT_ERROR_NOT_SUPPORTED; | ||
umf_memory_provider_handle_t hProvider = NULL; // TODO: find memory provider | ||
|
||
return umfMemoryProviderCloseIPCHandle(hProvider, ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.