I am new to ILNumerics and am investigating using it to replace a .Net assembly we created using the MATLAB compiler SDK.
I wrote a small sample app that uses some of the MATLAB code converted to ILNumerics. The process was relatively simple, and I was able to get up and running pretty quickly.
The app I wrote is a very simple WinForms app (still my goto when I want something quick and dirty - don't judge). It loads a data file and performs a couple of different optimizations - one simple and the other fairly complex. The optimizations run and the results are close to the results I get with the same optimization in MATLAB. The optimization is a bit slower, but I blame that on there not being an implementation of constrained least squares in ILNumerics.
When I run the more complicated optimization directly from the windows thread, my memory usage skyrockets pretty quickly. I know this type of thing is really bad form, but this is just a test application. Still, I wanted to make sure that the problem was caused by running in the UI thread, and not something else in ILNumerics. So I wrote an async version of the function that I was calling from the UI thread, and started calling that instead. This fixed the memory explosion, but it has caused me another problem. After hitting this async function a few times (with a button press on the UI), calls to any ILNumerics functions started to throw exceptions related to heap corruption or writing to protected memory. I won't include all of the code, but here is a snippet showing the difference between my async call and the standard call:
public static Task<CharacterizationData> CharacterizeAsync(this Component comp, Substrate charSubstrate, double assortmentViscosity)
{
return Task.Run(() =>
{
return comp.Characterize(charSubstrate, assortmentViscosity);
});
}
public static CharacterizationData Characterize(this Component comp, Substrate charSubstrate, double assortmentViscosity)
{
if (comp is null)
{
throw new ArgumentNullException(nameof(comp));
}
// Capture all reflectances in a single array.
Array<double> OW = comp.OW;
Array<double> OB = comp.OB;
var hasOB = OB.Length > 0;
Array<double> refSamples = OW;
All of the ILNumerics code is embedded in the synchronous call, so I can't see how calling the async version would cause any cross-thread referencing issues that would lead to heap corruption. The Component and Substrate classes use System.Array objects to store the data, which is why you see some of the members being copied to Array objects within the code.
I've read as much as I can find about the rules around creating and manipulating Array objects and I believe that I am following all rules correctly. The fact that the code runs fine within the UI thread leads me to believe that I have not violated any rules, and I just have some missing knowledge about multithreading with ILNumerics.
Does anyone have any insight into what I might be doing wrong here? Has anyone used ILNumerics in an async call like this?
I wrote a number of additional test functions that perform very simple tasks (no optimizations, just straight Array manipulation), and I'm finding the same thing with these simple functions. If I run them in the UI thread, no problem as long as I haven't run any async functions. But running any of these functions asynchronously will eventually lead to heap corruption or protected memory access violations, no matter where I call the functions (UI thread or asynchronously).