Skip to content
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

Nvidia target via NVRTC and Nim ↦ CUDA DSL #487

Draft
wants to merge 60 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
f8eadcc
commit the initial Nim ⇒ CUDA DSL, NVRTC & CUDA execution helpers
Vindaar Nov 29, 2024
19ac26e
[tests/examples] add example for a BigInt modular addition
Vindaar Nov 29, 2024
462c9e8
remove `nimcuda` dependency, wrap everything we need manually
Vindaar Feb 18, 2025
cb10aaa
merge `execCuda` logic for LLVM & NVRTC
Vindaar Feb 18, 2025
9f9b785
add `quitOnFailure` for the `check` calls to avoid regression
Vindaar Feb 18, 2025
0ae1fbe
[tests] turn big int `modadd` example into real test
Vindaar Feb 18, 2025
dd074f2
copy `libpaths.nim` over from nimcuda
Vindaar Feb 19, 2025
38e592f
[cuda] add partial support for `const` in CUDA generator
Vindaar Feb 20, 2025
6ec730a
[cuda] fix minor issue in `if` statements in CUDA generator
Vindaar Feb 20, 2025
772899a
[cuda] add support for named blocks
Vindaar Feb 20, 2025
9164de5
[cuda] add support for `bool`
Vindaar Feb 20, 2025
ac7489e
[cuda] remove unnecessary semicolon
Vindaar Feb 20, 2025
3db25f1
[cuda] add support for `{.volatile.}` variables
Vindaar Feb 20, 2025
17efeb5
[nvrtc] add `modadd`, `modsub`, `mtymul` implementations using inline…
Vindaar Feb 20, 2025
016d44c
[cuda] support basic type conversions
Vindaar Feb 21, 2025
4a8224a
[cuda] support `var` parameters in procs
Vindaar Feb 21, 2025
14cb2a8
[cuda] make sure proc body is a block
Vindaar Feb 21, 2025
6bede35
[cuda] support boolean / bitwise AND/OR and XOR, NOT
Vindaar Feb 21, 2025
8acae98
[cuda] support int32 literals
Vindaar Feb 21, 2025
538acb6
[cuda] handle prefix `not`
Vindaar Feb 21, 2025
f0328e1
[cuda] make sure to pass `array` types by pointer instead of copy
Vindaar Feb 21, 2025
5ae697b
[nvrtc] add more helpers, add TODO to investigate `slct` calls
Vindaar Feb 21, 2025
609fc46
[nvrtc] add many more field arithmetic / bigint operations
Vindaar Feb 21, 2025
3db60be
[tests] add test to pass by pointer and `var`
Vindaar Feb 21, 2025
96260f0
[tests] add test case for modadd/sub/mtymul
Vindaar Feb 21, 2025
cb525b3
[tests] add basic test cases for all new NVRTC operations
Vindaar Feb 21, 2025
c91904f
[tests] update modadd/sub, mtymul test for new `getFieldModulus`
Vindaar Feb 21, 2025
8599327
add BabyBear field
Vindaar Feb 24, 2025
9022e6c
[nvrtc] handle `mtymul` for fields with 1 limb
Vindaar Feb 24, 2025
aef1a21
[cuda] support nested array types, unpack generic instantiatons
Vindaar Feb 24, 2025
a55862f
[cuda] correctly generate ptr to array & ptr to array return types
Vindaar Feb 24, 2025
e4dafb4
[cuda] automatically generate `memcpy` for static array types
Vindaar Feb 25, 2025
6f35b3c
[nvrtc] use `const` for field modulus and other CT constants
Vindaar Feb 26, 2025
49524ff
[cuda] extend error message for non copyable inputs
Vindaar Feb 26, 2025
1cc222a
[cuda] disable passStructByPointer also for CUDA
Vindaar Feb 26, 2025
66f9cda
[cuda] add `{.nimonly.}` pragma one can use in `cuda` block
Vindaar Feb 26, 2025
d509583
[cuda] better logic for detection of type names
Vindaar Feb 26, 2025
3828a80
[cuda] support `const` by mapping it to a `__constant__`
Vindaar Feb 26, 2025
0ab26ae
[cuda] allow type determination from array literal
Vindaar Feb 26, 2025
0f854f7
[nvrtc] get rid of complexity with custom uint32 constants
Vindaar Feb 26, 2025
f54e59e
[staticFor] add stepped variant of `staticFor`
Vindaar Feb 26, 2025
f36f161
[cuda] support func, discard and command nnkCommand
Vindaar Feb 27, 2025
8780afe
[cuda] better handle required semicolons
Vindaar Feb 27, 2025
763f464
[cuda] extract type from `getType` for execution helper
Vindaar Mar 3, 2025
3d59fb9
[cuda] special case `CUdeviceptr` as a type that *must not* be copied
Vindaar Mar 3, 2025
ff6d1f2
[cuda] allow passing in shared memory size for a kernel
Vindaar Mar 3, 2025
3604775
[nvidia ABI] wrap cuModuleGetGlobal, cudaMemcpyKind and a couple more
Vindaar Mar 3, 2025
b907baf
[cuda] support while loops
Vindaar Mar 3, 2025
304373e
[cuda] support void pointers and `nil` literals
Vindaar Mar 3, 2025
b84c27d
[cuda] refactor out module loading from execution
Vindaar Mar 3, 2025
3fe306c
[cuda] store PTX before echoeing it
Vindaar Mar 3, 2025
8b4873e
[cuda] add `copyToSymbol` helper to copy to constant symbol in CUDA code
Vindaar Mar 3, 2025
529539e
[cuda] generalize `volatile` annotation to support other pragmas
Vindaar Mar 3, 2025
8dcd58f
[cuda] `cudaName` pragma for custom name for a proc, eg __syncthreads
Vindaar Mar 3, 2025
0a1da24
[cuda] support float literals
Vindaar Mar 3, 2025
f4c71a6
[cuda] map arrays of explicit length 0 to `[]` arrays in CUDA
Vindaar Mar 3, 2025
aabd3bd
[cuda] explicitly support constants, mapped to `__constant__`
Vindaar Mar 3, 2025
eb99ff6
[cuda] minor cleanup
Vindaar Mar 3, 2025
bc5b2f0
[cuda] add `gridDim`, `cuExtern` and `share` + device malloc/free
Vindaar Mar 3, 2025
0900bc0
force compilation with `-d:CTT_32` for the moment
Vindaar Mar 3, 2025
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
Prev Previous commit
Next Next commit
[cuda] better logic for detection of type names
Vindaar committed Feb 26, 2025

