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