This is an archive of the discontinued LLVM Phabricator instance.

[mlir] Initialize CUDA context lazily.
ClosedPublic

Authored by csigg on Mar 3 2021, 8:35 AM.

Details

Summary

So we can remove the ignore-warning pragma again.

Diff Detail

Event Timeline

csigg created this revision.Mar 3 2021, 8:35 AM
csigg requested review of this revision.Mar 3 2021, 8:35 AM
mehdi_amini added inline comments.Mar 3 2021, 10:15 AM
mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
44–45

This has to be done before the initialization of static CUcontext Context below? It wasn't the case before this revision?
(it seems like the only reason to pull the cuInit out and move it above though.

csigg updated this revision to Diff 328046.Mar 4 2021, 12:20 AM

Simplify.

mlir/lib/ExecutionEngine/CudaRuntimeWrappers.cpp
44–45

Before this revision, cuInit() was called during static initialization.

The current context and behavior cuCtxCreate vs cuDevicePrimaryPrimaryRetain always confuse me, so I took the safe route. It's not very well documented, but here is how it works:

There is a stack of contexts, the top one is current. A context needs to be active (ref-count > 0) to be usable.

cuDevicePrimaryPrimaryRetain/Release: increment/decrement primary.ref-count
cuCtxSetCurrent(ctx): stack.pop(), if (ctx != null) stack.push(ctx)
cuCtxCreate: create ctx, ctx.ref-count = 1, stack.push(ctx)
cuCtxDestroy(ctx): ctx.ref-count = 0, if (stack.top() == ctx) stack.pop()

Before calling cuCtxGetCurrent(), we need to call cuInit() and according to the explanation above we also /can/ retain the primary context because it does not affect the stack. I've changed the code now because it's indeed simpler and easier to read.

Thanks a lot for noticing/asking.

csigg updated this revision to Diff 328052.Mar 4 2021, 12:27 AM

Switch to context push/pop.

herhut accepted this revision.Mar 4 2021, 2:59 AM

So we get to use the context stack after all. Looks very clean now.

This revision is now accepted and ready to land.Mar 4 2021, 2:59 AM
This revision was landed with ongoing or failed builds.Mar 4 2021, 4:08 AM
This revision was automatically updated to reflect the committed changes.