// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace Microsoft.ML.OnnxRuntime
{
///
/// Represents a synchronization primitive for stream operations.
///
public class OrtSyncStream : SafeHandle
{
internal OrtSyncStream(IntPtr streamHandle)
: base(IntPtr.Zero, true) // Provide required arguments to SafeHandle constructor
{
handle = streamHandle;
}
///
/// Fetch sync stream handle for possible use
/// in session options.
///
/// Opaque stream handle
public IntPtr GetHandle()
{
return NativeMethods.OrtSyncStream_GetHandle(handle);
}
internal IntPtr Handle => handle;
///
/// Implements SafeHandle interface
///
public override bool IsInvalid => handle == IntPtr.Zero;
///
/// Implements SafeHandle interface to release native handle
///
/// always true
protected override bool ReleaseHandle()
{
NativeMethods.OrtReleaseSyncStream(handle);
handle = IntPtr.Zero;
return true;
}
}
///
/// Represents the combination of an execution provider and a hardware device
/// that the execution provider can utilize.
///
public class OrtEpDevice
{
///
/// Construct an OrtEpDevice from an existing native OrtEpDevice instance.
///
/// Native OrtEpDevice handle.
internal OrtEpDevice(IntPtr epDeviceHandle)
{
_handle = epDeviceHandle;
}
internal IntPtr Handle => _handle;
///
/// The name of the execution provider.
///
public string EpName
{
get
{
IntPtr namePtr = NativeMethods.OrtEpDevice_EpName(_handle);
return NativeOnnxValueHelper.StringFromNativeUtf8(namePtr);
}
}
///
/// The vendor who owns the execution provider.
///
public string EpVendor
{
get
{
IntPtr vendorPtr = NativeMethods.OrtEpDevice_EpVendor(_handle);
return NativeOnnxValueHelper.StringFromNativeUtf8(vendorPtr);
}
}
///
/// Execution provider metadata.
///
public OrtKeyValuePairs EpMetadata
{
get
{
return new OrtKeyValuePairs(NativeMethods.OrtEpDevice_EpMetadata(_handle));
}
}
///
/// Execution provider options.
///
public OrtKeyValuePairs EpOptions
{
get
{
return new OrtKeyValuePairs(NativeMethods.OrtEpDevice_EpOptions(_handle));
}
}
///
/// The hardware device that the execution provider can utilize.
///
public OrtHardwareDevice HardwareDevice
{
get
{
IntPtr devicePtr = NativeMethods.OrtEpDevice_Device(_handle);
return new OrtHardwareDevice(devicePtr);
}
}
///
/// The OrtMemoryInfo instance describing the memory characteristics of the device.
///
/// memory type requested
///
public OrtMemoryInfo GetMemoryInfo(OrtDeviceMemoryType deviceMemoryType)
{
IntPtr memoryInfoPtr = NativeMethods.OrtEpDevice_MemoryInfo(_handle, deviceMemoryType);
return new OrtMemoryInfo(memoryInfoPtr, /* owned= */ false);
}
///
/// Creates a synchronization stream for operations on this device.
/// Can be used to implement async operations on the device such as
/// CopyTensors.
///
/// stream options can be null
///
public OrtSyncStream CreateSyncStream(IReadOnlyDictionary streamOptions)
{
OrtKeyValuePairs options = null;
IntPtr optionsHandle = IntPtr.Zero;
try
{
if (streamOptions != null)
{
options = new OrtKeyValuePairs(streamOptions);
optionsHandle = options.Handle;
}
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateSyncStreamForEpDevice(_handle,
optionsHandle, out IntPtr syncStream));
return new OrtSyncStream(syncStream);
}
finally
{
options?.Dispose();
}
}
private readonly IntPtr _handle;
}
}