1 #include <petsc/private/petscimpl.h> 2 #include <../src/ksp/pc/impls/bddc/bddcprivate.h> 3 #include <../src/ksp/pc/impls/bddc/bddcstructs.h> 4 5 PetscErrorCode PCBDDCGraphGetDirichletDofsB(PCBDDCGraph graph, IS* dirdofs) 6 { 7 PetscErrorCode ierr; 8 9 PetscFunctionBegin; 10 if (graph->dirdofsB) { 11 ierr = PetscObjectReference((PetscObject)graph->dirdofsB);CHKERRQ(ierr); 12 } else if (graph->has_dirichlet) { 13 PetscInt i,size; 14 PetscInt *dirdofs_idxs; 15 16 size = 0; 17 for (i=0;i<graph->nvtxs;i++) { 18 if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++; 19 } 20 21 ierr = PetscMalloc1(size,&dirdofs_idxs);CHKERRQ(ierr); 22 size = 0; 23 for (i=0;i<graph->nvtxs;i++) { 24 if (graph->count[i] && graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i; 25 } 26 ierr = ISCreateGeneral(PETSC_COMM_SELF,size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofsB);CHKERRQ(ierr); 27 ierr = PetscObjectReference((PetscObject)graph->dirdofsB);CHKERRQ(ierr); 28 } 29 *dirdofs = graph->dirdofsB; 30 PetscFunctionReturn(0); 31 } 32 33 PetscErrorCode PCBDDCGraphGetDirichletDofs(PCBDDCGraph graph, IS* dirdofs) 34 { 35 PetscErrorCode ierr; 36 37 PetscFunctionBegin; 38 if (graph->dirdofs) { 39 ierr = PetscObjectReference((PetscObject)graph->dirdofs);CHKERRQ(ierr); 40 } else if (graph->has_dirichlet) { 41 PetscInt i,size; 42 PetscInt *dirdofs_idxs; 43 44 size = 0; 45 for (i=0;i<graph->nvtxs;i++) { 46 if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) size++; 47 } 48 49 ierr = PetscMalloc1(size,&dirdofs_idxs);CHKERRQ(ierr); 50 size = 0; 51 for (i=0;i<graph->nvtxs;i++) { 52 if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK) dirdofs_idxs[size++] = i; 53 } 54 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)graph->l2gmap),size,dirdofs_idxs,PETSC_OWN_POINTER,&graph->dirdofs);CHKERRQ(ierr); 55 ierr = PetscObjectReference((PetscObject)graph->dirdofs);CHKERRQ(ierr); 56 } 57 *dirdofs = graph->dirdofs; 58 PetscFunctionReturn(0); 59 } 60 61 PetscErrorCode PCBDDCGraphASCIIView(PCBDDCGraph graph, PetscInt verbosity_level, PetscViewer viewer) 62 { 63 PetscInt i,j,tabs; 64 PetscInt* queue_in_global_numbering; 65 PetscErrorCode ierr; 66 67 PetscFunctionBegin; 68 ierr = PetscViewerASCIIPushSynchronized(viewer);CHKERRQ(ierr); 69 ierr = PetscViewerASCIIGetTab(viewer,&tabs);CHKERRQ(ierr); 70 ierr = PetscViewerASCIIPrintf(viewer,"--------------------------------------------------\n");CHKERRQ(ierr); 71 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 72 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Local BDDC graph for subdomain %04d\n",PetscGlobalRank);CHKERRQ(ierr); 73 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Number of vertices %d\n",graph->nvtxs);CHKERRQ(ierr); 74 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Custom minimal size %d\n",graph->custom_minimal_size);CHKERRQ(ierr); 75 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Max count %d\n",graph->maxcount);CHKERRQ(ierr); 76 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Topological two dim? %d (set %d)\n",graph->twodim,graph->twodimset);CHKERRQ(ierr); 77 if (verbosity_level > 2) { 78 for (i=0;i<graph->nvtxs;i++) { 79 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"%d:\n",i);CHKERRQ(ierr); 80 ierr = PetscViewerASCIISynchronizedPrintf(viewer," which_dof: %d\n",graph->which_dof[i]);CHKERRQ(ierr); 81 ierr = PetscViewerASCIISynchronizedPrintf(viewer," special_dof: %d\n",graph->special_dof[i]);CHKERRQ(ierr); 82 ierr = PetscViewerASCIISynchronizedPrintf(viewer," neighbours: %d\n",graph->count[i]);CHKERRQ(ierr); 83 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 84 if (graph->count[i]) { 85 ierr = PetscViewerASCIISynchronizedPrintf(viewer," set of neighbours:");CHKERRQ(ierr); 86 for (j=0;j<graph->count[i];j++) { 87 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[i][j]);CHKERRQ(ierr); 88 } 89 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 90 } 91 ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr); 92 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 93 if (graph->mirrors) { 94 ierr = PetscViewerASCIISynchronizedPrintf(viewer," mirrors: %d\n",graph->mirrors[i]);CHKERRQ(ierr); 95 if (graph->mirrors[i]) { 96 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 97 ierr = PetscViewerASCIISynchronizedPrintf(viewer," set of mirrors:");CHKERRQ(ierr); 98 for (j=0;j<graph->mirrors[i];j++) { 99 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->mirrors_set[i][j]);CHKERRQ(ierr); 100 } 101 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 102 ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr); 103 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 104 } 105 } 106 if (verbosity_level > 3) { 107 if (graph->xadj) { 108 ierr = PetscViewerASCIISynchronizedPrintf(viewer," local adj list:");CHKERRQ(ierr); 109 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 110 for (j=graph->xadj[i];j<graph->xadj[i+1];j++) { 111 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->adjncy[j]);CHKERRQ(ierr); 112 } 113 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 114 ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr); 115 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 116 } else { 117 ierr = PetscViewerASCIISynchronizedPrintf(viewer," no adj info\n");CHKERRQ(ierr); 118 } 119 } 120 if (graph->n_local_subs) { 121 ierr = PetscViewerASCIISynchronizedPrintf(viewer," local sub id: %d\n",graph->local_subs[i]);CHKERRQ(ierr); 122 } 123 ierr = PetscViewerASCIISynchronizedPrintf(viewer," interface subset id: %d\n",graph->subset[i]);CHKERRQ(ierr); 124 if (graph->subset[i] && graph->subset_ncc) { 125 ierr = PetscViewerASCIISynchronizedPrintf(viewer," ncc for subset: %d\n",graph->subset_ncc[graph->subset[i]-1]);CHKERRQ(ierr); 126 } 127 } 128 } 129 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"Total number of connected components %d\n",graph->ncc);CHKERRQ(ierr); 130 ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_in_global_numbering);CHKERRQ(ierr); 131 ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_in_global_numbering);CHKERRQ(ierr); 132 for (i=0;i<graph->ncc;i++) { 133 PetscInt node_num=graph->queue[graph->cptr[i]]; 134 PetscBool printcc = PETSC_FALSE; 135 ierr = PetscViewerASCIISynchronizedPrintf(viewer," cc %d (size %d, fid %d, neighs:",i,graph->cptr[i+1]-graph->cptr[i],graph->which_dof[node_num]);CHKERRQ(ierr); 136 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);CHKERRQ(ierr); 137 for (j=0;j<graph->count[node_num];j++) { 138 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d",graph->neighbours_set[node_num][j]);CHKERRQ(ierr); 139 } 140 if (verbosity_level > 1) { 141 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"):");CHKERRQ(ierr); 142 if (graph->twodim || graph->count[node_num] > 1 || (graph->count[node_num] == 1 && graph->special_dof[node_num] == PCBDDCGRAPH_NEUMANN_MARK)) { 143 printcc = PETSC_TRUE; 144 } 145 if (printcc) { 146 for (j=graph->cptr[i];j<graph->cptr[i+1];j++) { 147 ierr = PetscViewerASCIISynchronizedPrintf(viewer," %d (%d)",graph->queue[j],queue_in_global_numbering[j]);CHKERRQ(ierr); 148 } 149 } 150 } else { 151 ierr = PetscViewerASCIISynchronizedPrintf(viewer,")");CHKERRQ(ierr); 152 } 153 ierr = PetscViewerASCIISynchronizedPrintf(viewer,"\n");CHKERRQ(ierr); 154 ierr = PetscViewerASCIISetTab(viewer,tabs);CHKERRQ(ierr); 155 ierr = PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);CHKERRQ(ierr); 156 } 157 ierr = PetscFree(queue_in_global_numbering);CHKERRQ(ierr); 158 ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 159 PetscFunctionReturn(0); 160 } 161 162 PetscErrorCode PCBDDCGraphRestoreCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS) 163 { 164 PetscInt i; 165 PetscErrorCode ierr; 166 167 PetscFunctionBegin; 168 if (n_faces) { 169 if (FacesIS) { 170 for (i=0;i<*n_faces;i++) { 171 ierr = ISDestroy(&((*FacesIS)[i]));CHKERRQ(ierr); 172 } 173 ierr = PetscFree(*FacesIS);CHKERRQ(ierr); 174 } 175 *n_faces = 0; 176 } 177 if (n_edges) { 178 if (EdgesIS) { 179 for (i=0;i<*n_edges;i++) { 180 ierr = ISDestroy(&((*EdgesIS)[i]));CHKERRQ(ierr); 181 } 182 ierr = PetscFree(*EdgesIS);CHKERRQ(ierr); 183 } 184 *n_edges = 0; 185 } 186 if (VerticesIS) { 187 ierr = ISDestroy(VerticesIS);CHKERRQ(ierr); 188 } 189 PetscFunctionReturn(0); 190 } 191 192 PetscErrorCode PCBDDCGraphGetCandidatesIS(PCBDDCGraph graph, PetscInt *n_faces, IS *FacesIS[], PetscInt *n_edges, IS *EdgesIS[], IS *VerticesIS) 193 { 194 IS *ISForFaces,*ISForEdges,ISForVertices; 195 PetscInt i,nfc,nec,nvc,*idx,*mark; 196 PetscErrorCode ierr; 197 198 PetscFunctionBegin; 199 ierr = PetscCalloc1(graph->ncc,&mark);CHKERRQ(ierr); 200 /* loop on ccs to evalute number of faces, edges and vertices */ 201 nfc = 0; 202 nec = 0; 203 nvc = 0; 204 for (i=0;i<graph->ncc;i++) { 205 PetscInt repdof = graph->queue[graph->cptr[i]]; 206 if (graph->cptr[i+1]-graph->cptr[i] > graph->custom_minimal_size && graph->count[repdof] < graph->maxcount) { 207 if (!graph->twodim && graph->count[repdof] == 1 && graph->special_dof[repdof] != PCBDDCGRAPH_NEUMANN_MARK) { 208 nfc++; 209 mark[i] = 2; 210 } else { 211 nec++; 212 mark[i] = 1; 213 } 214 } else { 215 nvc += graph->cptr[i+1]-graph->cptr[i]; 216 } 217 } 218 219 /* allocate IS arrays for faces, edges. Vertices need a single index set. */ 220 if (FacesIS) { 221 ierr = PetscMalloc1(nfc,&ISForFaces);CHKERRQ(ierr); 222 } 223 if (EdgesIS) { 224 ierr = PetscMalloc1(nec,&ISForEdges);CHKERRQ(ierr); 225 } 226 if (VerticesIS) { 227 ierr = PetscMalloc1(nvc,&idx);CHKERRQ(ierr); 228 } 229 230 /* loop on ccs to compute index sets for faces and edges */ 231 if (!graph->queue_sorted) { 232 PetscInt *queue_global; 233 234 ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_global);CHKERRQ(ierr); 235 ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);CHKERRQ(ierr); 236 for (i=0;i<graph->ncc;i++) { 237 ierr = PetscSortIntWithArray(graph->cptr[i+1]-graph->cptr[i],&queue_global[graph->cptr[i]],&graph->queue[graph->cptr[i]]);CHKERRQ(ierr); 238 } 239 ierr = PetscFree(queue_global);CHKERRQ(ierr); 240 graph->queue_sorted = PETSC_TRUE; 241 } 242 nfc = 0; 243 nec = 0; 244 for (i=0;i<graph->ncc;i++) { 245 if (mark[i] == 2) { 246 if (FacesIS) { 247 ierr = ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForFaces[nfc]);CHKERRQ(ierr); 248 } 249 nfc++; 250 } else if (mark[i] == 1) { 251 if (EdgesIS) { 252 ierr = ISCreateGeneral(PETSC_COMM_SELF,graph->cptr[i+1]-graph->cptr[i],&graph->queue[graph->cptr[i]],PETSC_USE_POINTER,&ISForEdges[nec]);CHKERRQ(ierr); 253 } 254 nec++; 255 } 256 } 257 258 /* index set for vertices */ 259 if (VerticesIS) { 260 nvc = 0; 261 for (i=0;i<graph->ncc;i++) { 262 if (!mark[i]) { 263 PetscInt j; 264 265 for (j=graph->cptr[i];j<graph->cptr[i+1];j++) { 266 idx[nvc]=graph->queue[j]; 267 nvc++; 268 } 269 } 270 } 271 /* sort vertex set (by local ordering) */ 272 ierr = PetscSortInt(nvc,idx);CHKERRQ(ierr); 273 ierr = ISCreateGeneral(PETSC_COMM_SELF,nvc,idx,PETSC_OWN_POINTER,&ISForVertices);CHKERRQ(ierr); 274 } 275 ierr = PetscFree(mark);CHKERRQ(ierr); 276 277 /* get back info */ 278 if (n_faces) *n_faces = nfc; 279 if (FacesIS) *FacesIS = ISForFaces; 280 if (n_edges) *n_edges = nec; 281 if (EdgesIS) *EdgesIS = ISForEdges; 282 if (VerticesIS) *VerticesIS = ISForVertices; 283 PetscFunctionReturn(0); 284 } 285 286 PetscErrorCode PCBDDCGraphComputeConnectedComponents(PCBDDCGraph graph) 287 { 288 PetscBool adapt_interface_reduced; 289 MPI_Comm interface_comm; 290 PetscMPIInt size; 291 PetscInt i; 292 PetscErrorCode ierr; 293 294 PetscFunctionBegin; 295 /* compute connected components locally */ 296 ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&interface_comm);CHKERRQ(ierr); 297 ierr = PCBDDCGraphComputeConnectedComponentsLocal(graph);CHKERRQ(ierr); 298 /* check consistency of connected components among neighbouring subdomains -> it adapt them in case it is needed */ 299 ierr = MPI_Comm_size(interface_comm,&size);CHKERRQ(ierr); 300 adapt_interface_reduced = PETSC_FALSE; 301 if (size > 1) { 302 PetscInt i; 303 PetscBool adapt_interface = PETSC_FALSE; 304 for (i=0;i<graph->n_subsets;i++) { 305 /* We are not sure that on a given subset of the local interface, 306 with two connected components, the latters be the same among sharing subdomains */ 307 if (graph->subset_ncc[i] > 1) { 308 adapt_interface = PETSC_TRUE; 309 break; 310 } 311 } 312 ierr = MPIU_Allreduce(&adapt_interface,&adapt_interface_reduced,1,MPIU_BOOL,MPI_LOR,interface_comm);CHKERRQ(ierr); 313 } 314 315 if (graph->n_subsets && adapt_interface_reduced) { 316 PetscBT subset_cc_adapt; 317 MPI_Request *send_requests,*recv_requests; 318 PetscInt *send_buffer,*recv_buffer; 319 PetscInt sum_requests,start_of_recv,start_of_send; 320 PetscInt *cum_recv_counts; 321 PetscInt *labels; 322 PetscInt ncc,cum_queue,mss,mns,j,k,s; 323 PetscInt **refine_buffer=NULL,*private_labels = NULL; 324 325 ierr = PetscMalloc1(graph->nvtxs,&labels);CHKERRQ(ierr); 326 ierr = PetscMemzero(labels,graph->nvtxs*sizeof(*labels));CHKERRQ(ierr); 327 for (i=0;i<graph->ncc;i++) 328 for (j=graph->cptr[i];j<graph->cptr[i+1];j++) 329 labels[graph->queue[j]] = i; 330 331 /* allocate some space */ 332 ierr = PetscMalloc1(graph->n_subsets+1,&cum_recv_counts);CHKERRQ(ierr); 333 ierr = PetscMemzero(cum_recv_counts,(graph->n_subsets+1)*sizeof(*cum_recv_counts));CHKERRQ(ierr); 334 335 /* first count how many neighbours per connected component I will receive from */ 336 cum_recv_counts[0] = 0; 337 for (i=0;i<graph->n_subsets;i++) cum_recv_counts[i+1] = cum_recv_counts[i]+graph->count[graph->subset_idxs[i][0]]; 338 ierr = PetscMalloc1(cum_recv_counts[graph->n_subsets],&recv_buffer);CHKERRQ(ierr); 339 ierr = PetscMalloc2(cum_recv_counts[graph->n_subsets],&send_requests,cum_recv_counts[graph->n_subsets],&recv_requests);CHKERRQ(ierr); 340 for (i=0;i<cum_recv_counts[graph->n_subsets];i++) { 341 send_requests[i] = MPI_REQUEST_NULL; 342 recv_requests[i] = MPI_REQUEST_NULL; 343 } 344 345 /* exchange with my neighbours the number of my connected components on the subset of interface */ 346 sum_requests = 0; 347 for (i=0;i<graph->n_subsets;i++) { 348 PetscMPIInt neigh,tag; 349 PetscInt count,*neighs; 350 351 count = graph->count[graph->subset_idxs[i][0]]; 352 neighs = graph->neighbours_set[graph->subset_idxs[i][0]]; 353 ierr = PetscMPIIntCast(2*graph->subset_ref_node[i],&tag);CHKERRQ(ierr); 354 for (k=0;k<count;k++) { 355 ierr = PetscMPIIntCast(neighs[k],&neigh);CHKERRQ(ierr); 356 ierr = MPI_Isend(&graph->subset_ncc[i],1,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr); 357 ierr = MPI_Irecv(&recv_buffer[sum_requests],1,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr); 358 sum_requests++; 359 } 360 } 361 ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr); 362 ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr); 363 364 /* determine the subsets I have to adapt (those having more than 1 cc) */ 365 ierr = PetscBTCreate(graph->n_subsets,&subset_cc_adapt);CHKERRQ(ierr); 366 ierr = PetscBTMemzero(graph->n_subsets,subset_cc_adapt);CHKERRQ(ierr); 367 for (i=0;i<graph->n_subsets;i++) { 368 if (graph->subset_ncc[i] > 1) { 369 ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr); 370 continue; 371 } 372 for (j=cum_recv_counts[i];j<cum_recv_counts[i+1];j++){ 373 if (recv_buffer[j] > 1) { 374 ierr = PetscBTSet(subset_cc_adapt,i);CHKERRQ(ierr); 375 break; 376 } 377 } 378 } 379 ierr = PetscFree(recv_buffer);CHKERRQ(ierr); 380 381 /* determine send/recv buffers sizes */ 382 j = 0; 383 mss = 0; 384 for (i=0;i<graph->n_subsets;i++) { 385 if (PetscBTLookup(subset_cc_adapt,i)) { 386 j += graph->subset_size[i]; 387 mss = PetscMax(graph->subset_size[i],mss); 388 } 389 } 390 k = 0; 391 mns = 0; 392 for (i=0;i<graph->n_subsets;i++) { 393 if (PetscBTLookup(subset_cc_adapt,i)) { 394 k += (cum_recv_counts[i+1]-cum_recv_counts[i])*graph->subset_size[i]; 395 mns = PetscMax(cum_recv_counts[i+1]-cum_recv_counts[i],mns); 396 } 397 } 398 ierr = PetscMalloc2(j,&send_buffer,k,&recv_buffer);CHKERRQ(ierr); 399 400 /* fill send buffer (order matters: subset_idxs ordered by global ordering) */ 401 j = 0; 402 for (i=0;i<graph->n_subsets;i++) 403 if (PetscBTLookup(subset_cc_adapt,i)) 404 for (k=0;k<graph->subset_size[i];k++) 405 send_buffer[j++] = labels[graph->subset_idxs[i][k]]; 406 407 /* now exchange the data */ 408 start_of_recv = 0; 409 start_of_send = 0; 410 sum_requests = 0; 411 for (i=0;i<graph->n_subsets;i++) { 412 if (PetscBTLookup(subset_cc_adapt,i)) { 413 PetscMPIInt neigh,tag; 414 PetscInt size_of_send = graph->subset_size[i]; 415 416 j = graph->subset_idxs[i][0]; 417 ierr = PetscMPIIntCast(2*graph->subset_ref_node[i]+1,&tag);CHKERRQ(ierr); 418 for (k=0;k<graph->count[j];k++) { 419 ierr = PetscMPIIntCast(graph->neighbours_set[j][k],&neigh);CHKERRQ(ierr); 420 ierr = MPI_Isend(&send_buffer[start_of_send],size_of_send,MPIU_INT,neigh,tag,interface_comm,&send_requests[sum_requests]);CHKERRQ(ierr); 421 ierr = MPI_Irecv(&recv_buffer[start_of_recv],size_of_send,MPIU_INT,neigh,tag,interface_comm,&recv_requests[sum_requests]);CHKERRQ(ierr); 422 start_of_recv += size_of_send; 423 sum_requests++; 424 } 425 start_of_send += size_of_send; 426 } 427 } 428 ierr = MPI_Waitall(sum_requests,recv_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr); 429 430 /* refine connected components */ 431 start_of_recv = 0; 432 /* allocate some temporary space */ 433 if (mss) { 434 ierr = PetscMalloc1(mss,&refine_buffer);CHKERRQ(ierr); 435 ierr = PetscMalloc2(mss*(mns+1),&refine_buffer[0],mss,&private_labels);CHKERRQ(ierr); 436 } 437 ncc = 0; 438 cum_queue = 0; 439 graph->cptr[0] = 0; 440 for (i=0;i<graph->n_subsets;i++) { 441 if (PetscBTLookup(subset_cc_adapt,i)) { 442 PetscInt subset_counter = 0; 443 PetscInt sharingprocs = cum_recv_counts[i+1]-cum_recv_counts[i]+1; /* count myself */ 444 PetscInt buffer_size = graph->subset_size[i]; 445 446 /* compute pointers */ 447 for (j=1;j<buffer_size;j++) refine_buffer[j] = refine_buffer[j-1] + sharingprocs; 448 /* analyze contributions from subdomains that share the i-th subset 449 The stricture of refine_buffer is suitable to find intersections of ccs among sharingprocs. 450 supposing the current subset is shared by 3 processes and has dimension 5 with global dofs 0,1,2,3,4 (local 0,4,3,1,2) 451 sharing procs connected components: 452 neigh 0: [0 1 4], [2 3], labels [4,7] (2 connected components) 453 neigh 1: [0 1], [2 3 4], labels [3 2] (2 connected components) 454 neigh 2: [0 4], [1], [2 3], labels [1 5 6] (3 connected components) 455 refine_buffer will be filled as: 456 [ 4, 3, 1; 457 4, 2, 1; 458 7, 2, 6; 459 4, 3, 5; 460 7, 2, 6; ]; 461 The connected components in local ordering are [0], [1], [2 3], [4] */ 462 /* fill temp_buffer */ 463 for (k=0;k<buffer_size;k++) refine_buffer[k][0] = labels[graph->subset_idxs[i][k]]; 464 for (j=0;j<sharingprocs-1;j++) { 465 for (k=0;k<buffer_size;k++) refine_buffer[k][j+1] = recv_buffer[start_of_recv+k]; 466 start_of_recv += buffer_size; 467 } 468 ierr = PetscMemzero(private_labels,buffer_size*sizeof(PetscInt));CHKERRQ(ierr); 469 for (j=0;j<buffer_size;j++) { 470 if (!private_labels[j]) { /* found a new cc */ 471 PetscBool same_set; 472 473 graph->cptr[ncc] = cum_queue; 474 ncc++; 475 subset_counter++; 476 private_labels[j] = subset_counter; 477 graph->queue[cum_queue++] = graph->subset_idxs[i][j]; 478 for (k=j+1;k<buffer_size;k++) { /* check for other nodes in new cc */ 479 same_set = PETSC_TRUE; 480 for (s=0;s<sharingprocs;s++) { 481 if (refine_buffer[j][s] != refine_buffer[k][s]) { 482 same_set = PETSC_FALSE; 483 break; 484 } 485 } 486 if (same_set) { 487 private_labels[k] = subset_counter; 488 graph->queue[cum_queue++] = graph->subset_idxs[i][k]; 489 } 490 } 491 } 492 } 493 graph->cptr[ncc] = cum_queue; 494 graph->subset_ncc[i] = subset_counter; 495 graph->queue_sorted = PETSC_FALSE; 496 } else { /* this subset does not need to be adapted */ 497 ierr = PetscMemcpy(graph->queue+cum_queue,graph->subset_idxs[i],graph->subset_size[i]*sizeof(PetscInt));CHKERRQ(ierr); 498 ncc++; 499 cum_queue += graph->subset_size[i]; 500 graph->cptr[ncc] = cum_queue; 501 } 502 } 503 graph->cptr[ncc] = cum_queue; 504 graph->ncc = ncc; 505 if (mss) { 506 ierr = PetscFree2(refine_buffer[0],private_labels);CHKERRQ(ierr); 507 ierr = PetscFree(refine_buffer);CHKERRQ(ierr); 508 } 509 ierr = PetscFree(labels);CHKERRQ(ierr); 510 ierr = MPI_Waitall(sum_requests,send_requests,MPI_STATUSES_IGNORE);CHKERRQ(ierr); 511 ierr = PetscFree2(send_requests,recv_requests);CHKERRQ(ierr); 512 ierr = PetscFree2(send_buffer,recv_buffer);CHKERRQ(ierr); 513 ierr = PetscFree(cum_recv_counts);CHKERRQ(ierr); 514 ierr = PetscBTDestroy(&subset_cc_adapt);CHKERRQ(ierr); 515 } 516 517 /* Determine if we are in 2D or 3D */ 518 if (!graph->twodimset) { 519 PetscBool twodim = PETSC_TRUE; 520 for (i=0;i<graph->ncc;i++) { 521 PetscInt repdof = graph->queue[graph->cptr[i]]; 522 PetscInt ccsize = graph->cptr[i+1]-graph->cptr[i]; 523 if (graph->count[repdof] > 1 && ccsize > graph->custom_minimal_size) { 524 twodim = PETSC_FALSE; 525 break; 526 } 527 } 528 ierr = MPIU_Allreduce(&twodim,&graph->twodim,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)graph->l2gmap));CHKERRQ(ierr); 529 graph->twodimset = PETSC_TRUE; 530 } 531 PetscFunctionReturn(0); 532 } 533 534 535 PETSC_STATIC_INLINE PetscErrorCode PCBDDCGraphComputeCC_Private(PCBDDCGraph graph,PetscInt pid,PetscInt* queue_tip,PetscInt n_prev,PetscInt* n_added) 536 { 537 PetscInt i,j,n; 538 PetscInt *xadj = graph->xadj,*adjncy = graph->adjncy; 539 PetscBT touched = graph->touched; 540 PetscBool havecsr = (PetscBool)(!!xadj); 541 PetscBool havesubs = (PetscBool)(!!graph->n_local_subs); 542 PetscErrorCode ierr; 543 544 PetscFunctionBegin; 545 n = 0; 546 if (havecsr && !havesubs) { 547 for (i=-n_prev;i<0;i++) { 548 PetscInt start_dof = queue_tip[i]; 549 /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs */ 550 if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) { 551 for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */ 552 PetscInt dof = graph->subset_idxs[pid-1][j]; 553 if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) { 554 ierr = PetscBTSet(touched,dof);CHKERRQ(ierr); 555 queue_tip[n] = dof; 556 n++; 557 } 558 } 559 } else { 560 for (j=xadj[start_dof];j<xadj[start_dof+1];j++) { 561 PetscInt dof = adjncy[j]; 562 if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid) { 563 ierr = PetscBTSet(touched,dof);CHKERRQ(ierr); 564 queue_tip[n] = dof; 565 n++; 566 } 567 } 568 } 569 } 570 } else if (havecsr && havesubs) { 571 PetscInt sid = graph->local_subs[queue_tip[-n_prev]]; 572 for (i=-n_prev;i<0;i++) { 573 PetscInt start_dof = queue_tip[i]; 574 /* we assume that if a dof has a size 1 adjacency list and the corresponding entry is negative, it is connected to all dofs belonging to the local sub */ 575 if (xadj[start_dof+1]-xadj[start_dof] == 1 && adjncy[xadj[start_dof]] < 0) { 576 for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */ 577 PetscInt dof = graph->subset_idxs[pid-1][j]; 578 if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) { 579 ierr = PetscBTSet(touched,dof);CHKERRQ(ierr); 580 queue_tip[n] = dof; 581 n++; 582 } 583 } 584 } else { 585 for (j=xadj[start_dof];j<xadj[start_dof+1];j++) { 586 PetscInt dof = adjncy[j]; 587 if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) { 588 ierr = PetscBTSet(touched,dof);CHKERRQ(ierr); 589 queue_tip[n] = dof; 590 n++; 591 } 592 } 593 } 594 } 595 } else { /* sub info only */ 596 PetscInt sid = graph->local_subs[queue_tip[-n_prev]]; 597 for (j=0;j<graph->subset_size[pid-1];j++) { /* pid \in [1,graph->n_subsets] */ 598 PetscInt dof = graph->subset_idxs[pid-1][j]; 599 if (!PetscBTLookup(touched,dof) && graph->subset[dof] == pid && graph->local_subs[dof] == sid) { 600 ierr = PetscBTSet(touched,dof);CHKERRQ(ierr); 601 queue_tip[n] = dof; 602 n++; 603 } 604 } 605 } 606 *n_added = n; 607 PetscFunctionReturn(0); 608 } 609 610 PetscErrorCode PCBDDCGraphComputeConnectedComponentsLocal(PCBDDCGraph graph) 611 { 612 PetscInt ncc,cum_queue,n; 613 PetscMPIInt commsize; 614 PetscErrorCode ierr; 615 616 PetscFunctionBegin; 617 if (!graph->setupcalled) SETERRQ(PetscObjectComm((PetscObject)graph->l2gmap),PETSC_ERR_ORDER,"PCBDDCGraphSetUp should be called first"); 618 /* quiet return if there isn't any local info */ 619 if (!graph->xadj && !graph->n_local_subs) { 620 PetscFunctionReturn(0); 621 } 622 623 /* reset any previous search of connected components */ 624 ierr = PetscBTMemzero(graph->nvtxs,graph->touched);CHKERRQ(ierr); 625 ierr = MPI_Comm_size(PetscObjectComm((PetscObject)graph->l2gmap),&commsize);CHKERRQ(ierr); 626 if (commsize > graph->commsizelimit) { 627 PetscInt i; 628 for (i=0;i<graph->nvtxs;i++) { 629 if (graph->special_dof[i] == PCBDDCGRAPH_DIRICHLET_MARK || !graph->count[i]) { 630 ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr); 631 } 632 } 633 } 634 635 /* begin search for connected components */ 636 cum_queue = 0; 637 ncc = 0; 638 for (n=0;n<graph->n_subsets;n++) { 639 PetscInt pid = n+1; /* partition labeled by 0 is discarded */ 640 PetscInt found = 0,prev = 0,first = 0,ncc_pid = 0; 641 while (found != graph->subset_size[n]) { 642 PetscInt added = 0; 643 if (!prev) { /* search for new starting dof */ 644 while (PetscBTLookup(graph->touched,graph->subset_idxs[n][first])) first++; 645 ierr = PetscBTSet(graph->touched,graph->subset_idxs[n][first]);CHKERRQ(ierr); 646 graph->queue[cum_queue] = graph->subset_idxs[n][first]; 647 graph->cptr[ncc] = cum_queue; 648 prev = 1; 649 cum_queue++; 650 found++; 651 ncc_pid++; 652 ncc++; 653 } 654 ierr = PCBDDCGraphComputeCC_Private(graph,pid,graph->queue + cum_queue,prev,&added);CHKERRQ(ierr); 655 if (!added) { 656 graph->subset_ncc[n] = ncc_pid; 657 graph->cptr[ncc] = cum_queue; 658 } 659 prev = added; 660 found += added; 661 cum_queue += added; 662 if (added && found == graph->subset_size[n]) { 663 graph->subset_ncc[n] = ncc_pid; 664 graph->cptr[ncc] = cum_queue; 665 } 666 } 667 } 668 graph->ncc = ncc; 669 graph->queue_sorted = PETSC_FALSE; 670 PetscFunctionReturn(0); 671 } 672 673 PetscErrorCode PCBDDCGraphSetUp(PCBDDCGraph graph, PetscInt custom_minimal_size, IS neumann_is, IS dirichlet_is, PetscInt n_ISForDofs, IS ISForDofs[], IS custom_primal_vertices) 674 { 675 IS subset,subset_n; 676 MPI_Comm comm; 677 const PetscInt *is_indices; 678 PetscInt n_neigh,*neigh,*n_shared,**shared,*queue_global; 679 PetscInt i,j,k,s,total_counts,nodes_touched,is_size; 680 PetscMPIInt commsize; 681 PetscBool same_set,mirrors_found; 682 PetscErrorCode ierr; 683 684 PetscFunctionBegin; 685 PetscValidLogicalCollectiveInt(graph->l2gmap,custom_minimal_size,2); 686 if (neumann_is) { 687 PetscValidHeaderSpecific(neumann_is,IS_CLASSID,3); 688 PetscCheckSameComm(graph->l2gmap,1,neumann_is,3); 689 } 690 graph->has_dirichlet = PETSC_FALSE; 691 if (dirichlet_is) { 692 PetscValidHeaderSpecific(dirichlet_is,IS_CLASSID,4); 693 PetscCheckSameComm(graph->l2gmap,1,dirichlet_is,4); 694 graph->has_dirichlet = PETSC_TRUE; 695 } 696 PetscValidLogicalCollectiveInt(graph->l2gmap,n_ISForDofs,5); 697 for (i=0;i<n_ISForDofs;i++) { 698 PetscValidHeaderSpecific(ISForDofs[i],IS_CLASSID,6); 699 PetscCheckSameComm(graph->l2gmap,1,ISForDofs[i],6); 700 } 701 if (custom_primal_vertices) { 702 PetscValidHeaderSpecific(custom_primal_vertices,IS_CLASSID,6); 703 PetscCheckSameComm(graph->l2gmap,1,custom_primal_vertices,7); 704 } 705 ierr = PetscObjectGetComm((PetscObject)(graph->l2gmap),&comm);CHKERRQ(ierr); 706 ierr = MPI_Comm_size(comm,&commsize);CHKERRQ(ierr); 707 708 /* custom_minimal_size */ 709 graph->custom_minimal_size = custom_minimal_size; 710 /* get info l2gmap and allocate work vectors */ 711 ierr = ISLocalToGlobalMappingGetInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr); 712 /* check if we have any local periodic nodes (periodic BCs) */ 713 mirrors_found = PETSC_FALSE; 714 if (graph->nvtxs && n_neigh) { 715 for (i=0; i<n_shared[0]; i++) graph->count[shared[0][i]] += 1; 716 for (i=0; i<n_shared[0]; i++) { 717 if (graph->count[shared[0][i]] > 1) { 718 mirrors_found = PETSC_TRUE; 719 break; 720 } 721 } 722 } 723 /* compute local mirrors (if any) */ 724 if (mirrors_found) { 725 IS to,from; 726 PetscInt *local_indices,*global_indices; 727 728 ierr = ISCreateStride(PETSC_COMM_SELF,graph->nvtxs,0,1,&to);CHKERRQ(ierr); 729 ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,to,&from);CHKERRQ(ierr); 730 /* get arrays of local and global indices */ 731 ierr = PetscMalloc1(graph->nvtxs,&local_indices);CHKERRQ(ierr); 732 ierr = ISGetIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr); 733 ierr = PetscMemcpy(local_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 734 ierr = ISRestoreIndices(to,(const PetscInt**)&is_indices);CHKERRQ(ierr); 735 ierr = PetscMalloc1(graph->nvtxs,&global_indices);CHKERRQ(ierr); 736 ierr = ISGetIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr); 737 ierr = PetscMemcpy(global_indices,is_indices,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 738 ierr = ISRestoreIndices(from,(const PetscInt**)&is_indices);CHKERRQ(ierr); 739 /* allocate space for mirrors */ 740 ierr = PetscMalloc2(graph->nvtxs,&graph->mirrors,graph->nvtxs,&graph->mirrors_set);CHKERRQ(ierr); 741 ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 742 graph->mirrors_set[0] = 0; 743 744 k=0; 745 for (i=0;i<n_shared[0];i++) { 746 j=shared[0][i]; 747 if (graph->count[j] > 1) { 748 graph->mirrors[j]++; 749 k++; 750 } 751 } 752 /* allocate space for set of mirrors */ 753 ierr = PetscMalloc1(k,&graph->mirrors_set[0]);CHKERRQ(ierr); 754 for (i=1;i<graph->nvtxs;i++) 755 graph->mirrors_set[i]=graph->mirrors_set[i-1]+graph->mirrors[i-1]; 756 757 /* fill arrays */ 758 ierr = PetscMemzero(graph->mirrors,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 759 for (j=0;j<n_shared[0];j++) { 760 i=shared[0][j]; 761 if (graph->count[i] > 1) 762 graph->mirrors_set[i][graph->mirrors[i]++]=global_indices[i]; 763 } 764 ierr = PetscSortIntWithArray(graph->nvtxs,global_indices,local_indices);CHKERRQ(ierr); 765 for (i=0;i<graph->nvtxs;i++) { 766 if (graph->mirrors[i] > 0) { 767 ierr = PetscFindInt(graph->mirrors_set[i][0],graph->nvtxs,global_indices,&k);CHKERRQ(ierr); 768 j = global_indices[k]; 769 while ( k > 0 && global_indices[k-1] == j) k--; 770 for (j=0;j<graph->mirrors[i];j++) { 771 graph->mirrors_set[i][j]=local_indices[k+j]; 772 } 773 ierr = PetscSortInt(graph->mirrors[i],graph->mirrors_set[i]);CHKERRQ(ierr); 774 } 775 } 776 ierr = PetscFree(local_indices);CHKERRQ(ierr); 777 ierr = PetscFree(global_indices);CHKERRQ(ierr); 778 ierr = ISDestroy(&to);CHKERRQ(ierr); 779 ierr = ISDestroy(&from);CHKERRQ(ierr); 780 } 781 ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr); 782 783 /* Count total number of neigh per node */ 784 k = 0; 785 for (i=1;i<n_neigh;i++) { 786 k += n_shared[i]; 787 for (j=0;j<n_shared[i];j++) { 788 graph->count[shared[i][j]] += 1; 789 } 790 } 791 /* Allocate space for storing the set of neighbours for each node */ 792 if (graph->nvtxs) { 793 ierr = PetscMalloc1(k,&graph->neighbours_set[0]);CHKERRQ(ierr); 794 } 795 for (i=1;i<graph->nvtxs;i++) { /* dont count myself */ 796 graph->neighbours_set[i]=graph->neighbours_set[i-1]+graph->count[i-1]; 797 } 798 /* Get information for sharing subdomains */ 799 ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(*graph->count));CHKERRQ(ierr); 800 for (i=1;i<n_neigh;i++) { /* dont count myself */ 801 s = n_shared[i]; 802 for (j=0;j<s;j++) { 803 k = shared[i][j]; 804 graph->neighbours_set[k][graph->count[k]] = neigh[i]; 805 graph->count[k] += 1; 806 } 807 } 808 /* sort set of sharing subdomains */ 809 for (i=0;i<graph->nvtxs;i++) { 810 ierr = PetscSortRemoveDupsInt(&graph->count[i],graph->neighbours_set[i]);CHKERRQ(ierr); 811 } 812 /* free memory allocated by ISLocalToGlobalMappingGetInfo */ 813 ierr = ISLocalToGlobalMappingRestoreInfo(graph->l2gmap,&n_neigh,&neigh,&n_shared,&shared);CHKERRQ(ierr); 814 815 /* 816 Get info for dofs splitting 817 User can specify just a subset; an additional field is considered as a complementary field 818 */ 819 for (i=0;i<graph->nvtxs;i++) graph->which_dof[i] = n_ISForDofs; /* by default a dof belongs to the complement set */ 820 for (i=0;i<n_ISForDofs;i++) { 821 ierr = ISGetLocalSize(ISForDofs[i],&is_size);CHKERRQ(ierr); 822 ierr = ISGetIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr); 823 for (j=0;j<is_size;j++) { 824 if (is_indices[j] > -1 && is_indices[j] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */ 825 graph->which_dof[is_indices[j]] = i; 826 } 827 } 828 ierr = ISRestoreIndices(ISForDofs[i],(const PetscInt**)&is_indices);CHKERRQ(ierr); 829 } 830 831 /* Take into account Neumann nodes */ 832 if (neumann_is) { 833 ierr = ISGetLocalSize(neumann_is,&is_size);CHKERRQ(ierr); 834 ierr = ISGetIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr); 835 for (i=0;i<is_size;i++) { 836 if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */ 837 graph->special_dof[is_indices[i]] = PCBDDCGRAPH_NEUMANN_MARK; 838 } 839 } 840 ierr = ISRestoreIndices(neumann_is,(const PetscInt**)&is_indices);CHKERRQ(ierr); 841 } 842 /* Take into account Dirichlet nodes (they overwrite any neumann boundary mark previously set) */ 843 if (dirichlet_is) { 844 ierr = ISGetLocalSize(dirichlet_is,&is_size);CHKERRQ(ierr); 845 ierr = ISGetIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr); 846 for (i=0;i<is_size;i++){ 847 if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs) { /* out of bounds indices (if any) are skipped */ 848 if (commsize > graph->commsizelimit) { /* dirichlet nodes treated as internal */ 849 ierr = PetscBTSet(graph->touched,is_indices[i]);CHKERRQ(ierr); 850 graph->subset[is_indices[i]] = 0; 851 } 852 graph->special_dof[is_indices[i]] = PCBDDCGRAPH_DIRICHLET_MARK; 853 } 854 } 855 ierr = ISRestoreIndices(dirichlet_is,(const PetscInt**)&is_indices);CHKERRQ(ierr); 856 } 857 /* mark local periodic nodes (if any) and adapt CSR graph (if any) */ 858 if (graph->mirrors) { 859 for (i=0;i<graph->nvtxs;i++) 860 if (graph->mirrors[i]) 861 graph->special_dof[i] = PCBDDCGRAPH_LOCAL_PERIODIC_MARK; 862 863 if (graph->xadj) { 864 PetscInt *new_xadj,*new_adjncy; 865 /* sort CSR graph */ 866 for (i=0;i<graph->nvtxs;i++) 867 ierr = PetscSortInt(graph->xadj[i+1]-graph->xadj[i],&graph->adjncy[graph->xadj[i]]);CHKERRQ(ierr); 868 869 /* adapt local CSR graph in case of local periodicity */ 870 k = 0; 871 for (i=0;i<graph->nvtxs;i++) 872 for (j=graph->xadj[i];j<graph->xadj[i+1];j++) 873 k += graph->mirrors[graph->adjncy[j]]; 874 875 ierr = PetscMalloc1(graph->nvtxs+1,&new_xadj);CHKERRQ(ierr); 876 ierr = PetscMalloc1(k+graph->xadj[graph->nvtxs],&new_adjncy);CHKERRQ(ierr); 877 new_xadj[0] = 0; 878 for (i=0;i<graph->nvtxs;i++) { 879 k = graph->xadj[i+1]-graph->xadj[i]; 880 ierr = PetscMemcpy(&new_adjncy[new_xadj[i]],&graph->adjncy[graph->xadj[i]],k*sizeof(PetscInt));CHKERRQ(ierr); 881 new_xadj[i+1] = new_xadj[i]+k; 882 for (j=graph->xadj[i];j<graph->xadj[i+1];j++) { 883 k = graph->mirrors[graph->adjncy[j]]; 884 ierr = PetscMemcpy(&new_adjncy[new_xadj[i+1]],graph->mirrors_set[graph->adjncy[j]],k*sizeof(PetscInt));CHKERRQ(ierr); 885 new_xadj[i+1] += k; 886 } 887 k = new_xadj[i+1]-new_xadj[i]; 888 ierr = PetscSortRemoveDupsInt(&k,&new_adjncy[new_xadj[i]]);CHKERRQ(ierr); 889 new_xadj[i+1] = new_xadj[i]+k; 890 } 891 /* set new CSR into graph */ 892 ierr = PetscFree(graph->xadj);CHKERRQ(ierr); 893 ierr = PetscFree(graph->adjncy);CHKERRQ(ierr); 894 graph->xadj = new_xadj; 895 graph->adjncy = new_adjncy; 896 } 897 } 898 899 /* mark special nodes (if any) -> each will become a single node equivalence class */ 900 if (custom_primal_vertices) { 901 ierr = ISGetLocalSize(custom_primal_vertices,&is_size);CHKERRQ(ierr); 902 ierr = ISGetIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr); 903 for (i=0,j=0;i<is_size;i++){ 904 if (is_indices[i] > -1 && is_indices[i] < graph->nvtxs && graph->special_dof[is_indices[i]] != PCBDDCGRAPH_DIRICHLET_MARK) { /* out of bounds indices (if any) are skipped */ 905 graph->special_dof[is_indices[i]] = PCBDDCGRAPH_SPECIAL_MARK-j; 906 j++; 907 } 908 } 909 ierr = ISRestoreIndices(custom_primal_vertices,(const PetscInt**)&is_indices);CHKERRQ(ierr); 910 } 911 912 /* mark interior nodes (if commsize > graph->commsizelimit) as touched and belonging to partition number 0 */ 913 if (commsize > graph->commsizelimit) { 914 for (i=0;i<graph->nvtxs;i++) { 915 if (!graph->count[i]) { 916 ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr); 917 graph->subset[i] = 0; 918 } 919 } 920 } 921 922 /* init graph structure and compute default subsets */ 923 nodes_touched = 0; 924 for (i=0;i<graph->nvtxs;i++) { 925 if (PetscBTLookup(graph->touched,i)) { 926 nodes_touched++; 927 } 928 } 929 i = 0; 930 graph->ncc = 0; 931 total_counts = 0; 932 933 /* allocated space for queues */ 934 if (commsize == graph->commsizelimit) { 935 ierr = PetscMalloc2(graph->nvtxs+1,&graph->cptr,graph->nvtxs,&graph->queue);CHKERRQ(ierr); 936 } else { 937 PetscInt nused = graph->nvtxs - nodes_touched; 938 ierr = PetscMalloc2(nused+1,&graph->cptr,nused,&graph->queue);CHKERRQ(ierr); 939 } 940 941 while (nodes_touched<graph->nvtxs) { 942 /* find first untouched node in local ordering */ 943 while (PetscBTLookup(graph->touched,i)) i++; 944 ierr = PetscBTSet(graph->touched,i);CHKERRQ(ierr); 945 graph->subset[i] = graph->ncc+1; 946 graph->cptr[graph->ncc] = total_counts; 947 graph->queue[total_counts] = i; 948 total_counts++; 949 nodes_touched++; 950 /* now find all other nodes having the same set of sharing subdomains */ 951 for (j=i+1;j<graph->nvtxs;j++) { 952 /* check for same number of sharing subdomains, dof number and same special mark */ 953 if (!PetscBTLookup(graph->touched,j) && graph->count[i] == graph->count[j] && graph->which_dof[i] == graph->which_dof[j] && graph->special_dof[i] == graph->special_dof[j]) { 954 /* check for same set of sharing subdomains */ 955 same_set = PETSC_TRUE; 956 for (k=0;k<graph->count[j];k++){ 957 if (graph->neighbours_set[i][k] != graph->neighbours_set[j][k]) { 958 same_set = PETSC_FALSE; 959 } 960 } 961 /* I found a friend of mine */ 962 if (same_set) { 963 ierr = PetscBTSet(graph->touched,j);CHKERRQ(ierr); 964 graph->subset[j] = graph->ncc+1; 965 nodes_touched++; 966 graph->queue[total_counts] = j; 967 total_counts++; 968 } 969 } 970 } 971 graph->ncc++; 972 } 973 /* set default number of subsets (at this point no info on csr and/or local_subs has been taken into account, so n_subsets = ncc */ 974 graph->n_subsets = graph->ncc; 975 ierr = PetscMalloc1(graph->n_subsets,&graph->subset_ncc);CHKERRQ(ierr); 976 for (i=0;i<graph->n_subsets;i++) { 977 graph->subset_ncc[i] = 1; 978 } 979 /* final pointer */ 980 graph->cptr[graph->ncc] = total_counts; 981 982 /* For consistency reasons (among neighbours), I need to sort (by global ordering) each connected component */ 983 /* Get a reference node (min index in global ordering) for each subset for tagging messages */ 984 ierr = PetscMalloc1(graph->ncc,&graph->subset_ref_node);CHKERRQ(ierr); 985 ierr = PetscMalloc1(graph->cptr[graph->ncc],&queue_global);CHKERRQ(ierr); 986 ierr = ISLocalToGlobalMappingApply(graph->l2gmap,graph->cptr[graph->ncc],graph->queue,queue_global);CHKERRQ(ierr); 987 for (j=0;j<graph->ncc;j++) { 988 ierr = PetscSortIntWithArray(graph->cptr[j+1]-graph->cptr[j],&queue_global[graph->cptr[j]],&graph->queue[graph->cptr[j]]);CHKERRQ(ierr); 989 graph->subset_ref_node[j] = graph->queue[graph->cptr[j]]; 990 } 991 ierr = PetscFree(queue_global);CHKERRQ(ierr); 992 graph->queue_sorted = PETSC_TRUE; 993 994 /* save information on subsets (needed when analyzing the connected components) */ 995 if (graph->ncc) { 996 ierr = PetscMalloc2(graph->ncc,&graph->subset_size,graph->ncc,&graph->subset_idxs);CHKERRQ(ierr); 997 ierr = PetscMalloc1(graph->cptr[graph->ncc],&graph->subset_idxs[0]);CHKERRQ(ierr); 998 ierr = PetscMemzero(graph->subset_idxs[0],graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr); 999 for (j=1;j<graph->ncc;j++) { 1000 graph->subset_size[j-1] = graph->cptr[j] - graph->cptr[j-1]; 1001 graph->subset_idxs[j] = graph->subset_idxs[j-1] + graph->subset_size[j-1]; 1002 } 1003 graph->subset_size[graph->ncc-1] = graph->cptr[graph->ncc] - graph->cptr[graph->ncc-1]; 1004 ierr = PetscMemcpy(graph->subset_idxs[0],graph->queue,graph->cptr[graph->ncc]*sizeof(PetscInt));CHKERRQ(ierr); 1005 } 1006 1007 /* renumber reference nodes */ 1008 ierr = ISCreateGeneral(PetscObjectComm((PetscObject)(graph->l2gmap)),graph->ncc,graph->subset_ref_node,PETSC_COPY_VALUES,&subset_n);CHKERRQ(ierr); 1009 ierr = ISLocalToGlobalMappingApplyIS(graph->l2gmap,subset_n,&subset);CHKERRQ(ierr); 1010 ierr = ISDestroy(&subset_n);CHKERRQ(ierr); 1011 ierr = ISRenumber(subset,NULL,NULL,&subset_n);CHKERRQ(ierr); 1012 ierr = ISDestroy(&subset);CHKERRQ(ierr); 1013 ierr = ISGetLocalSize(subset_n,&k);CHKERRQ(ierr); 1014 if (k != graph->ncc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Invalid size of new subset! %D != %D",k,graph->ncc); 1015 ierr = ISGetIndices(subset_n,&is_indices);CHKERRQ(ierr); 1016 ierr = PetscMemcpy(graph->subset_ref_node,is_indices,graph->ncc*sizeof(PetscInt));CHKERRQ(ierr); 1017 ierr = ISRestoreIndices(subset_n,&is_indices);CHKERRQ(ierr); 1018 ierr = ISDestroy(&subset_n);CHKERRQ(ierr); 1019 1020 /* free workspace */ 1021 graph->setupcalled = PETSC_TRUE; 1022 PetscFunctionReturn(0); 1023 } 1024 1025 PetscErrorCode PCBDDCGraphResetCSR(PCBDDCGraph graph) 1026 { 1027 PetscErrorCode ierr; 1028 1029 PetscFunctionBegin; 1030 if (!graph) PetscFunctionReturn(0); 1031 if (graph->freecsr) { 1032 ierr = PetscFree(graph->xadj);CHKERRQ(ierr); 1033 ierr = PetscFree(graph->adjncy);CHKERRQ(ierr); 1034 } else { 1035 graph->xadj = NULL; 1036 graph->adjncy = NULL; 1037 } 1038 graph->freecsr = PETSC_FALSE; 1039 graph->nvtxs_csr = 0; 1040 PetscFunctionReturn(0); 1041 } 1042 1043 PetscErrorCode PCBDDCGraphReset(PCBDDCGraph graph) 1044 { 1045 PetscErrorCode ierr; 1046 1047 PetscFunctionBegin; 1048 if (!graph) PetscFunctionReturn(0); 1049 ierr = ISLocalToGlobalMappingDestroy(&graph->l2gmap);CHKERRQ(ierr); 1050 ierr = PetscFree(graph->subset_ncc);CHKERRQ(ierr); 1051 ierr = PetscFree(graph->subset_ref_node);CHKERRQ(ierr); 1052 if (graph->nvtxs) { 1053 ierr = PetscFree(graph->neighbours_set[0]);CHKERRQ(ierr); 1054 } 1055 ierr = PetscBTDestroy(&graph->touched);CHKERRQ(ierr); 1056 ierr = PetscFree5(graph->count, 1057 graph->neighbours_set, 1058 graph->subset, 1059 graph->which_dof, 1060 graph->special_dof);CHKERRQ(ierr); 1061 ierr = PetscFree2(graph->cptr,graph->queue);CHKERRQ(ierr); 1062 if (graph->mirrors) { 1063 ierr = PetscFree(graph->mirrors_set[0]);CHKERRQ(ierr); 1064 } 1065 ierr = PetscFree2(graph->mirrors,graph->mirrors_set);CHKERRQ(ierr); 1066 if (graph->subset_idxs) { 1067 ierr = PetscFree(graph->subset_idxs[0]);CHKERRQ(ierr); 1068 } 1069 ierr = PetscFree2(graph->subset_size,graph->subset_idxs);CHKERRQ(ierr); 1070 ierr = ISDestroy(&graph->dirdofs);CHKERRQ(ierr); 1071 ierr = ISDestroy(&graph->dirdofsB);CHKERRQ(ierr); 1072 if (graph->n_local_subs) { 1073 ierr = PetscFree(graph->local_subs);CHKERRQ(ierr); 1074 } 1075 graph->has_dirichlet = PETSC_FALSE; 1076 graph->twodimset = PETSC_FALSE; 1077 graph->twodim = PETSC_FALSE; 1078 graph->nvtxs = 0; 1079 graph->nvtxs_global = 0; 1080 graph->n_subsets = 0; 1081 graph->custom_minimal_size = 1; 1082 graph->n_local_subs = 0; 1083 graph->maxcount = PETSC_MAX_INT; 1084 graph->setupcalled = PETSC_FALSE; 1085 PetscFunctionReturn(0); 1086 } 1087 1088 PetscErrorCode PCBDDCGraphInit(PCBDDCGraph graph, ISLocalToGlobalMapping l2gmap, PetscInt N, PetscInt maxcount) 1089 { 1090 PetscInt n; 1091 PetscErrorCode ierr; 1092 1093 PetscFunctionBegin; 1094 PetscValidPointer(graph,1); 1095 PetscValidHeaderSpecific(l2gmap,IS_LTOGM_CLASSID,2); 1096 PetscValidLogicalCollectiveInt(l2gmap,N,3); 1097 PetscValidLogicalCollectiveInt(l2gmap,maxcount,4); 1098 /* raise an error if already allocated */ 1099 if (graph->nvtxs_global) SETERRQ(PetscObjectComm((PetscObject)l2gmap),PETSC_ERR_PLIB,"BDDCGraph already initialized"); 1100 /* set number of vertices */ 1101 ierr = PetscObjectReference((PetscObject)l2gmap);CHKERRQ(ierr); 1102 graph->l2gmap = l2gmap; 1103 ierr = ISLocalToGlobalMappingGetSize(l2gmap,&n);CHKERRQ(ierr); 1104 graph->nvtxs = n; 1105 graph->nvtxs_global = N; 1106 /* allocate used space */ 1107 ierr = PetscBTCreate(graph->nvtxs,&graph->touched);CHKERRQ(ierr); 1108 ierr = PetscMalloc5(graph->nvtxs,&graph->count, 1109 graph->nvtxs,&graph->neighbours_set, 1110 graph->nvtxs,&graph->subset, 1111 graph->nvtxs,&graph->which_dof, 1112 graph->nvtxs,&graph->special_dof);CHKERRQ(ierr); 1113 /* zeroes memory */ 1114 ierr = PetscMemzero(graph->count,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 1115 ierr = PetscMemzero(graph->subset,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 1116 /* use -1 as a default value for which_dof array */ 1117 for (n=0;n<graph->nvtxs;n++) graph->which_dof[n] = -1; 1118 ierr = PetscMemzero(graph->special_dof,graph->nvtxs*sizeof(PetscInt));CHKERRQ(ierr); 1119 /* zeroes first pointer to neighbour set */ 1120 if (graph->nvtxs) { 1121 graph->neighbours_set[0] = 0; 1122 } 1123 /* zeroes workspace for values of ncc */ 1124 graph->subset_ncc = 0; 1125 graph->subset_ref_node = 0; 1126 /* maxcount for cc */ 1127 graph->maxcount = maxcount; 1128 PetscFunctionReturn(0); 1129 } 1130 1131 PetscErrorCode PCBDDCGraphDestroy(PCBDDCGraph* graph) 1132 { 1133 PetscErrorCode ierr; 1134 1135 PetscFunctionBegin; 1136 ierr = PCBDDCGraphResetCSR(*graph);CHKERRQ(ierr); 1137 ierr = PCBDDCGraphReset(*graph);CHKERRQ(ierr); 1138 ierr = PetscFree(*graph);CHKERRQ(ierr); 1139 PetscFunctionReturn(0); 1140 } 1141 1142 PetscErrorCode PCBDDCGraphCreate(PCBDDCGraph *graph) 1143 { 1144 PCBDDCGraph new_graph; 1145 PetscErrorCode ierr; 1146 1147 PetscFunctionBegin; 1148 ierr = PetscNew(&new_graph);CHKERRQ(ierr); 1149 new_graph->custom_minimal_size = 1; 1150 new_graph->commsizelimit = 1; 1151 *graph = new_graph; 1152 PetscFunctionReturn(0); 1153 } 1154