Verified

This commit was signed with the committer’s verified signature.
Vindaar Sebastian
commit d5095834dec7916efb357021d5b28830a3ec789f
13 changes: 11 additions & 2 deletions constantine/math_compiler/experimental/nim_ast_to_cuda_ast.nim
Original file line number Diff line number Diff line change
@@ -265,6 +265,13 @@ constant to force the Nim compiler to bind the symbol.
doAssert n[1][1].intVal == 0, "No is: " & $n.treerepr
result = n[1][2].intVal + 1

proc getTypeName(n: NimNode): string =
## Returns the name of the type
case n.kind
of nnkIdent, nnkSym: result = n.strVal
of nnkObjConstr: result = n.getTypeInst.strVal
else: raiseAssert "Unexpected node in `getTypeName`: " & $n.treerepr

proc parseTypeFields(node: NimNode): seq[GpuTypeField]
proc nimToGpuType(n: NimNode): GpuType =
## Maps a Nim type to a type on the GPU
@@ -294,7 +301,8 @@ proc nimToGpuType(n: NimNode): GpuType =
of ntyObject:
let impl = n.getTypeImpl
let flds = impl.parseTypeFields()
result = initGpuObjectType(n.strVal, flds)
let typName = getTypeName(n) # might be an object construction
result = initGpuObjectType(typName, flds)
of ntyArray:
# For a generic, static array type, e.g.:
# BracketExpr
@@ -613,7 +621,8 @@ proc toGpuAst(ctx: var GpuContext, node: NimNode): GpuAst =
result = GpuAst(kind: gpuTypeDef, tName: node[0].strVal)
result.tFields = parseTypeFields(node[2])
of nnkObjConstr:
result = GpuAst(kind: gpuObjConstr, ocName: node[0].strVal)
let typName = getTypeName(node)
result = GpuAst(kind: gpuObjConstr, ocName: typName)
# get all fields of the type
let flds = node[0].getTypeImpl.parseTypeFields() # sym
# find all fields that have been defined by the user