Matlab crashes on library initialize when called from Java - matlab

The setup I have is I'm using a Java application to call native C-code with JNI, which in turn starts up the MATLAB runtime and calls functions on it (I know there are other solutions to call MATLAB methods from Java).
The problem is that the MATLAB engine crashes at some point during the initialization and I don't know what's causing it exactly. The crash causes my jvm to terminate, I assume it's some kind of memory corruption.
The C++ code calling MATLAB functions that is actually crashing is
JNIEXPORT void JNICALL
some_jni_vodoo_initializeLibrary(JNIEnv* env, jclass thisClass) {
try {
if (!mclInitializeApplication(NULL, 0)) {
THROW_EXCEPTION(env, "Could not initialize the application properly.");
return;
}
if (!<library>Initialize()) {
THROW_EXCEPTION(env, "Could not initialize the library.");
return;
}
} ...
The function <library>Initialize() crashes here, the Java error log reads
Stack Trace:
[0] jmi.dll:0x793f4175(0x7934cdca, 1, 0x7937e67c "à;.y`E.y#E.ychar in C:\BUILD_ARE..", 0x792d6a32)
[1] jvm.dll:0x792df9a5(0xc0000005, 0x79356791, 0x4961b400 "Ð\8y", 0x6d8b29de)
[2] jvm.dll:0x792e0431(0x8b515008, 0x70f0e8ce, 0x8b5ffffa, 0xc25d5ec6)
------------------------------------------------------------------------
Fatal Java Exception detected at Fri Apr 30 11:08:08 2010
------------------------------------------------------------------------
Configuration:
MATLAB Version: 7.8.0.347 (R2009a)
MATLAB License: unknown
Operating System: Microsoft Windows Vista
Window System: Version 6.0 (Build 6002: Service Pack 2)
Processor ID: x86 Family 6 Model 10 Stepping 5, GenuineIntel
Virtual Machine: Java is not enabled
Default Encoding: windows-1252
Java is not enabled
I really have no idea what could be wrong. Is there not enough memory from the jvm? I guess the problem is somehow related to Java, since calling the JNI functions from a simple test C++ program works fine...
Thanks

Initial Ideas
The only similar thing I've ever seen is a silent exit from MATLAB due to an invalid license.
Another possibility is the nested Java environments. Since MATLAB expects its own Java environment, perhaps there is some problem here. Edit: See below!
Are you running the MATLAB Engine? Or are you calling separate MATLAB commands using some "system" or "exec" call? This might give a good clue.
Answer
The answer appears below in the comments. It is copied here for readability:
When nesting an invocation of MATLAB's engine in a process that has its own JVM, you must either tell MATLAB to run without a JVM by passing -nojvm to mclInitializeApplication or tell it which JVM to use by setting the global MATLAB_JAVA to the location of the JVM you wish to use, C:\Program Files\Java\jre6 for example.
The first option will disable any functionality in MATLAB that requires the JVM, so the second option is preferable.

Related

TIMESCALEMOD verilator error when attempting to add a new black box in chisel

I'm trying to add a new blackboxed verilog module to the chipyard hardware generation framework and simulate it with verilator.
My changes pass chipyard's scala compilation phase in which the chisel hardware specification is compiled into verilog. However, it appears during the "verilation" process in which that verilog is translated into a C++ executable, I'm encountering an error:
%Error-TIMESCALEMOD: [filename].v:238066:15: Timescale missing on this
module as other modules have it (IEEE 1800-2017 3.14.2.2)
chipyard/sims/verilator/generated-src/. . ./ClockDividerN.sv:8:8: ...
Location of module with timescale
8 | module ClockDividerN #(parameter DIV = 1)(output logic clk_out = 1'b0, input clk_in);
| ^~~~~~~~~~~~~ %Error: Exiting due to 1 error(s)
Searching around, the "timescale" this is referring to appears to be a configuration option for verilator simulations to do with how much time (usually in picoseconds) advances during one step of the simulation.
What's strange is the error claims that this module "ClockDividerN" (also a blackboxed verilog module included in the chipyard generator's vsrc directory) has a timescale, but the verilog source for ClockDividerN does not contain anything that appears to be a timescale.
Likewise, adding a timescale directive to the verilog source I'm trying to integrate produces the same error message. There are some verilator command-line options to do with timescales, but they're difficult to add in in the chipyard framework (it uses a pretty opaque makefile to run verilator).
Any help?
Update: the documentation for handling a TIMESCALEMOD error recommends using the "--timescale" command-line argument, but it turns out chipyard's Makefile for verilator simulations already uses that argument!
When adding your blackbox resources, something has gone wrong. Make sure the addResource path is correct. The TIMESCALEMOD error has nothing to do with this, but the blackbox path being included incorrectly causes that error to go off.

