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