Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

DeltaShell provides an implementation to run services remote (in another process / on another machine). The most common usage is to load and the calculation kernel (typically fortran) of a model in a separate process to increase DeltaShell stability and prevent memory corruption issues when running a model several times.

...

One of the disadvantages of this approach is that if a native dll is loaded using DllImport, it will never be unloaded until the process (DeltaShell) existsexits. As a consequence, any memory leaks in the native dll will build up in the parent process, omissions in re-initialization may cause model corruption, or in general: previous runs may affect the current run. Most notably, when the dlls dll aborts / causes an access violation, it will instantly crash the entire parent process (eg DeltaShell) without chance of recovery.

...

There is one extra step you have to take to clean things up when you're done with the run:

Code Block
languagecsharp
RemoteInstanceContainer.RemoveInstance(api);

This will tell DeltaShell to end the worker process and cleanup any resources. After this the api variable is no longer usable. Next run you should call CreateInstance again.

For examples see the following usages in DeltaShell: IModelApi, IRRModelApi, IXBeachApi, PcRasterModel or RemoteInstanceContainerTest

Requirements on the interface

The interface you use to communicate (IMyModelApi in the example above) should follow a few basic rules to work smoothly with the remoting implementation. Any interface will do: no special attributes are required, but you should try to use mostly simple (value) types (as is common when defining an interface of a native dlldlls as well).

Default supported types:

  • all primitive types (int, double, string, etc)
  • enums
  • NOT decimal
  • Wiki Markup
    double\[\], bool\[\], int\[\], short\[\], float\[\], byte\[\]
  • 'Array' if of above type at runtime!
  • ref/out keywords
  • Type (through type converter)
  • DateTime (through type converter)
  • ..more through type converters
  • ..more support can be added in the code, ask!

To support custom classes a little bit of extra work is required. You can either annotate the class with the required attributes for serialization (see below), or you can introduce a type converter which does custom serialization (see DateTimeToProtoConverter.cs). The latter is useful if you do not have access to the source code (eg, a .NET object) or do not which to mix remoting and domain.

...

The attributes come from the protobuf .net library and the annotation should be done according to their specification. In short; the class should have the ProtoContract attribute and each member you want to serialize to the other process should have the ProtoMember attribute with a unique number. Only non primitive or proto-annotated classes can be members. Also the class should have a default constructor.

...

Running code in another process obviously has some performance overhead when compared to running it in the same process. The overhead comes from starting the other process, intercepting the calls, serializing the call & data into bytes, sending the information to the other process, decoding the information on the other end, doing the work, serializing the resulting data into bytes, sending the data back, and then decoding the result value(s). Although the absolute time to do all this work is actually quite small (say, 1 millisecond per call), it may significantly impact your performance depending on how many calls you are making versus the amount of work being done per call. Also if your parameters or result values are large (eg, arrays of data), the time it takes to serialize and deserialize the data increases and/or could lead to increased memory overhead. When running the remote instance on the same machine, DeltaShell counters this problem by using a technique called 'Shared Memory' to transfer large arrays more efficiently. In short; it doesn't serialize the array, instead it just memcpy's it between processes. This happens automatically when the array size exceeds some threshold and requires no additional configuration. It only works for one dimensional arrays however and also not for arrays defined inside custom types.

...