Eclipse remote debugging arm-linux library

I am running:
Eclipse 3.8.1 (Build id: deb build).
On Ubuntu 16.04 LTS in a VM and UbuntuMATE 16.04 LTS on target Raspberry Pi 3.
gdb-multiarch(architecture set to arm in .gdbinit) locally andgdbserver` on the target.
Cross compilation and remote deployment is successful. However, there appears to be some library issue when I run on the remote target. Output from gdb-multiarch below (superfluous text removed):
GNU gdb (Ubuntu 7.11-0ubuntu1) 7.11 Copyright (C) 2016 Free Software
Foundation, Inc. License GPLv3+: GNU GPL version 3 or later
http://gnu.org/licenses/gpl.html This GDB was configured as
"x86_64-linux-gnu".
Program stopped. 0x76fd7a40 in ?? () from
/home/fred/raspberrypi/rootfs/lib/arm-linux-gnueabihf/ld-2.23.so
Execution stops at main. However, it doesn't appear to stop due to a breakpoint. The stop appears to be the result of some library issue possibly and unresolved symbol (or something more serious) resulting in the ??.
I have set sysroot in the .gdbinit file to indicate where the shared libraries can be found. The StepIn, StepOut icons are hi-lighted in Eclipse, and I can read ARM registers if I press pause and see which core is being used to run the process (with process ID)!
After further resume & pause operations a segmentation fault occurs:
Program stopped. 0x76fe2e92 in ?? () from
/home/fred/raspberrypi/rootfs/lib/arm-linux-gnueabihf/ld-2.23.so
Program received signal SIGSEGV, Segmentation fault. 0x76fd905e in ??
() from
/home/fred/raspberrypi/rootfs/lib/arm-linux-gnueabihf/ld-2.23.so
Program terminated with signal SIGSEGV, Segmentation fault. The
program no longer exists.
Any ideas? (I am very much new to Linux as it probably shows.)
Thanks for the questions which have resulted in further exploration below:
In Eclipse I started GDB by selecting Debug Configuration and then choosing the remote configuration that I had set up.
The code is very simple, consisting of a stream operator to output some text and then a loop, however it is never reached. I think I have just managed to reproduce the issue from the command line. Incidentally I started the target going first. (Again superfluous text was removed for clarity.)
gdb-multiarch Hello_Raspberry_Pi
Reading symbols from Hello_Raspberry_Pi...done.
The target architecture is assumed to be arm
(gdb) target remote ubuntumate-pi
(gdb) target remote 192.168.0.12:2345
Remote debugging using 192.168.0.12:2345
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initialisers
and track explicitly loaded dynamic code.
0x76fd7a40 in ?? ()
(gdb) set architecture arm
The target architecture is assumed to be arm
(gdb) set sysroot /home/fred/raspberrypi/rootfs/lib/arm-linux-gnueabihf
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initialisers
and track explicitly loaded dynamic code.
(gdb)
The target side behaved as would be expected:
fred#UbuntuMATE-Pi:~/Hello_Raspberry_Pi$ gdbserver 192.168.0.7:2345 Hello_Raspberry_Pi
Process Hello_Raspberry_Pi created; pid = 17363
Listening on port 2345
Remote debugging from host 192.168.0.7
So perhaps Unable to find dynamic linker breakpoint function. is a big clue?
It would appear that I had defined Shared Libraries incorrectly. When I deleted this setting something closer to expected behaviour occurred. As shown below, however I there is still a warning that I would like to remove:
For help, type "help".
Type "apropos word" to search for commands related to "word".
warning: Unable to find dynamic linker breakpoint function.
GDB will be unable to debug shared library initialisers
and track explicitly loaded dynamic code.
0x76fd7a40 in ?? ()
Breakpoint 2, main () at ../src/Hello_Raspberry_Pi.cpp:18
18 cout << "I'm in the While loop and the value of variable i is: " << i << endl;
Breakpoint 1, main () at ../src/Hello_Raspberry_Pi.cpp:20
20 usleep(1000000); //wait for 1 seconds
So now Eclipse does breakpoint at main and permit stepping - finally!
I also faced problems similar to this, getting segmentation fault from ld.so. Did the following steps to fix it.
Install libc6-dbg package in both sysroot and target.
create a folder /lib/.debug in both sysroot and target.
copy /usr/lib/debug/lib/arm-linux-gnueabihf/ld-2.23.so to /lib/.debug in both sysroot and target.

Matlab missing dependency MEX-file

I have a script in matlab that calls other libraries. I use matlab version 2012a on linux . I get below error and I don't know how to fix it.
The error is :
Invalid MEX-file '/home/XXX/nearest_neighbors.mexa64':
libflann.so.1.8: cannot open shared object file: No such file or
directory
Error in flann_search (line 82)
[indices,dists] = nearest_neighbors('find_nn', data, testset, n, params);
Error in MyScript (line 73)
[nresult, ndists] = flann_search(Ntraindata', Ntraindata', resu.KNN, struct('algorithm','composite',...
That library you are referring to - https://github.com/mariusmuja/flann/ - has the nearest_neighbors function written in MEX code. MEX code is C code that is used to interface with MATLAB. People usually write heavily computationally burdening code over in MEX as it is known to handle loops and other things faster. The inputs come from MATLAB and are sent to this MEX function, and the outputs come from the MEX function and are piped back to MATLAB. It's basically a nice black box where you can use it just like any other MATLAB function. In fact, a lot of the functions that come shipped with MATLAB have MEX wrappers written to promote acceleration.
You are getting that error because you need to compile the nearest_neighbors function so that there is a MEX wrapper that can be called in MATLAB. That wrapper is missing because you haven't compiled the code.
First, you need to set up MEX. Make sure you have a compiler that is compatible with your version of MATLAB. You can do that by visiting this link:
http://www.mathworks.com/support/compilers/R20xxy/index.html
xx is the version number that belongs to your MATLAB and y is the character code that comes after it. For example, if you are using R2013a, you would visit:
http://www.mathworks.com/support/compilers/R2013a/index.html
Once you're there, go to your Operating System and ensure you have one of those supported compilers installed. Once you have that installed, go into MATLAB, and in the command prompt, type in:
mex -setup
This will allow you to set up MEX and choose the compiler you want. Given your error, you're using Linux 64-bit, so it should be very easy for you to get GCC. Just make sure that you choose a version of GCC that is compatible with your version of MATLAB. Once you choose the compiler, you can compile the code by doing this in the command prompt:
>> mex -v -O nearest_neighbors.cpp
This should generate the nearest_neighbors MEX file for you. Once that's done, you can now run the code.
For more detailed instructions, check out FLANN's user manual - http://people.cs.ubc.ca/~mariusm/uploads/FLANN/flann_manual-1.8.4.pdf - It tells you how to build and compile it for MATLAB use.
Good luck!

Problems compiling CUDA with MEX in Matlab2014a

I am trying to compile a CUDA program using MEX within Matlab2014a and can't get it to work.
I installed the newest CUDA toolbox (6.5) and driver (340.62) and the samples work correctly, so I guess it is correctly installed.
I also installed MS Visual Studio 2012 Professional and Matlab 2014a.
I followed the exact description in the Matlab Help in "GPU Computing -> Examples and How To -> Run MEX-Functions Containing CUDA Code".
My CUDA file ends with .cu (so I am trying to compile Matlab's standard example mexGPUExample.cu, but I get the same error with other .cu files), the mex_CUDA_win64.xml is in the same folder, the environment variable MW_NVCC_PATH is set correctly in the user variables and just to be sure I also called "setenv('MW_NVCC_PATH,...)" in Matlab.
If I try to compile a CUDA example with
mex -largeArrayDims mexGPUExample.cu
I get the following error:
cl : Command line warning D9024 : unrecognized source file type 'mexGPUExample.cu', object file assumed
cl : Command line warning D9027 : source file 'mexGPUExample.cu' ignored
cl : Command line warning D9021 : no action performed
D:\PROGRAMS\MATLAB~1\BIN\MEX.PL: Error: Compile of 'mexGPUExample.cu' failed.
Unable to complete successfully.
I also tried to set the compiler correctly using
mex -setup
and chose the MS Visual Studio Compiler.
The code itself works because I tested it on other systems.
I have no idea what I am doing wrong.
Any help is appreciated.
Matlab only supports CUDA 5.5 on R2014a, so your CUDA 6.5 by default cannot compile mex files under Matlab. Matlab R2014a supports VS 2012 now, but you need to verify you have VS 64-bit compiler if your Matlab is 64-bit. You can compile a normal .mex file with .cpp source file, (lots of these files under matlabroot/extern folder) to see whether Matlab works well with your VS.
Note that Matlab has a lag in supporting the latest compilers, both VS/gcc and Cuda. It is always good choice to check the compiler requirement by Matlab, before using the latest compilers.
Also check whether your Cuda kit and graphics drivers are correctly installed and work seamlessly with VS. You can compile and run some .cu files under the VS environment.
If you have finished all above and changed to the correct version of compilers, follow the instructions on matheworks website to compile the mexGPUExample file. If my memory is correct, you need to set environment variables and copy the XML file to the .mex file path. Then it should work.

Running SimpleOpenNI and leJOS in same project

We have a project in which we want to control a Lego Mindstorms robot with the Microsoft Kinect sensor. We decided to use SimpleOpenNI with Processing to read the sensor and leJOS to control the robot for a simple reason: since both is in Java, we can specifiy a kind of RoboHandler and call its methods from the sensor reading class.
In order to achieve that, we downloaded the leJOS plugin for eclipse, created a new leJOS NXT project and put the processing and SimpleOpenNI libraries in the build path. Note that to use SimpleOpenNI, it is required an old processing version without Java.
Now the problem is, that when installing leJOS, it strictly requires a 32-bit Java JDK, so I had to download this and point to it at installation.
At this point, following build paths are in the project:
LeJOS NXT Runtime (which has among others java.awt classes)
core.jar (used to run Processing in eclipse)
SimpleOpenNI.jar
Having all this, and setting up a first Processing sketch like
import processing.core.*;
public class Test extends PApplet{
(....)
}
results in following error code:
Multiple markers at this line
- The type java.awt.event.MouseMotionListener cannot be resolved. It is indirectly referenced from
required .class files
- The type java.applet.Applet cannot be resolved. It is indirectly referenced from required .class files
- The type java.awt.event.MouseListener cannot be resolved. It is indirectly referenced from required .class files
- The hierarchy of the type FirstTry is inconsistent
- The type java.awt.event.FocusListener cannot be resolved. It is indirectly referenced from required .class files
- The type java.awt.event.KeyListener cannot be resolved. It is indirectly referenced from required .class files
When I downloaded the Processing without Java version, I wasn't able to choose from 32bit- or 64bit version. The SimpleOpenNI is installed as 64bit-version.
When adding the 64bit-JDK (eclipse workspace default) as workspace too, the error disappears, but when trying to run a sketch, following error gets printed in the console:
Invalid layout of java.lang.Thread at name
#
# A fatal error has been detected by the Java Runtime Environment:
#
# Internal Error (javaClasses.cpp:128), pid=9176, tid=8740
# fatal error: Invalid layout of preloaded class
#
# JRE version: 7.0_04-b22
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.0-b21 mixed mode windows-amd64 compressed oops)
# Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
#
# An error report file with more information is saved as:
# C:\Users\Valentino\Desktop\workspaces\workspace_processing\RoboZeugs\bin\hs_err_pid9176.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.sun.com/bugreport/crash.jsp
#
So in my oppinion there is a problem with the JDK versions (32 and 64 bit), but there must be a way to handle this. Has anyone experience on this specific topic, maybe even with Kinect -> Mindstorms? Can anybody tell me what this error exactly means?
You're mixing classes that are designed to run with the JVM on the NXT brick (i.e. everything in classes.jar) with classes that are designed to run with a fully fledged JVM on the PC.
Don't do that. If you want to remote control an NXT, use pccomms.jar and everything else in lib/pc. But never add classes.jar or anything else in lib/nxt to the classpath when you're starting a Java program on your PC.
The error message about java.lang.Thread is probably due to the fact, that java.lang.Thread in classes.jar in by no means compatible with java.lang.Thread that comes with the JVM on your PC.