Versions Compared

Key

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

Intro

DeltaShell provides an implementation to run services remote (in another process / on another machine). The most common usage is to load 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. Another reason could be that you only have a 32bit version of a dll and want to run it from a 64bit process (Delta Shell).

...

What is typically done to work around these issues, is that for each model-run a new 'worker' process is spawned. That worker process then loads the native dll, does the model calculations and then exits. For subsequent runs a new process is launched, thus making sure each run is 'clean'. To allow for interaction during the run, the DeltaShell process and the worker process need to communicate, which is called 'remoting'. The worker process may run on the local machine, but could also run on another machine.

Solution

To hide all the complexities of remoting, Delta Shell provides a few simple methods to work with a 'remote' version of your api. Typically you will have to change very little in your model code if you already have a well defined interface for your api.

...

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 dlls as well).

...

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.

Performance considerations

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.

...