tag:blogger.com,1999:blog-15250116.post6043659888452755628..comments2013-03-02T12:38:23.550+01:00Comments on Marginally Interesting: Matrices, JNI, DirectBuffers, and Number Crunching in JavaMikio Braunhttp://www.blogger.com/profile/12784222133031101134noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-15250116.post-86885310596860257702009-05-12T01:39:00.000+02:002009-05-12T01:39:00.000+02:00But I think that the basic problem remains that th...<I>But I think that the basic problem remains that the JVM is oblivious to the memory allocated "off the JVM heap" and it won't be as aggressive reclaiming those buffers if they aren't accessible anymore.</I>I suppose this reply is a bit after the fact now, but what I meant was to allocate your own block of memory using, say, the C library's malloc(), wrapping that pointer using NewDirectByteBuffer(), and using this block as an NIO buffer for however long you need it. Then, when you're done, use the C library's free() to release the block of native memory and set your Java reference to null. Since you manually reclaim, the timing of Java's GC isn't important anymore.Jesse Pavel (jpavel@alum.mit.edu)noreply@blogger.comtag:blogger.com,1999:blog-15250116.post-78564481156732799572008-11-10T16:10:00.000+01:002008-11-10T16:10:00.000+01:00Hi Steve,thanks for your comments. Regarding your ...Hi Steve,<BR/><BR/>thanks for your comments. <BR/><BR/>Regarding your question, I'm still calling JNI, I'm only storing the matrices as double arrays now. Works very nicely, although one now has to do some things in Java now (especially those which take linear time like copying or even vector addition). Luckily, Java appears to be as quick as the native code there.<BR/><BR/>Thanks also for the link to the JVM languages group. I think it would already help if the JVM would take the memory allocated by the direct buffers into account and trigger garbage collections accordingly.<BR/><BR/>Concerning your plans with Scala, I'm setting up some m@tlab lookalike up in JRuby myself, and it works pretty well. In particular because you can add all kinds of syntactic sugar on the ruby side without subclassing. No idea how that would work in Scala... .<BR/><BR/>We're just now brushing up the documentation a bit, so look out for our first release of jblas.Mikio Braunhttps://www.blogger.com/profile/12784222133031101134noreply@blogger.comtag:blogger.com,1999:blog-15250116.post-11806962630990385622008-11-10T15:32:00.000+01:002008-11-10T15:32:00.000+01:00Hi Mikio,Thanks for the writeup ... I'm always cur...Hi Mikio,<BR/><BR/>Thanks for the writeup ... I'm always curious about numerical stuff in Java, so I found this very interesting.<BR/><BR/>Two comments:<BR/><BR/>(1) I'm not quite sure what you mean by this from your post on Nov 6:<BR/><BR/>"""... have recently converted all my matrix code to using normals double arrays."""<BR/><BR/>Are you not calling out to JNI anymore? Do you just mean that your n-dim matrix is now stored as a 1d double array? (I think this is actually how COT stores its n-dim matrices, also).<BR/><BR/>(2) I think you might get some good feedback if you pop into the JVM Languages group[1] and ask them if they have any input into your situation.<BR/><BR/>While your problem isn't really related to new languages hosted on the JVM, the folks there are quite friendly and really know the JVM internals inside and out.<BR/><BR/>Anyway, I'd be interested in any follow up posts you have about related work (subscribing to your RSS feed now) as I'd like to actually use Scala for similar work down the road.<BR/><BR/>-steve<BR/><BR/>[1] JVM Languages Group: http://groups.google.com/group/jvm-languagesSteve Lianoglouhttps://www.blogger.com/profile/01538274673179158781noreply@blogger.comtag:blogger.com,1999:blog-15250116.post-30631922318194152222008-11-07T10:54:00.000+01:002008-11-07T10:54:00.000+01:00Hi Kiran,thanks for pointing me to MTJ. I think I ...Hi Kiran,<BR/><BR/>thanks for pointing me to MTJ. I think I have already taken a look at it, but forgot about it in the mean time. I couldn't really see all their source code but it also seems that they have taken the route of primitive arrays as well.<BR/><BR/>Their performance numbers are in fact quite interesting and also underline the need for highly optimized native matrix routines. There are just so many things you have to get right, starting with memory locality down to how to order the floating point operations to fill your pipelines optimally. Something which is maybe hard to control in Java.<BR/><BR/>It seems that MTJ takes a similar approach to rely on BLAS and LAPACK, and it also looks pretty complete featurewise. In our project JBLAS we have taken a slightly different approach with the interface. For example, there are only 2d matrices. It turns out the separation between vectors and matrices which only have one row or one column is a bit artificial and often requires you to recast vectors as matrices and vice versa. We have also used a lot of overloading to make using the classes as comfortable as possible.<BR/><BR/>Looks like it's time to release JBLAS soon!Mikio Braunhttps://www.blogger.com/profile/12784222133031101134noreply@blogger.comtag:blogger.com,1999:blog-15250116.post-41329012836281413122008-11-06T19:09:00.000+01:002008-11-06T19:09:00.000+01:00I am not sure if you have seen this, MTJ: http://r...I am not sure if you have seen this, MTJ: http://ressim.berlios.de/. They have native and ATLAS based implementations of linear algebra stuff. Their benchmarks are interesting.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-15250116.post-38522126630291086492008-11-06T11:58:00.000+01:002008-11-06T11:58:00.000+01:00Hi Jesse,thanks for pointing me to the NewDirectBy...Hi Jesse,<BR/><BR/>thanks for pointing me to the NewDirectByteBuffer() function. But I think that the basic problem remains that the JVM is oblivious to the memory allocated "off the JVM heap" and it won't be as aggressive reclaiming those buffers if they aren't accessible anymore.<BR/><BR/>By the way, I just couldn't stand the instability anymore (having your machine freeze a few times a day on larger computations due to memory leaks is not nice) and have recently converted all my matrix code to using normals double arrays. It actually only took a few hours and now even huge computations run very smoothly.<BR/><BR/>I had to make sure that I do some stuff in Java now, in particular when the complexity is linear in the data. For example, copying data with <I>dcopy</I> just doesn't make any sense. On the other hand, for the really computationally expensive operations like matrix-matrix computations, the copying costs are negligible. And then there is the stuff like computing eigenvalues or solving linear equations where I don't even want to know too much about.<BR/><BR/>So, stability wise, it looks very nice now. I'll tweak the performance a bit more till I'm happy with that and then I think I'll release my matrix library.Mikio Braunhttps://www.blogger.com/profile/12784222133031101134noreply@blogger.comtag:blogger.com,1999:blog-15250116.post-26585318017791439232008-11-05T19:29:00.000+01:002008-11-05T19:29:00.000+01:00Nothing should prevent you from doing your own mem...Nothing should prevent you from doing your own memory management on buffers. Instead of having the JVM allocate the direct buffer, with all the uncertainty of its lifespan, allocate it yourself from native code, and then use the JNI function NewDirectByteBuffer() to wrap the memory in a ByteBuffer for use on the Java side. When you want to free the backing buffer, use JNI and the native free (or whatever memory management you're using).Anonymousnoreply@blogger.com