How to use memmapfiles safely for inter-process communication?
Show older comments
I am interested in using memory mapped files to implement inter-process communication between a Matlab process and a foreign process. Portability (Windows / Linux) is a concern, but my main concern is reliability.
Looking at the example in Share Memory Between Applications, I am surprised the code is that simple. Does the code actually work? The shared byte m.Data(1) controls which process is allowed to access the shared data, but m.Data(1) itself doesn't seem to be protected against data races. To implement this example in C++, one would typically add some synchronization object, either a locking one (mutex, semaphore or condition variable) or a lock-free one (involving some kind of memory barrier). The Boost library provides some good examples of such mechanisms.
Can we use such synchronization objects with Matlab's memmapfiles? Or is there some kind of magic the Matlab compiler adds behind the scenes, that makes my concern pointless?
Edit: I am specifically concerned by the compiled code of this example.
Answers (1)
Philip Borghesani
on 8 Jun 2017
0 votes
In MATLAB on Intel (x86) platforms I believe this code is safe and will work correctly. Because the communication process is token based and designed to only work between two processes a simple mechanism is possible. If this was done in C with a memory mapped file the only change needed would be to declare the first memory location atomic.
Also note that this is not the most efficient way to do this type of thing. The sleep calls dictate the maximum call throughput and the poling mechanism is inefficient. Doing this with one or two mex files and proper inter-process communication would have much better performance.
11 Comments
Igor Dimitrijevic
on 9 Jun 2017
Philip Borghesani
on 9 Jun 2017
MATLAB will not optimize a matrix read operation on a memmaped objects data so there is no need for special handling. In c the atomic would be needed to prevent the poling loop from being optimized out or having the data element placed in a register.
This is a simple example because there are well defined ownership/writing rules and only two process. If you tried to have multiple processing threads then this solution would break down.
I am not sure what you mean by complied code. Are you planning on using MATLAB coder to produce a mex file or the MATLAB Compiler to produce an application?
Igor Dimitrijevic
on 12 Jun 2017
Philip Borghesani
on 12 Jun 2017
That link is correct and theoretically the MATLAB code could have a problem due to out of order execution however given the amount of code that most likely executes between the reads in typical MATLAB code execution I feel that encountering a problem is somewhere between very unlikely and never going to happen. The optimized c code in that example is a completely different situation. In the future it is possible that MATLAB or processor changes could effect the odds...
The MATLAB compiler runs code identically to how it runs in MATLAB so it's use has no effect on the answer.
Igor Dimitrijevic
on 13 Jun 2017
Walter Roberson
on 13 Jun 2017
There is a confusion here between MATLAB Compiler and MATLAB Coder.
MATLAB Compiler is MATLAB without the desktop or command prompt, so any MATLAB behavior will be the same under MATLAB Compiler. MATLAB Compiler builds threaded data structures that are interpreted at run time -- it is, in that way, much the same as pcode, all the tokens parsed into data structures. MATLAB Compiler does not generate C/C++ code. There is no possibility of instructions being reordered compared to what MATLAB itself would use.
MATLAB Coder generates C/C++ code, and there are risks with that in the area of optimization and reordering.
Igor Dimitrijevic
on 14 Jun 2017
Walter Roberson
on 14 Jun 2017
"But what about reordering at execution time (out-of-order execution)? Does the MATLAB compiler add some stuff behind the scenes to prevent reordering at execution time?"
No, the MATLAB Compiler does not add anything. It is exactly the same execution engine as MATLAB itself. If MATLAB gets the order right then MATLAB Compiler gets the order right.
MATLAB only reorders execution for some combinations of mathematical operations that it recognizes as matching the LINPACK or BLAS patterns or MKL, and then only when the operations are actually dispatched to those libraries. For example, A + B.*C could potentially get dispatched through a Multiply and Add routine that was technically B .* C + A, and that libraries in turn might convert that into the SSE2 version of Fused Multiply and Add using SIMD for up to 8 fused operations per instruction. It could happen. But it isn't going to reorder your test of the synchronization value, for example.
Philip Borghesani
on 14 Jun 2017
If I write "if (data[0]=value) then b=data[1]" in c then that could end up being only a few assembly statements and it is quite possible that a processor could execute the fetch for data 1 before the fetch for data[0]. If I write the equivalent statements in MATLAB there will be more intervening instructions, and calls, between the two operations then the size of the execution pipeline and probably more then the instruction decode pipeline such that out of order execution of the two MATLAB statements is impossible for the processor.
It is conceivable that there could be problems if you did mydata=m.Data and then accessed mydata(0) and mydata(1) but m.data must go through an overloaded subsref operation.
Walter Roberson
on 14 Jun 2017
If you were to access m.data(I,J) with vector I or J then the order of the accesses to the elements is not defined and could be different for large enough arrays (because the pattern of copying out to call the high performance libraries could be different)
Igor Dimitrijevic
on 15 Jun 2017
Categories
Find more on Get Started with MATLAB Coder in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!