Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Igraph random number generator in c

I was wondering wether the igraph_rng_default() gives a thread safe random number generator or not, and if that's the case, what rng should I use? Furthermore, if you call igraph_sbm_game in every thread, the generation of these graphs is thread safe?


Edit: to be more clear, this is what I wrote in my program:

  #define N 2048
  #define R 16
  #define rngseed 1763984
 int main(void)
 {   
  igraph_rng_seed(igraph_rng_default(), rngseed);  
 #pragma omp parallel shared(some_shared_variables) private(some_private_variables)
 { 
   igraph_matrix_t pref_matrix;    
   igraph_matrix_init(&pref_matrix, R, R);
   igraph_vector_int_t block_sizes;
    /*SBM initialization*/ 
    igraph_vector_int_init(&block_sizes, R);
   ...
   ...
   #pragma omp for ordered schedule(static,1)//ordered schedule(static,1) per risultati in ordine #pragma omp ordered per stampare in ordine    
    for(q1=0;q1<100;q1++)
    { 
      igraph graph; 
      igraph_sbm_game(&graph, N, &pref_matrix, &block_sizes, IGRAPH_DIRECTED, IGRAPH_NO_LOOPS);
      ....
    }
  }

My question is, since igraph_sbm_game generates a random graph based on the chosen rng, is this randomness guaranteed to be thread safe? In other words, with the default igraph random number generator, do I get random numbers in a thread safe way? My doubt comes both from the fact that the igraph_sbm_game() function doesn't need a seed and the fact that I can't find which random number generator is used by igraph...

like image 809
Francesco Di Lauro Avatar asked Jan 31 '26 14:01

Francesco Di Lauro


2 Answers

igraph is not designed to be thread-safe - there are certain data structures (such as a global "cleanup" stack for pointers that igraph allocates during the course of a function) that are shared between threads, so it is not safe to call two igraph functions at the same time from different threads. This probably answers your original question: no, the random number generator is not thread-safe either.

There is an experimental switch in its ./configure script (--enable-tls) that moves the global data structures into thread-local storage if the compiler supports it. This has the potential of making igraph thread-safe as long as you don't manipulate the same graph from different threads (or you use locking to coordinate access to the graph). However, this has not been tested thoroughly, so proceed carefully.

like image 164
Tamás Avatar answered Feb 03 '26 05:02

Tamás


Some updates valid for igraph version 0.9.x:

As the other answer says, igraph has limited thread support when compiled with thread-local storage enabled. To do so, set IGRAPH_ENABLE_TLS to ON when building igraph.

Issues relating to thread support are documented here:

  • https://igraph.org/c/html/latest/igraph-Advanced.html#using-igraph-in-multi-threaded-programs

In a nutshell:

  • If thread support was enabled, then the macro IGRAPH_THREAD_SAFE is defined to 1.
  • The default random number generator that is active on startup is not thread-safe.
  • You will need to explicitly initialize, seed and set a separate random number generator for each thread, using igraph_rng_set_default(). Only then will it be safe to use igraph functions that use random numbers from multiple threads.
like image 25
Szabolcs Avatar answered Feb 03 '26 04:02

Szabolcs