Commit 9cfd28c
authored
Nvidia target via NVRTC and Nim ↦ CUDA DSL (#487)
* commit the initial Nim ⇒ CUDA DSL, NVRTC & CUDA execution helpers
* [tests/examples] add example for a BigInt modular addition
* remove `nimcuda` dependency, wrap everything we need manually
* merge `execCuda` logic for LLVM & NVRTC
IMPORTANT NOTE:
For LLVM we generate `array_t` types for the finite field elements. By
doing this we make it impossible to just copy over a Constantine
finite field element or elliptic curve point (which are also an
`array_t` type). Therefore, we have a `CUfunctionLLVM` type, which is
used to differentiate between different `execCuda` calls, based on
their "origin" (i.e. LLVM or NVRTC backends).
Based on that backend we either allow passing simple structs by their
host pointer or force a copy.
* add `quitOnFailure` for the `check` calls to avoid regression
This was added for a reason after all in 5d66b52
* [tests] turn big int `modadd` example into real test
* copy `libpaths.nim` over from nimcuda
* [cuda] add partial support for `const` in CUDA generator
* [cuda] fix minor issue in `if` statements in CUDA generator
* [cuda] add support for named blocks
* [cuda] add support for `bool`
* [cuda] remove unnecessary semicolon
* [cuda] add support for `{.volatile.}` variables
* [nvrtc] add `modadd`, `modsub`, `mtymul` implementations using inline PTX
* [cuda] support basic type conversions
By mapping them to a regular cast.
* [cuda] support `var` parameters in procs
* [cuda] make sure proc body is a block
* [cuda] support boolean / bitwise AND/OR and XOR, NOT
* [cuda] support int32 literals
* [cuda] handle prefix `not`
* [cuda] make sure to pass `array` types by pointer instead of copy
* [nvrtc] add more helpers, add TODO to investigate `slct` calls
* [nvrtc] add many more field arithmetic / bigint operations
```
proc setZero(a: var BigInt) {.device.} =
proc setOne(a: var BigInt) {.device.} =
proc add(r: var BigInt, a, b: BigInt) {.device.} =
proc sub(r: var BigInt, a, b: BigInt) {.device.} =
proc mul(r: var BigInt, a, b: BigInt) {.device.} =
proc ccopy(a: var BigInt, b: BigInt, condition: bool) {.device.} =
proc csetZero(r: var BigInt, condition: bool) {.device.} =
proc csetOne(r: var BigInt, condition: bool) {.device.} =
proc cadd(r: var BigInt, a: BigInt, condition: bool) {.device.} =
proc csub(r: var BigInt, a: BigInt, condition: bool) {.device.} =
proc doubleElement(r: var BigInt, a: BigInt) {.device.} =
proc nsqr(r: var BigInt, a: BigInt, count: int) {.device.} =
proc isZero(r: var bool, a: BigInt) {.device.} =
proc isOdd(r: var bool, a: BigInt) {.device.} =
proc neg(r: var BigInt, a: BigInt) {.device.} =
proc cneg(r: var BigInt, a: BigInt, condition: bool) {.device.} =
proc shiftRight(r: var BigInt, k: uint32) {.device.} =
proc div2(r: var BigInt) {.device.} =
```
[back]
* [tests] add test to pass by pointer and `var`
* [tests] add test case for modadd/sub/mtymul
* [tests] add basic test cases for all new NVRTC operations
* [tests] update modadd/sub, mtymul test for new `getFieldModulus`
* add BabyBear field
* [nvrtc] handle `mtymul` for fields with 1 limb
* [cuda] support nested array types, unpack generic instantiatons
* [cuda] correctly generate ptr to array & ptr to array return types
* [cuda] automatically generate `memcpy` for static array types
Static array types cannot be assigned in C/C++/CUDA. We generate a
`memcpy` call for them.
Obviously this does not work for arrays with structs that have pointer
fields or similar (at least not if you expect to copy the values)
* [nvrtc] use `const` for field modulus and other CT constants
* [cuda] extend error message for non copyable inputs
* [cuda] disable passStructByPointer also for CUDA
Once one wants to launch multiple blocks / threads per block this
approach breaks for slightly larger objects
* [cuda] add `{.nimonly.}` pragma one can use in `cuda` block
This pragma makes us not emit any code for that function. Useful to
call it in the definition of a `const`
* [cuda] better logic for detection of type names
* [cuda] support `const` by mapping it to a `__constant__`
* [cuda] allow type determination from array literal
* [nvrtc] get rid of complexity with custom uint32 constants
* [staticFor] add stepped variant of `staticFor`
* [cuda] support func, discard and command nnkCommand
* [cuda] better handle required semicolons
To fix:
```nim
const code = cuda:
proc sum() {.device.} =
let inputIdx = 0
let rateIdx = 0
if inputIdx > 0 and rateIdx > 0:
discard
return
echo code
```
which previously produced:
```
extern "C" __device__ void sum(){
long long inputIdx = 0;
long long rateIdx = 0;
if (((0 < inputIdx); && (0 < rateIdx);)) {
;
};
return ;
};
```
and now:
```
extern "C" __device__ void sum(){
long long inputIdx = 0;
long long rateIdx = 0;
if (((0 < inputIdx) && (0 < rateIdx))) {
;
};
return ;
};
```
* [cuda] extract type from `getType` for execution helper
* [cuda] special case `CUdeviceptr` as a type that *must not* be copied
This allows the user to e.g. allocate and copy memory to a CUDA device
before calling `execute`.
* [cuda] allow passing in shared memory size for a kernel
* [nvidia ABI] wrap cuModuleGetGlobal, cudaMemcpyKind and a couple more
* [cuda] support while loops
* [cuda] support void pointers and `nil` literals
* [cuda] refactor out module loading from execution
so that one can e.g. copy to a global symbol before execution
* [cuda] store PTX before echoeing it
* [cuda] add `copyToSymbol` helper to copy to constant symbol in CUDA code
* [cuda] generalize `volatile` annotation to support other pragmas
i.e. to write
```cuda
extern shared int foo[];
```
we will now support
```nim
var foo {.cuExtern, shared.}: array[0, int]
```
(the 0 size is the current placeholder on how to designate a `[]`
array from Nim)
* [cuda] `cudaName` pragma for custom name for a proc, eg __syncthreads
Allows to map a proc to a custom name. Useful for names that we can't
write due to Nim limitations (i.e. starting with an underscore or
names that match Nim keywords)
* [cuda] support float literals
* [cuda] map arrays of explicit length 0 to `[]` arrays in CUDA
* [cuda] explicitly support constants, mapped to `__constant__`
One can either define a Nim `const` for a variable that is already a
const at the Nim compile time or use
```nim
var foo {.constant.}: theType
```
if one wishes to copy to the symbol before kernel execution. This is
useful for global constants that are not filled in at CUDA compile
time, but before execution. For example:
```nim
# Filled with `copyToSymbol` at runtime from host!
var rc16 {.constant.}: array[30, array[BABYBEAR_WIDTH, BigInt]]
var matInternalDiagM1 {.constant.}: array[BABYBEAR_WIDTH, BigInt]
var montyInverse {.constant.}: BigInt
```
And in the host code:
```nim
var nvrtc = initNvrtc(CudaCode)
nvrtc.compile()
nvrtc.getPtx()
nvrtc.load()
var p2bb = Poseidon2BabyBear.init()
# copy Poseidon2 constants to CUDA kernel
nvrtc.copyToSymbol("rc16", p2bb.rc16)
nvrtc.copyToSymbol("matInternalDiagM1", p2bb.matInternalDiagM1)
nvrtc.copyToSymbol("montyInverse", p2bb.montyInverse)
```
* [cuda] minor cleanup
* [cuda] add `gridDim`, `cuExtern` and `share` + device malloc/free
* force compilation with `-d:CTT_32` for the moment
Need to finalize the logic of mapping 64 bit limbs to 32 bit limb
constants1 parent 8265585 commit 9cfd28c
15 files changed
Lines changed: 4883 additions & 297 deletions
File tree
- constantine
- math_compiler
- experimental
- named
- platforms
- abis
- tests/gpu
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
74 | | - | |
75 | | - | |
76 | | - | |
77 | | - | |
78 | | - | |
79 | | - | |
80 | | - | |
81 | | - | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
86 | 74 | | |
87 | 75 | | |
88 | 76 | | |
| |||
221 | 209 | | |
222 | 210 | | |
223 | 211 | | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
262 | | - | |
263 | | - | |
264 | | - | |
265 | | - | |
266 | | - | |
267 | | - | |
268 | | - | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
275 | | - | |
276 | | - | |
277 | | - | |
278 | | - | |
279 | | - | |
280 | | - | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
285 | | - | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
290 | | - | |
291 | | - | |
292 | | - | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
315 | | - | |
316 | | - | |
317 | | - | |
318 | | - | |
319 | | - | |
320 | | - | |
321 | | - | |
322 | | - | |
323 | | - | |
324 | | - | |
325 | | - | |
326 | | - | |
327 | | - | |
328 | | - | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
350 | | - | |
351 | | - | |
352 | | - | |
353 | | - | |
354 | | - | |
355 | | - | |
356 | | - | |
357 | | - | |
358 | | - | |
359 | | - | |
360 | | - | |
361 | | - | |
362 | | - | |
363 | | - | |
364 | | - | |
365 | | - | |
366 | | - | |
367 | | - | |
368 | | - | |
369 | | - | |
370 | | - | |
371 | | - | |
372 | | - | |
373 | | - | |
374 | | - | |
375 | | - | |
376 | | - | |
377 | | - | |
378 | | - | |
379 | | - | |
380 | | - | |
381 | | - | |
382 | | - | |
383 | | - | |
384 | | - | |
385 | | - | |
386 | | - | |
387 | | - | |
388 | | - | |
389 | | - | |
390 | | - | |
391 | | - | |
392 | | - | |
393 | | - | |
394 | | - | |
395 | | - | |
396 | | - | |
397 | | - | |
398 | | - | |
399 | | - | |
400 | | - | |
401 | | - | |
402 | | - | |
403 | | - | |
404 | | - | |
405 | | - | |
406 | | - | |
407 | | - | |
408 | | - | |
409 | | - | |
410 | | - | |
411 | | - | |
412 | | - | |
413 | | - | |
414 | | - | |
415 | | - | |
416 | | - | |
417 | | - | |
418 | | - | |
419 | | - | |
420 | | - | |
421 | | - | |
422 | | - | |
423 | | - | |
424 | | - | |
425 | | - | |
426 | | - | |
427 | | - | |
428 | | - | |
429 | | - | |
430 | | - | |
431 | | - | |
432 | | - | |
433 | | - | |
434 | | - | |
435 | | - | |
436 | | - | |
437 | | - | |
438 | | - | |
439 | | - | |
440 | | - | |
441 | | - | |
442 | | - | |
443 | | - | |
444 | | - | |
445 | | - | |
446 | | - | |
447 | | - | |
448 | | - | |
449 | | - | |
450 | | - | |
451 | | - | |
452 | | - | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
470 | | - | |
471 | | - | |
472 | | - | |
473 | | - | |
474 | | - | |
475 | | - | |
476 | | - | |
477 | | - | |
478 | | - | |
479 | | - | |
480 | | - | |
481 | | - | |
482 | | - | |
483 | | - | |
484 | | - | |
485 | | - | |
486 | | - | |
487 | | - | |
488 | | - | |
489 | | - | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | 212 | | |
496 | 213 | | |
497 | 214 | | |
| |||
516 | 233 | | |
517 | 234 | | |
518 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
519 | 242 | | |
520 | 243 | | |
521 | 244 | | |
| |||
592 | 315 | | |
593 | 316 | | |
594 | 317 | | |
595 | | - | |
| 318 | + | |
596 | 319 | | |
597 | 320 | | |
598 | 321 | | |
| |||
617 | 340 | | |
618 | 341 | | |
619 | 342 | | |
620 | | - | |
| 343 | + | |
621 | 344 | | |
622 | | - | |
| 345 | + | |
623 | 346 | | |
624 | 347 | | |
625 | 348 | | |
626 | 349 | | |
627 | | - | |
| 350 | + | |
628 | 351 | | |
629 | | - | |
| 352 | + | |
630 | 353 | | |
631 | 354 | | |
632 | 355 | | |
633 | 356 | | |
634 | | - | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
0 commit comments