wo80 / csparse-interop

C# bindings for sparse matrix solvers.
BSD 3-Clause "New" or "Revised" License
9 stars 3 forks source link

Multithreading #2

Closed ajos6183 closed 6 years ago

ajos6183 commented 6 years ago

Hi @wo80,

I am calling the Umfpack solver in multiple threads at the same time. This seems to cause my application to crash under some circumstances (release mode about 80% of the times) without any error message or any exception. I note that using CSparse.Net in the same code works fine with no problems.

Is this something that should be expected?

Thanks, Anthony

wo80 commented 6 years ago

No, this shouldn't happen.

I cannot reproduce the error on my dual-core AMD processor (x86, x64, release, debug - everything works fine).

The managed code should be thread-safe. Also, the fact that no error message is displayed is a strong hint, that the problem is on the native side.

I'm not sure if UMFPACK factorization is expected to be thread-safe, but I guess it should. The LAPACK version I use is v3.2.1 (see wiki), which might not be thread-safe all the way (see SO).

If this might help, here is the debug version of the UMFPACK dll: Debug.7z

wo80 commented 6 years ago

Here's the code I used for testing: https://gist.github.com/wo80/db66d00571bd71017798e6bd1258f7b8

Let me know, if you get errors using this code. If you don't, please post your code.

ajos6183 commented 6 years ago

Thanks @wo80 for your detailed answer.

I don't seem to have any problems running your code.

This is the log from the windows event viewer: Windows Event Viewer Log.txt

Putting the whole code in a try statement seems to fix things even though the code never seems to hit the catch statement. I am not sure what this means. Here is a sample of the local code that i am using. Note i am passing a compresssedcolumnstorage matrix rather than a SparseMatrix. I tried using a SparseMatrix without the try statement and i get the same result. I am happy with this solution as it seems to be working. OLD Dim S_free_ccs As CompressedColumnStorage(Of Double) = Converter.ToCompressedColumnStorage(S_free) Dim LU As Umfpack = New Umfpack(S_free_ccs) LU.Solve(Res, dV_free) NEW Dim S_free_ccs As CompressedColumnStorage(Of Double) = Converter.ToCompressedColumnStorage(S_free) Try Dim LU As Umfpack = New Umfpack(S_free_ccs) LU.Solve(Res, dV_free) Catch ex As Exception System.Windows.Forms.MessageBox.Show("Error!") End Try

On another note, I had a look at your vs-suitesparse project. Thanks for explaining the process of generating the dlls very clearly. If i wanted to get a bit more speed, what would you recommend i use when generating the umfpack dll? Note i am not targeting a particular processor.

wo80 commented 6 years ago

Always dispose of the Umfpack instance, either by wrapping it in a using statement or manually calling LU.Dispose(). Let me know if this fixes the error!

To get a bit more speed, there are basically two options: 1.) Use an optimed BLAS provider. For Intel processors the natural choice will be MKL. 2.) Enable multi-threading for the BLAS provider. But keep in mind, that this doesn't make much sense, if your application is using all cores already.

ajos6183 commented 6 years ago

Thanks, that seems to fix it!