hughperkins / jeigen

Java wrapper for Eigen C++ fast matrix library
Other
106 stars 31 forks source link

jeigen support multi thread and sse #15

Open superclocks opened 7 years ago

superclocks commented 7 years ago

i kown how to use multi thread and sse in c++ eigen. I want to know if your jeigen have these function. If it has, pls show me how to set it, thanks

hughperkins commented 7 years ago

jeigen simply wraps the underlying eigen. I havent done anything special to enable sse or multithread. How would you enable sse or multithread when using from c++ directly?

Halberdier commented 2 years ago

Hi, after conversation here, I investigated a bit. According with Eigen documentation, it takes advantage of underlying OpenMP for multithreading excecution in C++, therefore enabling it in the library should be sufficient. After some research, it seems that the modern way to do so should be in the first native CMakeLists.txt, so I tried adding at the end:

find_package(OpenMP)

if(OpenMP_CXX_FOUND)
  target_link_libraries(jeigen PUBLIC OpenMP::OpenMP_CXX)
endif()

However, after jeigen rebuild, then recompilation and relaunch of a test, it appears to be still using a single CPU. This is as far as I got, suggestions are welcome.

hughperkins commented 2 years ago

Nice! Sounds like pretty close. Is it possible that it's just a super-old version of Eigen? What happens if you upgrade to a newer version of Eigen? (Alternatively, if you write a c++ test case using the version of Eigen in the repo, does that use OMP?)

hughperkins commented 2 years ago

The c++ part of jeigen is pretty short. You could take e.g. the dense multiply function, https://github.com/hughperkins/jeigen/blob/e4655773ed7dc6885976a93a4c8935e2612f2516/src/native/jeigen.cpp#L114-L119 , put it into a separate c++ file, and try calling it, and see what happens. (the matrices are passed in as 1-d arrays, containing the matrix values. You'll need to pass in an array to receive the result too)

Halberdier commented 2 years ago

I tried to compile a file linking to your /native folders as library: according to Eigen docs, you don't need to build/make if you work in pure C++. I did it with and without -fopenmp flag, then compared. $ g++ -I /home/halberdier/jeigen/jeigen/src/native test.cpp -o test $ g++ -fopenmp -I /home/halberdier/jeigen/jeigen/src/native test.cpp -o testomp In this case it seems to work properly. In addition to considerably faster execution time, the system monitor shows clearly that all the cores are used in the testomp call (second wave in the picture): Screenshot at 2022-08-24 18-27-16 Then it must be something to be added when crossing JNI from C++ to Java that I failed to find.

hughperkins commented 2 years ago

I doubt Java is setting omp num threads. Did you grep my code just in case I am? Otherwise note that eigen is a template library, which is why you don't have to build it first. However, when using from Java you do have to build it first, so the templating functionality goes away: you're stuck with whatever types and so on that you declared at compilation time. I wonder if building as a library results then in different behavior from using directly as header files only? Perhaps you can try building eigen into a c++ library, and linking to that, and checking the behavior? (In fact, you can simply link directly with the jeigen c++ library, from c++, I imagine?)

On Wed, Aug 24, 2022, 12:40 Halberdier @.***> wrote:

I tried to compile a file linking to your /native folders as library: according to Eigen docs, you don't need to build/make if you work in pure C++. I did it with and without -fopenmp flag, then compared. $ g++ -I /home/halberdier/jeigen/jeigen/src/native test.cpp -o test $ g++ -fopenmp -I /home/halberdier/jeigen/jeigen/src/native test.cpp -o testomp In this case it seems to work properly. In addition to considerably faster execution time, the system monitor shows clearly that all the cores are used in the testomp call (second wave in the picture): [image: Screenshot at 2022-08-24 18-27-16] https://user-images.githubusercontent.com/5478542/186473838-b5a3ef77-9979-4e54-bbb6-96ff28b26b62.png Then it must be something to be added when crossing JNI from C++ to Java that I failed to find.

— Reply to this email directly, view it on GitHub https://github.com/hughperkins/jeigen/issues/15#issuecomment-1225972600, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAA6FKFYTGIMQTFYY652CO3V2ZGBLANCNFSM4DQJXVOQ . You are receiving this because you commented.Message ID: @.***>