From 4199d8f7a6c2f1767262b8dfbfa21669e5fc4914 Mon Sep 17 00:00:00 2001 From: dmed256 Date: Thu, 9 Aug 2018 09:50:26 -0600 Subject: [PATCH] [#191][Core] Added props to memory::ptr --- include/occa/c/memory.h | 3 ++- include/occa/memory.hpp | 13 +++++++++---- include/occa/mode/cuda/device.hpp | 2 +- include/occa/mode/cuda/memory.hpp | 4 +++- include/occa/mode/cuda/utils.hpp | 2 -- include/occa/mode/hip/device.hpp | 4 ---- include/occa/mode/hip/memory.hpp | 3 ++- include/occa/mode/opencl/memory.hpp | 2 ++ include/occa/mode/opencl/utils.hpp | 2 -- src/c/memory.cpp | 9 +++++++-- src/memory.cpp | 24 ++++++++++++++++++++++-- src/mode/cuda/device.cpp | 13 +++++++------ src/mode/cuda/memory.cpp | 11 +++++++++-- src/mode/cuda/utils.cpp | 7 ------- src/mode/hip/device.cpp | 16 +--------------- src/mode/hip/memory.cpp | 11 ++++++++--- src/mode/opencl/device.cpp | 2 +- src/mode/opencl/memory.cpp | 7 +++++++ src/mode/opencl/utils.cpp | 7 ------- tests/src/c/memory.cpp | 13 ++++++++----- 20 files changed, 89 insertions(+), 66 deletions(-) diff --git a/include/occa/c/memory.h b/include/occa/c/memory.h index 260ab01e9..31b073c5c 100644 --- a/include/occa/c/memory.h +++ b/include/occa/c/memory.h @@ -30,7 +30,8 @@ OCCA_START_EXTERN_C OCCA_LFUNC int OCCA_RFUNC occaMemoryIsInitialized(occaMemory memory); -OCCA_LFUNC void* OCCA_RFUNC occaMemoryPtr(occaMemory memory); +OCCA_LFUNC void* OCCA_RFUNC occaMemoryPtr(occaMemory memory, + occaProperties props); OCCA_LFUNC occaDevice OCCA_RFUNC occaMemoryGetDevice(occaMemory memory); diff --git a/include/occa/memory.hpp b/include/occa/memory.hpp index 7353e5f69..ce08fe9a9 100644 --- a/include/occa/memory.hpp +++ b/include/occa/memory.hpp @@ -40,10 +40,10 @@ namespace occa { typedef hashedMemoryMap::const_iterator cHashedMemoryMapIterator; namespace uvaFlag { - static const int none = 0; - static const int isManaged = (1 << 0); - static const int inDevice = (1 << 1); - static const int isStale = (1 << 2); + static const int none = 0; + static const int isManaged = (1 << 0); + static const int inDevice = (1 << 1); + static const int isStale = (1 << 2); } //---[ modeMemory_t ]--------------------- @@ -75,6 +75,8 @@ namespace occa { virtual modeMemory_t* addOffset(const dim_t offset) = 0; + virtual void* getPtr(const occa::properties &props); + virtual void copyTo(void *dest, const udim_t bytes, const udim_t offset = 0, @@ -148,6 +150,9 @@ namespace occa { void* ptr(); const void* ptr() const; + void* ptr(const occa::properties &props); + const void* ptr(const occa::properties &props) const; + modeMemory_t* getModeMemory() const; modeDevice_t* getModeDevice() const; diff --git a/include/occa/mode/cuda/device.hpp b/include/occa/mode/cuda/device.hpp index 559ab2095..8df8ed4d4 100644 --- a/include/occa/mode/cuda/device.hpp +++ b/include/occa/mode/cuda/device.hpp @@ -112,7 +112,7 @@ namespace occa { const void *src, const occa::properties &props); - modeMemory_t* managedAlloc(const udim_t bytes, + modeMemory_t* unifiedAlloc(const udim_t bytes, const void *src, const occa::properties &props); diff --git a/include/occa/mode/cuda/memory.hpp b/include/occa/mode/cuda/memory.hpp index 2971e9c31..ac262df00 100644 --- a/include/occa/mode/cuda/memory.hpp +++ b/include/occa/mode/cuda/memory.hpp @@ -47,7 +47,7 @@ namespace occa { public: CUdeviceptr &cuPtr; char *mappedPtr; - bool isManaged; + bool isUnified; memory(const occa::properties &properties_ = occa::properties()); ~memory(); @@ -56,6 +56,8 @@ namespace occa { modeMemory_t* addOffset(const dim_t offset); + void* getPtr(const occa::properties &props); + void copyTo(void *dest, const udim_t bytes, const udim_t destOffset = 0, diff --git a/include/occa/mode/cuda/utils.hpp b/include/occa/mode/cuda/utils.hpp index d51157116..cfd6d79be 100644 --- a/include/occa/mode/cuda/utils.hpp +++ b/include/occa/mode/cuda/utils.hpp @@ -100,8 +100,6 @@ namespace occa { CUcontext getContext(occa::device device); - void* getMappedPtr(occa::memory mem); - occa::device wrapDevice(CUdevice device, CUcontext context, const occa::properties &props = occa::properties()); diff --git a/include/occa/mode/hip/device.hpp b/include/occa/mode/hip/device.hpp index 9d1f0ccfc..e3b95b3dd 100644 --- a/include/occa/mode/hip/device.hpp +++ b/include/occa/mode/hip/device.hpp @@ -111,10 +111,6 @@ namespace occa { const void *src, const occa::properties &props); - modeMemory_t* managedAlloc(const udim_t bytes, - const void *src, - const occa::properties &props); - virtual udim_t memorySize() const; //================================ }; diff --git a/include/occa/mode/hip/memory.hpp b/include/occa/mode/hip/memory.hpp index 7eaafb9c6..c5b8214df 100644 --- a/include/occa/mode/hip/memory.hpp +++ b/include/occa/mode/hip/memory.hpp @@ -44,7 +44,6 @@ namespace occa { public: hipDeviceptr_t hipPtr; char *mappedPtr; - bool isManaged; memory(const occa::properties &properties_ = occa::properties()); ~memory(); @@ -53,6 +52,8 @@ namespace occa { modeMemory_t* addOffset(const dim_t offset); + void* getPtr(const occa::properties &props); + void copyTo(void *dest, const udim_t bytes, const udim_t destOffset = 0, diff --git a/include/occa/mode/opencl/memory.hpp b/include/occa/mode/opencl/memory.hpp index 1153f55ce..aaba9527d 100644 --- a/include/occa/mode/opencl/memory.hpp +++ b/include/occa/mode/opencl/memory.hpp @@ -57,6 +57,8 @@ namespace occa { modeMemory_t* addOffset(const dim_t offset); + void* getPtr(const occa::properties &props); + void copyTo(void *dest, const udim_t bytes, const udim_t destOffset = 0, diff --git a/include/occa/mode/opencl/utils.hpp b/include/occa/mode/opencl/utils.hpp index d13526aaa..4f9f940ab 100644 --- a/include/occa/mode/opencl/utils.hpp +++ b/include/occa/mode/opencl/utils.hpp @@ -123,8 +123,6 @@ namespace occa { cl_kernel getCLKernel(occa::kernel kernel); - void* getMappedPtr(occa::memory memory); - occa::device wrapDevice(cl_device_id clDevice, cl_context context, const occa::properties &props = occa::properties()); diff --git a/src/c/memory.cpp b/src/c/memory.cpp index 80b77dd18..20ea5981a 100644 --- a/src/c/memory.cpp +++ b/src/c/memory.cpp @@ -29,8 +29,13 @@ int OCCA_RFUNC occaMemoryIsInitialized(occaMemory memory) { return occa::c::memory(memory).isInitialized(); } -void* OCCA_RFUNC occaMemoryPtr(occaMemory memory) { - return occa::c::memory(memory).ptr(); +void* OCCA_RFUNC occaMemoryPtr(occaMemory memory, + occaProperties props) { + occa::memory mem = occa::c::memory(memory); + if (occa::c::isDefault(props)) { + return mem.ptr(); + } + return mem.ptr(occa::c::properties(props)); } occaDevice OCCA_RFUNC occaMemoryGetDevice(occaMemory memory) { diff --git a/src/memory.cpp b/src/memory.cpp index 00784cf5e..94a4f1bcd 100644 --- a/src/memory.cpp +++ b/src/memory.cpp @@ -46,6 +46,10 @@ namespace occa { modeMemory_t::~modeMemory_t() {} + void* modeMemory_t::getPtr(const occa::properties &props) { + return ptr; + } + bool modeMemory_t::isManaged() const { return (memInfo & uvaFlag::isManaged); } @@ -136,11 +140,27 @@ namespace occa { } void* memory::ptr() { - return (modeMemory ? modeMemory->ptr : NULL); + return (modeMemory + ? modeMemory->ptr + : NULL); } const void* memory::ptr() const { - return (modeMemory ? modeMemory->ptr : NULL); + return (modeMemory + ? modeMemory->ptr + : NULL); + } + + void* memory::ptr(const occa::properties &props) { + return (modeMemory + ? modeMemory->getPtr(props) + : NULL); + } + + const void* memory::ptr(const occa::properties &props) const { + return (modeMemory + ? modeMemory->getPtr(props) + : NULL); } modeMemory_t* memory::getModeMemory() const { diff --git a/src/mode/cuda/device.cpp b/src/mode/cuda/device.cpp index 8e5726136..45dd6bded 100644 --- a/src/mode/cuda/device.cpp +++ b/src/mode/cuda/device.cpp @@ -555,10 +555,11 @@ namespace occa { const void *src, const occa::properties &props) { - if (props.get("cuda/mapped", false)) { + if (props.get("mapped", false)) { return mappedAlloc(bytes, src, props); - } else if (props.get("cuda/managed", false)) { - return managedAlloc(bytes, src, props); + } + if (props.get("unified", false)) { + return unifiedAlloc(bytes, src, props); } cuda::memory &mem = *(new cuda::memory(props)); @@ -600,21 +601,21 @@ namespace occa { return &mem; } - modeMemory_t* device::managedAlloc(const udim_t bytes, + modeMemory_t* device::unifiedAlloc(const udim_t bytes, const void *src, const occa::properties &props) { cuda::memory &mem = *(new cuda::memory(props)); #if CUDA_VERSION >= 8000 mem.modeDevice = this; mem.size = bytes; - mem.isManaged = true; + mem.isUnified = true; const unsigned int flags = (props.get("cuda/attachedHost", false) ? CU_MEM_ATTACH_HOST : CU_MEM_ATTACH_GLOBAL); OCCA_CUDA_ERROR("Device: Setting Context", cuCtxSetCurrent(cuContext)); - OCCA_CUDA_ERROR("Device: managed alloc", + OCCA_CUDA_ERROR("Device: Unified alloc", cuMemAllocManaged(&(mem.cuPtr), bytes, flags)); diff --git a/src/mode/cuda/memory.cpp b/src/mode/cuda/memory.cpp index 17b4c6e0c..a7bb12f95 100644 --- a/src/mode/cuda/memory.cpp +++ b/src/mode/cuda/memory.cpp @@ -34,7 +34,7 @@ namespace occa { occa::modeMemory_t(properties_), cuPtr((CUdeviceptr&) ptr), mappedPtr(NULL), - isManaged(false) {} + isUnified(false) {} memory::~memory() {} @@ -57,7 +57,7 @@ namespace occa { if (mappedPtr) { m->mappedPtr = mappedPtr + offset; } - m->isManaged = isManaged; + m->isUnified = isUnified; m->canBeFreed = false; return m; } @@ -105,6 +105,13 @@ namespace occa { } } + void* memory::getPtr(const occa::properties &props) { + if (props.get("mapped", false)) { + return mappedPtr; + } + return ptr; + } + void memory::copyTo(void *dest, const udim_t bytes, const udim_t offset, diff --git a/src/mode/cuda/utils.cpp b/src/mode/cuda/utils.cpp index 60dc8722e..3bbd54f39 100644 --- a/src/mode/cuda/utils.cpp +++ b/src/mode/cuda/utils.cpp @@ -223,13 +223,6 @@ namespace occa { return ((cuda::device*) device.getModeDevice())->cuContext; } - void* getMappedPtr(occa::memory mem) { - cuda::memory *cudaMemory = (cuda::memory*) mem.getModeMemory(); - return (cudaMemory - ? cudaMemory->mappedPtr - : NULL); - } - occa::device wrapDevice(CUdevice device, CUcontext context, const occa::properties &props) { diff --git a/src/mode/hip/device.cpp b/src/mode/hip/device.cpp index 4e26254fd..c324d9a0d 100644 --- a/src/mode/hip/device.cpp +++ b/src/mode/hip/device.cpp @@ -513,10 +513,8 @@ namespace occa { const void *src, const occa::properties &props) { - if (props.get("hip/mapped", false)) { + if (props.get("mapped", false)) { return mappedAlloc(bytes, src, props); - } else if (props.get("hip/managed", false)) { - return managedAlloc(bytes, src, props); } hip::memory &mem = *(new hip::memory(props)); @@ -558,18 +556,6 @@ namespace occa { return &mem; } - modeMemory_t* device::managedAlloc(const udim_t bytes, - const void *src, - const occa::properties &props) { - hip::memory &mem = *(new hip::memory(props)); - - OCCA_FORCE_ERROR("HIP version [" - << hip::getVersion() - << "] does not support unified memory allocation"); - - return &mem; - } - udim_t device::memorySize() const { return hip::getDeviceMemorySize(hipDevice); } diff --git a/src/mode/hip/memory.cpp b/src/mode/hip/memory.cpp index 6693e3bdd..3c076f7a3 100644 --- a/src/mode/hip/memory.cpp +++ b/src/mode/hip/memory.cpp @@ -33,8 +33,7 @@ namespace occa { memory::memory(const occa::properties &properties_) : occa::modeMemory_t(properties_), hipPtr((hipDeviceptr_t&) ptr), - mappedPtr(NULL), - isManaged(false) {} + mappedPtr(NULL) {} memory::~memory() {} @@ -57,7 +56,6 @@ namespace occa { if (mappedPtr) { m->mappedPtr = mappedPtr + offset; } - m->isManaged = isManaged; m->canBeFreed = false; return m; } @@ -105,6 +103,13 @@ namespace occa { } } + void* memory::getPtr(const occa::properties &props) { + if (props.get("mapped", false)) { + return mappedPtr; + } + return ptr; + } + void memory::copyTo(void *dest, const udim_t bytes, const udim_t offset, diff --git a/src/mode/opencl/device.cpp b/src/mode/opencl/device.cpp index 41ce5af43..97eea20d3 100644 --- a/src/mode/opencl/device.cpp +++ b/src/mode/opencl/device.cpp @@ -481,7 +481,7 @@ namespace occa { const void *src, const occa::properties &props) { - if (props.get("opencl/mapped", false)) { + if (props.get("mapped", false)) { return mappedAlloc(bytes, src, props); } diff --git a/src/mode/opencl/memory.cpp b/src/mode/opencl/memory.cpp index d3e58f8a8..1ae7667b6 100644 --- a/src/mode/opencl/memory.cpp +++ b/src/mode/opencl/memory.cpp @@ -102,6 +102,13 @@ namespace occa { 0, NULL, NULL)); } + void* memory::getPtr(const occa::properties &props) { + if (props.get("mapped", false)) { + return mappedPtr; + } + return ptr; + } + void memory::copyTo(void *dest, const udim_t bytes, const udim_t offset, diff --git a/src/mode/opencl/utils.cpp b/src/mode/opencl/utils.cpp index 4126ff54b..b990aee06 100644 --- a/src/mode/opencl/utils.cpp +++ b/src/mode/opencl/utils.cpp @@ -447,13 +447,6 @@ namespace occa { return ((opencl::kernel*) kernel.getModeKernel())->clKernel; } - void* getMappedPtr(occa::memory memory) { - opencl::memory *openclMemory = (opencl::memory*) memory.getModeMemory(); - return (openclMemory - ? openclMemory->mappedPtr - : NULL); - } - occa::device wrapDevice(cl_device_id clDevice, cl_context context, const occa::properties &props) { diff --git a/tests/src/c/memory.cpp b/tests/src/c/memory.cpp index f89fd0fde..ba7803425 100644 --- a/tests/src/c/memory.cpp +++ b/tests/src/c/memory.cpp @@ -64,11 +64,14 @@ void testInit() { OCCA_MEMORY); ASSERT_TRUE(occaMemoryIsInitialized(mem)); - int *ptr = (int*) occaMemoryPtr(mem); + int *ptr = (int*) occaMemoryPtr(mem, occaDefault); ASSERT_EQ(ptr[0], 0); ASSERT_EQ(ptr[1], 1); ASSERT_EQ(ptr[2], 2); + int *ptr2 = (int*) occaMemoryPtr(mem, props); + ASSERT_EQ(ptr, ptr2); + ASSERT_EQ(occa::c::device(occaMemoryGetDevice(mem)), occa::host()); @@ -89,7 +92,7 @@ void testInit() { ASSERT_EQ((size_t) occaMemorySize(subMem), bytes - (1 * sizeof(int))); - ptr = (int*) occaMemoryPtr(subMem); + ptr = (int*) occaMemoryPtr(subMem, occaDefault); ASSERT_EQ(ptr[0], 1); ASSERT_EQ(ptr[1], 2); @@ -161,8 +164,8 @@ void testCopyMethods() { occaCreatePropertiesFromString("foo: 'bar'") ); - int *ptr2 = (int*) occaMemoryPtr(mem2); - int *ptr4 = (int*) occaMemoryPtr(mem4); + int *ptr2 = (int*) occaMemoryPtr(mem2, occaDefault); + int *ptr4 = (int*) occaMemoryPtr(mem4, occaDefault); // Mem -> Mem // Copy over [2, 3] @@ -265,7 +268,7 @@ void testInteropMethods() { ASSERT_NEQ(occa::c::memory(mem1), occa::c::memory(mem2)); - int *ptr = (int*) occaMemoryPtr(mem2); + int *ptr = (int*) occaMemoryPtr(mem2, occaDefault); occaMemoryDetach(mem2); for (int i = 0; i < entries; ++i) {