|
| 1 | +//===--------- virtual_mem.cpp - CUDA Adapter -----------------------------===// |
| 2 | +// |
| 3 | +// Copyright (C) 2023 Intel Corporation |
| 4 | +// |
| 5 | +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM |
| 6 | +// Exceptions. See LICENSE.TXT |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | + |
| 11 | +#include "common.hpp" |
| 12 | +#include "context.hpp" |
| 13 | +#include "event.hpp" |
| 14 | +#include "physical_mem.hpp" |
| 15 | + |
| 16 | +#include <cassert> |
| 17 | +#include <cuda.h> |
| 18 | + |
| 19 | +UR_APIEXPORT ur_result_t UR_APICALL urVirtualMemGranularityGetInfo( |
| 20 | + ur_context_handle_t hContext, ur_device_handle_t hDevice, |
| 21 | + ur_virtual_mem_granularity_info_t propName, size_t propSize, |
| 22 | + void *pPropValue, size_t *pPropSizeRet) { |
| 23 | + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); |
| 24 | + |
| 25 | + ScopedContext Active(hContext); |
| 26 | + switch (propName) { |
| 27 | + case UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM: |
| 28 | + case UR_VIRTUAL_MEM_GRANULARITY_INFO_RECOMMENDED: { |
| 29 | + CUmemAllocationGranularity_flags Flags = |
| 30 | + propName == UR_VIRTUAL_MEM_GRANULARITY_INFO_MINIMUM |
| 31 | + ? CU_MEM_ALLOC_GRANULARITY_MINIMUM |
| 32 | + : CU_MEM_ALLOC_GRANULARITY_RECOMMENDED; |
| 33 | + CUmemAllocationProp AllocProps = {}; |
| 34 | + AllocProps.location.type = CU_MEM_LOCATION_TYPE_DEVICE; |
| 35 | + AllocProps.type = CU_MEM_ALLOCATION_TYPE_PINNED; |
| 36 | + UR_CHECK_ERROR(GetDeviceOrdinal(hDevice, AllocProps.location.id)); |
| 37 | + |
| 38 | + size_t Granularity; |
| 39 | + UR_CHECK_ERROR( |
| 40 | + cuMemGetAllocationGranularity(&Granularity, &AllocProps, Flags)); |
| 41 | + return ReturnValue(Granularity); |
| 42 | + } |
| 43 | + default: |
| 44 | + return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION; |
| 45 | + } |
| 46 | + |
| 47 | + return UR_RESULT_SUCCESS; |
| 48 | +} |
| 49 | + |
| 50 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 51 | +urVirtualMemReserve(ur_context_handle_t hContext, const void *pStart, |
| 52 | + size_t size, void **ppStart) { |
| 53 | + ScopedContext Active(hContext); |
| 54 | + UR_CHECK_ERROR(cuMemAddressReserve((CUdeviceptr *)ppStart, size, 0, |
| 55 | + (CUdeviceptr)pStart, 0)); |
| 56 | + return UR_RESULT_SUCCESS; |
| 57 | +} |
| 58 | + |
| 59 | +UR_APIEXPORT ur_result_t UR_APICALL urVirtualMemFree( |
| 60 | + ur_context_handle_t hContext, const void *pStart, size_t size) { |
| 61 | + ScopedContext Active(hContext); |
| 62 | + UR_CHECK_ERROR(cuMemAddressFree((CUdeviceptr)pStart, size)); |
| 63 | + return UR_RESULT_SUCCESS; |
| 64 | +} |
| 65 | + |
| 66 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 67 | +urVirtualMemSetAccess(ur_context_handle_t hContext, const void *pStart, |
| 68 | + size_t size, ur_virtual_mem_access_flags_t flags) { |
| 69 | + CUmemAccessDesc AccessDesc = {}; |
| 70 | + if (flags & UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE) |
| 71 | + AccessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READWRITE; |
| 72 | + else if (flags & UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY) |
| 73 | + AccessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_READ; |
| 74 | + else |
| 75 | + AccessDesc.flags = CU_MEM_ACCESS_FLAGS_PROT_NONE; |
| 76 | + AccessDesc.location.type = CU_MEM_LOCATION_TYPE_DEVICE; |
| 77 | + // TODO: When contexts support multiple devices, we should create a descriptor |
| 78 | + // for each. We may also introduce a variant of this function with a |
| 79 | + // specific device. |
| 80 | + UR_CHECK_ERROR( |
| 81 | + GetDeviceOrdinal(hContext->getDevice(), AccessDesc.location.id)); |
| 82 | + |
| 83 | + ScopedContext Active(hContext); |
| 84 | + UR_CHECK_ERROR(cuMemSetAccess((CUdeviceptr)pStart, size, &AccessDesc, 1)); |
| 85 | + return UR_RESULT_SUCCESS; |
| 86 | +} |
| 87 | + |
| 88 | +UR_APIEXPORT ur_result_t UR_APICALL |
| 89 | +urVirtualMemMap(ur_context_handle_t hContext, const void *pStart, size_t size, |
| 90 | + ur_physical_mem_handle_t hPhysicalMem, size_t offset, |
| 91 | + ur_virtual_mem_access_flags_t flags) { |
| 92 | + ScopedContext Active(hContext); |
| 93 | + UR_CHECK_ERROR( |
| 94 | + cuMemMap((CUdeviceptr)pStart, size, offset, hPhysicalMem->get(), 0)); |
| 95 | + if (flags) |
| 96 | + UR_CHECK_ERROR(urVirtualMemSetAccess(hContext, pStart, size, flags)); |
| 97 | + return UR_RESULT_SUCCESS; |
| 98 | +} |
| 99 | + |
| 100 | +UR_APIEXPORT ur_result_t UR_APICALL urVirtualMemUnmap( |
| 101 | + ur_context_handle_t hContext, const void *pStart, size_t size) { |
| 102 | + ScopedContext Active(hContext); |
| 103 | + UR_CHECK_ERROR(cuMemUnmap((CUdeviceptr)pStart, size)); |
| 104 | + return UR_RESULT_SUCCESS; |
| 105 | +} |
| 106 | + |
| 107 | +UR_APIEXPORT ur_result_t UR_APICALL urVirtualMemGetInfo( |
| 108 | + ur_context_handle_t hContext, const void *pStart, |
| 109 | + [[maybe_unused]] size_t size, ur_virtual_mem_info_t propName, |
| 110 | + size_t propSize, void *pPropValue, size_t *pPropSizeRet) { |
| 111 | + UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); |
| 112 | + |
| 113 | + ScopedContext Active(hContext); |
| 114 | + switch (propName) { |
| 115 | + case UR_VIRTUAL_MEM_INFO_ACCESS_MODE: { |
| 116 | + CUmemLocation MemLocation = {}; |
| 117 | + MemLocation.type = CU_MEM_LOCATION_TYPE_DEVICE; |
| 118 | + UR_CHECK_ERROR(GetDeviceOrdinal(hContext->getDevice(), MemLocation.id)); |
| 119 | + |
| 120 | + unsigned long long CuAccessFlags; |
| 121 | + UR_CHECK_ERROR( |
| 122 | + cuMemGetAccess(&CuAccessFlags, &MemLocation, (CUdeviceptr)pStart)); |
| 123 | + |
| 124 | + ur_virtual_mem_access_flags_t UrAccessFlags = 0; |
| 125 | + if (CuAccessFlags == CU_MEM_ACCESS_FLAGS_PROT_READWRITE) |
| 126 | + UrAccessFlags = UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE; |
| 127 | + else if (CuAccessFlags == CU_MEM_ACCESS_FLAGS_PROT_READ) |
| 128 | + UrAccessFlags = UR_VIRTUAL_MEM_ACCESS_FLAG_READ_ONLY; |
| 129 | + return ReturnValue(UrAccessFlags); |
| 130 | + } |
| 131 | + default: |
| 132 | + return UR_RESULT_ERROR_UNSUPPORTED_ENUMERATION; |
| 133 | + } |
| 134 | + return UR_RESULT_SUCCESS; |
| 135 | +} |
0 commit comments