vtraag / leidenalg

Implementation of the Leiden algorithm for various quality functions to be used with igraph in Python.
GNU General Public License v3.0
566 stars 76 forks source link

Is Leiden thread safe? #138

Closed pitsianis closed 12 months ago

pitsianis commented 1 year ago

Can I run multiple instances of leiden in a program to process the same graph for different clustering functions simultaneously? So is leiden thread safe?

vtraag commented 12 months ago

This is more challenging to answer, because I have to look in more detail at various aspects.

In principle, the library is only reading the graph, so that should in principle not be a problem. Of course, it would not be safe to operate on the same partition in different threads. Optimising two separate partitions in two separate threads, even if they use the same graph, would most likely be fine, but I'd need to look more closely at the details to make sure. There is some caching done for the graph, so depending on some of these details, this might cause some issues.

pitsianis commented 12 months ago

I practically answered my question, and the answer is no! The Leiden implementation (as of Dec 30, 2022) is not thread safe.

I ran the same call within a loop with the random number generator seed reset before each call. It consistently produced identical results. When I reran it with a parallel loop, where different threads executed the same calls, some of the results were different.

pitsianis commented 12 months ago

This was not really an issue, I am closing it for now.