// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Runtime.InteropServices;
namespace Microsoft.ML.OnnxRuntime
{
///
/// This class allows to specify global thread pool options
/// when instantiating the ONNX Runtime environment for the first time.
///
public class OrtThreadingOptions : SafeHandle
{
///
/// A pointer to a underlying native instance of ThreadingOptions
///
internal IntPtr Handle => handle;
///
/// Create an empty threading options.
///
public OrtThreadingOptions() : base(IntPtr.Zero, true)
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtCreateThreadingOptions(out handle));
}
///
/// Set global inter-op thread count.
/// Setting it to 0 will allow ORT to choose the number of threads used for parallelization of
/// multiple kernels. Setting it to 1 will cause the main thread to be used (i.e., no thread pools will be used).
/// This setting is only meaningful when the execution mode is set to Parallel.
///
/// The number of threads.
public int GlobalInterOpNumThreads
{
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtThreadingOptionsSetGlobalInterOpNumThreads(handle, value));
}
}
///
/// Sets the number of threads available for intra-op parallelism (i.e. within a single op).
/// Setting it to 0 will allow ORT to choose the number of threads, setting it to 1 will cause the main thread to be used (i.e., no thread pools will be used).
///
/// The number of threads.
public int GlobalIntraOpNumThreads
{
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtThreadingOptionsSetGlobalIntraOpNumThreads(handle, value));
}
}
///
/// Allows spinning of thread pools when their queues are empty in anticipation of imminent task arrival.
/// This call sets the value for both inter-op and intra-op thread pools.
/// If the CPU usage is very high then do not enable this.
///
/// If true allow the thread pools to spin.
public bool GlobalSpinControl
{
set
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtThreadingOptionsSetGlobalSpinControl(handle, value ? 1 : 0));
}
}
///
/// When this is set it causes intra-op and inter-op thread pools to flush denormal values to zero.
///
public void SetGlobalDenormalAsZero()
{
NativeApiStatus.VerifySuccess(NativeMethods.OrtThreadingOptionsSetGlobalDenormalAsZero(handle));
}
///
/// Overrides SafeHandle.ReleaseHandle() to properly dispose of
/// the native instance of ThreadingOptions
///
/// always returns true
protected override bool ReleaseHandle()
{
NativeMethods.OrtReleaseThreadingOptions(handle);
handle = IntPtr.Zero;
return true;
}
///
/// Overrides SafeHandle.IsInvalid
///
/// returns true if handle is equal to Zero
public override bool IsInvalid => handle == IntPtr.Zero;
}
}