简体   繁体   English

通过JNI在Java或C ++中实现矩阵运算?

[英]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 . 我基本上是想在Java中完成电路求解器的MNA(修改节点分析)。 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. MTJ和其他几个Java库都很棒,但是我的任务是自己实现它,并且因为我的整个项目都使用Java,所以我在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? 我想知道我应该继续进行Java实现还是通过JNI在C ++中实现更好的性能来保证其实现? 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? 我只是担心JNI在传递一万以上的矩阵时会产生瓶颈,这不是问题吗?

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. 以最大的清晰度和正确性为目的,用Java开发您的库。 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. 现代硬件与JRE的所有性能元素相结合,意味着在此阶段需要执行任何操作的情况要少得多。 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. 最快的矩阵运算将来自为您的平台量身定制的优化BLAS和LAPACK。 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. 如果需要速度,可以通过JNI链接到那些对象,但是如果需要速度,不要尝试做自己的矩阵包。 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. 如果不确定是否需要速度,请首先使用Jama或其他基于Java的软件包。

Also note that if you go down to BLAS / LAPACK, you're probably getting Fortran code at the bottom, not C, C++, or Java. 还要注意,如果您使用BLAS / LAPACK,您可能会在底部获得Fortran代码,而不是C,C ++或Java。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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