cuda.core.Program#
- class cuda.core.Program(
- code: str | bytes | bytearray,
- code_type: SourceCodeType | str,
- options: ProgramOptions | None = None,
Represent a compilation machinery to process programs into
ObjectCode.This object provides a unified interface to multiple underlying compiler libraries. Compilation support is enabled for a wide range of code types and compilation types.
- Parameters:
code (str | bytes | bytearray) – The source code to compile. For C++ and PTX, must be a string. For NVVM IR, can be str, bytes, or bytearray.
code_type (SourceCodeType | str) – The type of source code. Must be one of
"c++","ptx", or"nvvm".options (
ProgramOptions, optional) – Options to customize the compilation process.
Methods
- __init__(*args, **kwargs)#
- close(self)#
Destroy this program.
- compile(
- self,
- target_type: ObjectCodeFormatType | str,
- name_expressions: tuple | list = (),
- logs=None,
- *,
- cache: 'ProgramCacheResource | None' = None,
Compile the program to the specified target type.
- Parameters:
target_type (ObjectCodeFormatType | str) – The compilation target. Must be one of
"ptx","cubin", or"ltoir".name_expressions (tuple | list, optional) – Sequence of name expressions to make accessible in the compiled code. Used for template instantiation and similar cases.
logs (object, optional) – Object with a
writemethod to receive compilation logs. On a cache hit no compilation runs andlogsreceives nothing – callers that rely on log output to confirm a compile happened should compile withoutcache=.cache (
ProgramCacheResource, optional) –If provided, the compiled binary is looked up in
cachevia a key derived from the program’s code, options, andtarget_type. On a hit the cached bytes are wrapped in a freshObjectCode(with the sametarget_typeandProgramOptions.name) and returned without re-compiling; on a miss the compile output is stored as raw bytes (the cache extractsbytes(object_code.code)). Passing a non-emptyname_expressionstogether withcache=raisesValueError: NVRTC populatesObjectCode.symbol_mappingat compile time and that mapping is not carried in the binary the cache stores, so cache hits would silently missget_kernel(name_expression)lookups. Options that require anextra_digest(include_path,pre_include,pch,use_pch,pch_dir, NVVMuse_libdevice=True, or NVRTCoptions.namewith a directory component) raiseValueErrorviamake_program_cache_key(); for those compiles, use the manualmake_program_cache_key(...)pattern directly.cache=is independent ofProgramOptions.no_cache: the former controls this program-level cache (compiled-output reuse across calls), whileno_cacheis forwarded to the Linker to disable its in-process JIT cache for cuLink/nvJitLink. Settingoptions.no_cache=Truedoes not bypasscache=, and vice-versa.
- Returns:
The compiled object code.
- Return type:
Attributes
- backend#
Return this Program instance’s underlying
CompilerBackendType.
- handle#
Return the underlying handle object.
Note
The type of the returned object depends on the backend.
Caution
This handle is a Python object. To get the memory address of the underlying C handle, call
int(Program.handle).
- pch_status#
PCHStatusType | None
PCH creation outcome from the most recent
compile()call.Possible values:
"created"— PCH file was written successfully."not_attempted"— PCH creation was not attempted (e.g. the compiler decided not to, or automatic PCH processing skipped it)."failed"— an error prevented PCH creation.None— PCH was not requested, the program has not been compiled yet, the backend is not NVRTC (e.g. PTX or NVVM), or the NVRTC bindings are too old to report status.
When
create_pchis set inProgramOptionsand the PCH heap is too small,compile()automatically resizes the heap and retries, so"created"should be the common outcome.Note
PCH is only supported for
code_type="c++"programs that use the NVRTC backend. For PTX and NVVM programs this property always returnsNone.