void mk_alternating(tl_Node *p) /* generates an alternating automaton for p */ { if (tl_stats) { getrusage(RUSAGE_SELF, &tr_debut); } node_size = calculate_node_size(p) + 1; /* number of states in the automaton */ label = (tl_Node **) tl_emalloc(node_size * sizeof(tl_Node *)); transition = (ATrans **) tl_emalloc(node_size * sizeof(ATrans *)); node_size = node_size / (8 * sizeof(int)) + 1; sym_size = calculate_sym_size(p); /* number of predicates */ if (sym_size) { sym_table = (char **) tl_emalloc(sym_size * sizeof(char *)); } sym_size = sym_size / (8 * sizeof(int)) + 1; final_set = make_set(-1, 0); transition[0] = boolean(p); /* generates the alternating automaton */ if (tl_verbose) { fprintf(tl_out, "\nAlternating automaton before simplification\n"); print_alternating(); } if (tl_simp_diff) { simplify_astates(); /* keeps only accessible states */ if (tl_verbose) { fprintf(tl_out, "\nAlternating automaton after simplification\n"); print_alternating(); } } if (tl_stats) { getrusage(RUSAGE_SELF, &tr_fin); fprintf(tl_out, "\n%i states, %i transitions\n", astate_count, atrans_count); } releasenode(1, p); tfree(label); }
/*! \brief Symmetric elimination tree * * <pre> * p = spsymetree (A); * * Find the elimination tree for symmetric matrix A. * This uses Liu's algorithm, and runs in time O(nz*log n). * * Input: * Square sparse matrix A. No check is made for symmetry; * elements below and on the diagonal are ignored. * Numeric values are ignored, so any explicit zeros are * treated as nonzero. * Output: * Integer array of parents representing the etree, with n * meaning a root of the elimination forest. * Note: * This routine uses only the upper triangle, while sparse * Cholesky (as in spchol.c) uses only the lower. Matlab's * dense Cholesky uses only the upper. This routine could * be modified to use the lower triangle either by transposing * the matrix or by traversing it by rows with auxiliary * pointer and link arrays. * * John R. Gilbert, Xerox, 10 Dec 1990 * Based on code by JRG dated 1987, 1988, and 1990. * Modified by X.S. Li, November 1999. * </pre> */ int sp_symetree_dist( int_t *acolst, int_t *acolend, /* column starts and ends past 1 */ int_t *arow, /* row indices of A */ int_t n, /* dimension of A */ int_t *parent /* parent in elim tree */ ) { int_t *root; /* root of subtee of etree */ int_t rset, cset; int_t row, col; int_t rroot; int_t p; int_t *pp; #if ( DEBUGlevel>=1 ) CHECK_MALLOC(0, "Enter sp_symetree()"); #endif root = mxCallocInt (n); initialize_disjoint_sets (n, &pp); for (col = 0; col < n; col++) { cset = make_set (col, pp); root[cset] = col; parent[col] = n; /* Matlab */ for (p = acolst[col]; p < acolend[col]; p++) { row = arow[p]; if (row >= col) continue; rset = find (row, pp); rroot = root[rset]; if (rroot != col) { parent[rroot] = col; cset = link (cset, rset, pp); root[cset] = col; } } } SUPERLU_FREE (root); finalize_disjoint_sets (pp); #if ( DEBUGlevel>=1 ) CHECK_MALLOC(0, "Exit sp_symetree()"); #endif return 0; } /* SP_SYMETREE_DIST */
9 return A // CPP CODE: void kruskal() { int i, u, v, cnt; for (i = 0; i < cntNode; i++) make_set(i); // sort the edge in increasing order. qsort(edge, cntEdge, sizeof(Edge), cmp); for (i = 0; i < cntEdge ; i++) { int u = edge[i].u; int v = edge[i].v; if (find_set(u) != find_set(v)) { union_set(u, v); } } }
void lca(int u) { int i, j; make_set(u); ancestor[find_set(u)] = u; for(i = 1;i <= n; i++) { if(tree[u][i] == 1) { lca(i); union_set(u, i); ancestor[find_set(u)] = u; } } color[u] = 1; for(i = 1;i <= n; i++) if(q[u][i] && color[i] == 1) num[ancestor[find_set(i)]]++; }
int main() { int m, i, j, s = 1; while(1) { scanf("%d %d", &n, &m); if(n == 0 && m == 0) break; make_set(n); while(m--) { scanf("%d %d", &i, &j); union_set(i, j); } m = 0; for(i = 1;i <= n; i++) if(find_set(i) == i) m++; printf("Case %d: %d\n", s++, m); } return 0; }
int numIslands(vector<vector<char>>& grid) { int m = grid.size(); if (m == 0) return 0; int n = grid[0].size(); if (n == 0) return 0; make_set(id, m , n); num_components = (grid[0][0] == '1'); // i == 0 for (int j = 1; j < n; j++) { if (grid[0][j] == '0') continue; num_components++; if (grid[0][j - 1] == '1') unions(to1d(0, j, n), to1d(0, j - 1, n)); } // j == 0 for (int i = 1; i < m; i++) { if (grid[i][0] == '0') continue; num_components++; if (grid[i - 1][0] == '1') unions(to1d(i, 0, n), to1d(i - 1, 0, n)); } //i != 0 && j != 0 for (int i = 1; i < m; i++) { for (int j = 1; j < n; j++) { if (grid[i][j] == '0') continue; num_components++; if (grid[i - 1][j] =='1') { unions(to1d(i, j, n), to1d(i - 1, j, n)); } if (grid[i][j - 1] == '1') { unions(to1d(i, j, n), to1d(i, j - 1, n)); } } } return num_components; }
void connected_comp(){ int i=0; for(;i<vertex_size;i++){ nodes[i] = make_set(&vertex[i]); } i=0; /**jj for(;i<edge_size;i++){ char a=edge[i][0]; int ia = a-'a'; char b=edge[i][1]; int ib=b -'a'; struct set_node* nodea=nodes[ia]; struct set_node* nodeb=nodes[ib]; if(find_set(nodea)!=find_set(nodeb)){ unite_set(nodea,nodeb); } }*/ return ; }
void mk_alternating(Node *p) /* generates an alternating automaton for p */ { if(tl_stats) getrusage(RUSAGE_SELF, &tr_debut); node_size = calculate_node_size(p) + 1; /* number of states in the automaton */ label = (Node **) tl_emalloc(node_size * sizeof(Node *)); transition = (ATrans **) tl_emalloc(node_size * sizeof(ATrans *)); node_size = node_size / (8 * sizeof(int)) + 1; sym_size = calculate_sym_size(p); /* number of predicates */ if(sym_size) sym_table = (char **) tl_emalloc(sym_size * sizeof(char *)); sym_size = sym_size / (8 * sizeof(int)) + 1; final_set = make_set(-1, 0); transition[0] = boolean(p); /* generates the alternating automaton */ if(tl_verbose) { fprintf(tl_out, "\nAlternating automaton before simplification\n"); print_alternating(); } if(tl_simp_diff) { simplify_astates(); /* keeps only accessible states */ if(tl_verbose) { fprintf(tl_out, "\nAlternating automaton after simplification\n"); print_alternating(); } } #ifndef __MINGW__ if(tl_stats) { getrusage(RUSAGE_SELF, &tr_fin); timeval_subtract (&t_diff, &tr_fin.ru_utime, &tr_debut.ru_utime); fprintf(tl_out, "\nBuilding and simplification of the alternating automaton: %i.%06is", t_diff.tv_sec, t_diff.tv_usec); fprintf(tl_out, "\n%i states, %i transitions\n", astate_count, atrans_count); } #endif releasenode(1, p); tfree(label); }
Grafo* leArq(char *fileName) { FILE *file = fopen( fileName, "r" ); if ( file == NULL ) { printf( "Nao foi possivel ler o arquivo %s\n", fileName); } else { Grafo *grafo = (Grafo*) malloc(sizeof(Grafo)); int tamvert, tamarest; fscanf(file, "%d %d", &tamvert, &tamarest); grafo -> verticesQtd = tamvert; grafo -> arestasQtd = tamarest; grafo -> arestas = (Aresta **)malloc((sizeof(Aresta*) * tamarest)+1); grafo->arestas[tamarest] = 0; grafo->vertices = (Vertice **)malloc((sizeof(Vertice*) * tamvert)+1); grafo->vertices[tamvert] = 0; for(int i = 0; i < tamvert; i++) { Vertice *v = (Vertice*)malloc(sizeof(Vertice)); v->id = i; make_set(v); grafo->vertices[i] = v; } for(int i = 0; i< tamarest; i++) { int origem, destino, custo; fscanf(file, "%d %d %d", &origem, &destino, &custo); Aresta *aresta = (Aresta*) malloc(sizeof(Aresta)); aresta -> origem = grafo->vertices[origem]; aresta -> destino = grafo->vertices[destino]; aresta -> custo = custo; grafo->arestas[i] = aresta; } fclose( file ); return grafo; } return NULL; }
int main() { long i,j,sum,a,b,c,M; char s1[1000],s2[1000],str[1000]; while(scanf("%ld %ld",&N,&M)==2&&(N||M)) { getchar(); make_set(); for(i=0;i<N;i++) gets(name[i]); qsort(name,N,sizeof(name[0]),cmp); for(i=0;i<M;i++) { gets(str); sscanf(str,"%s %s %ld",s1,s2,&c); a=binser(s1); b=binser(s2); P[i].u=a;P[i].v=b;P[i].cost=c; } gets(str); qsort(P,M,sizeof(S),cmp1); j=0;sum=0; for(i=0;j<N-1&&i<M;i++) { a=find(P[i].u); b=find(P[i].v); if(a!=b) { j++; sum+=P[i].cost; link(a,b); } } if(j!=N-1) printf("Impossible\n"); else printf("%ld\n",sum); } return 0; }
int main() { int graph[MAXSIZE][MAXSIZE]; struct node * hash_table[MAXSIZE] = {NULL}; int i; int j; int k; int T; int V; int flag; freopen("detect_cycle_graph_inp.txt", "r", stdin); scanf("%d", &T); for (i = 0; i < T; i++) { scanf("%d", &V); for (j = 1; j <= V; j++) { make_set(hash_table, j); for (k = 1; k <= V; k++) { scanf("%d", &graph[j][k]); } } flag = detect_cycle(graph, V, hash_table); if (flag == 1) { printf("Yes\n"); } else { printf("No\n"); } } fclose(stdin); return 0; }
int Kruskal(Graph *g) { int steps = 0; unsigned int Parent[VERTICES]; unsigned int Rank[VERTICES]; unsigned int r1,r2; Knode edge; //Step1: Heapsort make heap is already called // in main loop. steps=+Heapsort(&(g->node_list)); //do makeset for(unsigned int v=0;v<VERTICES;v++) { make_set(Parent,Rank,v); steps++; } //perform the main algo for(unsigned int i=0;i<g->EDGES;i++) { //greedily pick the next edge edge = g->node_list[i]; //find operation r1 = Find(Parent,edge.u,&steps); r2 = Find(Parent,edge.v,&steps); // cout<<edge.u<<" e "<<edge.v<<" "<<edge.weight<<" "<<r1<<" "<<r2<<endl; if(r1!=r2) { //Union those two edges Union(Parent,Rank,r1,r2); //Mark those edges in_tree status IN_MST[edge.v][edge.u]=true; IN_MST[edge.u][edge.v]=true; } //just discard it. steps++; } return steps; }
void MST_kruskal(int G[][20],int no_of_nodes,int mst[][20]) { int i,j; int u,v; for(i=0;i<no_of_nodes;i++) for(j=0;j<no_of_nodes;j++) mst[i][j]=0; for(i=0;i<no_of_nodes;i++)make_set(i); get_sorted_edge_list(G,no_of_nodes); for(i=0;i<edge_list_size;i++) { u=edge_list[i][0]; v=edge_list[i][1]; if(find_set(u)!=find_set(v)) { mst[u][v]=mst[v][u]=edge_list[i][2]; unite(u,v); } } }
int main() { long i,a,b,c,kase,kas=1; scanf("%ld",&kase); while(kase--) { scanf("%ld %ld %ld",&N,&M,&A); make_set(); for(i=0;i<M;i++) { scanf("%ld %ld %ld",&a,&b,&c); gr[i].x=a;gr[i].y=b;gr[i].w=c; } qsort(gr,M,sizeof(graph),cmp); b=kruskal(); a=0; for(i=1;i<=N;i++) if(i==par[i]) a++; printf("Case #%ld: %ld %ld\n",kas++,b+a*A,a); } return 0; }
void simplify_astates() /* simplifies the alternating automaton */ { ATrans *t; int i, *acc = make_set(-1, 0); /* no state is accessible initially */ for(t = transition[0]; t; t = t->nxt, i = 0) merge_sets(acc, t->to, 0); /* all initial states are accessible */ for(i = node_id - 1; i > 0; i--) { if (!in_set(acc, i)) { /* frees unaccessible states */ label[i] = ZN; free_atrans(transition[i], 1); transition[i] = (ATrans *)0; continue; } astate_count++; simplify_atrans(&transition[i]); for(t = transition[i]; t; t = t->nxt) merge_sets(acc, t->to, 0); } tfree(acc); }
void init() { int i, j, tmp; m = 0; for(i=1; i<=n; i++) for(j=1; j<=n; j++) { scanf("%d", &tmp); if(i<j && tmp) { edges[m].from = i; edges[m].to = j; edges[m++].len = tmp; } } qsort(edges, m, sizeof(struct Edge), compare); make_set(); int q, f, t; scanf("%d", &q); for(i=0; i<q; i++) { scanf("%d %d", &f, &t); Union(f, t); } }
int main() { subset* sub = (subset*)malloc(10*sizeof(subset)); int i; printf("I am here\n"); for(i=1;i<=10;i++){ make_set(sub,i); } for(i=1;i<=10;i++){ printf("%d %d\n",sub[i].parent,sub[i].rank); } make_union(sub,1,2); make_union(sub,2,3); make_union(sub,3,4); for(i=1;i<=10;i++){ printf("%d %d\n",sub[i].parent,sub[i].rank); } for(i=1;i<=10;i++){ printf("Parent of %d is %d\n",i,find_set(sub,i)); } return 0; }
/* * Symmetric elimination tree */ int sp_symetree( int *acolst, int *acolend, /* column starts and ends past 1 */ int *arow, /* row indices of A */ int n, /* dimension of A */ int *parent /* parent in elim tree */ ) { int *root; /* root of subtree of etree */ int rset, cset; int row, col; int rroot; int p; root = mxCallocInt (n); initialize_disjoint_sets (n); for (col = 0; col < n; col++) { cset = make_set (col); root[cset] = col; parent[col] = n; /* Matlab */ for (p = acolst[col]; p < acolend[col]; p++) { row = arow[p]; if (row >= col) continue; rset = find (row); rroot = root[rset]; if (rroot != col) { parent[rroot] = col; cset = link (cset, rset); root[cset] = col; } } } SUPERLU_FREE (root); finalize_disjoint_sets (); return 0; } /* SP_SYMETREE */
int smo(bow_wv **docs, int *yvect, double *weights, double *a_b, double **W, int ndocs, double *error, float *cvect, int *nsv) { int changed; int inspect_all; struct svm_smo_model model; int nchanged; int num_words; double *original_weights; int i,j,k,n; num_words = bow_num_words(); m1 = m2 = m3 = m4 = 0; model.n_pair_suc = model.n_pair_tot = model.n_single_suc = model.n_single_tot = model.n_outer = 0; model.nsv = *nsv; model.docs = docs; model.error = error; model.ndocs = ndocs; model.cvect = cvect; original_weights = NULL; if (svm_kernel_type == 0 && !(*W)) { *W = model.W = (double *) malloc(sizeof(double)*num_words); } else { model.W = NULL; } model.weights = weights; model.yvect = yvect; /* figure out the # of positives */ for (i=j=k=n=0; i<ndocs; i++) { if (yvect[i] == 1) { k = i; j++; } else { n = i; } } /* k is set to the last positive example found, n is the last negative */ make_set(ndocs,ndocs,&(model.I0)); make_set(ndocs,j,&(model.I1)); make_set(ndocs,ndocs-j,&(model.I2)); make_set(ndocs,j,&(model.I3)); make_set(ndocs,ndocs-j,&(model.I4)); /* this is the code which initializes the sets according to the weights values */ for (i=0; i<ndocs; i++) { struct set *s; if (weights[i] > svm_epsilon_a && weights[i] < cvect[i] - svm_epsilon_a) { s = &(model.I0); } else if (yvect[i] == 1) { if (weights[i] < svm_epsilon_a) s = &(model.I1); else s = &(model.I3); } else { if (weights[i] < svm_epsilon_a) s = &(model.I4); else s = &(model.I2); } set_insert(i, s); } if (model.W) { for (i=0; i<num_words; i++) { model.W[i] = 0.0; } } if (model.I0.ilength == 0) { model.blow = 1; model.bup = -1; model.iup = k; model.ilow = n; error[k] = -1; error[n] = 1; } else { /* compute bup & blow */ int efrom, nitems; int *items; double e; nitems = model.I0.ilength; items = model.I0.items; for (i=0, e=-1*MAXDOUBLE; i<nitems; i++) { if (e < error[items[i]]) { e = error[items[i]]; efrom = items[i]; } } model.blow = e; model.ilow = efrom; for (i=0, e=MAXDOUBLE; i<nitems; i++) { if (e > error[items[i]]) { e = error[items[i]]; efrom = items[i]; } } model.bup = e; model.iup = efrom; if (model.W) { for (i=0; i<nitems; i++) { for (j=0; j<docs[items[i]]->num_entries; j++) { model.W[docs[items[i]]->entry[j].wi] += yvect[items[i]] * weights[items[i]] * docs[items[i]]->entry[j].weight; } } /* also need to include bound sv's (I2 & I3) */ for (k=0, nitems=model.I2.ilength, items=model.I2.items; k<2; k++, nitems=model.I3.ilength, items=model.I3.items) { for (i=0; i<nitems; i++) { for (j=0; j<docs[items[i]]->num_entries; j++) { model.W[docs[items[i]]->entry[j].wi] += yvect[items[i]] * weights[items[i]] * docs[items[i]]->entry[j].weight; } } } } } if (!model.W) { model.W = *W; } if (svm_weight_style == WEIGHTS_PER_MODEL) { kcache_init(ndocs); } inspect_all = 1; nchanged = 0; changed = 0; while (nchanged || inspect_all) { nchanged = 0; #ifdef DEBUG check_inv(&model,ndocs); #endif model.n_outer ++; PRINT_SMO_PROGRESS(stderr, &model); fflush(stderr); if (1 && inspect_all) { int ub = ndocs; i=j=random() % ndocs; for (k=0; k<2; k++,ub=j,i=0) { for (; i<ub; i++) { nchanged += opt_single(i, &model); #ifdef DEBUG check_inv(&model,ndocs); #endif } } inspect_all = 0; } else { /* greg's modification to keerthi, et al's modification 2 */ /* loop of optimizing all pairwise in a row with all elements * in I0 (just like above, but only those in I0) */ do { nchanged = 0; /* here's the continuous iup/ilow loop */ while (1) { if (!set_lookup(model.iup, &(model.I0))) { error[model.iup] = smo_evaluate_error(&model,model.iup); } if (!set_lookup(model.ilow, &(model.I0))) { error[model.ilow] = smo_evaluate_error(&model,model.ilow); } if (opt_pair(model.iup, model.ilow, &model)) { #ifdef DEBUG check_inv(&model,ndocs); #endif nchanged ++; } else { break; } if (model.bup > model.blow - 2*svm_epsilon_crit) break; } if (nchanged) { changed = 1; } nchanged = 0; /* now inspect all of the elements in I0 */ { int ub = ndocs; i=j=random() % ndocs; for (k=0; k<2; k++,ub=j,i=0) { for (; i<ub; i++) { if (set_lookup(i, &(model.I0))) { nchanged += opt_single(i, &model); #ifdef DEBUG check_inv(&model,ndocs); #endif } } } } } while (nchanged); /* of of the loop */ if (nchanged) { changed = 1; } inspect_all = 1; } /* note: both of the above blocks no when they are done so they flip inspect_all */ if (nchanged) { changed = 1; } } free_set(&model.I0); free_set(&model.I1); free_set(&model.I2); free_set(&model.I3); free_set(&model.I4); if (svm_weight_style == WEIGHTS_PER_MODEL) { kcache_clear(); } if (svm_verbosity > 3) fprintf(stderr,"\n"); //printf("bup=%f, blow=%f\n",model.bup,model.blow); *a_b = (model.bup + model.blow) / 2; if (svm_kernel_type == 0) { for (i=j=0; i<num_words; i++) { if (model.W[i] != 0.0) j++; } } //printf("m1: %d, m2: %d, m3: %d, m4: %d", m1,m2,m3,m4); *nsv = model.nsv; return (changed); }
/*! \brief Nonsymmetric elimination tree * * <pre> * Find the elimination tree for A'*A. * This uses something similar to Liu's algorithm. * It runs in time O(nz(A)*log n) and does not form A'*A. * * Input: * Sparse matrix A. Numeric values are ignored, so any * explicit zeros are treated as nonzero. * Output: * Integer array of parents representing the elimination * tree of the symbolic product A'*A. Each vertex is a * column of A, and nc means a root of the elimination forest. * * John R. Gilbert, Xerox, 10 Dec 1990 * Based on code by JRG dated 1987, 1988, and 1990. * </pre> */ int sp_coletree_dist( int_t *acolst, int_t *acolend, /* column start and end past 1 */ int_t *arow, /* row indices of A */ int_t nr, int_t nc, /* dimension of A */ int_t *parent /* parent in elim tree */ ) { int_t *root; /* root of subtee of etree */ int_t *firstcol; /* first nonzero col in each row*/ int_t rset, cset; int_t row, col; int_t rroot; int_t p; int_t *pp; #if ( DEBUGlevel>=1 ) int_t iam = 0; CHECK_MALLOC(iam, "Enter sp_coletree()"); #endif root = mxCallocInt (nc); initialize_disjoint_sets (nc, &pp); /* Compute firstcol[row] = first nonzero column in row */ firstcol = mxCallocInt (nr); for (row = 0; row < nr; firstcol[row++] = nc); for (col = 0; col < nc; col++) for (p = acolst[col]; p < acolend[col]; p++) { row = arow[p]; firstcol[row] = SUPERLU_MIN(firstcol[row], col); } /* Compute etree by Liu's algorithm for symmetric matrices, except use (firstcol[r],c) in place of an edge (r,c) of A. Thus each row clique in A'*A is replaced by a star centered at its first vertex, which has the same fill. */ for (col = 0; col < nc; col++) { cset = make_set (col, pp); root[cset] = col; parent[col] = nc; /* Matlab */ for (p = acolst[col]; p < acolend[col]; p++) { row = firstcol[arow[p]]; if (row >= col) continue; rset = find (row, pp); rroot = root[rset]; if (rroot != col) { parent[rroot] = col; cset = link (cset, rset, pp); root[cset] = col; } } } SUPERLU_FREE (root); SUPERLU_FREE (firstcol); finalize_disjoint_sets (pp); #if ( DEBUGlevel>=1 ) CHECK_MALLOC(iam, "Exit sp_coletree()"); #endif return 0; } /* SP_COLETREE_DIST */
int main(int argc, char* argv[]) { int a, b, max = -1, p1, p2, flag = 1, k = 0, i; make_set(); while(scanf("%d%d", &a, &b), a != -1 || b != -1) { point[a] = true; point[b] = true; if(a > max) { max = a; } if(b > max) { max = b; } if(max == 0) { puts("Yes\n"); continue; } p1 = find(a); p2 = find(b); if(p1 != p2) { father[p1] = p2; } else { flag = 0; } while(scanf("%d%d", &a, &b), a != 0 || b != 0) { if(flag == 0) { puts("No\n"); break; } if(a > max) { max = a; } if(b > max) { max = b; } point[a] = true; point[b] = true; p1 = find(a); p2 = find(b); if(p1 != p2) father[p1] = p2; else { flag = 0; continue; } } for(i = 0; i < max; i++) { if(point[i]) { if(father[i] == i) k++; } } if(k != 1) flag = 0; if(flag != 0) { puts("Yes\n"); } else { puts("No\n"); } make_set(); k = 0; flag = 1; max = -1; } }
int main(int argc, char **argv) #endif { int i, niter, step; double mflops, t, tmax; logical verified; char class; double tsum[t_last+2], t1[t_last+2], tming[t_last+2], tmaxg[t_last+2]; char *t_recs[t_last+2] = { "total", "rhs", "xsolve", "ysolve", "zsolve", "bpack", "exch", "xcomm", "ycomm", "zcomm", " totcomp", " totcomm" }; //--------------------------------------------------------------------- // Root node reads input file (if it exists) else takes // defaults from parameters //--------------------------------------------------------------------- printf("\n\n NAS Parallel Benchmarks (NPB3.3-OCL-MD) - SP Benchmark\n\n"); FILE *fp; fp = fopen("timer.flag", "r"); timeron = false; if (fp != NULL) { timeron = true; fclose(fp); } if ((fp = fopen("inputsp.data", "r")) != NULL) { int result; printf(" Reading from input file inputsp.data\n"); result = fscanf(fp, "%d", &niter); while (fgetc(fp) != '\n'); result = fscanf(fp, "%*f"); while (fgetc(fp) != '\n'); result = fscanf(fp, "%d%d%d", &grid_points[0], &grid_points[1], &grid_points[2]); fclose(fp); } else { printf(" No input file inputsp.data. Using compiled defaults\n"); niter = NITER_DEFAULT; grid_points[0] = PROBLEM_SIZE; grid_points[1] = PROBLEM_SIZE; grid_points[2] = PROBLEM_SIZE; } setup_opencl(argc, argv); printf(" Size: %4dx%4dx%4d\n", grid_points[0], grid_points[1], grid_points[2]); printf(" Iterations: %4d", niter); if (num_devices != MAXCELLS*MAXCELLS) printf(" WARNING: compiled for %5d devices \n", MAXCELLS*MAXCELLS); printf(" Number of active devices: %5d\n\n", num_devices); make_set(); for (i = 0; i < t_last; i++) { timer_clear(i); } set_constants(); initialize(); lhsinit(); exact_rhs(); compute_buffer_size(5); set_kernel_args(); //--------------------------------------------------------------------- // do one time step to touch all code, and reinitialize //--------------------------------------------------------------------- #ifdef MINIMD_SNUCL_OPTIMIZATIONS // set cmd queue property for(i = 0; i < num_devices; i++) { clSetCommandQueueProperty(cmd_queue[i], CL_QUEUE_AUTO_DEVICE_SELECTION | //CL_QUEUE_ITERATIVE | CL_QUEUE_COMPUTE_INTENSIVE, true, NULL); } #endif adi(); #ifdef MINIMD_SNUCL_OPTIMIZATIONS for(i = 0; i < num_devices; i++) { clSetCommandQueueProperty(cmd_queue[i], 0, true, NULL); } #endif initialize(); //--------------------------------------------------------------------- // Synchronize before placing time stamp //--------------------------------------------------------------------- for (i = 0; i < t_last; i++) { timer_clear(i); } timer_clear(0); timer_start(0); for (step = 1; step <= niter; step++) { if ((step % 20) == 0 || step == 1) { printf(" Time step %4d\n", step); } adi(); } timer_stop(0); t = timer_read(0); verify(niter, &class, &verified); tmax = t; if( tmax != 0.0 ) { mflops = (881.174*(double)( PROBLEM_SIZE*PROBLEM_SIZE*PROBLEM_SIZE ) -4683.91*(double)( PROBLEM_SIZE*PROBLEM_SIZE ) +11484.5*(double)( PROBLEM_SIZE ) -19272.4) * (double)( niter ) / (tmax*1000000.0); } else { mflops = 0.0; } c_print_results("SP", class, grid_points[0], grid_points[1], grid_points[2], niter, tmax, mflops, " floating point", verified, NPBVERSION,COMPILETIME, CS1, CS2, CS3, CS4, CS5, CS6, CS7, clu_GetDeviceTypeName(device_type), device_name, num_devices); if (timeron) { /* for (i = 0; i < t_last; i++) { t1[i] = timer_read(i); } t1[t_xsolve] = t1[t_xsolve] - t1[t_xcomm]; t1[t_ysolve] = t1[t_ysolve] - t1[t_ycomm]; t1[t_zsolve] = t1[t_zsolve] - t1[t_zcomm]; t1[t_last+2] = t1[t_xcomm]+t1[t_ycomm]+t1[t_zcomm]+t1[t_exch]; t1[t_last+1] = t1[t_total] - t1[t_last+2]; MPI_Reduce(&t1, tsum, t_last+2, dp_type, MPI_SUM, 0, comm_setup); MPI_Reduce(&t1, tming, t_last+2, dp_type, MPI_MIN, 0, comm_setup); MPI_Reduce(&t1, tmaxg, t_last+2, dp_type, MPI_MAX, 0, comm_setup); if (node == 0) { printf(" nprocs =%6d minimum maximum average\n", total_nodes); for (i = 0; i < t_last+2; i++) { tsum[i] = tsum[i] / total_nodes; printf(" timer %2d(%8s) : %10.4f %10.4f %10.4f\n", i+1, t_recs[i], tming[i], tmaxg[i], tsum[i]); } } */ } release_opencl(); return 0; }
// resolves collisions between bodies, returning new body count int collide(int n, body bodies[]) { // initialize disjoint set and bodies to include set* bsets[n]; int include[n]; for (int i = 0; i < n; i++) { bsets[i] = make_set(i); include[i] = 1; } // find largest object double maxrad = RADIUS(bodies[0].m); for (int i = 0; i < n; i++) { double rad = RADIUS(bodies[i].m); if (rad > maxrad) maxrad = rad; } // form mesh for collision detection mesh* m = mesh_new(maxrad * 2); for (int i = 0; i < n; i++) mesh_put(m, bodies[i].pos, i); // find collisions for (int i = 0; i < n; i++) { vector ipos = bodies[i].pos; double irad = RADIUS(bodies[i].m); // which bodies are in contact with this one? // look up position in mesh body_list* next = mesh_get(m, ipos, 1); for (body_list* cur = next; cur; cur = next) { // get candidate collider int j = cur->index; vector jpos = bodies[j].pos; double jrad = RADIUS(bodies[j].m); // merge sets of colliding objects if (dist(ipos, jpos) < (irad + jrad) * (irad + jrad)) merge(bsets[i], bsets[j]); // traverse and free next = cur->next; free(cur); } } // free the mesh mesh_free(m); // merge objects for (int i = 0; i < n; i++) { int rootidx = get_value(find(bsets[i])); if (rootidx != i) { include[i] = 0; bodies[rootidx] = body_merge(bodies[rootidx], bodies[i]); } } // free sets for (int i = 0; i < n; i++) free(bsets[i]); // copy down int j = 0; for (int i = 0; i < n; i++) { if (include[i]) bodies[j++] = bodies[i]; } return j; }
double kruskal(struct Graph *graph, int source_vertex, int target_vertex) { PRINT_TEXT("_________________________________________________"); PRINT_TEXT("...STARTING KRUSKAL..."); clock_t start = clock(); double cpu_time; int **adjacency_matrix = allocate_2D_matrix(MAX_VERTICES, MAX_VERTICES); int i, count = 0; struct Edge_Heap *heap = create_edge_heap(); struct Node *node, *node2; for (i = 0; i < graph->totalVertices; i++) { node = graph->list[i].next; while (node) { /*Used to avoid the duplicate addition of edges in the heap*/ if (adjacency_matrix[i][node->vertex] == 0) { adjacency_matrix[i][node->vertex] = 1; adjacency_matrix[node->vertex][i] = -1; insert_edge_heap(heap, i, node->vertex, node->weight); count++; } node = node->next; } } int size = heap->curr_size; heapsort(heap); // PRINT_TEXT("HEAP INSERTIONS DONE"); // PRINT_TEXT_VALUE("TOTAL:", count); struct Graph *new_graph = construct_graph(); struct Set_Arrays *set_data = make_set(); count = 0; i = heap->curr_size - 1; while (i >= 0) { struct Edge *edge = &heap->A[i--]; // while (is_edge_heap_empty(heap) != TRUE) { // struct Edge *edge = get_max(heap); // printf("Considering: %d %d %d\n", edge->vertex1, edge->vertex2, // edge->weight); // PRINT_VALUES(set_data->dad[edge->vertex1], set_data->dad[edge->vertex2]) if (union_by_rank(set_data, edge->vertex1, edge->vertex2)) { count++; link_creation_with_weight(new_graph, edge->vertex1, edge->vertex2, edge->weight); link_creation_with_weight(new_graph, edge->vertex2, edge->vertex1, edge->weight); // PRINT_TEXT("ADDED"); } // for (i = 0; i < MAX_VERTICES; i++) { // PRINT_VALUE(set_data->dad[i]) // } } // PRINT_TEXT_VALUE("TOTAL EDGES ADDED:", count); /*Not including max capacity finding using BFS in calculation*/ cpu_time = ((double) (clock() - start)) / CLOCKS_PER_SEC; // printGraph(new_graph); print_max_capacity_path_krukskal(new_graph, source_vertex, target_vertex); printf("KRUSKAL DONE, TIME TAKEN: %f\n", cpu_time); PRINT_TEXT("_________________________________________________"); for (i = 0; i < MAX_VERTICES; i++) free(adjacency_matrix[i]); free(adjacency_matrix); return cpu_time; }