-
Notifications
You must be signed in to change notification settings - Fork 77
issue/338: infinirt适配虚存接口 #341
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,4 +53,16 @@ __C __export infiniStatus_t infinirtMemcpyAsync(void *dst, const void *src, size | |
| __C __export infiniStatus_t infinirtMallocAsync(void **p_ptr, size_t size, infinirtStream_t stream); | ||
| __C __export infiniStatus_t infinirtFreeAsync(void *ptr, infinirtStream_t stream); | ||
|
|
||
| // Virtual memory & physical memory | ||
| typedef void *infinirtPhysicalMemoryHandle_t; | ||
|
|
||
| __C __export infiniStatus_t infinirtGetMemGranularityMinimum(size_t *granularity); | ||
| __C __export infiniStatus_t infinirtCreatePhysicalMem(infinirtPhysicalMemoryHandle_t *pm_handle, size_t len); | ||
| __C __export infiniStatus_t infinirtReleasePhysicalMem(infinirtPhysicalMemoryHandle_t pm_handle); | ||
|
|
||
| __C __export infiniStatus_t infinirtCreateVirtualMem(void **vm, size_t len); | ||
| __C __export infiniStatus_t infinirtMapVirtualMem(void *vm, size_t len, size_t offset, infinirtPhysicalMemoryHandle_t pm_handle); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 权限控制要暴露吗? |
||
| __C __export infiniStatus_t infinirtUnmapVirtualMem(void *vm, size_t len); | ||
| __C __export infiniStatus_t infinirtReleaseVirtualMem(void *vm, size_t len); | ||
|
|
||
| #endif // __INFINIRT_API_H__ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,4 +88,31 @@ infiniStatus_t freeAsync(void *ptr, infinirtStream_t stream) { | |
| return freeDevice(ptr); | ||
| } | ||
|
|
||
| infiniStatus_t getMemGranularityMinimum(size_t *granularity) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CPU 主存很容易实现虚拟化逻辑,没必要设置为不支持。可以手工做一套用户态虚拟化或者交由操作系统处理(主存正常分配自带操作系统提供的虚拟化) |
||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t createPhysicalMem(infinirtPhysicalMemoryHandle_t *pm_handle, size_t len) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t releasePhysicalMem(infinirtPhysicalMemoryHandle_t pm_handle) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t createVirtualMem(void **vm, size_t len) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t releaseVirtualMem(void *vm, size_t len) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t mapVirtualMem(void *vm, size_t len, size_t offset, infinirtPhysicalMemoryHandle_t pm_handle) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
|
|
||
| infiniStatus_t unmapVirtualMem(void *vm, size_t len) { | ||
| return INFINI_STATUS_NOT_IMPLEMENTED; | ||
| } | ||
| } // namespace infinirt::cpu | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,13 @@ | ||
| #include "../../utils.h" | ||
| #include "infinirt_cuda.cuh" | ||
| #include <cuda.h> | ||
| #include <cuda_runtime.h> | ||
| #include <string.h> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. .cu 里面理论上说不需要 cuda.h、cuda_runtime.h 对于 nvcc 来说是预置的。c/c++ 标准库头文件应该用 c++ 标准的 |
||
|
|
||
| #define CHECK_CUDART(RT_API) CHECK_INTERNAL(RT_API, cudaSuccess) | ||
|
|
||
| namespace infinirt::cuda { | ||
|
|
||
| infiniStatus_t getDeviceCount(int *count) { | ||
| CHECK_CUDART(cudaGetDeviceCount(count)); | ||
| return INFINI_STATUS_SUCCESS; | ||
|
|
@@ -134,4 +137,73 @@ infiniStatus_t freeAsync(void *ptr, infinirtStream_t stream) { | |
| CHECK_CUDART(cudaFreeAsync(ptr, (cudaStream_t)stream)); | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| CUmemAllocationProp *getMemProp() { | ||
| int device_id; | ||
| infinirtGetDevice(nullptr, &device_id); | ||
| CUmemAllocationProp *cuda_prop = new CUmemAllocationProp(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这东西为啥在堆上分配啊?没必要啊 |
||
| memset(cuda_prop, 0, sizeof(CUmemAllocationProp)); | ||
| cuda_prop->type = CU_MEM_ALLOCATION_TYPE_PINNED; | ||
| cuda_prop->requestedHandleTypes = CU_MEM_HANDLE_TYPE_NONE; | ||
| cuda_prop->location.type = CU_MEM_LOCATION_TYPE_DEVICE; | ||
| cuda_prop->location.id = device_id; | ||
| return cuda_prop; | ||
| } | ||
|
|
||
| infiniStatus_t getMemGranularityMinimum(size_t *granularity) { | ||
| CUmemAllocationProp *cuda_prop = getMemProp(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里用 |
||
| CHECK_CUDART(cuMemGetAllocationGranularity(granularity, cuda_prop, CU_MEM_ALLOC_GRANULARITY_MINIMUM)); | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t createPhysicalMem(infinirtPhysicalMemoryHandle_t *pm_handle, size_t len) { | ||
| CUmemGenericAllocationHandle handle; | ||
| CUmemAllocationProp *cuda_prop = getMemProp(); | ||
| CHECK_CUDART(cuMemCreate(&handle, len, cuda_prop, 0)); | ||
|
|
||
| *pm_handle = (infinirtPhysicalMemoryHandle_t)handle; | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t releasePhysicalMem(infinirtPhysicalMemoryHandle_t pm_handle) { | ||
| CHECK_CUDART(cuMemRelease((CUmemGenericAllocationHandle)pm_handle)); | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t createVirtualMem(void **vm, size_t len) { | ||
| CUdeviceptr device_ptr; | ||
| CHECK_CUDART(cuMemAddressReserve(&device_ptr, len, 0, (CUdeviceptr)0, 0)); | ||
|
|
||
| *vm = (void *)device_ptr; | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t releaseVirtualMem(void *vm, size_t len) { | ||
| CHECK_CUDART(cuMemAddressFree((CUdeviceptr)vm, len)); | ||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t mapVirtualMem(void *vm, size_t len, size_t offset, | ||
| infinirtPhysicalMemoryHandle_t pm_handle) { | ||
|
|
||
| CUdeviceptr ptr = (CUdeviceptr)vm + offset; | ||
| CHECK_CUDART(cuMemMap(ptr, len, 0, (CUmemGenericAllocationHandle)pm_handle, 0)); | ||
|
|
||
| CUmemAllocationProp *cuda_prop = getMemProp(); | ||
| CUmemAccessDesc desc = {}; | ||
| desc.location = cuda_prop->location; | ||
| desc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE; | ||
| CHECK_CUDART(cuMemSetAccess(ptr, len, &desc, 1)); | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| infiniStatus_t unmapVirtualMem(void *vm, size_t len) { | ||
| CUdeviceptr ptr = (CUdeviceptr)vm; | ||
| CHECK_CUDART(cuMemUnmap(ptr, len)); | ||
|
|
||
| return INFINI_STATUS_SUCCESS; | ||
| } | ||
|
|
||
| } // namespace infinirt::cuda | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我觉得 CUDA 搞这个名字挺奇怪的,要是想跟 CUDA 对齐就保持,不然的话叫 page size 之类的更好些