src/kmeans.cpp
// Assumes cluster means are up-to-date
Assumes cluster means are up-to-date
Assumes cluster means are up - to - date
void KMeans::ComputeIntraCentroidDistances(void) {
  time_t start_t = time(NULL);
  for (int c = 0; c < num_clusters_; ++c) {
    neighbor_vectors_[c].clear();
  }
  for (int i = 1; i < num_threads_; ++i) {
    threads_[i]->RunIntra();
  }
  threads_[0]->ComputeIntraCentroidDistances();
  for (int i = 1; i < num_threads_; ++i) {
    threads_[i]->Join();
  }
  fprintf(stderr, "Starting second loop\n");
  for (int c1 = 1; c1 < num_clusters_; ++c1) {
    if (cluster_sizes_[c1] == 0) continue;
    vector< pair<float, int> > *v = &neighbor_vectors_[c1];
    int num = v->size();
    for (int i = 0; i < num; ++i) {
      float dist = (*v)[i].first;
      int c0 = (*v)[i].second;
      neighbor_vectors_[c0].push_back(make_pair(dist, c1));
    }
  }
  for (int i = 1; i < num_threads_; ++i) {
    threads_[i]->RunSort();
  }
  threads_[0]->SortNeighbors();
  for (int i = 1; i < num_threads_; ++i) {
    threads_[i]->Join();
  }
  int sum_lens = 0;
  for (int c = 0; c < num_clusters_; ++c) {
    sum_lens += neighbor_vectors_[c].size();
  }
  fprintf(stderr, "Avg neighbor vector length: %.1f\n", sum_lens / (double)num_clusters_);
  time_t end_t = time(NULL);
  double diff_sec = difftime(end_t, start_t);
  intra_time_ += diff_sec;
  fprintf(stderr, "Cum intra time: %f\n", intra_time_);
}
void KMeans :: ComputeIntraCentroidDistances ( void ) { time_t start_t = time ( NULL ) ; for ( int c = 0 ; c < num_clusters_ ; ++ c ) { neighbor_vectors_ [ c ] . clear ( ) ; } for ( int i = 1 ; i < num_threads_ ; ++ i ) { threads_ [ i ] -> RunIntra ( ) ; } threads_ [ 0 ] -> ComputeIntraCentroidDistances ( ) ; for ( int i = 1 ; i < num_threads_ ; ++ i ) { threads_ [ i ] -> Join ( ) ; } fprintf ( stderr , " \n " ) ; for ( int c1 = 1 ; c1 < num_clusters_ ; ++ c1 ) { if ( cluster_sizes_ [ c1 ] == 0 ) continue ; vector < pair < float , int > > * v = & neighbor_vectors_ [ c1 ] ; int num = v -> size ( ) ; for ( int i = 0 ; i < num ; ++ i ) { float dist = ( * v ) [ i ] . first ; int c0 = ( * v ) [ i ] . second ; neighbor_vectors_ [ c0 ] . push_back ( make_pair ( dist , c1 ) ) ; } } for ( int i = 1 ; i < num_threads_ ; ++ i ) { threads_ [ i ] -> RunSort ( ) ; } threads_ [ 0 ] -> SortNeighbors ( ) ; for ( int i = 1 ; i < num_threads_ ; ++ i ) { threads_ [ i ] -> Join ( ) ; } int sum_lens = 0 ; for ( int c = 0 ; c < num_clusters_ ; ++ c ) { sum_lens += neighbor_vectors_ [ c ] . size ( ) ; } fprintf ( stderr , " \n " , sum_lens / ( double ) num_clusters_ ) ; time_t end_t = time ( NULL ) ; double diff_sec = difftime ( end_t , start_t ) ; intra_time_ += diff_sec ; fprintf ( stderr , " \n " , intra_time_ ) ; }
Assumes cluster means are up-to-date
Assumes cluster means are up - to - date
// Execute thread 0 in main execution thread // printf("%i\n", (int)neighbor_vectors_[c].size());