Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 27 additions & 24 deletions offload/plugins-nextgen/common/include/MemoryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "llvm/Support/Error.h"

using namespace llvm::omp::target::debug;

namespace llvm {

/// Base class of per-device allocator.
Expand Down Expand Up @@ -79,7 +81,8 @@ class MemoryManagerTy {
static int findBucket(size_t Size) {
const size_t F = floorToPowerOfTwo(Size);

DP("findBucket: Size %zu is floored to %zu.\n", Size, F);
ODBG(ODT_Alloc) << "findBucket: Size " << Size << " is floored to " << F
<< ".";

int L = 0, H = NumBuckets - 1;
while (H - L > 1) {
Expand All @@ -94,7 +97,7 @@ class MemoryManagerTy {

assert(L >= 0 && L < NumBuckets && "L is out of range");

DP("findBucket: Size %zu goes to bucket %d\n", Size, L);
ODBG(ODT_Alloc) << "findBucket: Size " << Size << " goes to bucket " << L;

return L;
}
Expand Down Expand Up @@ -192,17 +195,17 @@ class MemoryManagerTy {
// We cannot get memory from the device. It might be due to OOM. Let's
// free all memory in FreeLists and try again.
if (TgtPtr == nullptr) {
DP("Failed to get memory on device. Free all memory in FreeLists and "
"try again.\n");
ODBG(ODT_Alloc) << "Failed to get memory on device. Free all memory "
<< "in FreeLists and try again.";
TgtPtrOrErr = freeAndAllocate(Size, HstPtr);
if (!TgtPtrOrErr)
return TgtPtrOrErr.takeError();
TgtPtr = *TgtPtrOrErr;
}

if (TgtPtr == nullptr)
DP("Still cannot get memory on device probably because the device is "
"OOM.\n");
ODBG(ODT_Alloc) << "Still cannot get memory on device probably because "
<< "the device is OOM.";

return TgtPtr;
}
Expand All @@ -222,8 +225,7 @@ class MemoryManagerTy {
for (auto &PtrToNode : PtrToNodeTable) {
assert(PtrToNode.second.Ptr && "nullptr in map table");
if (auto Err = deleteOnDevice(PtrToNode.second.Ptr))
REPORT("Failure to delete memory: %s\n",
toString(std::move(Err)).data());
REPORT() << "Failure to delete memory: " << toString(std::move(Err));
}
}

Expand All @@ -235,21 +237,20 @@ class MemoryManagerTy {
if (Size == 0)
return nullptr;

DP("MemoryManagerTy::allocate: size %zu with host pointer " DPxMOD ".\n",
Size, DPxPTR(HstPtr));
ODBG(ODT_Alloc) << "MemoryManagerTy::allocate: size " << Size
<< " with host pointer " << HstPtr << ".";

// If the size is greater than the threshold, allocate it directly from
// device.
if (Size > SizeThreshold) {
DP("%zu is greater than the threshold %zu. Allocate it directly from "
"device\n",
Size, SizeThreshold);
ODBG(ODT_Alloc) << Size << " is greater than the threshold "
<< SizeThreshold << ". Allocate it directly from device";
auto TgtPtrOrErr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
if (!TgtPtrOrErr)
return TgtPtrOrErr.takeError();

DP("Got target pointer " DPxMOD ". Return directly.\n",
DPxPTR(*TgtPtrOrErr));
ODBG(ODT_Alloc) << "Got target pointer " << *TgtPtrOrErr
<< ". Return directly.";

return *TgtPtrOrErr;
}
Expand All @@ -272,12 +273,13 @@ class MemoryManagerTy {
}

if (NodePtr != nullptr)
DP("Find one node " DPxMOD " in the bucket.\n", DPxPTR(NodePtr));
ODBG(ODT_Alloc) << "Find one node " << NodePtr << " in the bucket.";

// We cannot find a valid node in FreeLists. Let's allocate on device and
// create a node for it.
if (NodePtr == nullptr) {
DP("Cannot find a node in the FreeLists. Allocate on device.\n");
ODBG(ODT_Alloc) << "Cannot find a node in the FreeLists. "
<< "Allocate on device.";
// Allocate one on device
auto TgtPtrOrErr = allocateOrFreeAndAllocateOnDevice(Size, HstPtr);
if (!TgtPtrOrErr)
Expand All @@ -294,8 +296,8 @@ class MemoryManagerTy {
NodePtr = &Itr.first->second;
}

DP("Node address " DPxMOD ", target pointer " DPxMOD ", size %zu\n",
DPxPTR(NodePtr), DPxPTR(TgtPtr), Size);
ODBG(ODT_Alloc) << "Node address " << NodePtr << ", target pointer "
<< TgtPtr << ", size " << Size;
}

assert(NodePtr && "NodePtr should not be nullptr at this point");
Expand All @@ -305,7 +307,7 @@ class MemoryManagerTy {

/// Deallocate memory pointed by \p TgtPtr
Error free(void *TgtPtr) {
DP("MemoryManagerTy::free: target memory " DPxMOD ".\n", DPxPTR(TgtPtr));
ODBG(ODT_Alloc) << "MemoryManagerTy::free: target memory " << TgtPtr << ".";

NodeTy *P = nullptr;

Expand All @@ -322,14 +324,15 @@ class MemoryManagerTy {

// The memory is not managed by the manager
if (P == nullptr) {
DP("Cannot find its node. Delete it on device directly.\n");
ODBG(ODT_Alloc) << "Cannot find its node. Delete it on device directly.";
return deleteOnDevice(TgtPtr);
}

// Insert the node to the free list
const int B = findBucket(P->Size);

DP("Found its node " DPxMOD ". Insert it to bucket %d.\n", DPxPTR(P), B);
ODBG(ODT_Alloc) << "Found its node " << P << ". Insert it to bucket " << B
<< ".";

{
std::lock_guard<std::mutex> G(FreeListLocks[B]);
Expand All @@ -352,8 +355,8 @@ class MemoryManagerTy {
size_t Threshold = MemoryManagerThreshold.get();

if (MemoryManagerThreshold.isPresent() && Threshold == 0) {
DP("Disabled memory manager as user set "
"LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD=0.\n");
ODBG(ODT_Alloc) << "Disabled memory manager as user set "
<< "LIBOMPTARGET_MEMORY_MANAGER_THRESHOLD=0.";
return std::make_pair(0, false);
}

Expand Down
9 changes: 6 additions & 3 deletions offload/plugins-nextgen/common/include/PluginInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Triple.h"

using namespace llvm::omp::target::debug;

namespace llvm {
namespace omp {
namespace target {
Expand Down Expand Up @@ -712,8 +714,8 @@ class PinnedAllocationMapTy {
IgnoreLockMappedFailures = false;
} else {
// Disable by default.
DP("Invalid value LIBOMPTARGET_LOCK_MAPPED_HOST_BUFFERS=%s\n",
OMPX_LockMappedBuffers.get().data());
ODBG(ODT_Alloc) << "Invalid value LIBOMPTARGET_LOCK_MAPPED_HOST_BUFFERS="
<< OMPX_LockMappedBuffers.get();
LockMappedBuffers = false;
}
}
Expand Down Expand Up @@ -1639,7 +1641,8 @@ template <typename ResourceRef> class GenericDeviceResourceManagerTy {
/// must be called before the destructor.
virtual Error deinit() {
if (NextAvailable)
DP("Missing %d resources to be returned\n", NextAvailable);
ODBG(ODT_Deinit) << "Missing " << NextAvailable
<< " resources to be returned";

// TODO: This prevents a bug on libomptarget to make the plugins fail. There
// may be some resources not returned. Do not destroy these ones.
Expand Down
23 changes: 13 additions & 10 deletions offload/plugins-nextgen/common/src/GlobalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ using namespace omp;
using namespace target;
using namespace plugin;
using namespace error;
using namespace debug;

Expected<std::unique_ptr<ObjectFile>>
GenericGlobalHandlerTy::getELFObjectFile(DeviceImageTy &Image) {
Expand Down Expand Up @@ -75,12 +76,13 @@ Error GenericGlobalHandlerTy::moveGlobalBetweenDeviceAndHost(
return Err;
}

DP("Successfully %s %u bytes associated with global symbol '%s' %s the "
"device "
"(%p -> %p).\n",
Device2Host ? "read" : "write", HostGlobal.getSize(),
HostGlobal.getName().data(), Device2Host ? "from" : "to",
DeviceGlobal.getPtr(), HostGlobal.getPtr());
ODBG(ODT_DataTransfer) << "Successfully " << (Device2Host ? "read" : "write")
<< " " << HostGlobal.getSize()
<< " bytes associated with global symbol '"
<< HostGlobal.getName() << "' "
<< (Device2Host ? "from" : "to") << " the device ("
<< DeviceGlobal.getPtr() << " -> "
<< HostGlobal.getPtr() << ").";

return Plugin::success();
}
Expand Down Expand Up @@ -157,10 +159,11 @@ Error GenericGlobalHandlerTy::readGlobalFromImage(GenericDeviceTy &Device,
HostGlobal.getName().data(), ImageGlobal.getSize(),
HostGlobal.getSize());

DP("Global symbol '%s' was found in the ELF image and %u bytes will copied "
"from %p to %p.\n",
HostGlobal.getName().data(), HostGlobal.getSize(), ImageGlobal.getPtr(),
HostGlobal.getPtr());
ODBG(ODT_DataTransfer) << "Global symbol '" << HostGlobal.getName()
<< "' was found in the ELF image and "
<< HostGlobal.getSize() << " bytes will copied from "
<< ImageGlobal.getPtr() << " to "
<< HostGlobal.getPtr() << ".";

assert(Image.getStart() <= ImageGlobal.getPtr() &&
utils::advancePtr(ImageGlobal.getPtr(), ImageGlobal.getSize()) <
Expand Down
Loading
Loading