Skip to content

Commit

Permalink
[Memory] Cleaned up how slices are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
dmed256 committed Sep 2, 2018
1 parent fc52919 commit dba0db9
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion include/occa/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace occa {
occa::modeDevice_t *modeDevice;

udim_t size;
bool canBeFreed;
bool isOrigin;

modeMemory_t(modeDevice_t *modeDevice_,
udim_t size_,
Expand Down
30 changes: 18 additions & 12 deletions src/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace occa {
uvaPtr(NULL),
modeDevice(modeDevice_),
size(size_),
canBeFreed(true) {
isOrigin(true) {
modeDevice->addMemoryRef(this);
}

Expand Down Expand Up @@ -396,12 +396,15 @@ namespace occa {
(offset + (dim_t) bytes_) <= (dim_t) size());

occa::memory m(modeMemory->addOffset(offset));
modeMemory_t &mv = *(m.modeMemory);
mv.modeDevice = modeMemory->modeDevice;
mv.size = bytes_;

modeMemory_t &mm = *(m.modeMemory);
mm.modeDevice = modeMemory->modeDevice;
mm.size = bytes_;
mm.isOrigin = false;
if (modeMemory->uvaPtr) {
mv.uvaPtr = (modeMemory->uvaPtr + offset);
mm.uvaPtr = (modeMemory->uvaPtr + offset);
}

return m;
}

Expand Down Expand Up @@ -550,19 +553,22 @@ namespace occa {
modeDevice_t *modeDevice = modeMemory->modeDevice;

// Free the actual backend memory object
if (modeMemory->canBeFreed) {
if (modeMemory->isOrigin) {
modeDevice->bytesAllocated -= (modeMemory->size);

if (modeMemory->uvaPtr) {
uvaMap.erase(modeMemory->uvaPtr);
modeDevice->uvaMap.erase(modeMemory->uvaPtr);
void *ptr = modeMemory->ptr;
void *uvaPtr = modeMemory->uvaPtr;

uvaMap.erase(uvaPtr);
modeDevice->uvaMap.erase(uvaPtr);

// CPU case where memory is shared
if (modeMemory->uvaPtr != modeMemory->ptr) {
uvaMap.erase(modeMemory->ptr);
modeDevice->uvaMap.erase(modeMemory->uvaPtr);
if (uvaPtr != ptr) {
uvaMap.erase(ptr);
modeDevice->uvaMap.erase(ptr);

sys::free(modeMemory->uvaPtr);
sys::free(uvaPtr);
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/mode/cuda/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ namespace occa {
m->mappedPtr = mappedPtr + offset;
}
m->isUnified = isUnified;
m->canBeFreed = false;
return m;
}

Expand Down Expand Up @@ -138,6 +137,13 @@ namespace occa {
}

void memory::free() {
if (!isOrigin) {
cuPtr = 0;
mappedPtr = NULL;
size = 0;
return;
}

if (mappedPtr) {
OCCA_CUDA_ERROR("Device: mappedFree()",
cuMemFreeHost(mappedPtr));
Expand Down
8 changes: 7 additions & 1 deletion src/mode/hip/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ namespace occa {
if (mappedPtr) {
m->mappedPtr = mappedPtr + offset;
}
m->canBeFreed = false;
return m;
}

Expand Down Expand Up @@ -136,6 +135,13 @@ namespace occa {
}

void memory::free() {
if (!isOrigin) {
hipPtr = 0;
mappedPtr = NULL;
size = 0;
return;
}

if (mappedPtr) {
OCCA_HIP_ERROR("Device: mappedFree()",
hipHostFree(mappedPtr));
Expand Down
5 changes: 3 additions & 2 deletions src/mode/serial/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ namespace occa {
size - offset,
properties);
m->ptr = ptr + offset;
m->canBeFreed = false;
return m;
}

Expand Down Expand Up @@ -89,7 +88,9 @@ namespace occa {

void memory::free() {
if (ptr) {
sys::free(ptr);
if (isOrigin) {
sys::free(ptr);
}
ptr = NULL;
size = 0;
}
Expand Down
3 changes: 1 addition & 2 deletions src/tools/sys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,7 @@ namespace occa {
cache = info.Size;
break;
}
}
}
}}
++pos;
off += sk;
}
Expand Down

0 comments on commit dba0db9

Please sign in to comment.