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