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