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