From 9d14cbdb1a1261e7f8bcd46c3d8e9132e529f9fe Mon Sep 17 00:00:00 2001 From: jignparm Date: Thu, 21 Feb 2019 11:34:44 -0800 Subject: [PATCH] Throw friendly error message when Linux distribution has libc version < 2.23 (#493) * Add check for linux version supporting glibc 2.23 or higher * Refactor the libc check to SessionOptions * removed whitespace * Update SessionOptions.cs --- .../SessionOptions.cs | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs index df7d12ec5e..607f98b77a 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/SessionOptions.cs @@ -48,6 +48,7 @@ namespace Microsoft.ML.OnnxRuntime private static SessionOptions MakeSessionOptionWithCpuProvider() { + CheckLibcVersionGreaterThanMinimum(); SessionOptions options = new SessionOptions(); NativeMethods.OrtSessionOptionsAppendExecutionProvider_CPU(options._nativePtr, 1); return options; @@ -69,6 +70,7 @@ namespace Microsoft.ML.OnnxRuntime /// A SessionsOptions() object configured for execution on deviceId public static SessionOptions MakeSessionOptionWithCudaProvider(int deviceId=0) { + CheckLibcVersionGreaterThanMinimum(); CheckCudaExecutionProviderDLLs(); SessionOptions options = new SessionOptions(); NativeMethods.OrtSessionOptionsAppendExecutionProvider_CUDA(options._nativePtr, deviceId); @@ -103,6 +105,32 @@ namespace Microsoft.ML.OnnxRuntime return true; } + [DllImport("libc", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] + private static extern IntPtr gnu_get_libc_version(); + + private static void CheckLibcVersionGreaterThanMinimum() + { + // require libc version 2.23 or higher + var minVersion = new Version(2, 23); + var curVersion = new Version(0, 0); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) + { + try + { + curVersion = Version.Parse(Marshal.PtrToStringAnsi(gnu_get_libc_version())); + if (curVersion >= minVersion) + return; + } + catch (Exception) + { + // trap any obscure exception + } + throw new OnnxRuntimeException(ErrorCode.RuntimeException, + $"libc.so version={curVersion} does not meet the minimun of 2.23 required by OnnxRuntime. " + + "Linux distribution should be similar to Ubuntu 16.04 or higher"); + } + } + #region destructors disposers ~SessionOptions()