Skip to content

Commit 6d789cb

Browse files
committed
commit squash: add ProgramOptions to Program
1 parent fdc76e8 commit 6d789cb

File tree

8 files changed

+425
-33
lines changed

8 files changed

+425
-33
lines changed

cuda_core/cuda/core/experimental/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
from cuda.core.experimental._device import Device
66
from cuda.core.experimental._event import EventOptions
77
from cuda.core.experimental._launcher import LaunchConfig, launch
8-
from cuda.core.experimental._program import Program
8+
from cuda.core.experimental._program import Program, ProgramOptions
99
from cuda.core.experimental._stream import Stream, StreamOptions

cuda_core/cuda/core/experimental/_program.py

Lines changed: 359 additions & 12 deletions
Large diffs are not rendered by default.

cuda_core/cuda/core/experimental/_stream.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,7 @@ def wait(self, event_or_stream: Union[Event, Stream]):
211211
try:
212212
stream = Stream._init(event_or_stream)
213213
except Exception as e:
214-
raise ValueError(
215-
"only an Event, Stream, or object supporting __cuda_stream__ can be waited"
216-
) from e
214+
raise ValueError("only an Event, Stream, or object supporting __cuda_stream__ can be waited") from e
217215
else:
218216
stream = event_or_stream
219217
event = handle_return(cuda.cuEventCreate(cuda.CUevent_flags.CU_EVENT_DISABLE_TIMING))

cuda_core/cuda/core/experimental/_utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ def check_or_create_options(cls, options, options_description, *, keep_none=Fals
8787
return options
8888

8989

90+
def _handle_boolean_option(option: bool) -> str:
91+
"""
92+
Convert a boolean option to a string representation.
93+
"""
94+
return str(option).lower()
95+
96+
9097
def precondition(checker: Callable[..., None], what: str = "") -> Callable:
9198
"""
9299
A decorator that adds checks to ensure any preconditions are met.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `cuda.core` Release notes
2+
3+
Released on <TODO>, 2024
4+
5+
## Hightlights
6+
- Add ProgramOptions to facilitate the passing of runtime compile options to [Program](#program)
7+
8+
## Limitations
9+
-<TODO>
10+
11+
## Breaking Changes
12+
- The `Program.Compile` method no longer accepts an options argument. Instead, you can optionally pass an instance of `ProgramOptions` to the constructor of `Program`.

cuda_core/examples/saxpy.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import cupy as cp
88

9-
from cuda.core.experimental import Device, LaunchConfig, Program, launch
9+
from cuda.core.experimental import Device, LaunchConfig, Program, ProgramOptions, launch
1010

1111
# compute out = a * x + y
1212
code = """
@@ -29,13 +29,10 @@
2929
s = dev.create_stream()
3030

3131
# prepare program
32-
prog = Program(code, code_type="c++")
32+
program_options = ProgramOptions(std="c++11", gpu_architecture="sm_" + "".join(f"{i}" for i in dev.compute_capability))
33+
prog = Program(code, code_type="c++", options=program_options)
3334
mod = prog.compile(
3435
"cubin",
35-
options=(
36-
"-std=c++11",
37-
"-arch=sm_" + "".join(f"{i}" for i in dev.compute_capability),
38-
),
3936
logs=sys.stdout,
4037
name_expressions=("saxpy<float>", "saxpy<double>"),
4138
)

cuda_core/examples/vector_add.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import cupy as cp
66

7-
from cuda.core.experimental import Device, LaunchConfig, Program, launch
7+
from cuda.core.experimental import Device, LaunchConfig, Program, ProgramOptions, launch
88

99
# compute c = a + b
1010
code = """
@@ -26,15 +26,9 @@
2626
s = dev.create_stream()
2727

2828
# prepare program
29-
prog = Program(code, code_type="c++")
30-
mod = prog.compile(
31-
"cubin",
32-
options=(
33-
"-std=c++17",
34-
"-arch=sm_" + "".join(f"{i}" for i in dev.compute_capability),
35-
),
36-
name_expressions=("vector_add<float>",),
37-
)
29+
program_options = ProgramOptions(std="c++17", gpu_architecture="sm_" + "".join(f"{i}" for i in dev.compute_capability))
30+
prog = Program(code, code_type="c++", options=program_options)
31+
mod = prog.compile("cubin", name_expressions=("vector_add<float>",))
3832

3933
# run in single precision
4034
ker = mod.get_kernel("vector_add<float>")

cuda_core/tests/test_program.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,45 @@
88

99
import pytest
1010

11-
from cuda.core.experimental import Program
1211
from cuda.core.experimental._module import Kernel, ObjectCode
12+
from cuda.core.experimental._program import Program, ProgramOptions
13+
14+
15+
def test_program_with_various_options(init_cuda):
16+
code = 'extern "C" __global__ void my_kernel() {}'
17+
18+
options_list = [
19+
ProgramOptions(ptxas_options="-v"),
20+
ProgramOptions(ptxas_options=["-v", "-O3"]),
21+
ProgramOptions(device_optimize=True, device_debug=True),
22+
ProgramOptions(relocatable_device_code=True, maxrregcount=32),
23+
ProgramOptions(ftz=True, prec_sqrt=False, prec_div=False),
24+
ProgramOptions(fmad=False, use_fast_math=True),
25+
ProgramOptions(extra_device_vectorization=True),
26+
ProgramOptions(dlink_time_opt=True, gen_opt_lto=True),
27+
ProgramOptions(define_macro="MY_MACRO"),
28+
ProgramOptions(define_macro=("MY_MACRO", "99")),
29+
ProgramOptions(define_macro=[("MY_MACRO", "99")]),
30+
ProgramOptions(define_macro=[("MY_MACRO", "99"), ("MY_OTHER_MACRO", "100")]),
31+
ProgramOptions(undefine_macro=["MY_MACRO", "MY_OTHER_MACRO"]),
32+
ProgramOptions(undefine_macro="MY_MACRO", include_path="/usr/local/include"),
33+
ProgramOptions(pre_include="my_header.h", no_source_include=True),
34+
ProgramOptions(builtin_initializer_list=False, disable_warnings=True),
35+
ProgramOptions(restrict=True, device_as_default_execution_space=True),
36+
ProgramOptions(device_int128=True, optimization_info="inline"),
37+
ProgramOptions(no_display_error_number=True),
38+
ProgramOptions(diag_error="1234", diag_suppress="5678"),
39+
ProgramOptions(diag_warn="91011", brief_diagnostics=True),
40+
ProgramOptions(time="compile_time.csv", split_compile=4),
41+
ProgramOptions(fdevice_syntax_only=True, minimal=True),
42+
]
43+
44+
# TODO compile the program once the CI is set up
45+
for options in options_list:
46+
program = Program(code, "c++", options)
47+
assert program.backend == "nvrtc"
48+
program.close()
49+
assert program.handle is None
1350

1451

1552
def test_program_init_valid_code_type():

0 commit comments

Comments
 (0)