~vtraag/louvain/release-0.1

« back to all changes in this revision

Viewing changes to src/Base/greedy_louvain.cpp

  • Committer: Vincent Traag
  • Date: 2011-05-26 17:28:12 UTC
  • Revision ID: vtraag@f-m.fm-20110526172812-qtsq5n47ptwtmxs2
Fixed some minor issues, removed dependence on pthread library, changed the consideration of communities for negative links.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
int             GreedyLouvain::iterate_randomly               = 0;
49
49
int             GreedyLouvain::move_individual                = 0;
50
50
int             GreedyLouvain::max_nb_threads                 = 1;
 
51
#ifdef THREAD_SUPPORT
51
52
pthread_mutex_t GreedyLouvain::rand_lock                      = PTHREAD_MUTEX_INITIALIZER;
 
53
#endif
52
54
 
53
55
long waiting_time = 0;
54
56
long skipped_nodes = 0;
132
134
  double mod1   = cc->modularity(); //Start modularity
133
135
  int nb_nodes  = cc->g->nb_nodes;
134
136
 
 
137
#ifdef THREAD_SUPPORT
135
138
  int nb_threads    = (int)(double)nb_nodes/NODES_PER_THREAD;
136
139
 
137
140
  if (nb_threads > max_nb_threads) // Never use more than NUM_THREADS
139
142
 
140
143
  if (nb_threads < 1) // Use (obviously) at least one
141
144
    nb_threads = 1;
142
 
 
 
145
  
143
146
  ThreadSync* ts = new ThreadSync(nb_nodes, nb_threads);
144
147
 
145
148
  if (nb_threads == 1)
174
177
    delete [] threads;
175
178
 
176
179
  }
 
180
#else //If no thread support
 
181
  ThreadSync* ts = new ThreadSync(nb_nodes, 1);
 
182
  one_level_single(cc, ts);
 
183
#endif  
177
184
  delete ts;
178
185
  cc->renumber_communities();
179
186
 
183
190
 
184
191
double GreedyLouvain::one_level_single(Community* cc, ThreadSync* ts)
185
192
{
 
193
#ifdef THREAD_SUPPORT
186
194
  pthread_mutex_lock(&rand_lock);
187
195
  MTRand* r = new MTRand();
188
196
  //cerr << "MTRand initialized with seed " << s << endl;
189
197
  pthread_mutex_unlock(&rand_lock);
 
198
#else
 
199
  MTRand* r = new MTRand();
 
200
#endif  
190
201
 
191
202
  int nb_pass_done      = 0;
192
203
  double m              = cc->g->total_weight();
274
285
  return ts->diff;
275
286
}
276
287
 
 
288
#ifdef THREAD_SUPPORT
277
289
void* GreedyLouvain::one_level_thread(void* data)
278
290
{
279
291
  level_thread_data* ltd = (GreedyLouvain::level_thread_data*) data;
536
548
  delete r;
537
549
  return NULL;
538
550
}
 
551
#endif
539
552
 
540
553
void GreedyLouvain::random_shuffle(deque<int> &v, MTRand* r)
541
554
{
553
566
 
554
567
GreedyLouvain::ThreadSync::ThreadSync(int nb_nodes, int nb_threads)
555
568
{
 
569
#ifdef THREAD_SUPPORT
556
570
  if (nb_threads > 1)
557
571
  {
558
572
    thread_id_per_node        = new int[nb_nodes];
580
594
    }
581
595
  }
582
596
  this->nb_threads = nb_threads;
 
597
#endif
583
598
 
584
599
  reset_sync();
585
600
}
586
601
 
587
602
GreedyLouvain::ThreadSync::~ThreadSync()
588
603
{
 
604
#ifdef THREAD_SUPPORT
589
605
  if (nb_threads > 1)
590
606
  {
591
607
    delete [] thread_id_per_node;
592
608
    delete [] node_locks;
593
609
    delete [] comm_locks;
594
610
  }
 
611
#endif
595
612
}
596
613
 
597
614
void GreedyLouvain::ThreadSync::reset_sync()
598
615
{
 
616
#ifdef THREAD_SUPPORT
599
617
  stop                      = false;
 
618
  nb_threads_finished_pass  = 0;  
 
619
#endif
600
620
  nb_improvements           = 0;
601
621
  diff                      = 0.0;
602
 
  nb_threads_finished_pass  = 0;
603
622
}