Skip to content

[SYCL][USM] Enable per-context USM behavior. #467

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

jbrodman
Copy link
Contributor

@jbrodman jbrodman commented Aug 6, 2019

Keep track of USM "state" per-context to handle multiple device scenarios.

Signed-off-by: James Brodman [email protected]

@@ -108,23 +105,24 @@ class CLUSM {
} // namespace usm

namespace cliext {
bool initializeExtensions(cl_platform_id platform);
bool initializeExtensions(cl_context context, cl_platform_id platform);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, follow llvm coding guidelines. name variables starting from big letter, functions from small.

cl::sycl::detail::usm::CLUSM *retVal = nullptr;
if (gCLUSM.find(ctxt) == gCLUSM.end()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest rewriting it to:

cl::sycl::detail::usm::CLUSM *&retVal = gCLUSM[ctxt];
if(nullptr == retVal)
retVal = new cl::sycl::detail::usm::CLUSM();

And move check for backend to the function beginning. Now you are allocating CLUSM even for non OpenCL BE.

cl::sycl::detail::usm::CLUSM::Create(gCLUSM);
}

__SYCL_EXPORTED extern std::map<cl_context, cl::sycl::detail::usm::CLUSM *>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you store pointer to CLUSM in the context object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. Wasn't sure if you guys preferred it to be that invasive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually - not sure I can really do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See below in the opencl_shim part.

detail::getSyclObjImpl(Context)->getHandleRef());
auto clusm = GetCLUSM(context);
if (usesUSM && clusm) {
cl_bool t = CL_TRUE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please make this variable name more descriptive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which one?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t

auto theKernel = pi::pi_cast<cl_kernel>(Kernel);
// Enable USM Indirect Access for Kernels
if (clusm->useCLUSM()) {
CHECK_OCL_CODE(clusm->setKernelExecInfo(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems you do the same 3 calls to setKernelExecInfo in both branches of if. Could you please move it outside of the if?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are different calls. The ones through the clusm object do extra book keeping.

static clHostMemAllocINTEL_fn pfn_clHostMemAllocINTEL = NULL;
// Changing to per-context tracking
// TODO: use piContext everywhere
static std::map<cl_context, clHostMemAllocINTEL_fn> pfn_clHostMemAllocINTEL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using global objects. It seems you can easily store them in context object or in some object which depends on context object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought. By the time we're here, we've left sycl land. We don't have a handle on the sycl context. Not sure I can avoid global objects here unless we break the CL extension interface in a really non-CL friendly way.

Copy link
Contributor

@romanovvlad romanovvlad Aug 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take hostMemAlloc for example, we can have something like this:

// The class holds all data you currently using globals for.
// CLUSM and USMHandle can be merged.
class USMHandle  {
  FuncType *pfn_clDeviceMemAllocINTEL = nullptr;
  ...
  CLUSM CLUSM;
};
class context_impl {

  context_impl () {
    ...
    MUSMHandle.reset(getUSMHandleFor(m_Context));
  }
  void *alignedAlloc(...) {
    // The following logic can be implemented in USMHandle
    if(m_USMHandle->pfn_clDeviceMemAllocINTEL) {
      pfn_clHostMemAllocINTEL(m_Context, ...);
    }
    else {
      USMHandle->CLUSM->hostMemAlloc(...);
    }
  }
  ...
  unique_ptr<USMHandle> m_USMHandle;
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is what I meant. Now we're driving all this branching logic into the implementation instead of having a nice clean shim. Would you guys prefer this? It's a non-trivial rewrite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably the only way forward here is to also do the PI rewrite - this would let us do a clean interface using SYCL objects instead of CL objects and let us do this type of book keeping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer this as having global objects cause a lot of problems.

Signed-off-by: James Brodman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants