pcluster build_simsub_cluster(pclustergeometry cf, uint size, uint * idx, uint clf) { pcluster t; uint leaves; pcluster s; assert(size > 0); leaves = 0; if (size > clf) { s = build_help_cluster(cf, idx, size, clf, 0, &leaves); t = new_cluster(size, idx, leaves, cf->dim); leaves = 0; leaves_into_sons(cf, clf, s, t, &leaves); update_bbox_cluster(t); } else { t = new_cluster(size, idx, 0, cf->dim); update_support_bbox_cluster(cf, t); } update_cluster(t); return t; }
void coarsen_cluster(pcluster t, uint minsize) { bool a = false; uint i; for (i = 0; i < t->sons; i++) { if (t->son[i]->size < minsize) { a = true; break; } } if (a) { for (i = 0; i < t->sons; i++) del_cluster(t->son[i]); t->sons = 0; freemem(t->son); t->son = NULL; } else for (i = 0; i < t->sons; i++) coarsen_cluster(t->son[i], minsize); update_cluster(t); }
void balance_cluster(pcluster t, uint depth) { uint i; if ((t->son) && (depth > 0)) for (i = 0; i < t->sons; i++) balance_cluster(t->son[i], depth - 1); else if ((t->son) && (depth == 0)) cut_cluster(t, depth); else if ((!t->son) && (depth > 0)) extend_cluster(t, depth); update_cluster(t); }
void cut_cluster(pcluster t, uint depth) { uint i; if ((t->son) && (depth > 0)) for (i = 0; i < t->sons; i++) cut_cluster(t->son[i], depth - 1); else if ((t->son) && (depth == 0)) { for (i = 0; i < t->sons; i++) del_cluster(t->son[i]); t->sons = 0; freemem(t->son); t->son = NULL; } update_cluster(t); }
void extend_cluster(pcluster t, uint depth) { uint i; if ((t->son) && (depth > 0)) for (i = 0; i < t->sons; i++) extend_cluster(t->son[i], depth - 1); else if ((!t->son) && (depth > 0)) { t->sons = 1; t->son = (pcluster *) allocmem(sizeof(pcluster)); t->son[0] = new_cluster(t->size, t->idx, 0, t->dim); for (i = 0; i < t->dim; i++) { t->son[0]->bmin[i] = t->bmin[i]; t->son[0]->bmax[i] = t->bmax[i]; } extend_cluster(t->son[0], depth - 1); } update_cluster(t); }
int setup_configfs(void) { int rv; clear_configfs(); rv = add_configfs_base(); if (rv < 0) return rv; /* add configfs entries for existing nodes */ update_cluster(); /* the kernel has its own defaults for these values which we don't want to change unless these have been set; -1 means they have not been set on command line or config file */ if (cfgk_debug != -1) set_configfs_debug(cfgk_debug); if (cfgk_timewarn != -1) set_configfs_timewarn(cfgk_timewarn); if (cfgk_protocol == PROTO_DETECT) { rv = detect_protocol(); if (rv == PROTO_TCP || rv == PROTO_SCTP) cfgk_protocol = rv; } if (cfgk_protocol == PROTO_TCP || cfgk_protocol == PROTO_SCTP) set_configfs_protocol(cfgk_protocol); if (cfgk_protocol == PROTO_SCTP) set_proc_rmem(); return 0; }
int set_configfs_members(char *name, int new_count, int *new_members, int renew_count, int *renew_members) { char path[PATH_MAX]; char buf[32]; int i, w, fd, rv, id, old_count, *old_members; int do_renew; /* * create lockspace dir if it doesn't exist yet */ memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s", SPACES_DIR, name); if (!path_exists(path)) { if (create_path(path)) return -1; } /* * remove/add lockspace members */ rv = update_dir_members(name); if (rv) return rv; old_members = dir_members; old_count = dir_members_count; for (i = 0; i < old_count; i++) { id = old_members[i]; if (id_exists(id, new_count, new_members)) continue; memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s/nodes/%d", SPACES_DIR, name, id); log_debug("set_members rmdir \"%s\"", path); rv = rmdir(path); if (rv) { log_error("%s: rmdir failed: %d", path, errno); goto out; } } /* * remove lockspace dir after we've removed all the nodes * (when we're shutting down and adding no new nodes) */ if (!new_count) { memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s", SPACES_DIR, name); log_debug("set_members lockspace rmdir \"%s\"", path); rv = rmdir(path); if (rv) log_error("%s: rmdir failed: %d", path, errno); } for (i = 0; i < new_count; i++) { id = new_members[i]; do_renew = 0; if (id_exists(id, renew_count, renew_members)) do_renew = 1; else if (id_exists(id, old_count, old_members)) continue; if (!is_cluster_member(id)) update_cluster(); /* * create node's dir */ memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s/nodes/%d", SPACES_DIR, name, id); if (do_renew) { log_debug("set_members renew rmdir \"%s\"", path); rv = rmdir(path); if (rv) { log_error("%s: renew rmdir failed: %d", path, errno); /* don't quit here, there's a case where * this can happen, where a node identified * for renewal was not really added * previously */ } } log_debug("set_members mkdir \"%s\"", path); rv = create_path(path); if (rv) goto out; /* * set node's nodeid */ memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s/nodes/%d/nodeid", SPACES_DIR, name, id); rv = fd = open(path, O_WRONLY); if (rv < 0) { log_error("%s: open failed: %d", path, errno); goto out; } memset(buf, 0, 32); snprintf(buf, 32, "%d", id); rv = do_write(fd, buf, strlen(buf)); if (rv < 0) { log_error("%s: write failed: %d, %s", path, errno, buf); close(fd); goto out; } close(fd); /* * set node's weight */ w = get_weight(id, name); memset(path, 0, PATH_MAX); snprintf(path, PATH_MAX, "%s/%s/nodes/%d/weight", SPACES_DIR, name, id); rv = fd = open(path, O_WRONLY); if (rv < 0) { log_error("%s: open failed: %d", path, errno); goto out; } memset(buf, 0, 32); snprintf(buf, 32, "%d", w); rv = do_write(fd, buf, strlen(buf)); if (rv < 0) { log_error("%s: write failed: %d, %s", path, errno, buf); close(fd); goto out; } close(fd); } rv = 0; out: return rv; }
pcluster build_adaptive_cluster(pclustergeometry cf, uint size, uint * idx, uint clf) { pcluster t; uint direction; uint size0, size1; uint i, j; real a, m; assert(size > 0); if (size > clf) { update_point_bbox_clustergeometry(cf, size, idx); /* compute the direction of partition */ direction = 0; a = cf->hmax[0] - cf->hmin[0]; for (j = 1; j < cf->dim; j++) { m = cf->hmax[j] - cf->hmin[j]; if (a < m) { a = m; direction = j; } } /* build sons */ if (a > 0.0) { m = (cf->hmax[direction] + cf->hmin[direction]) / 2.0; size0 = 0; size1 = 0; for (i = 0; i < size; i++) { if (cf->x[idx[i]][direction] < m) { j = idx[i]; idx[i] = idx[size0]; idx[size0] = j; size0++; } else { size1++; } } /* build sons */ if (size0 > 0) { if (size1 > 0) { /* both sons are not empty */ t = new_cluster(size, idx, 2, cf->dim); t->son[0] = build_adaptive_cluster(cf, size0, idx, clf); t->son[1] = build_adaptive_cluster(cf, size1, idx + size0, clf); update_bbox_cluster(t); } else { /* only the first son is not empty */ assert(size0 == size); t = new_cluster(size, idx, 0, cf->dim); update_bbox_cluster(t); } } else { /* only the second son is not empty */ assert(size1 > 0); assert(size1 == size); t = new_cluster(size, idx, 0, cf->dim); update_bbox_cluster(t); } } else { assert(a == 0.0); t = new_cluster(size, idx, 0, cf->dim); update_support_bbox_cluster(cf, t); } } else { t = new_cluster(size, idx, 0, cf->dim); update_support_bbox_cluster(cf, t); } update_cluster(t); return t; }
pcluster build_pca_cluster(pclustergeometry cf, uint size, uint * idx, uint clf) { const uint dim = cf->dim; pamatrix C, Q; pavector v; prealavector lambda; real *x, *y; real w; uint i, j, k, size0, size1; pcluster t; assert(size > 0); size0 = 0; size1 = 0; if (size > clf) { x = allocreal(dim); y = allocreal(dim); /* determine weight of current cluster */ w = 0.0; for (i = 0; i < size; ++i) { w += cf->w[idx[i]]; } w = 1.0 / w; for (j = 0; j < dim; ++j) { x[j] = 0.0; } /* determine center of mass */ for (i = 0; i < size; ++i) { for (j = 0; j < dim; ++j) { x[j] += cf->w[idx[i]] * cf->x[idx[i]][j]; } } for (j = 0; j < dim; ++j) { x[j] *= w; } C = new_zero_amatrix(dim, dim); Q = new_zero_amatrix(dim, dim); lambda = new_realavector(dim); /* setup covariance matrix */ for (i = 0; i < size; ++i) { for (j = 0; j < dim; ++j) { y[j] = cf->x[idx[i]][j] - x[j]; } for (j = 0; j < dim; ++j) { for (k = 0; k < dim; ++k) { C->a[j + k * C->ld] += cf->w[idx[i]] * y[j] * y[k]; } } } /* get eigenvalues and eigenvectors of covariance matrix */ eig_amatrix(C, lambda, Q); /* get eigenvector from largest eigenvalue */ v = new_avector(0); init_column_avector(v, Q, dim - 1); /* separate cluster with v as separation-plane */ for (i = 0; i < size; ++i) { /* x_i - X */ for (j = 0; j < dim; ++j) { y[j] = cf->x[idx[i]][j] - x[j]; } /* <y,v> */ w = 0.0; for (j = 0; j < dim; ++j) { w += y[j] * v->v[j]; } if (w >= 0.0) { j = idx[i]; idx[i] = idx[size0]; idx[size0] = j; size0++; } else { size1++; } } assert(size0 + size1 == size); del_amatrix(Q); del_amatrix(C); del_realavector(lambda); del_avector(v); freemem(x); freemem(y); /* recursion */ if (size0 > 0) { if (size1 > 0) { t = new_cluster(size, idx, 2, cf->dim); t->son[0] = build_pca_cluster(cf, size0, idx, clf); t->son[1] = build_pca_cluster(cf, size1, idx + size0, clf); update_bbox_cluster(t); } else { t = new_cluster(size, idx, 1, cf->dim); t->son[0] = build_pca_cluster(cf, size0, idx, clf); update_bbox_cluster(t); } } else { assert(size1 > 0); t = new_cluster(size, idx, 1, cf->dim); t->son[0] = build_pca_cluster(cf, size1, idx, clf); update_bbox_cluster(t); } } else { t = new_cluster(size, idx, 0, cf->dim); update_support_bbox_cluster(cf, t); } update_cluster(t); return t; }
pcluster build_regular_cluster(pclustergeometry cf, uint size, uint * idx, uint clf, uint direction) { pcluster t; uint newd; uint size0, size1; uint i, j; real a, b, m; assert(size > 0); if (size > clf) { size0 = 0; size1 = 0; update_point_bbox_clustergeometry(cf, size, idx); if (direction < cf->dim - 1) { newd = direction + 1; } else { newd = 0; } m = cf->hmax[direction] - cf->hmin[direction]; if (m > 0.0) { m = (cf->hmax[direction] + cf->hmin[direction]) / 2.0; for (i = 0; i < size; i++) { if (cf->x[idx[i]][direction] < m) { j = idx[i]; idx[i] = idx[size0]; idx[size0] = j; size0++; } else { size1++; } } /* build sons */ if (size0 > 0) { if (size1 > 0) { /* both sons are not empty */ t = new_cluster(size, idx, 2, cf->dim); a = cf->hmin[direction]; b = cf->hmax[direction]; cf->hmax[direction] = m; t->son[0] = build_regular_cluster(cf, size0, idx, clf, newd); cf->hmax[direction] = b; cf->hmin[direction] = m; t->son[1] = build_regular_cluster(cf, size1, idx + size0, clf, newd); cf->hmin[direction] = a; update_bbox_cluster(t); } else { /* only the first son is not empty */ t = new_cluster(size, idx, 1, cf->dim); b = cf->hmax[direction]; cf->hmax[direction] = m; t->son[0] = build_regular_cluster(cf, size, idx, clf, newd); cf->hmax[direction] = b; update_bbox_cluster(t); } } else { /* only the second son is not empty */ assert(size1 > 0); t = new_cluster(size, idx, 1, cf->dim); a = cf->hmin[direction]; cf->hmin[direction] = m; t->son[0] = build_regular_cluster(cf, size, idx, clf, newd); cf->hmin[direction] = a; update_bbox_cluster(t); } } else { assert(m == 0.0); t = new_cluster(size, idx, 1, cf->dim); t->son[0] = build_regular_cluster(cf, size, idx, clf, newd); update_bbox_cluster(t); } } else { t = new_cluster(size, idx, 0, cf->dim); update_support_bbox_cluster(cf, t); } update_cluster(t); return t; }
static pcluster read_cdf_part(int nc_file, size_t clusters, size_t coeffs, int nc_sons, int nc_size, int nc_coeff, uint * idx, int dim, size_t * clusteridx, size_t * coeffidx) { pcluster t, t1; uint *idx1; uint size; uint sons; uint i; size_t start, count; ptrdiff_t stride; int val, result; /* Get number of sons */ start = *clusteridx; count = 1; stride = 1; result = nc_get_vars(nc_file, nc_sons, &start, &count, &stride, &val); assert(result == NC_NOERR); sons = val; /* Get size of cluster */ result = nc_get_vars(nc_file, nc_size, &start, &count, &stride, &val); assert(result == NC_NOERR); size = val; /* Create new cluster */ t = new_cluster(size, idx, sons, dim); /* Increase cluster index */ (*clusteridx)++; /* Handle sons */ if (sons > 0) { idx1 = idx; for (i = 0; i < sons; i++) { t1 = read_cdf_part(nc_file, clusters, coeffs, nc_sons, nc_size, nc_coeff, idx1, dim, clusteridx, coeffidx); t->son[i] = t1; idx1 += t1->size; } assert(idx1 == idx + size); } /* Get bounding box */ start = (*coeffidx); count = dim; result = nc_get_vars(nc_file, nc_coeff, &start, &count, &stride, t->bmin); start += dim; result = nc_get_vars(nc_file, nc_coeff, &start, &count, &stride, t->bmax); start += dim; (*coeffidx) = start; /* Finish initialization */ update_cluster(t); return t; }
/*--------------- Clustering by K-Mean Method ----------------------*/ bool k_Mean(int cluster_no, Double2D obs, int* obs_c, int dim, int pattern_no) { int s,t; int check; int flag,no=0,n; double dist,dmin; bool success=true; if(cluster_no > pattern_no){ obs_c[pattern_no] = random_no(cluster_no); } else{ Cluster *cl = new Cluster[cluster_no]; for(int i=0;i<cluster_no;i++){ cl[i].vec = AllocDouble_1D(dim); cl[i].acc = AllocDouble_1D(dim); } for(int i=0; i<cluster_no; i++){ cl[i].c = i; n = random_no(pattern_no); for(int j=0; j< dim; j++) cl[i].vec[j] = obs[j][n]; } flag = 0; check = 0; while(!flag && check<CHECK){ check++; for(int i=0; i<cluster_no; i++){ cl[i].patterns = 0; for(int k=0; k< dim; k++) cl[i].acc[k]= 0.0; } for(n=0; n < pattern_no; n++){ dmin=0.0; for(s=0; s<cluster_no; s++){ for(t=0, dist=0.0; t < dim; t++) dist += (obs[t][n]-cl[s].vec[t])*(obs[t][n]-cl[s].vec[t]); if(!s || (dist < dmin)){ dmin = dist; no = s; } } obs_c[n] = cl[no].c; for(int k=0; k<dim; k++) cl[no].acc[k] += obs[k][n]; cl[no].patterns++; } flag = update_cluster(cl,cluster_no,dim); } for(int i=0;i<cluster_no;i++){ FreeDouble_1D(cl[i].vec); FreeDouble_1D(cl[i].acc); } for(s=0; s<cluster_no; s++){ //printf("%d %d %f\n",s,cl[s].patterns,cl[s].vec[0]); if (cl[s].patterns==0) success=false; } delete [] cl; } return success; }