Skip to content

Commit

Permalink
OpenCL + cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pvelesko committed Aug 20, 2022
1 parent 8c60480 commit de44c88
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 61 deletions.
3 changes: 3 additions & 0 deletions Testing/Temporary/LastTest.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Start testing: Aug 20 09:44 UTC
----------------------------------------------------------
End testing: Aug 20 09:44 UTC
25 changes: 16 additions & 9 deletions src/CHIPBackend.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,11 @@ void CHIPDevice::init() {
if (!AllocationTracker)
AllocationTracker = new CHIPAllocationTracker(
HipDeviceProps_.totalGlobalMem, HipDeviceProps_.name);

unsigned int Flags = 0;
int Priority = 0;
auto ChipQueue = createQueue(Flags, Priority);
LegacyDefaultQueue = std::unique_ptr<CHIPQueue>(ChipQueue);
}

void CHIPDevice::copyDeviceProperties(hipDeviceProp_t *Prop) {
Expand Down Expand Up @@ -762,8 +767,8 @@ void CHIPDevice::registerDeviceVariable(std::string *ModuleStr,
}

void CHIPDevice::addQueue(CHIPQueue *ChipQueue) {
std::lock_guard<std::mutex> LockDevice(DeviceMtx);
logDebug("CHIPDevice::addQueue ", (char *)ChipQueue);
Backend->addQueue(ChipQueue);

auto QueueFound =
std::find(ChipQueues_.begin(), ChipQueues_.end(), ChipQueue);
Expand Down Expand Up @@ -793,19 +798,19 @@ void CHIPEvent::trackImpl() {
CHIPQueue *CHIPDevice::createQueueAndRegister(unsigned int Flags,
int Priority) {

std::lock_guard<std::mutex> Lock(Backend->BackendMtx);
auto ChipQueue = addQueueImpl(Flags, Priority);
auto ChipQueue = createQueue(Flags, Priority);
// Add the queue handle to the device and the Backend
addQueue(ChipQueue);
Backend->addQueue(ChipQueue);
return ChipQueue;
}

CHIPQueue *CHIPDevice::createQueueAndRegister(const uintptr_t *NativeHandles,
const size_t NumHandles) {
std::lock_guard<std::mutex> Lock(Backend->BackendMtx);
auto ChipQueue = addQueueImpl(NativeHandles, NumHandles);
auto ChipQueue = createQueue(NativeHandles, NumHandles);
// Add the queue handle to the device and the Backend
addQueue(ChipQueue);
Backend->addQueue(ChipQueue);
return ChipQueue;
}

Expand Down Expand Up @@ -979,8 +984,8 @@ void CHIPContext::syncQueues(CHIPQueue *TargetQueue) {

void CHIPContext::addDevice(CHIPDevice *ChipDevice) {
logDebug("CHIPContext.add_device() {}", ChipDevice->getName());
std::lock_guard<std::mutex> LockContext(ContextMtx);
ChipDevices_.push_back(ChipDevice);
// TODO: add to backend as well
}

std::vector<CHIPDevice *> &CHIPContext::getDevices() {
Expand Down Expand Up @@ -1166,6 +1171,7 @@ void CHIPBackend::addContext(CHIPContext *ChipContext) {
}
void CHIPBackend::addQueue(CHIPQueue *ChipQueue) {
logDebug("CHIPBackend::addQueue()");
std::lock_guard<std::mutex> LockBackend(BackendMtx);
auto QueueFound = std::find(ChipQueues.begin(), ChipQueues.end(), ChipQueue);
if (QueueFound == ChipQueues.end()) {
ChipQueues.push_back(ChipQueue);
Expand All @@ -1178,12 +1184,13 @@ void CHIPBackend::addQueue(CHIPQueue *ChipQueue) {
}
void CHIPBackend::addDevice(CHIPDevice *ChipDevice) {
logDebug("CHIPDevice.add_device() {}", ChipDevice->getName());
std::lock_guard<std::mutex> LockBackend(BackendMtx);
ChipDevices.push_back(ChipDevice);
}

void CHIPBackend::registerModuleStr(std::string *ModuleStr) {
logDebug("CHIPBackend->register_module()");
std::lock_guard<std::mutex> Lock(BackendMtx);
std::lock_guard<std::mutex> LockBackend(BackendMtx);
getModulesStr().push_back(ModuleStr);
}

Expand All @@ -1202,7 +1209,7 @@ void CHIPBackend::unregisterModuleStr(std::string *ModuleStr) {

hipError_t CHIPBackend::configureCall(dim3 Grid, dim3 Block, size_t SharedMem,
hipStream_t ChipQueue) {
std::lock_guard<std::mutex> Lock(BackendMtx);
std::lock_guard<std::mutex> LockBackend(BackendMtx);
logDebug("CHIPBackend->configureCall(grid=({},{},{}), block=({},{},{}), "
"shared={}, q={}",
Grid.x, Grid.y, Grid.z, Block.x, Block.y, Block.z, SharedMem,
Expand All @@ -1215,7 +1222,7 @@ hipError_t CHIPBackend::configureCall(dim3 Grid, dim3 Block, size_t SharedMem,

hipError_t CHIPBackend::setArg(const void *Arg, size_t Size, size_t Offset) {
logDebug("CHIPBackend->set_arg()");
std::lock_guard<std::mutex> Lock(BackendMtx);
std::lock_guard<std::mutex> LockBackend(BackendMtx);
CHIPExecItem *ExecItem = ChipExecStack.top();
ExecItem->setArg(Arg, Size, Offset);

Expand Down
6 changes: 3 additions & 3 deletions src/CHIPBackend.hh
Original file line number Diff line number Diff line change
Expand Up @@ -1154,9 +1154,9 @@ public:
* @return CHIPQueue* pointer to the newly created queue (can also be found
* in chip_queues vector)
*/
virtual CHIPQueue *addQueueImpl(unsigned int Flags, int Priority) = 0;
virtual CHIPQueue *addQueueImpl(const uintptr_t *NativeHandles,
int NumHandles) = 0;
virtual CHIPQueue *createQueue(unsigned int Flags, int Priority) = 0;
virtual CHIPQueue *createQueue(const uintptr_t *NativeHandles,
int NumHandles) = 0;

/**
* @brief Add a queue to this device and the backend
Expand Down
14 changes: 4 additions & 10 deletions src/backend/Level0/CHIPBackendLevel0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1371,12 +1371,6 @@ void CHIPBackendLevel0::initializeImpl(std::string CHIPPlatformStr,
if (AnyDeviceType || ZeDeviceType == DeviceProperties.type) {
CHIPDeviceLevel0 *ChipL0Dev = CHIPDeviceLevel0::create(Dev, ChipL0Ctx, i);
ChipL0Ctx->addDevice(ChipL0Dev);

// ChipL0Dev->createQueueAndRegister((int)0, (int)0);
std::lock_guard<std::mutex> Lock(Backend->BackendMtx);
auto ChipQueue = new CHIPQueueLevel0(ChipL0Dev, 0, 0);
ChipL0Dev->LegacyDefaultQueue = std::unique_ptr<CHIPQueue>(ChipQueue);

Backend->addDevice(ChipL0Dev);
break; // For now don't add more than one device
}
Expand Down Expand Up @@ -1404,7 +1398,7 @@ void CHIPBackendLevel0::initializeFromNative(const uintptr_t *NativeHandles,
addDevice(ChipDev);

std::lock_guard<std::mutex> Lock(Backend->BackendMtx);
auto ChipQueue = ChipDev->addQueueImpl(NativeHandles, NumHandles);
auto ChipQueue = ChipDev->createQueue(NativeHandles, NumHandles);
ChipDev->LegacyDefaultQueue = std::unique_ptr<CHIPQueue>(ChipQueue);

StaleEventMonitor =
Expand Down Expand Up @@ -1675,13 +1669,13 @@ void CHIPDeviceLevel0::populateDevicePropertiesImpl() {
HipDeviceProps_.texturePitchAlignment = 1;
}

CHIPQueue *CHIPDeviceLevel0::addQueueImpl(unsigned int Flags, int Priority) {
CHIPQueue *CHIPDeviceLevel0::createQueue(unsigned int Flags, int Priority) {
CHIPQueueLevel0 *NewQ = new CHIPQueueLevel0(this, Flags, Priority);
return NewQ;
}

CHIPQueue *CHIPDeviceLevel0::addQueueImpl(const uintptr_t *NativeHandles,
int NumHandles) {
CHIPQueue *CHIPDeviceLevel0::createQueue(const uintptr_t *NativeHandles,
int NumHandles) {
ze_command_queue_handle_t CmdQ = (ze_command_queue_handle_t)NativeHandles[3];
CHIPQueueLevel0 *NewQ;
if (!CmdQ) {
Expand Down
6 changes: 3 additions & 3 deletions src/backend/Level0/CHIPBackendLevel0.hh
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ public:
return Mod;
}

virtual CHIPQueue *addQueueImpl(unsigned int Flags, int Priority) override;
virtual CHIPQueue *addQueueImpl(const uintptr_t *NativeHandles,
int NumHandles) override;
virtual CHIPQueue *createQueue(unsigned int Flags, int Priority) override;
virtual CHIPQueue *createQueue(const uintptr_t *NativeHandles,
int NumHandles) override;

ze_device_properties_t *getDeviceProps() { return &(this->ZeDeviceProps_); };

Expand Down
37 changes: 11 additions & 26 deletions src/backend/OpenCL/CHIPBackendOpenCL.cc
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ CHIPDeviceOpenCL::CHIPDeviceOpenCL(CHIPContextOpenCL *ChipCtx,
"pointer");
}

CHIPDeviceOpenCL *CHIPDeviceOpenCL::create(CHIPContextOpenCL *ChipContext,
cl::Device *ClDevice, int Idx) {
CHIPDeviceOpenCL *CHIPDeviceOpenCL::create(cl::Device *ClDevice,
CHIPContextOpenCL *ChipContext,
int Idx) {
CHIPDeviceOpenCL *Dev = new CHIPDeviceOpenCL(ChipContext, ClDevice, Idx);
Dev->init();
return Dev;
Expand Down Expand Up @@ -488,8 +489,6 @@ void CHIPEventOpenCL::decreaseRefCount(std::string Reason) {
logTrace("CHIP Refc: {}->{} OpenCL Refc: {}->{} REASON: {}", *Refc_,
*Refc_ - 1, R, R - 1, Reason);
(*Refc_)--;
// Destructor to be called by event monitor once backend is done using it
// if (*refc == 1) delete this;
}

void CHIPEventOpenCL::increaseRefCount(std::string Reason) {
Expand Down Expand Up @@ -518,7 +517,8 @@ void CHIPEventOpenCL::recordStream(CHIPQueue *ChipQueue) {
std::lock_guard<std::mutex> Lock(EventMtx);

logDebug("CHIPEvent::recordStream()");
assert(ChipQueue->getLastEvent() != nullptr);
if (!ChipQueue->getLastEvent())
ChipQueue->enqueueMarker();
this->takeOver(ChipQueue->getLastEvent());
EventStatus_ = EVENT_STATUS_RECORDING;
}
Expand Down Expand Up @@ -730,13 +730,13 @@ void CHIPModuleOpenCL::compile(CHIPDevice *ChipDev) {
}
}

CHIPQueue *CHIPDeviceOpenCL::addQueueImpl(unsigned int Flags, int Priority) {
CHIPQueue *CHIPDeviceOpenCL::createQueue(unsigned int Flags, int Priority) {
CHIPQueueOpenCL *NewQ = new CHIPQueueOpenCL(this);
return NewQ;
}

CHIPQueue *CHIPDeviceOpenCL::addQueueImpl(const uintptr_t *NativeHandles,
int NumHandles) {
CHIPQueue *CHIPDeviceOpenCL::createQueue(const uintptr_t *NativeHandles,
int NumHandles) {
cl_command_queue CmdQ = (cl_command_queue)NativeHandles[3];
CHIPQueueOpenCL *NewQ = new CHIPQueueOpenCL(this, CmdQ);
return NewQ;
Expand Down Expand Up @@ -938,17 +938,6 @@ CHIPQueueOpenCL::CHIPQueueOpenCL(CHIPDevice *ChipDevice, cl_command_queue Queue)
CHIPERR_CHECK_LOG_AND_THROW(Status, CL_SUCCESS,
hipErrorInitializationError);
}
/**
* queues should always have lastEvent. Can't do this in the constuctor
* because enqueueMarker is virtual and calling overriden virtual methods from
* constructors is undefined behavior.
*
* Also, must call implementation method enqueueMarker_ as opposed to wrapped
* one (enqueueMarker) because the wrapped method enforces queue semantics
* which require LastEvent to be initialized.
*
*/
updateLastEvent(enqueueMarkerImpl());
}

CHIPQueueOpenCL::~CHIPQueueOpenCL() {}
Expand Down Expand Up @@ -1288,16 +1277,14 @@ void CHIPBackendOpenCL::initializeImpl(std::string CHIPPlatformStr,
Backend->addContext(ChipContext);
for (int i = 0; i < Devices.size(); i++) {
cl::Device *Dev = new cl::Device(Devices[i]);
CHIPDeviceOpenCL *ChipDev = CHIPDeviceOpenCL::create(ChipContext, Dev, i);
CHIPDeviceOpenCL *ChipDev = CHIPDeviceOpenCL::create(Dev, ChipContext, i);

logTrace("CHIPDeviceOpenCL {}",
ChipDev->ClDevice->getInfo<CL_DEVICE_NAME>());

// Add device to context & backend
ChipContext->addDevice(ChipDev);
Backend->addDevice(ChipDev);

// Create and add queue queue to Device and Backend
ChipDev->createQueueAndRegister((int)0, (int)0);
}
logDebug("OpenCL Context Initialized.");
};
Expand All @@ -1315,15 +1302,13 @@ void CHIPBackendOpenCL::initializeFromNative(const uintptr_t *NativeHandles,
addContext(ChipContext);

cl::Device *Dev = new cl::Device(DevId);
CHIPDeviceOpenCL *ChipDev = CHIPDeviceOpenCL::create(ChipContext, Dev, 0);
CHIPDeviceOpenCL *ChipDev = CHIPDeviceOpenCL::create(Dev, ChipContext, 0);
logTrace("CHIPDeviceOpenCL {}", ChipDev->ClDevice->getInfo<CL_DEVICE_NAME>());

// Add device to context & backend
ChipContext->addDevice(ChipDev);
addDevice(ChipDev);

ChipDev->createQueueAndRegister(NativeHandles, NumHandles);

setActiveDevice(ChipDev);

logDebug("OpenCL Context Initialized.");
Expand Down
22 changes: 12 additions & 10 deletions src/backend/OpenCL/CHIPBackendOpenCL.hh
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public:
void *allocateImpl(size_t Size, size_t Alignment, hipMemoryType MemType,
CHIPHostAllocFlags Flags = CHIPHostAllocFlags()) override;

bool isAllocatedPtrUSM(void* Ptr) override { return true; }
bool isAllocatedPtrUSM(void *Ptr) override { return true; }
virtual void freeImpl(void *Ptr) override;
cl::Context *get();
};
Expand All @@ -133,16 +133,17 @@ class CHIPDeviceOpenCL : public CHIPDevice {
int Idx);

public:
static CHIPDeviceOpenCL *create(CHIPContextOpenCL *ChipContext,
cl::Device *ClDevice, int Idx);
static CHIPDeviceOpenCL *create(cl::Device *ClDevice,
CHIPContextOpenCL *ChipContext, int Idx);
cl::Device *ClDevice;
cl::Context *ClContext;
cl::Device *get() { return ClDevice; }
virtual void populateDevicePropertiesImpl() override;
virtual void resetImpl() override;
virtual CHIPModuleOpenCL *addModule(std::string *ModuleStr) override;
virtual CHIPQueue *addQueueImpl(unsigned int Flags, int Priority) override;
virtual CHIPQueue *addQueueImpl(const uintptr_t *NativeHandles, int NumHandles) override;
virtual CHIPQueue *createQueue(unsigned int Flags, int Priority) override;
virtual CHIPQueue *createQueue(const uintptr_t *NativeHandles,
int NumHandles) override;

virtual CHIPTexture *
createTexture(const hipResourceDesc *ResDesc, const hipTextureDesc *TexDesc,
Expand Down Expand Up @@ -183,7 +184,8 @@ public:
size_t Width, size_t Height,
size_t Depth) override;

virtual hipError_t getBackendHandles(uintptr_t *NativeInfo, int *NumHandles) override;
virtual hipError_t getBackendHandles(uintptr_t *NativeInfo,
int *NumHandles) override;
virtual CHIPEvent *
enqueueBarrierImpl(std::vector<CHIPEvent *> *EventsToWaitFor) override;
virtual CHIPEvent *enqueueMarkerImpl() override;
Expand Down Expand Up @@ -233,7 +235,8 @@ public:
virtual void initializeImpl(std::string CHIPPlatformStr,
std::string CHIPDeviceTypeStr,
std::string CHIPDeviceStr) override;
virtual void initializeFromNative(const uintptr_t *NativeHandles, int NumHandles) override;
virtual void initializeFromNative(const uintptr_t *NativeHandles,
int NumHandles) override;

virtual std::string getDefaultJitFlags() override;

Expand All @@ -249,9 +252,8 @@ public:
virtual CHIPEventMonitor *createCallbackEventMonitor() override;
virtual CHIPEventMonitor *createStaleEventMonitor() override;

virtual hipEvent_t getHipEvent(void* NativeEvent) override;
virtual void* getNativeEvent(hipEvent_t HipEvent) override;

virtual hipEvent_t getHipEvent(void *NativeEvent) override;
virtual void *getNativeEvent(hipEvent_t HipEvent) override;
};

class CHIPTextureOpenCL : public CHIPTexture {
Expand Down

0 comments on commit de44c88

Please sign in to comment.