// @HEADER // ***************************************************************************** // Zoltan Toolkit for Load-balancing, Partitioning, Ordering and Coloring // // Copyright 2012 NTESS and the Zoltan contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #ifdef __cplusplus /* if C++, define the rest of this header file as extern C */ extern "C" { #endif /*****************************************************************************/ /* This file contains the functions that define a path, along with in */ /* and out vertices, through the elements of the coarse grid */ /*****************************************************************************/ #include #include "zz_const.h" #include "zz_sort.h" #include "reftree.h" #include "hsfc_hilbert_const.h" /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ /* Prototypes for functions internal to this file */ static void free_lists(int all_triangles); static int init_stack(ZZ *zz); static int push(int elem, int list, ZZ *zz); static int pop(int list); static int set_neigh(ZOLTAN_ID_PTR vertices, int *num_vert, int all_triangles, ZZ *zz); static int add_neigh_pair(int v,int e1,int e2, ZZ *zz); static int add_to_to_add(int element, ZZ *zz); static int initial_cycle(ZZ *zz); static int add_around_vertices(ZZ *zz); static int add_around_vertex(int vert, ZZ *zz); static int get_element_to_add(void); static int add_to_cycle(int element); static int add_to_cycle_by_element(int elementD, int indexB1, int indexB2); static int try_adding_all(int *ierr, ZZ *zz); static int element_swap(int *ierr, ZZ *zz); static int element_swap_recur(int element, int *ierr, ZZ *zz); static int hanging_nodes(int *ierr, ZZ *zz); static int vertex_swap(int *ierr, ZZ *zz); static int broken_link(int *ierr, ZZ *zz); static int sfc_coarse_grid_path(int nobj,int *num_vert, ZOLTAN_ID_PTR vertices, ZOLTAN_ID_PTR in_vertex, ZOLTAN_ID_PTR out_vertex, double *coords, int *order, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, char *initpath_method, int all_triangles, ZZ *zz); static int find_inout(int elem, int pprev, int prevprev, ZOLTAN_ID_PTR in_vertex, ZOLTAN_ID_PTR out_vertex, ZOLTAN_ID_PTR vertices, int *num_vert, int *first_vert, ZZ *zz); static double InvSierpinski2d(ZZ *zz, double *coord); static double sier2d(double x, double y, int state, int level, double addf, double addp, double peakx, double peaky); /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ /* Variables global to this file */ /* num_neigh[i][j] - number of neighbors of i that share j vertices */ static int **num_neigh; /* neigh[i][j][k] - the kth neighbor that shares j vertices with element i */ static int ***neigh; /* neigh_dim[i][j] - the dimension (of the 3rd index) of neigh[i][j] */ static int **neigh_dim; /* shared_vert[i][j][k][l] - the l'th shared vertex of the kth neighbor ... */ static int ****shared_vert; /* num_elem[i] - number of elements that contain vertex i */ static int *num_elem; /* element_list[i][j] - the jth element that contains vertex i */ static int **element_list; /* element_list_dim - the dimension (of the first index) of element_list */ static int element_list_dim; /* elem_dim[i] - the dimension (of the second index) of element_list[i] */ static int *elem_dim; /* to_add[j][] - an array used as a stack of elements that share j vertices with an element on the path, hence is a candidate to add. to_add[0] is used as a stack of vertices to examine when using add_around_vertices. */ static int **to_add; /* to_add_dim[j] - current size of to_add[j][] */ static int *to_add_dim; /* to_add_ptr[j] - pointer top of stack j */ static int *to_add_ptr; /* in this file the vertices are integers from 0 to nvert-1, but in the input/output arguments they are gids. vertex_gid maps from int to gid */ ZOLTAN_ID_PTR vertex_gid; /* variables for each element which define the path */ static int *prev, *next, *in, *out, *onpath; /* visited marks which elements have already been visited in element_swap */ static int *visited; static int num_obj, path_length; /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static void free_lists(int all_triangles) { /* * Free the space in the lists */ int i, j, k; for (i=0; iProc, yo, "Insufficient memory."); Zoltan_Multifree(__FILE__, __LINE__, 3, &to_add, &to_add_dim, &to_add_ptr); return(ZOLTAN_MEMERR); } for (i=0; i<=MAXVERT; i++) { to_add_ptr[i] = -1; to_add_dim[i] = 100; /* arbitrary initial size */ to_add[i] = (int *) ZOLTAN_MALLOC(sizeof(int)*to_add_dim[i]); if (to_add[i] == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); for (j=0; j= to_add_dim[list]-1) { to_add_dim[list] *= 2; to_add[list] = (int *) ZOLTAN_REALLOC(to_add[list], sizeof(int)*to_add_dim[list]); if (to_add[list] == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); return(ZOLTAN_MEMERR); } } /* push */ to_add_ptr[list]++; to_add[list][to_add_ptr[list]] = elem; return(ZOLTAN_OK); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int pop(int list) { /* * Returns the top of stack number list, or -1 if it is empty */ if (to_add_ptr[list] < 0) { return(-1); } else { to_add_ptr[list]--; return(to_add[list][to_add_ptr[list]+1]); } } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int set_neigh(ZOLTAN_ID_PTR vertices, int *num_vert, int all_triangles, ZZ *zz) { /* * determine the neighbor relationships and list of shared vertices */ char *yo = "set_neigh"; struct Zoltan_Reftree_inthash_node **hashtable; int **temp_element_list; int i, j, k, l, nvert, vert, index, element, vert_count, ierr; /* * first create a list of elements for each vertex */ /* Allocate memory for the element lists. Start by assuming there are more elements than vertices, and no more than 8 elements around each vertex, and increase memory later if needed */ element_list = (int **) ZOLTAN_MALLOC(sizeof(int *)*num_obj); num_elem = (int *) ZOLTAN_MALLOC(sizeof(int)*num_obj); elem_dim = (int *) ZOLTAN_MALLOC(sizeof(int)*num_obj); vertex_gid = ZOLTAN_MALLOC_GID_ARRAY(zz,num_obj); if (element_list == NULL || num_elem == NULL || elem_dim == NULL || vertex_gid == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); Zoltan_Multifree(__FILE__, __LINE__, 3, &element_list, &num_elem, &elem_dim); return(ZOLTAN_MEMERR); } element_list_dim = num_obj; for (i=0; iProc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (j=0; jNum_GID]),DEFAULT_HASH_TABLE_SIZE); if (vert == -1) { Zoltan_Reftree_IntHash_Insert(zz,&(vertices[index*zz->Num_GID]), nvert,hashtable,DEFAULT_HASH_TABLE_SIZE); vert = nvert; nvert++; ZOLTAN_SET_GID(zz, &(vertex_gid[vert*zz->Num_GID]), &(vertices[index*zz->Num_GID])); } /* see if we need more memory for the element_list's */ if (nvert >= element_list_dim) { element_list_dim *= 2; temp_element_list = (int **) ZOLTAN_REALLOC(element_list, sizeof(int *)*element_list_dim); if (temp_element_list != NULL) element_list = temp_element_list; num_elem = (int *) ZOLTAN_REALLOC(num_elem, sizeof(int)*element_list_dim); elem_dim = (int *) ZOLTAN_REALLOC(elem_dim, sizeof(int)*element_list_dim); vertex_gid = ZOLTAN_REALLOC_GID_ARRAY(zz,vertex_gid,element_list_dim); if (temp_element_list == NULL || num_elem == NULL || elem_dim == NULL || vertex_gid == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (j=0; j elem_dim[vert]-1) { elem_dim[vert] *= 2; element_list[vert] = (int *) ZOLTAN_REALLOC(element_list[vert], sizeof(int)*elem_dim[vert]); if (element_list[vert] == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (j=0; jProc, yo, "Insufficient memory."); for (k=0; kProc, yo, "Insufficient memory."); for (l=0; lProc, yo, "Error returned from add_neigh_pair."); return(ierr); } } } } /* free memory for element lists, unless all elements are triangles */ if (!all_triangles) { for (j=0; jProc, yo, "Insufficient memory."); return(ZOLTAN_MEMERR); } for (k=neigh_dim[e1][nshare+1]/2; kProc, yo, "Insufficient memory."); return(ZOLTAN_MEMERR); } } } /* add e2 to the list */ neigh[e1][nshare+1][num_neigh[e1][nshare+1]] = e2; /* copy the shared vertices from the previous list (if nshare!=0) and add v */ for (i=0; iProc, yo, "e1 missing from e2's neighbor list"); return(ZOLTAN_FATAL); } } /* see if we need more memory */ if (neigh_dim[e2][nshare+1] <= num_neigh[e2][nshare+1]) { neigh_dim[e2][nshare+1] *= 2; neigh[e2][nshare+1] = (int *) ZOLTAN_REALLOC(neigh[e2][nshare+1], sizeof(int)*neigh_dim[e2][nshare+1]); shared_vert[e2][nshare+1] = (int **) ZOLTAN_REALLOC(shared_vert[e2][nshare+1], sizeof(int *)*neigh_dim[e2][nshare+1]); if (neigh[e2][nshare+1] == NULL || shared_vert[e2][nshare+1] == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); return(ZOLTAN_MEMERR); } for (k=neigh_dim[e2][nshare+1]/2; kProc, yo, "Insufficient memory."); return(ZOLTAN_MEMERR); } } } /* add e1 to the list */ neigh[e2][nshare+1][num_neigh[e2][nshare+1]] = e1; /* copy the shared vertices from the previous list (if nshare!=0) and add v */ for (i=0; i 1 && num_neigh[elementA][j] == 0) j--; if (j==1) { elementA++; if (elementA >= num_obj) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "couldn't find two elements that share a side"); return(ZOLTAN_FATAL); } } else { elementB = neigh[elementA][j][0]; } } verta = shared_vert[elementA][j][0][0]; vertb = shared_vert[elementA][j][0][1]; /* * create the cycle aAbBa */ prev[elementA] = elementB; in [elementA] = verta; onpath[elementA] = TRUE; out [elementA] = vertb; next[elementA] = elementB; prev[elementB] = elementA; in [elementB] = vertb; onpath[elementB] = TRUE; out [elementB] = verta; next[elementB] = elementA; path_length = 2; /* put all the neighbors onto a to_add list */ ierr = add_to_to_add(elementA,zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); ierr = add_to_to_add(elementB,zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); /* put the in and out vertices onto the vertex to_add (to add around) list */ ierr = push(verta, 0, zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); ierr = push(vertb, 0, zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); return(ZOLTAN_OK); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int add_around_vertices(ZZ *zz) { /* * This function adds elements to the path by adding several elements around * vertices on the path at once. See add_around_vertex for details. */ int vert, element, i, ierr; /* repeat until there are no more vertices to consider */ vert = pop(0); while (vert != -1) { ierr = add_around_vertex(vert,zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); vert = pop(0); } /* if that didn't get all the elements, put elements that are not on the path but are adjacent to an element on the path onto the to_add lists */ if (path_length != num_obj) { element = 0; i = 0; while ((element != 0 && i <= num_obj) || i == 0) { ierr = add_to_to_add(element,zz); if (ierr == ZOLTAN_FATAL || ierr == ZOLTAN_MEMERR) return(ierr); element = next[element]; i++; } } return(ZOLTAN_OK); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int add_around_vertex(int vert, ZZ *zz) { /* * This function adds a contiguous set of elements around vertex vert to * the path. vert must be on the path, there must be more than one * element of vert that is not on the path, and they must share a vertex * other than vert. If the path currently contains A vert B and there are * elements C, D, ..., E which share vertices c, d, ... which are not vert, * then the new path is A vert C c D d ... E vert B * * This comes from the proof that a path exists for triangles. I don't know * what it will do with other element shapes, especially in 3D where the * notion of going around a vertex is not well defined. */ int elementA, elementB, elementC, elementD = 0, elementE; int vertexc = -1, vertexd = -1; int i, j, k, l, num_share, pass, success, ierr; /* find an element with vert as the out vertex */ elementA = -1; for (i=0; i 0 && element == -1) { element = pop(j); j--; } return(element); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int add_to_cycle(int element) { /* * Adds element to the cycle, if it goes in next to one of its neighbors */ int success, num_share, nay; /* * can't add it if it is already on the cycle */ if (onpath[element]) return(FALSE); /* * go through the neighbors until element is added to the cycle or no * more neighbors */ success = FALSE; num_share = MAXVERT; while (num_share > 0 && !success) { nay = 0; while (nay < num_neigh[element][num_share] && !success) { if (onpath[neigh[element][num_share][nay]]) { success = add_to_cycle_by_element(element,num_share,nay); } nay++; } num_share--; } return(success); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int add_to_cycle_by_element(int elementD, int indexB1, int indexB2) { /* * Adds elementD to the cycle next to elementB (given by * neigh[elementD][indexB1][indexB2]) if it meets one of the * criteria for easily slipping it in. */ int elementA, elementB, elementC; int verta, vertb, vertc, vertd, verte, vertf; /* indexA1, indexB1 and indexC1 also tell how many vertices are shared with D */ int indexA1, indexA2, indexC1, indexC2; int j, k, ivert1, ivert2, D_has_b_and_c, success; /* * The nearby segment of the cycle is aAbBcCd where lower case * letters are vertices and upper case letters are elements */ elementB = neigh[elementD][indexB1][indexB2]; elementA = prev[elementB]; elementC = next[elementB]; verta = in [elementA]; vertb = in [elementB]; vertc = out[elementB]; vertd = out[elementC]; /* see if A and C share vertices with D, and get indices if they do */ indexA1 = 0; indexA2 = 0; indexC1 = 0; indexC2 = 0; for (j=1; j<=MAXVERT; j++) { for (k=0; k1 && !success; j--) { for (k=0; k1 && !success; j--) { for (k=0; kProc, yo, "Insufficient memory."); *ierr = ZOLTAN_MEMERR; return(FALSE); } for (i=0; i0 && !success; j--) { for (k=0; k0 && !success; j2--) { for (k2=0; k20 && !success; j3--) { for (k3=0; k30 && !success; j2--) { for (k2=0; k20 && !success; j3--) { for (k3=0; k31 && !success; j--) { for (k=0; k1 && !found_prev; j2--) { for (k2=0; k20 && !success; j--) { for (k=0; k0 && !success1; j1--) { for (k1=0; k1Get_Num_Geom == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Must register ZOLTAN_NUM_GEOM_FN."); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_FATAL); } num_geom = zz->Get_Num_Geom(zz->Get_Num_Geom_Data, &ierr); if (ierr) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from user function Get_Num_Geom."); ZOLTAN_TRACE_EXIT(zz, yo); return(ierr); } if (!(num_geom==2 || num_geom==3)) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Geometry must be either 2D or 3D."); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_FATAL); } /* * For each element, determine its SFC mapping from its coordinates to 1D */ /* find min and max coordinates */ xmin = 1.0e50; xmax = -1.0e50; ymin = 1.0e50; ymax = -1.0e50; zmin = 1.0e50; zmax = -1.0e50; for (i = 0; i < nobj; i++) { if (coords[num_geom*i ] < xmin) xmin = coords[num_geom*i ]; if (coords[num_geom*i ] > xmax) xmax = coords[num_geom*i ]; if (coords[num_geom*i+1] < ymin) ymin = coords[num_geom*i+1]; if (coords[num_geom*i+1] > ymax) ymax = coords[num_geom*i+1]; if (num_geom == 3) { if (coords[num_geom*i+2] < zmin) zmin = coords[num_geom*i+2]; if (coords[num_geom*i+2] > zmax) zmax = coords[num_geom*i+2]; } } /* allocate space for the SFC coordinate */ sfccoord = (double *) ZOLTAN_MALLOC(nobj*sizeof(double)); if (sfccoord == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_MEMERR); } /* scale the coordinates to [0,1] and compute the SFC mapping */ for (i=0; iProc, yo, "Sierpinski SFC only applies to 2D problems."); ZOLTAN_FREE(&sfccoord); ZOLTAN_TRACE_EXIT(zz, yo); return(ierr); } } else { /* Hilbert */ if (num_geom == 2) { sfccoord[i] = Zoltan_HSFC_InvHilbert2d(zz,loc_coords); } else { loc_coords[2] = (coords[num_geom*i+2]-zmin)/(zmax-zmin); sfccoord[i] = Zoltan_HSFC_InvHilbert3d(zz,loc_coords); } } } /* * sort the SFC coordinates to get the order of the elements */ ind = (int *) ZOLTAN_MALLOC(nobj*sizeof(int)); if (ind == NULL) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Insufficient memory."); ZOLTAN_FREE(&sfccoord); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_MEMERR); } for (i=0; iProc, yo, "Insufficient memory."); Zoltan_Multifree(__FILE__,__LINE__, 2, &sfccoord, &ind); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_MEMERR); } first_vert[0] = 0; for (i=1; iNum_GID*elem]), &(vertices[zz->Num_GID*first_vert[elem]])); for (i=1; iProc, yo, "Error returned from find_inout."); Zoltan_Multifree(__FILE__,__LINE__, 3, &sfccoord, &ind, &first_vert); ZOLTAN_TRACE_EXIT(zz, yo); return(ierr); } } if (ZOLTAN_EQ_GID(zz,&(in_vertex[zz->Num_GID*elem]), &( vertices[zz->Num_GID*first_vert[elem]]))) { ZOLTAN_SET_GID(zz,&(out_vertex[zz->Num_GID*elem]), &( vertices[zz->Num_GID*(first_vert[elem]+1)])); } else { ZOLTAN_SET_GID(zz,&(out_vertex[zz->Num_GID*elem]), &( vertices[zz->Num_GID*first_vert[elem]])); } Zoltan_Multifree(__FILE__,__LINE__, 3, &sfccoord, &ind, &first_vert); return(ZOLTAN_OK); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static int find_inout(int elem, int pprev, int prevprev, ZOLTAN_ID_PTR in_vertex, ZOLTAN_ID_PTR out_vertex, ZOLTAN_ID_PTR vertices, int *num_vert, int *first_vert, ZZ *zz) { /* * This routine finds and sets an in-vertex for elem and out-vertex for pprev. * Usually they will be the same vertex, but if necessary they will be * different which will cause the Hamiltonian path to be disconnected. * If necessary and possible, this routine will change the in-vertex of pprev * and out-vertex of prevprev to make in and out be the same. */ char *yo = "find_inout"; int looking, i, j, ngid, shared; ZOLTAN_TRACE_ENTER(zz, yo); ngid = zz->Num_GID; /* * first look for a shared vertex of elem and pprev that is not the * in-vertex of pprev */ looking = TRUE; for (i=0; i 1.0) || (coord[1] < 0.0) || (coord[1] > 1.0)) ZOLTAN_PRINT_ERROR (zz->Proc, yo, "Spatial Coordinates out of range."); /* begin recursion that computes key */ return sier2d(coord[0], coord[1], 0, 0, (double) 0.5, (double) 0.5, (double) 0.0, (double) 0.0); } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ static double sier2d(double x, double y, int state, int level, double addf, double addp, double peakx, double peaky) { static int MAXLEV = 24; /* * Recursively compute the Sierpinski mapping from the unit square to the * unit interval */ if (level >= MAXLEV) return (double) 0; switch (state) { case 0: if (y > x) return sier2d(x,y,1,level+1,addf/2,addp,(double) 0.0, (double) 1.0); else return sier2d(x,y,2,level+1,addf/2,addp,(double) 1.0, (double) 0.0)+addf; case 1: if ( y-peaky < -(x-peakx) ) return sier2d(x,y,5,level+1,addf/2,addp,peakx+addp,peaky-addp); else return sier2d(x,y,6,level+1,addf/2,addp,peakx+addp,peaky-addp) + addf; case 2: if ( y-peaky >= -(x-peakx) ) return sier2d(x,y,7,level+1,addf/2,addp,peakx-addp,peaky+addp); else return sier2d(x,y,8,level+1,addf/2,addp,peakx-addp,peaky+addp) + addf; case 3: if ( y-peaky <= x-peakx ) return sier2d(x,y,8,level+1,addf/2,addp,peakx+addp,peaky+addp); else return sier2d(x,y,5,level+1,addf/2,addp,peakx+addp,peaky+addp) + addf; case 4: if ( y-peaky > x-peakx ) return sier2d(x,y,6,level+1,addf/2,addp,peakx-addp,peaky-addp); else return sier2d(x,y,7,level+1,addf/2,addp,peakx-addp,peaky-addp) + addf; case 5: if ( y < peaky ) return sier2d(x,y,1,level+1,addf/2,addp/2,peakx-addp,peaky); else return sier2d(x,y,3,level+1,addf/2,addp/2,peakx-addp,peaky) + addf; case 6: if ( x < peakx ) return sier2d(x,y,4,level+1,addf/2,addp/2,peakx,peaky+addp); else return sier2d(x,y,1,level+1,addf/2,addp/2,peakx,peaky+addp) + addf; case 7: if ( y >= peaky ) return sier2d(x,y,2,level+1,addf/2,addp/2,peakx+addp,peaky); else return sier2d(x,y,4,level+1,addf/2,addp/2,peakx+addp,peaky) + addf; case 8: if ( x >= peakx ) return sier2d(x,y,3,level+1,addf/2,addp/2,peakx,peaky-addp); else return sier2d(x,y,2,level+1,addf/2,addp/2,peakx,peaky-addp) + addf; } return (double) 0; } /*****************************************************************************/ /*****************************************************************************/ /*****************************************************************************/ int Zoltan_Reftree_Coarse_Grid_Path(int nobj, int *num_vert, ZOLTAN_ID_PTR vertices, ZOLTAN_ID_PTR in_vertex, ZOLTAN_ID_PTR out_vertex, double *coords, int *order, ZOLTAN_ID_PTR gids, ZOLTAN_ID_PTR lids, char *initpath_method, ZZ *zz) { /* * Find a cycle through all the elements of the coarse grid which passes * from element to element through vertices, not going in and out of an * element through the same vertex. */ char *yo = "Zoltan_Reftree_Coarse_Grid_Path"; int all_triangles = 0, i, element, success, ierr; ZOLTAN_TRACE_ENTER(zz, yo); num_obj = nobj; ierr = ZOLTAN_OK; /* check for a non-triangle */ all_triangles = TRUE; for (element=0; elementProc, yo, "Error returned from init_stack."); free_lists(all_triangles); return(ierr); } /* determine the neighbor relationships and list of shared vertices */ ierr = set_neigh(vertices, num_vert, all_triangles, zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from set_neigh."); free_lists(all_triangles); return(ierr); } /* create the initial cycle */ ierr = initial_cycle(zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from initial_cycle."); free_lists(all_triangles); return(ierr); } /* if the grid is all triangles, add elements around vertices first */ if (all_triangles) { ierr = add_around_vertices(zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from add_around_vertices."); free_lists(all_triangles); return(ierr); } } /* repeat until all elements are on the path */ while (path_length < num_obj) { /* while there are elements on the to_add list, get one, add it if you can, and put its neighbors that are not on the path onto the to_add list */ while ( (element = get_element_to_add() ) != -1 && path_length < num_obj) { success = add_to_cycle(element); if (success) { ierr = add_to_to_add(element,zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from add_to_to_add."); free_lists(all_triangles); return(ierr); } } } /* if all elements are on the path, then done */ success = (path_length == num_obj); /* if not done, then first try adding all elements that are not on the cycle to make sure I didn't miss any */ if (!success) { success = try_adding_all(&ierr,zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from try_adding_all."); free_lists(all_triangles); return(ierr); } } /* if the elements are triangles, then swap which elements are on the path to start it up again */ if (!success && all_triangles) { success = element_swap(&ierr, zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from element_swap."); free_lists(all_triangles); return(ierr); } } /* look for a vertex in the path that can be replaced to start it up again */ if (!success) { success = vertex_swap(&ierr, zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from vertex_swap."); free_lists(all_triangles); return(ierr); } } /* look for hanging node type relationships that can get it started again */ if (!success) { success = hanging_nodes(&ierr,zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from hanging_nodes."); free_lists(all_triangles); return(ierr); } } /* for non-triangles, try swapping which elements are on the path */ if (!success && !all_triangles) { success = element_swap(&ierr, zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from element_swap."); free_lists(all_triangles); return(ierr); } } /* give up and put in a broken link */ if (!success) { success = broken_link(&ierr, zz); if (ierr==ZOLTAN_FATAL || ierr==ZOLTAN_MEMERR) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "Error returned from broken_link."); free_lists(all_triangles); return(ierr); } } /* broken_link should always succeed, so if success is still false, something went wrong */ if (!success) { ZOLTAN_PRINT_ERROR(zz->Proc, yo, "failed to insert broken link"); ZOLTAN_TRACE_EXIT(zz, yo); free_lists(all_triangles); return(ZOLTAN_FATAL); } } /* set the return values */ element = 0; i = 0; while ((element != 0 && i <= num_obj) || i == 0) { order[element] = i; ZOLTAN_SET_GID(zz,&(in_vertex[element*zz->Num_GID]), &(vertex_gid[in[element]*zz->Num_GID])); ZOLTAN_SET_GID(zz,&(out_vertex[element*zz->Num_GID]), &(vertex_gid[out[element]*zz->Num_GID])); element = next[element]; i++; } free_lists(all_triangles); ZOLTAN_TRACE_EXIT(zz, yo); return(ZOLTAN_OK); } #ifdef __cplusplus } /* closing bracket for extern "C" */ #endif