简体   繁体   中英

Implement Matrix operations in Java or in C++ through JNI?

I am basically trying to accomplish MNA(Modified Nodal Analysis) in java for circuit solvers . They basically involve solving a ton of linear equations and so I ended up with matrix algebra . MTJ and couple of other Java libraries are great but i'm tasked with implementing it on my own and did it as well in Java since my entire project is in Java. I was wondering if I should go ahead with the Java implementation or would doing it in C++ through JNI give a better enough performance to warrant its implementation? I'm just concerned about the bottleneck that JNI would incur when passing matrices the order of ten thousand and above or would that not be a problem?

My strongest recommendation to you is to not attempt to optimise your code for performance as you develop it. It is almost impossible to know ahead of time what code needs optimising. Generally you would end up with less readable, maintainable code that doesn't perform any better.

  1. Develop your library in Java aiming for maximum clarity and correctness. Ignore performance.

  2. Benchmark performance against realistic loads. For example if you will need to process millions of matrices then test how long that will take.

  3. Decide if you have a problem. Modern hardware coupled with all the performance elements of the JRE mean that there are far fewer situations in which anything needs to be done at this stage. If something needs to be done, consider running on a more powerful machine instead of optimising your code. That's often a cheaper option.

  4. If you need to optimise your code, use a profiler to find bottlenecks. Generally there are only a few small areas that consume the majority of the resources. You can waste a lot of time optimising code that has very little impact.

  5. Optimise the code in those bottlenecks. There are a ton of good resources out there to help you with this. Rerun the benchmarks regularly to make sure you a making a difference. Unwind optimisations that turn out to make no difference.

The fastest matrix operations will come from an optimized BLAS and LAPACK tailored to your platform. You could link to those via JNI if you need the speed, but don't try to do your own matrix package if you need speed. These standards are heavily optimized by people familiar with the quirks of individual HW systems. If you're not sure that you need the speed, use Jama or some other Java-based package first.

Also note that if you go down to BLAS / LAPACK, you're probably getting Fortran code at the bottom, not C, C++, or Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM