예제 #1
0
파일: upgC.c 프로젝트: xclose/eda282
int kernel(int npoints, int nclusters) {

  int i, j, delta = 0, index;
  
  #pragma omp parallel for default(shared) \
                            private(i,j,index) \
                            schedule(static) \
                            reduction(+:delta)

  for(i = 0; i < npoints; i++) {
    
    index = find_nearest_point(feature[i], 2, clusters, nclusters);
    
    if (membership[i] != index) {
      delta += 1;
    }
    
    membership[i] = index;
    
    #pragma omp critical
    { 
      new_centers_len[index]++;
      for(j = 0; j < 2; j++) {
        new_centers[index][j] += feature[i][j];
      }
    }

  }
  return delta;
}
예제 #2
0
파일: upgC.c 프로젝트: xclose/eda282
int kernel(int npoints, int nclusters) {
  
  int i, j, delta = 0, index;

  #pragma omp parallel for default(shared) \
                            private(i,j,index) \
                            schedule(static)
  
  for(i = 0; i < npoints; i++) {
    
    index = find_nearest_point(feature[i], 2, clusters, nclusters);
    
    if (membership[i] != index) {
      omp_set_lock(&delta_lock);
        delta += 1;
      omp_unset_lock(&delta_lock);
    }
    
    membership[i] = index;
    
    omp_set_lock(&new_centers_len_locks[index]);
      new_centers_len[index]++;
    omp_unset_lock(&new_centers_len_locks[index]);

    for(j = 0; j < 2; j++) {
      omp_set_lock(&new_centers_locks[index][j]);
        new_centers[index][j] += feature[i][j];
      omp_unset_lock(&new_centers_locks[index][j]);   
    }
  }

  return delta;
}
예제 #3
0
파일: rmse.c 프로젝트: zangcq/rodinia
/*----< rms_err(): calculates RMSE of clustering
 * >-------------------------------------*/
float rms_err(float **feature, /* [npoints][nfeatures] */
              int nfeatures, int npoints,
              float **cluster_centres, /* [nclusters][nfeatures] */
              int nclusters) {
    int i;
    int nearest_cluster_index; /* cluster center id with min distance to pt */
    float sum_euclid = 0.0;    /* sum of Euclidean distance squares */
    float ret;                 /* return value */

/* calculate and sum the sqaure of euclidean distance*/
#pragma omp parallel for shared(feature, cluster_centres) firstprivate(        \
    npoints, nfeatures,                                                        \
    nclusters) private(i, nearest_cluster_index) schedule(static)
    for (i = 0; i < npoints; i++) {
        nearest_cluster_index = find_nearest_point(feature[i], nfeatures,
                                                   cluster_centres, nclusters);

        sum_euclid += euclid_dist_2(
            feature[i], cluster_centres[nearest_cluster_index], nfeatures);
    }
    /* divide by n, then take sqrt */
    ret = sqrt(sum_euclid / npoints);

    return (ret);
}
예제 #4
0
/* The hard part is when your candidate point does not satisfy other
   constraints, so you need to translate the point until it meets the new hypersurface.
   How far is that? Project beta onto the new surface, and find the
   distance between that projection and the original surface. Then
   translate beta toward the original surface by that amount. The
   projection of the translated beta onto the new surface now also touches the old
   surface.
   */
static void get_candiate(gsl_vector *beta, apop_data *constraint, int current, gsl_vector *candidate, double margin){
    double k, ck, off_by, s;
    gsl_vector *pseudobeta        = NULL;
    gsl_vector *pseudocandidate   = NULL;
    gsl_vector *pseudocandidate2  = NULL;
    gsl_vector *fix               = NULL;
    Apop_row_v(constraint, current, cc);
    ck = gsl_vector_get(constraint->vector, current);
    find_nearest_point(beta, ck, cc, candidate);
    for (size_t i=0; i< constraint->vector->size; i++){
        if (i!=current){
            Apop_row_v(constraint, i, other);
            k   =apop_data_get(constraint, i, -1);
            if (binds(candidate, k, other, margin)){
                if (!pseudobeta){
                    pseudobeta          = gsl_vector_alloc(beta->size);
                    gsl_vector_memcpy(pseudobeta, beta);
                    pseudocandidate     = gsl_vector_alloc(beta->size);
                    pseudocandidate2    = gsl_vector_alloc(beta->size);
                    fix                 = gsl_vector_alloc(beta->size);
                }
                find_nearest_point(pseudobeta, k, other, pseudocandidate);
                find_nearest_point(pseudocandidate, ck, cc, pseudocandidate2);
                off_by  = apop_vector_distance(pseudocandidate, pseudocandidate2);
                s       = trig_bit(cc, other, off_by);
                gsl_vector_memcpy(fix, cc);
                gsl_vector_scale(fix, magnitude(cc));
                gsl_vector_scale(fix, s);
                gsl_vector_add(pseudobeta, fix);
                find_nearest_point(pseudobeta, k, other, candidate);
                gsl_vector_memcpy(pseudobeta, candidate);
            } 
        }
    }
    if (fix){ 
        gsl_vector_free(fix); gsl_vector_free(pseudobeta);
        gsl_vector_free(pseudocandidate); gsl_vector_free(pseudocandidate2);
    }
}
예제 #5
0
static gint
motion_notify_cb (GtkWidget * w, GdkEventMotion * event, splotd * sp)
{
  displayd *display = sp->displayptr;
  cpaneld *cpanel = &display->cpanel;
  GGobiStage *d = display->d;
  GGobiSession *gg = GGobiFromSPlot (sp);
  gboolean button1_p, button2_p;
  gint k;

  if (cpanel->ee_mode == ADDING_EDGES) {
    mousepos_get_motion (w, event, &button1_p, &button2_p, sp);
    k = find_nearest_point (&sp->mousepos, sp, d, gg);
    d->nearest_point = k;
    if (k != d->nearest_point_prev)
      displays_plot (NULL, QUICK, gg);
    d->nearest_point_prev = d->nearest_point;
  }
  else if (cpanel->ee_mode == ADDING_POINTS) {
    ;
  }

  return true;
}
예제 #6
0
long double apop_linear_constraint(gsl_vector *beta, apop_data * constraint, double margin){
#else
apop_varad_head(long double, apop_linear_constraint){
    static threadlocal apop_data *default_constraint;
    gsl_vector * apop_varad_var(beta, NULL);
    double apop_varad_var(margin, 0);
    apop_data * apop_varad_var(constraint, NULL);
    Apop_assert(beta, "The vector to be checked is NULL.");
    if (!constraint){
        if (default_constraint && beta->size != default_constraint->vector->size){
            apop_data_free(default_constraint);
            default_constraint = NULL;
        }
        if (!default_constraint){
            default_constraint = apop_data_alloc(0,beta->size, beta->size);
            default_constraint->vector = gsl_vector_calloc(beta->size);
            gsl_matrix_set_identity(default_constraint->matrix);
        }
        constraint = default_constraint;
    }
    return apop_linear_constraint_base(beta, constraint, margin);
}

 long double apop_linear_constraint_base(gsl_vector *beta, apop_data * constraint, double margin){
#endif
    static threadlocal gsl_vector *closest_pt = NULL;
    static threadlocal gsl_vector *candidate  = NULL;
    static threadlocal gsl_vector *fix        = NULL;
    int constraint_ct = constraint->matrix->size1;
    int bindlist[constraint_ct];
    int i, bound = 0;
    /* For added efficiency, keep a scratch vector or two on hand. */
    if (closest_pt==NULL || closest_pt->size != constraint->matrix->size2){
        closest_pt  = gsl_vector_calloc(beta->size);
        candidate   = gsl_vector_alloc(beta->size);
        fix         = gsl_vector_alloc(beta->size);
        closest_pt->data[0] = GSL_NEGINF;
    }
    /* Do any constraints bind?*/
    memset(bindlist, 0, sizeof(int)*constraint_ct);
    for (i=0; i< constraint_ct; i++){
        Apop_row_v(constraint, i, c);
        bound           +=
        bindlist[i]      = binds(beta, apop_data_get(constraint, i, -1), c, margin);
    }
    if (!bound) return 0;   //All constraints met.
    gsl_vector *base_beta = apop_vector_copy(beta);
    /* With only one constraint, it's easy. */
    if (constraint->vector->size==1){
        Apop_row_v(constraint, 0, c);
        find_nearest_point(base_beta, constraint->vector->data[0], c, beta);
        goto add_margin;
    }
    /* Finally, multiple constraints, at least one binding.
       For each surface, pick a candidate point.
       Check whether the point meets the other constraints. 
            if not, translate to a new point that works.
            [Do this by maintaining a pseudopoint that translates by the
            necessary amount.]
        Once you have a candidate point, compare its distance to the
        current favorite; keep the best.
     */
    for (i=0; i< constraint_ct; i++)
        if (bindlist[i]){
            get_candiate(base_beta, constraint, i, candidate, margin);
            if(apop_vector_distance(base_beta, candidate) < apop_vector_distance(base_beta, closest_pt))
                gsl_vector_memcpy(closest_pt, candidate);
        }
    gsl_vector_memcpy(beta, closest_pt);
add_margin:
    for (i=0; i< constraint_ct; i++){
        if(bindlist[i]){
            Apop_row_v(constraint, i, c);
            gsl_vector_memcpy(fix, c);
            gsl_vector_scale(fix, magnitude(fix));
            gsl_vector_scale(fix, margin);
            gsl_vector_add(beta, fix);
        }
    }
    long double out = apop_vector_distance(base_beta, beta);
    gsl_vector_free(base_beta);
    return out;
}
예제 #7
0
void
move_points_proc(xgobidata *xg)
{
  int root_x, root_y;
  unsigned int kb;
  Window root, child;
  static int ocpos_x = 0, ocpos_y = 0;
  icoords cpos;
  static int inwindow = 1;
  int wasinwindow;
  Boolean pointer_moved = False;

  wasinwindow = inwindow;

/*
 * Find the nearest point, just as in identification, and highlight it
*/

/*
 * Get the current pointer position.
*/
  if (XQueryPointer(display, xg->plot_window, &root, &child,
            &root_x, &root_y, &cpos.x, &cpos.y, &kb))
  {
    inwindow = (0 < cpos.x && cpos.x < xg->max.x &&
                0 < cpos.y && cpos.y < xg->max.y) ;
    /*
     * If the pointer is inside the plotting region ...
    */
    if (inwindow)
    {
      /*
       * If the pointer has moved ...
      */
      if ((cpos.x != ocpos_x) || (cpos.y != ocpos_y)) {
        pointer_moved = True;
        ocpos_x = cpos.x;
        ocpos_y = cpos.y;
      }
    }
  }

  if (pointer_moved) {
    
    if (buttonpos == UP) {
      moving_point = -1;
      if ( (xg->nearest_point = find_nearest_point(&cpos, xg)) != -1) {
        quickplot_once(xg);
	/* AB would like to draw a point in bottom right with selected glyph/color */
      }
    }
    else {
      /*
       * If the left button is down, move the point: compute the
       * data pipeline in reverse, (then run it forward again?) and
       * draw the plot.
      */
      if (buttonpos == DOWN && xg->nearest_point != -1) {
        moving_point = xg->nearest_point;
        move_pt(xg->nearest_point, cpos.x, cpos.y, xg);
      }
    }
  }

  if (!inwindow && wasinwindow) {
    /*
     * Don't draw the diamond if the pointer leaves the plot window.
    */
    xg->nearest_point = -1;
    quickplot_once(xg);
  }
}