Quantcast
Channel: R.NET
Viewing all articles
Browse latest Browse all 1634

Closed Issue: Periodic parse error when static wrapper class is used in multitask project (multithread) [79]

$
0
0
Hello. I have strange error in my code, which I get episodically: "An exception of type 'RDotNet.ParseException' occurred in RDotNet.dll but was not handled in user code".
Sometimes code works correct, sometimes - not.
Console gave me this message "Error: C stack usage is too close to the limit".

This is my dummy code (I cut it from my other big project, where I use R.NET) - as an example.

```
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RDotNet;
using RDotNet.Devices;
using RDotNet.Dynamic;
using RDotNet.Internals;
using RDotNet.NativeLibrary;
using System.IO;
using System.Threading;
using System.Collections.Generic;


namespace R_Test_Project
{

public static class RNetFactory
{
public static SemaphoreSlim augmentedRNetLocker = new SemaphoreSlim(1, 1);

private static string envPath = Environment.GetEnvironmentVariable("PATH");
private static string rBinPath = @"C:\Program Files\R\R-3.0.2\bin\x64";
private static REngine engine;

static RNetFactory()
{
Environment.SetEnvironmentVariable("PATH", envPath + Path.PathSeparator + rBinPath);
engine = REngine.CreateInstance("RDotNet");

#region R Initialize

engine.Initialize();

engine.Evaluate(@"C:\Program Files\R\R-3.0.2\library");
engine.Evaluate("library(signal)");
engine.Evaluate("library(dlm)");
engine.Evaluate("library(circular)");
engine.Evaluate("library(fume)");
engine.Evaluate("library(stats)");
engine.Evaluate("library(MASS)");
engine.Evaluate("library(nlme)");
engine.Evaluate("library(astsa)");
engine.Evaluate("library(wmtsa)");

engine.Evaluate("gc(verbose=FALSE)");

#endregion
}

public static void SetAndExecute(string RNETExpression, NumericVector dataVector, string RNETVectorName)
{
SetVector(dataVector, RNETVectorName);
Execute(RNETExpression);
}

public static void SetVector(NumericVector dataVector, string RNETVectorName)
{
NumericVector vec = engine.CreateNumericVector(dataVector.Select<double, double>(x => { return (double)x; }));
engine.SetSymbol(RNETVectorName, vec);
}

public static void Execute(string RNETExpression)
{
engine.Evaluate(RNETExpression);
}

public static NumericVector GetVector(string RVectorName)
{
return engine.GetSymbol(RVectorName).AsNumeric();
}
}
class Kalman
{
private NumericVector randomVector;
public NumericVector DynamicVectorCreation()
{
RNetFactory.Execute("vecLength <- as.integer(5000 * runif(1, min = 0, max = 1)) + 50");
RNetFactory.Execute("data <- rnorm(vecLength, 0, 1)");

return RNetFactory.GetVector("data");
}

public NumericVector KalmanExecution(NumericVector rawDataVector)
{
RNetFactory.SetVector(rawDataVector, "rawSeries");
RNetFactory.Execute("mu0 <- 0; sigma0 <- 1000000; phi <- 1; cQ <- 1; cR <- 1");
RNetFactory.Execute("ks <- Ksmooth0(length(rawSeries), rawSeries, 1, mu0, sigma0, phi, cQ, cR)");
RNetFactory.Execute("kalmanSmoothedSeries <- as.vector(ks$xs)");

return RNetFactory.GetVector("kalmanSmoothedSeries");
}
}

class Program
{


static void Main(string[] args)
{
List<Task> allTasks = new List<Task>();
for (int i=0; i<50; i++)
{
allTasks.Add(new Task(() =>
{
var kalman = new Kalman();

RNetFactory.augmentedRNetLocker.Wait();
var rawData = kalman.DynamicVectorCreation();
RNetFactory.augmentedRNetLocker.Release();

Task.Delay(30);

RNetFactory.augmentedRNetLocker.Wait();
var refinedData = kalman.KalmanExecution(rawData);
RNetFactory.augmentedRNetLocker.Release();
}));

allTasks.Last().Start();
}

Task.WaitAll(allTasks.ToArray());
}
}
}
```

Viewing all articles
Browse latest Browse all 1634

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>