-
Notifications
You must be signed in to change notification settings - Fork 34
Kernel embeddings #98
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
Open
badnikhil
wants to merge
10
commits into
libmir:master
Choose a base branch
from
badnikhil:kernel_embeddings
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
bdfe811
cuda : created runtime.d for lazy init
badnikhil ad38a67
cuda: embed PTX dynamically inside launch! template
badnikhil 5f39f6c
updated flags
badnikhil 3e012aa
added ensureinit check in buffer for safety
badnikhil c5a45ed
update the test
badnikhil e52a7b7
simplify fromEmbedded syntax to take filename directly
badnikhil 8d674f9
moved launch
badnikhil 2d06e07
updated fromembedded
badnikhil 25e2c7a
Refactor DCompute to use injected PTX global variables
badnikhil 5ffeced
Gate injected-PTX fromModule and its test on __VERSION__ >= 2113 (LD…
badnikhil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,15 @@ struct Buffer(T) | |
|
|
||
| this(size_t elems) | ||
| { | ||
| ensureInit(); | ||
| status = cast(Status)cuMemAlloc(&raw,elems * T.sizeof); | ||
| checkErrors(); | ||
| hostMemory = null; | ||
| } | ||
|
|
||
| this(T[] arr) | ||
| { | ||
| ensureInit(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| status = cast(Status)cuMemAlloc(&raw,arr.length * T.sizeof); | ||
| checkErrors(); | ||
| hostMemory = arr; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * dcompute.driver.cuda.runtime | ||
| * | ||
| * initialisation of the CUDA runtime: | ||
| * | ||
| * shared static this() — runs once at program startup (before main()), on | ||
| * the main thread. Initialises Platform, Device, | ||
| * Context and the embedded Program. | ||
| * | ||
| * static this() — runs once per thread (including the main thread) | ||
| * when that thread starts. Creates the thread-local | ||
| * Queue and pushes the shared Context onto the | ||
| * thread's CUDA context stack. | ||
| * | ||
| * | ||
| * ensureInit() is kept as a defensive fallback. It is a no-op once the | ||
| * module constructors have run, so calling it is always safe and costs only | ||
| * a single bool check on the fast path. | ||
| */ | ||
| module dcompute.driver.cuda.runtime; | ||
|
|
||
| import dcompute.driver.cuda; | ||
| import std.experimental.allocator.mallocator : Mallocator; | ||
|
|
||
| // Global state (shared across every thread) | ||
| // __gshared: lives in a single memory location, accessible from all threads. | ||
| private __gshared Device _defaultDevice; | ||
| private __gshared Context _defaultContext; | ||
| private __gshared bool _platformReady = false; // safety-net flag | ||
|
|
||
| // Thread-local state | ||
| // plain `static`: D gives each thread its own copy automatically. | ||
| private static Queue _threadQueue; | ||
| private static bool _threadReady = false; // safety-net flag | ||
|
|
||
| // Primary init: shared static constructor | ||
| // D runtime calls this exactly once, before main(), on the main thread, | ||
| // in module-dependency order. No locking needed here. | ||
| shared static this() | ||
| { | ||
| _initPlatform(); | ||
| } | ||
|
|
||
| // Per-thread init: thread-local static constructor | ||
| // D runtime calls this automatically for every thread (including main) when | ||
| // that thread begins. For the main thread it runs after shared static this(). | ||
| static this() | ||
| { | ||
| _initThread(); | ||
| } | ||
|
|
||
| // Public API | ||
|
|
||
| /** | ||
| * Defensive fallback: ensures the runtime is fully initialised for the | ||
| * calling thread. | ||
| * | ||
| * Under normal operation this is a no-op (both flags are already true before | ||
| * main() starts). It guards against unusual call sites — e.g. Buffer | ||
| * constructed at global scope, or code reached before module constructors | ||
| * have finished in a pathological import order. | ||
| * | ||
| * Cost on the fast path: two bool reads, no locking. | ||
| */ | ||
| void ensureInit() | ||
| { | ||
| if (!_platformReady) _initPlatform(); // global guard (synchronized inside) | ||
| if (!_threadReady) _initThread(); // per-thread guard (no lock, TLS) | ||
| } | ||
|
|
||
| /** | ||
| * Returns the default Device (same object from every thread). | ||
| */ | ||
| Device defaultDevice() | ||
| { | ||
| return _defaultDevice; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the default Queue for the *calling* thread. | ||
| * Each thread gets its own independent Queue — no contention, no locking. | ||
| */ | ||
| Queue defaultQueue() | ||
| { | ||
| return _threadQueue; | ||
| } | ||
|
|
||
| // Private implementation | ||
|
|
||
| private void _initPlatform() | ||
| { | ||
| // Double-checked locking: fast no-lock path after first init. | ||
| if (_platformReady) return; | ||
|
|
||
| synchronized | ||
| { | ||
| if (_platformReady) return; | ||
|
|
||
| Platform.initialise(); | ||
| // Device 0 is the default. Users needing a specific device can call | ||
| // Platform.getDevices() and manage their own Context + Queue. | ||
|
|
||
| // TODO : Make multi-device usage better and easy. | ||
| _defaultDevice = Platform.getDevices(Mallocator.instance)[0]; | ||
|
|
||
| // cuCtxCreate creates the context AND pushes it onto the calling | ||
| // thread's CUDA context stack. | ||
| _defaultContext = Context(_defaultDevice); | ||
|
|
||
| // Compile-time PTX embedding | ||
| // LDC compiles @compute modules to PTX first, then compiles host code, | ||
| // so import() resolves in a single build pass with no file I/O at | ||
| // runtime. | ||
| // The version ladder is pure data — one line per SM level. Adding a | ||
| // new target only requires one new `else version` line here plus the | ||
| // matching "DComputeCUDA_XXX" entry in dub.json. | ||
|
|
||
| _platformReady = true; | ||
| } | ||
| } | ||
|
|
||
| private void _initThread() | ||
| { | ||
| if (_threadReady) return; | ||
|
|
||
| // The thread that ran _initPlatform() already has _defaultContext on its | ||
| // CUDA context stack (cuCtxCreate pushes automatically). | ||
| // Every other thread must push it explicitly before creating a stream. | ||
| if (Context.current.raw != _defaultContext.raw) | ||
| Context.push(_defaultContext); | ||
|
|
||
| _threadQueue = Queue(false); // non-async stream, bound to this thread | ||
| _threadReady = true; | ||
| } | ||
|
|
||
| /** | ||
| * Launch kernel `k` on the calling thread's default Queue using the globally | ||
| * loaded Program. Platform, Device, Context, Queue and Program are all | ||
| * initialised lazily on the first call — the user needs no boilerplate. | ||
| * | ||
| * Example: | ||
| * launch!saxpy([N,1,1], [1,1,1], b_res, alpha, b_x, b_y, N); | ||
| * | ||
| * Params: | ||
| * grid = Grid dimensions [x, y, z]. | ||
| * block = Block dimensions [x, y, z]. | ||
| * args = Kernel arguments (host types, Buffer/UnifiedBuffer ). | ||
| */ | ||
| auto launch(alias k)(uint[3] grid, uint[3] block, | ||
| HostArgsOf!(typeof(k)) args) | ||
| { | ||
| if (Program.globalProgram.raw is null) | ||
| { | ||
| import std.traits : moduleName; | ||
| ensureInit(); | ||
| Program.globalProgram = Program.fromModule!(moduleName!(__traits(parent, k)))(); | ||
| } | ||
| defaultQueue().enqueue!k(grid, block)(args); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are these calls needed here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#98 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer these not to be here so that the user does not need to rely only on the
runtimemodule.