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