OrtApi Structure Reference
TheOrtApi structure contains function pointers for all ONNX Runtime C API operations. Access it through the OrtApiBase.
Accessing the API
API Version
GetApi() to ensure compatibility.
Status and Error Handling
CreateStatus
code: Error code fromOrtErrorCodeenummsg: Null-terminated error message (will be copied)
OrtStatus object (must be freed with ReleaseStatus)
Example:
GetErrorCode
status: Status object to query
OrtErrorCode value
Error Codes:
GetErrorMessage
status: Status object to query
ReleaseStatus
status: Status object to free (can be NULL)
Environment Management
CreateEnv
log_severity_level: Minimum severity level for log messageslogid: Identifier for loggingout: Newly created environment (must be freed withReleaseEnv)
NULL on success, error status otherwise
Logging Levels:
CreateEnvWithCustomLogger
logging_function: Custom logging callbacklogger_param: User data passed to logging callbacklog_severity_level: Minimum severity levellogid: Log identifierout: Newly created environment
CreateEnvWithGlobalThreadPools
log_severity_level: Minimum severity levellogid: Log identifiertp_options: Threading options for global thread poolsout: Newly created environment
DisablePerSessionThreads to share thread pools across sessions.
ReleaseEnv
env: Environment to free (can be NULL)
Session Options
CreateSessionOptions
options: Newly created session options (must be freed withReleaseSessionOptions)
NULL on success
SetIntraOpNumThreads
options: Session options to modifyintra_op_num_threads: Number of threads (0 = use default)
SetInterOpNumThreads
options: Session options to modifyinter_op_num_threads: Number of threads (0 = use default)
SetSessionGraphOptimizationLevel
options: Session options to modifygraph_optimization_level: Optimization level
SetSessionExecutionMode
options: Session options to modifyexecution_mode: Execution mode
SetOptimizedModelFilePath
options: Session optionsoptimized_model_filepath: Path where optimized model will be saved
EnableProfiling
options: Session optionsprofile_file_prefix: Prefix for the profile data file
DisableProfiling
EnableMemPattern
DisableMemPattern
EnableCpuMemArena
DisableCpuMemArena
AddSessionConfigEntry
options: Session optionsconfig_key: Configuration key (null-terminated)config_value: Configuration value (null-terminated)
onnxruntime_session_options_config_keys.h for valid keys and values.
ReleaseSessionOptions
Session Creation and Management
See Session Management page for detailed session-related functions.Tensor and Value Operations
See Tensor Operations page for tensor creation and manipulation functions.Execution Provider Configuration
See Execution Providers page for provider-specific configuration functions.Memory Management
GetAllocatorWithDefaultOptions
out: Pointer to the default allocator (do not free)
NULL on success
Example:
CreateAllocator
session: Session to create allocator formem_info: Memory info specifying allocation behaviorout: Newly created allocator (must be freed withReleaseAllocator)
ReleaseAllocator
CreateAllocator.
Threading Options
CreateThreadingOptions
out: Newly created threading options (must be freed withReleaseThreadingOptions)
SetGlobalIntraOpNumThreads
CreateEnvWithGlobalThreadPools.
Parameters:
tp_options: Threading optionsintra_op_num_threads: Number of threads (0 = default, 1 = use calling thread)
SetGlobalInterOpNumThreads
SetGlobalSpinControl
tp_options: Threading optionsallow_spinning: 0 = don’t spin (recommended for high CPU usage), 1 = spin
ReleaseThreadingOptions
Utility Functions
GetAvailableProviders
out_ptr: Array of provider name strings (must be freed withReleaseAvailableProviders)provider_length: Number of providers in the array
ReleaseAvailableProviders
GetAvailableProviders.