From ecf50aada220a053fc005a751e393a11c4f6465d Mon Sep 17 00:00:00 2001
From: Carl Philipp Klemm <philipp@uvos.xyz>
Date: Mon, 8 Apr 2024 11:59:23 +0200
Subject: [PATCH] Inital fully operational version

Use exceptions to propagate backend failures
---
 kissinference.cs | 21 ++++++++++++++++-----
 program.cs       | 12 +++++++++---
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/kissinference.cs b/kissinference.cs
index 3d30a72..0908cdc 100644
--- a/kissinference.cs
+++ b/kissinference.cs
@@ -17,9 +17,16 @@ public struct VersionFixed
 	public static extern VersionFixed GetVersion();
 }
 
+public class InferenceException : Exception
+{
+	public InferenceException(){}
+	public InferenceException(string message): base(message){}
+	public InferenceException(string message, Exception inner): base(message, inner){}
+}
+
 public class Network
 {
-	public delegate void ResultDlg(float[] result);
+	public delegate void ResultDlg(float[] result, Network network);
 
 	private struct Flight
 	{
@@ -39,7 +46,7 @@ public class Network
 		Capi.free(result);
 		var flight = inflight[data];
 		inflight.Remove(data);
-		flight.Callback(managedResult);
+		flight.Callback(managedResult, this);
 		flight.Signal.Set();
 	}
 
@@ -59,7 +66,10 @@ public class Network
 		flight.Callback = callback;
 		flight.Signal = new AutoResetEvent(false);
 		inflight.Add(flightcounter, flight);
-		Capi.kiss_async_run_inference_complex(ref net, spectra.Real, spectra.Imaginary, flightcounter);
+		byte ret = Capi.kiss_async_run_inference_complex(ref net, spectra.Real, spectra.Imaginary, flightcounter);
+		if(ret == 0)
+			throw new InferenceException(getError());
+
 		flightcounter += 1;
 		return flight.Signal;
 	}
@@ -72,7 +82,9 @@ public class Network
 		flight.Callback = callback;
 		flight.Signal = new AutoResetEvent(false);
 		inflight.Add(flightcounter, flight);
-		Capi.kiss_async_run_inference(ref net, data, flightcounter);
+		byte ret = Capi.kiss_async_run_inference(ref net, data, flightcounter);
+		if(ret == 0)
+			throw new InferenceException(getError());
 		flightcounter += 1;
 		return flight.Signal;
 	}
@@ -120,7 +132,6 @@ public class Network
 			return Capi.IntPtrToUtf8Array(net.outputLabels);
 		}
 	}
-
 }
 
 public class Spectra
diff --git a/program.cs b/program.cs
index d5ec3de..eb5e0ec 100644
--- a/program.cs
+++ b/program.cs
@@ -9,15 +9,21 @@ public class Program
 			Console.WriteLine("{0:F}+{1:F}i", spectra.Real[i], spectra.Imaginary[i]);
 	}
 
-	public static void Result(float[] result)
+	public static void Result(float[] result, Kiss.Network network)
 	{
+		Console.WriteLine("Thread id: {0:D}", System.Environment.CurrentManagedThreadId);
+
 		Console.WriteLine("Got result");
+		string[] outputLabels = network.OutputLabels;
+		for(int i = 0; i < result.Length; ++i)
+			Console.WriteLine("{0:S}: {1:F}", outputLabels[i], result[i]);
 	}
 
 	public static int Main(string[] args)
 	{
 		Kiss.VersionFixed version = Kiss.VersionFixed.GetVersion();
 		Console.WriteLine("libkissinference version: {0:D}.{1:D}.{2:D}", version.Major, version.Minor, version.Patch);
+		Console.WriteLine("Thread id: {0:D}", System.Environment.CurrentManagedThreadId);
 
 		var spectra = new Kiss.Spectra(Kiss.Utils.CreateRange(1, 10, 10, true), Kiss.Utils.CreateRange(1, 10, 10, true), Kiss.Utils.CreateRange(1, 10, 10, true));
 
@@ -43,7 +49,7 @@ public class Program
 
 		try
 		{
-			var net = new Kiss.Network(filename, true);
+			var net = new Kiss.Network(filename, false);
 			Console.WriteLine("Input size: {0:D} OutputSize: {1:D} Purpose: {2:S} InputLabel: {3:S}", net.InputSize, net.OutputSize, net.Purpose, net.InputLabel);
 			spectra.Resample(net.InputSize/2);
 			Console.WriteLine("Expanded spectra:");
@@ -52,7 +58,7 @@ public class Program
 			signal.WaitOne();
 			Console.WriteLine("Awaited");
 		}
-		catch (System.IO.FileLoadException e)
+		catch(System.IO.FileLoadException e)
 		{
 			Console.WriteLine(e.Message);
 		}
-- 
GitLab