1be1d678aSKris Buschelman #define PETSCMAT_DLL 2632d0f97SHong Zhang 3632d0f97SHong Zhang /* 4632d0f97SHong Zhang Routines to compute overlapping regions of a parallel MPI matrix. 5632d0f97SHong Zhang Used for finding submatrices that were shared across processors. 6632d0f97SHong Zhang */ 7632d0f97SHong Zhang #include "src/mat/impls/sbaij/mpi/mpisbaij.h" 8632d0f97SHong Zhang #include "petscbt.h" 9632d0f97SHong Zhang 1013f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat,PetscInt,IS*); 1113f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat,PetscInt*,PetscInt,PetscInt*,PetscBT*); 12632d0f97SHong Zhang 13632d0f97SHong Zhang #undef __FUNCT__ 14632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ" 1513f74950SBarry Smith PetscErrorCode MatIncreaseOverlap_MPISBAIJ(Mat C,PetscInt is_max,IS is[],PetscInt ov) 16632d0f97SHong Zhang { 176849ba73SBarry Smith PetscErrorCode ierr; 18d0f46423SBarry Smith PetscInt i,N=C->cmap->N, bs=C->rmap->bs; 19632d0f97SHong Zhang IS *is_new; 20632d0f97SHong Zhang 21632d0f97SHong Zhang PetscFunctionBegin; 22c910923dSHong Zhang ierr = PetscMalloc(is_max*sizeof(IS),&is_new);CHKERRQ(ierr); 23632d0f97SHong Zhang /* Convert the indices into block format */ 2427f478b1SHong Zhang ierr = ISCompressIndicesGeneral(N,bs,is_max,is,is_new);CHKERRQ(ierr); 25632d0f97SHong Zhang if (ov < 0){ SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Negative overlap specified\n");} 26632d0f97SHong Zhang for (i=0; i<ov; ++i) { 27c910923dSHong Zhang ierr = MatIncreaseOverlap_MPISBAIJ_Once(C,is_max,is_new);CHKERRQ(ierr); 28632d0f97SHong Zhang } 29c910923dSHong Zhang for (i=0; i<is_max; i++) {ierr = ISDestroy(is[i]);CHKERRQ(ierr);} 3027f478b1SHong Zhang ierr = ISExpandIndicesGeneral(N,bs,is_max,is_new,is);CHKERRQ(ierr); 31c910923dSHong Zhang for (i=0; i<is_max; i++) {ierr = ISDestroy(is_new[i]);CHKERRQ(ierr);} 32632d0f97SHong Zhang ierr = PetscFree(is_new);CHKERRQ(ierr); 33632d0f97SHong Zhang PetscFunctionReturn(0); 34632d0f97SHong Zhang } 35632d0f97SHong Zhang 364a69c536SBarry Smith typedef enum {MINE,OTHER} WhoseOwner; 370472cc68SHong Zhang /* data1, odata1 and odata2 are packed in the format (for communication): 38a2a9f22aSHong Zhang data[0] = is_max, no of is 39a2a9f22aSHong Zhang data[1] = size of is[0] 40a2a9f22aSHong Zhang ... 41a2a9f22aSHong Zhang data[is_max] = size of is[is_max-1] 42a2a9f22aSHong Zhang data[is_max + 1] = data(is[0]) 43a2a9f22aSHong Zhang ... 44a2a9f22aSHong Zhang data[is_max+1+sum(size of is[k]), k=0,...,i-1] = data(is[i]) 45a2a9f22aSHong Zhang ... 460472cc68SHong Zhang data2 is packed in the format (for creating output is[]): 470472cc68SHong Zhang data[0] = is_max, no of is 480472cc68SHong Zhang data[1] = size of is[0] 490472cc68SHong Zhang ... 500472cc68SHong Zhang data[is_max] = size of is[is_max-1] 510472cc68SHong Zhang data[is_max + 1] = data(is[0]) 520472cc68SHong Zhang ... 530472cc68SHong Zhang data[is_max + 1 + Mbs*i) = data(is[i]) 540472cc68SHong Zhang ... 55a2a9f22aSHong Zhang */ 56632d0f97SHong Zhang #undef __FUNCT__ 57632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Once" 5813f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Once(Mat C,PetscInt is_max,IS is[]) 59632d0f97SHong Zhang { 60632d0f97SHong Zhang Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data; 616849ba73SBarry Smith PetscErrorCode ierr; 6213f74950SBarry Smith PetscMPIInt size,rank,tag1,tag2,*len_s,nrqr,nrqs,*id_r1,*len_r1,flag,len; 63*5d0c19d7SBarry Smith const PetscInt *idx_i; 64*5d0c19d7SBarry Smith PetscInt idx,isz,col,*n,*data1,**data1_start,*data2,*data2_i,*data,*data_i, 6513f74950SBarry Smith Mbs,i,j,k,*odata1,*odata2, 6613f74950SBarry Smith proc_id,**odata2_ptr,*ctable=0,*btable,len_max,len_est; 6713f74950SBarry Smith PetscInt proc_end=0,*iwork,len_unused,nodata2; 6813f74950SBarry Smith PetscInt ois_max; /* max no of is[] in each of processor */ 69bfc6803cSHong Zhang char *t_p; 70632d0f97SHong Zhang MPI_Comm comm; 71e8527bf2SHong Zhang MPI_Request *s_waits1,*s_waits2,r_req; 72632d0f97SHong Zhang MPI_Status *s_status,r_status; 7345f43ab7SHong Zhang PetscBT *table; /* mark indices of this processor's is[] */ 74fc70d14eSHong Zhang PetscBT table_i; 75bfc6803cSHong Zhang PetscBT otable; /* mark indices of other processors' is[] */ 76d0f46423SBarry Smith PetscInt bs=C->rmap->bs,Bn = c->B->cmap->n,Bnbs = Bn/bs,*Bowners; 7710eea884SHong Zhang IS garray_local,garray_gl; 785483b11dSHong Zhang 79632d0f97SHong Zhang PetscFunctionBegin; 807adad957SLisandro Dalcin comm = ((PetscObject)C)->comm; 81632d0f97SHong Zhang size = c->size; 82632d0f97SHong Zhang rank = c->rank; 83632d0f97SHong Zhang Mbs = c->Mbs; 84632d0f97SHong Zhang 85c910923dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)C,&tag1);CHKERRQ(ierr); 86c910923dSHong Zhang ierr = PetscObjectGetNewTag((PetscObject)C,&tag2);CHKERRQ(ierr); 87c910923dSHong Zhang 88430a0127SHong Zhang /* create tables used in 89430a0127SHong Zhang step 1: table[i] - mark c->garray of proc [i] 9045f43ab7SHong Zhang step 3: table[i] - mark indices of is[i] when whose=MINE 91430a0127SHong Zhang table[0] - mark incideces of is[] when whose=OTHER */ 92430a0127SHong Zhang len = PetscMax(is_max, size);CHKERRQ(ierr); 93430a0127SHong Zhang len_max = len*sizeof(PetscBT) + (Mbs/PETSC_BITS_PER_BYTE+1)*len*sizeof(char) + 1; 94430a0127SHong Zhang ierr = PetscMalloc(len_max,&table);CHKERRQ(ierr); 95430a0127SHong Zhang t_p = (char *)(table + len); 96430a0127SHong Zhang for (i=0; i<len; i++) { 97430a0127SHong Zhang table[i] = t_p + (Mbs/PETSC_BITS_PER_BYTE+1)*i; 98430a0127SHong Zhang } 99430a0127SHong Zhang 10013f74950SBarry Smith ierr = MPI_Allreduce(&is_max,&ois_max,1,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr); 10110eea884SHong Zhang 1025483b11dSHong Zhang /* 1. Send this processor's is[] to other processors */ 1035483b11dSHong Zhang /*---------------------------------------------------*/ 104e8527bf2SHong Zhang /* allocate spaces */ 10513f74950SBarry Smith ierr = PetscMalloc(is_max*sizeof(PetscInt),&n);CHKERRQ(ierr); 1065483b11dSHong Zhang len = 0; 107c910923dSHong Zhang for (i=0; i<is_max; i++) { 108632d0f97SHong Zhang ierr = ISGetLocalSize(is[i],&n[i]);CHKERRQ(ierr); 109632d0f97SHong Zhang len += n[i]; 110632d0f97SHong Zhang } 111958c9bccSBarry Smith if (!len) { 1125483b11dSHong Zhang is_max = 0; 1135483b11dSHong Zhang } else { 1145483b11dSHong Zhang len += 1 + is_max; /* max length of data1 for one processor */ 1155483b11dSHong Zhang } 1165483b11dSHong Zhang 117430a0127SHong Zhang 11813f74950SBarry Smith ierr = PetscMalloc((size*len+1)*sizeof(PetscInt),&data1);CHKERRQ(ierr); 11913f74950SBarry Smith ierr = PetscMalloc(size*sizeof(PetscInt*),&data1_start);CHKERRQ(ierr); 1205483b11dSHong Zhang for (i=0; i<size; i++) data1_start[i] = data1 + i*len; 1215483b11dSHong Zhang 12213f74950SBarry Smith ierr = PetscMalloc((size*4+1)*sizeof(PetscInt),&len_s);CHKERRQ(ierr); 12313f74950SBarry Smith btable = (PetscInt*)(len_s + size); 124e8527bf2SHong Zhang iwork = btable + size; 12510eea884SHong Zhang Bowners = iwork + size; 126e8527bf2SHong Zhang 12776f244e2SHong Zhang /* gather c->garray from all processors */ 12876f244e2SHong Zhang ierr = ISCreateGeneral(comm,Bnbs,c->garray,&garray_local);CHKERRQ(ierr); 12976f244e2SHong Zhang ierr = ISAllGather(garray_local, &garray_gl);CHKERRQ(ierr); 13076f244e2SHong Zhang ierr = ISDestroy(garray_local);CHKERRQ(ierr); 131a7cc72afSBarry Smith ierr = MPI_Allgather(&Bnbs,1,MPIU_INT,Bowners+1,1,MPIU_INT,comm);CHKERRQ(ierr); 13276f244e2SHong Zhang Bowners[0] = 0; 13376f244e2SHong Zhang for (i=0; i<size; i++) Bowners[i+1] += Bowners[i]; 13476f244e2SHong Zhang 1355483b11dSHong Zhang if (is_max){ 136430a0127SHong Zhang /* hash table ctable which maps c->row to proc_id) */ 13713f74950SBarry Smith ierr = PetscMalloc(Mbs*sizeof(PetscInt),&ctable);CHKERRQ(ierr); 1385483b11dSHong Zhang for (proc_id=0,j=0; proc_id<size; proc_id++) { 139d0f46423SBarry Smith for (; j<C->rmap->range[proc_id+1]; j++) { 1405483b11dSHong Zhang ctable[j] = proc_id; 1415483b11dSHong Zhang } 1425483b11dSHong Zhang } 1435483b11dSHong Zhang 144430a0127SHong Zhang /* hash tables marking c->garray */ 14510eea884SHong Zhang ierr = ISGetIndices(garray_gl,&idx_i); 1465483b11dSHong Zhang for (i=0; i<size; i++){ 147430a0127SHong Zhang table_i = table[i]; 148430a0127SHong Zhang ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr); 149430a0127SHong Zhang for (j = Bowners[i]; j<Bowners[i+1]; j++){ /* go through B cols of proc[i]*/ 150430a0127SHong Zhang ierr = PetscBTSet(table_i,idx_i[j]);CHKERRQ(ierr); 1515483b11dSHong Zhang } 1525483b11dSHong Zhang } 15310eea884SHong Zhang ierr = ISRestoreIndices(garray_gl,&idx_i);CHKERRQ(ierr); 1545483b11dSHong Zhang } /* if (is_max) */ 15576f244e2SHong Zhang ierr = ISDestroy(garray_gl);CHKERRQ(ierr); 1565483b11dSHong Zhang 1575483b11dSHong Zhang /* evaluate communication - mesg to who, length, and buffer space */ 158e8527bf2SHong Zhang for (i=0; i<size; i++) len_s[i] = 0; 1595483b11dSHong Zhang 1605483b11dSHong Zhang /* header of data1 */ 1615483b11dSHong Zhang for (proc_id=0; proc_id<size; proc_id++){ 1625483b11dSHong Zhang iwork[proc_id] = 0; 1635483b11dSHong Zhang *data1_start[proc_id] = is_max; 1645483b11dSHong Zhang data1_start[proc_id]++; 1655483b11dSHong Zhang for (j=0; j<is_max; j++) { 1665483b11dSHong Zhang if (proc_id == rank){ 1675483b11dSHong Zhang *data1_start[proc_id] = n[j]; 1685483b11dSHong Zhang } else { 1695483b11dSHong Zhang *data1_start[proc_id] = 0; 1705483b11dSHong Zhang } 1715483b11dSHong Zhang data1_start[proc_id]++; 1725483b11dSHong Zhang } 1735483b11dSHong Zhang } 1745483b11dSHong Zhang 1755483b11dSHong Zhang for (i=0; i<is_max; i++) { 1765483b11dSHong Zhang ierr = ISGetIndices(is[i],&idx_i);CHKERRQ(ierr); 1775483b11dSHong Zhang for (j=0; j<n[i]; j++){ 1785483b11dSHong Zhang idx = idx_i[j]; 1795483b11dSHong Zhang *data1_start[rank] = idx; data1_start[rank]++; /* for local proccessing */ 1805483b11dSHong Zhang proc_end = ctable[idx]; 1815483b11dSHong Zhang for (proc_id=0; proc_id<=proc_end; proc_id++){ /* for others to process */ 182e8527bf2SHong Zhang if (proc_id == rank ) continue; /* done before this loop */ 183430a0127SHong Zhang if (proc_id < proc_end && !PetscBTLookup(table[proc_id],idx)) 184430a0127SHong Zhang continue; /* no need for sending idx to [proc_id] */ 1855483b11dSHong Zhang *data1_start[proc_id] = idx; data1_start[proc_id]++; 1865483b11dSHong Zhang len_s[proc_id]++; 1875483b11dSHong Zhang } 1885483b11dSHong Zhang } 1895483b11dSHong Zhang /* update header data */ 1902cfbe0a4SHong Zhang for (proc_id=0; proc_id<size; proc_id++){ 1915483b11dSHong Zhang if (proc_id== rank) continue; 1925483b11dSHong Zhang *(data1 + proc_id*len + 1 + i) = len_s[proc_id] - iwork[proc_id]; 1935483b11dSHong Zhang iwork[proc_id] = len_s[proc_id] ; 1945483b11dSHong Zhang } 1955483b11dSHong Zhang ierr = ISRestoreIndices(is[i],&idx_i);CHKERRQ(ierr); 196e8527bf2SHong Zhang } 1975483b11dSHong Zhang 1985483b11dSHong Zhang nrqs = 0; nrqr = 0; 1995483b11dSHong Zhang for (i=0; i<size; i++){ 2005483b11dSHong Zhang data1_start[i] = data1 + i*len; 2015483b11dSHong Zhang if (len_s[i]){ 2025483b11dSHong Zhang nrqs++; 2035483b11dSHong Zhang len_s[i] += 1 + is_max; /* add no. of header msg */ 2045483b11dSHong Zhang } 2055483b11dSHong Zhang } 2065483b11dSHong Zhang 2075483b11dSHong Zhang for (i=0; i<is_max; i++) { 208a2a9f22aSHong Zhang ierr = ISDestroy(is[i]);CHKERRQ(ierr); 209632d0f97SHong Zhang } 210bfc6803cSHong Zhang ierr = PetscFree(n);CHKERRQ(ierr); 21105b42c5fSBarry Smith ierr = PetscFree(ctable);CHKERRQ(ierr); 2125483b11dSHong Zhang 2135483b11dSHong Zhang /* Determine the number of messages to expect, their lengths, from from-ids */ 2145483b11dSHong Zhang ierr = PetscGatherNumberOfMessages(comm,PETSC_NULL,len_s,&nrqr);CHKERRQ(ierr); 2155483b11dSHong Zhang ierr = PetscGatherMessageLengths(comm,nrqs,nrqr,len_s,&id_r1,&len_r1);CHKERRQ(ierr); 216632d0f97SHong Zhang 217632d0f97SHong Zhang /* Now post the sends */ 2185483b11dSHong Zhang ierr = PetscMalloc(2*size*sizeof(MPI_Request),&s_waits1);CHKERRQ(ierr); 2195483b11dSHong Zhang s_waits2 = s_waits1 + size; 220632d0f97SHong Zhang k = 0; 2215483b11dSHong Zhang for (proc_id=0; proc_id<size; proc_id++){ /* send data1 to processor [proc_id] */ 2225483b11dSHong Zhang if (len_s[proc_id]){ 22313f74950SBarry Smith ierr = MPI_Isend(data1_start[proc_id],len_s[proc_id],MPIU_INT,proc_id,tag1,comm,s_waits1+k);CHKERRQ(ierr); 224632d0f97SHong Zhang k++; 225632d0f97SHong Zhang } 226632d0f97SHong Zhang } 227632d0f97SHong Zhang 22845f43ab7SHong Zhang /* 2. Receive other's is[] and process. Then send back */ 229bfc6803cSHong Zhang /*-----------------------------------------------------*/ 2305483b11dSHong Zhang len = 0; 2315483b11dSHong Zhang for (i=0; i<nrqr; i++){ 2325483b11dSHong Zhang if (len_r1[i] > len)len = len_r1[i]; 2335483b11dSHong Zhang } 23445f43ab7SHong Zhang ierr = PetscFree(len_r1);CHKERRQ(ierr); 23545f43ab7SHong Zhang ierr = PetscFree(id_r1);CHKERRQ(ierr); 23645f43ab7SHong Zhang 23745f43ab7SHong Zhang for (proc_id=0; proc_id<size; proc_id++) 23845f43ab7SHong Zhang len_s[proc_id] = iwork[proc_id] = 0; 23945f43ab7SHong Zhang 24013f74950SBarry Smith ierr = PetscMalloc((len+1)*sizeof(PetscInt),&odata1);CHKERRQ(ierr); 24113f74950SBarry Smith ierr = PetscMalloc(size*sizeof(PetscInt**),&odata2_ptr);CHKERRQ(ierr); 24276f244e2SHong Zhang ierr = PetscBTCreate(Mbs,otable);CHKERRQ(ierr); 24310eea884SHong Zhang 24410eea884SHong Zhang len_max = ois_max*(Mbs+1); /* max space storing all is[] for each receive */ 245240e5896SHong Zhang len_est = 2*len_max; /* estimated space of storing is[] for all receiving messages */ 24613f74950SBarry Smith ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr); 24710eea884SHong Zhang nodata2 = 0; /* nodata2+1: num of PetscMalloc(,&odata2_ptr[]) called */ 248240e5896SHong Zhang odata2_ptr[nodata2] = odata2; 24910eea884SHong Zhang len_unused = len_est; /* unused space in the array odata2_ptr[nodata2]-- needs to be >= len_max */ 25010eea884SHong Zhang 251632d0f97SHong Zhang k = 0; 2525483b11dSHong Zhang while (k < nrqr){ 253632d0f97SHong Zhang /* Receive messages */ 254bfc6803cSHong Zhang ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag1,comm,&flag,&r_status);CHKERRQ(ierr); 255632d0f97SHong Zhang if (flag){ 25613f74950SBarry Smith ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr); 257632d0f97SHong Zhang proc_id = r_status.MPI_SOURCE; 25813f74950SBarry Smith ierr = MPI_Irecv(odata1,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr); 2595483b11dSHong Zhang ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr); 260632d0f97SHong Zhang 261fc70d14eSHong Zhang /* Process messages */ 262240e5896SHong Zhang /* make sure there is enough unused space in odata2 array */ 26310eea884SHong Zhang if (len_unused < len_max){ /* allocate more space for odata2 */ 26413f74950SBarry Smith ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr); 265240e5896SHong Zhang odata2_ptr[++nodata2] = odata2; 26610eea884SHong Zhang len_unused = len_est; 26710eea884SHong Zhang } 26810eea884SHong Zhang 26910eea884SHong Zhang ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,odata1,OTHER,odata2,&otable);CHKERRQ(ierr); 270a2a9f22aSHong Zhang len = 1 + odata2[0]; 271a2a9f22aSHong Zhang for (i=0; i<odata2[0]; i++){ 272a2a9f22aSHong Zhang len += odata2[1 + i]; 273fc70d14eSHong Zhang } 274632d0f97SHong Zhang 275632d0f97SHong Zhang /* Send messages back */ 27613f74950SBarry Smith ierr = MPI_Isend(odata2,len,MPIU_INT,proc_id,tag2,comm,s_waits2+k);CHKERRQ(ierr); 277fc70d14eSHong Zhang k++; 27810eea884SHong Zhang odata2 += len; 27910eea884SHong Zhang len_unused -= len; 28045f43ab7SHong Zhang len_s[proc_id] = len; /* num of messages sending back to [proc_id] by this proc */ 281632d0f97SHong Zhang } 2825483b11dSHong Zhang } 2835483b11dSHong Zhang ierr = PetscFree(odata1);CHKERRQ(ierr); 28445f43ab7SHong Zhang ierr = PetscBTDestroy(otable);CHKERRQ(ierr); 285632d0f97SHong Zhang 28645f43ab7SHong Zhang /* 3. Do local work on this processor's is[] */ 28745f43ab7SHong Zhang /*-------------------------------------------*/ 28845f43ab7SHong Zhang /* make sure there is enough unused space in odata2(=data) array */ 28945f43ab7SHong Zhang len_max = is_max*(Mbs+1); /* max space storing all is[] for this processor */ 29010eea884SHong Zhang if (len_unused < len_max){ /* allocate more space for odata2 */ 29113f74950SBarry Smith ierr = PetscMalloc((len_est+1)*sizeof(PetscInt),&odata2);CHKERRQ(ierr); 292240e5896SHong Zhang odata2_ptr[++nodata2] = odata2; 29310eea884SHong Zhang len_unused = len_est; 29410eea884SHong Zhang } 295bfc6803cSHong Zhang 296240e5896SHong Zhang data = odata2; 29745f43ab7SHong Zhang ierr = MatIncreaseOverlap_MPISBAIJ_Local(C,data1_start[rank],MINE,data,table);CHKERRQ(ierr); 29845f43ab7SHong Zhang ierr = PetscFree(data1_start);CHKERRQ(ierr); 29945f43ab7SHong Zhang 30045f43ab7SHong Zhang /* 4. Receive work done on other processors, then merge */ 30145f43ab7SHong Zhang /*------------------------------------------------------*/ 30245f43ab7SHong Zhang /* get max number of messages that this processor expects to recv */ 30313f74950SBarry Smith ierr = MPI_Allreduce(len_s,iwork,size,MPIU_INT,MPI_MAX,comm);CHKERRQ(ierr); 30413f74950SBarry Smith ierr = PetscMalloc((iwork[rank]+1)*sizeof(PetscInt),&data2);CHKERRQ(ierr); 30545f43ab7SHong Zhang ierr = PetscFree(len_s);CHKERRQ(ierr); 30645f43ab7SHong Zhang 307632d0f97SHong Zhang k = 0; 3085483b11dSHong Zhang while (k < nrqs){ 309632d0f97SHong Zhang /* Receive messages */ 310c910923dSHong Zhang ierr = MPI_Iprobe(MPI_ANY_SOURCE,tag2,comm,&flag,&r_status); 311632d0f97SHong Zhang if (flag){ 31213f74950SBarry Smith ierr = MPI_Get_count(&r_status,MPIU_INT,&len);CHKERRQ(ierr); 313632d0f97SHong Zhang proc_id = r_status.MPI_SOURCE; 31413f74950SBarry Smith ierr = MPI_Irecv(data2,len,MPIU_INT,proc_id,r_status.MPI_TAG,comm,&r_req);CHKERRQ(ierr); 3155483b11dSHong Zhang ierr = MPI_Wait(&r_req,&r_status);CHKERRQ(ierr); 3165483b11dSHong Zhang if (len > 1+is_max){ /* Add data2 into data */ 3170472cc68SHong Zhang data2_i = data2 + 1 + is_max; 318fc70d14eSHong Zhang for (i=0; i<is_max; i++){ 319fc70d14eSHong Zhang table_i = table[i]; 320bfc6803cSHong Zhang data_i = data + 1 + is_max + Mbs*i; 321bfc6803cSHong Zhang isz = data[1+i]; 3220472cc68SHong Zhang for (j=0; j<data2[1+i]; j++){ 3230472cc68SHong Zhang col = data2_i[j]; 324bfc6803cSHong Zhang if (!PetscBTLookupSet(table_i,col)) {data_i[isz++] = col;} 325fc70d14eSHong Zhang } 326bfc6803cSHong Zhang data[1+i] = isz; 3270472cc68SHong Zhang if (i < is_max - 1) data2_i += data2[1+i]; 328fc70d14eSHong Zhang } 3295483b11dSHong Zhang } 330632d0f97SHong Zhang k++; 331632d0f97SHong Zhang } 3325483b11dSHong Zhang } 33345f43ab7SHong Zhang ierr = PetscFree(data2);CHKERRQ(ierr); 33445f43ab7SHong Zhang ierr = PetscFree(table);CHKERRQ(ierr); 3355483b11dSHong Zhang 3365483b11dSHong Zhang /* phase 1 sends are complete */ 3375483b11dSHong Zhang ierr = PetscMalloc(size*sizeof(MPI_Status),&s_status);CHKERRQ(ierr); 3380c468ba9SBarry Smith if (nrqs) {ierr = MPI_Waitall(nrqs,s_waits1,s_status);CHKERRQ(ierr);} 3395483b11dSHong Zhang ierr = PetscFree(data1);CHKERRQ(ierr); 3405483b11dSHong Zhang 341240e5896SHong Zhang /* phase 2 sends are complete */ 3420c468ba9SBarry Smith if (nrqr){ierr = MPI_Waitall(nrqr,s_waits2,s_status);CHKERRQ(ierr);} 34345f43ab7SHong Zhang ierr = PetscFree(s_waits1);CHKERRQ(ierr); 34445f43ab7SHong Zhang ierr = PetscFree(s_status);CHKERRQ(ierr); 345632d0f97SHong Zhang 346c910923dSHong Zhang /* 5. Create new is[] */ 347c910923dSHong Zhang /*--------------------*/ 348c910923dSHong Zhang for (i=0; i<is_max; i++) { 349bfc6803cSHong Zhang data_i = data + 1 + is_max + Mbs*i; 350bfc6803cSHong Zhang ierr = ISCreateGeneral(PETSC_COMM_SELF,data[1+i],data_i,is+i);CHKERRQ(ierr); 351632d0f97SHong Zhang } 35245f43ab7SHong Zhang for (k=0; k<=nodata2; k++){ 35345f43ab7SHong Zhang ierr = PetscFree(odata2_ptr[k]);CHKERRQ(ierr); 35445f43ab7SHong Zhang } 35545f43ab7SHong Zhang ierr = PetscFree(odata2_ptr);CHKERRQ(ierr); 3565483b11dSHong Zhang 357632d0f97SHong Zhang PetscFunctionReturn(0); 358632d0f97SHong Zhang } 359632d0f97SHong Zhang 360632d0f97SHong Zhang #undef __FUNCT__ 361632d0f97SHong Zhang #define __FUNCT__ "MatIncreaseOverlap_MPISBAIJ_Local" 362632d0f97SHong Zhang /* 363dc008846SHong Zhang MatIncreaseOverlap_MPISBAIJ_Local - Called by MatIncreaseOverlap, to do 364632d0f97SHong Zhang the work on the local processor. 365632d0f97SHong Zhang 366632d0f97SHong Zhang Inputs: 367632d0f97SHong Zhang C - MAT_MPISBAIJ; 368bfc6803cSHong Zhang data - holds is[]. See MatIncreaseOverlap_MPISBAIJ_Once() for the format. 369bfc6803cSHong Zhang whose - whose is[] to be processed, 370bfc6803cSHong Zhang MINE: this processor's is[] 371bfc6803cSHong Zhang OTHER: other processor's is[] 372632d0f97SHong Zhang Output: 37310eea884SHong Zhang nidx - whose = MINE: 3740472cc68SHong Zhang holds input and newly found indices in the same format as data 3750472cc68SHong Zhang whose = OTHER: 3760472cc68SHong Zhang only holds the newly found indices 3770472cc68SHong Zhang table - table[i]: mark the indices of is[i], i=0,...,is_max. Used only in the case 'whose=MINE'. 378632d0f97SHong Zhang */ 37976f244e2SHong Zhang /* Would computation be reduced by swapping the loop 'for each is' and 'for each row'? */ 38013f74950SBarry Smith static PetscErrorCode MatIncreaseOverlap_MPISBAIJ_Local(Mat C,PetscInt *data,PetscInt whose,PetscInt *nidx,PetscBT *table) 381632d0f97SHong Zhang { 382632d0f97SHong Zhang Mat_MPISBAIJ *c = (Mat_MPISBAIJ*)C->data; 383dc008846SHong Zhang Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)(c->A)->data; 384dc008846SHong Zhang Mat_SeqBAIJ *b = (Mat_SeqBAIJ*)(c->B)->data; 385dfbe8321SBarry Smith PetscErrorCode ierr; 38613f74950SBarry Smith PetscInt row,mbs,Mbs,*nidx_i,col,col_max,isz,isz0,*ai,*aj,*bi,*bj,*garray,rstart,l; 38713f74950SBarry Smith PetscInt a_start,a_end,b_start,b_end,i,j,k,is_max,*idx_i,n; 388bfc6803cSHong Zhang PetscBT table0; /* mark the indices of input is[] for look up */ 389bfc6803cSHong Zhang PetscBT table_i; /* poits to i-th table. When whose=OTHER, a single table is used for all is[] */ 390632d0f97SHong Zhang 391632d0f97SHong Zhang PetscFunctionBegin; 39231f99336SHong Zhang Mbs = c->Mbs; mbs = a->mbs; 393dc008846SHong Zhang ai = a->i; aj = a->j; 394dc008846SHong Zhang bi = b->i; bj = b->j; 395632d0f97SHong Zhang garray = c->garray; 396899cda47SBarry Smith rstart = c->rstartbs; 397dc008846SHong Zhang is_max = data[0]; 398632d0f97SHong Zhang 399fc70d14eSHong Zhang ierr = PetscBTCreate(Mbs,table0);CHKERRQ(ierr); 400fc70d14eSHong Zhang 401fc70d14eSHong Zhang nidx[0] = is_max; 402fc70d14eSHong Zhang idx_i = data + is_max + 1; /* ptr to input is[0] array */ 403bfc6803cSHong Zhang nidx_i = nidx + is_max + 1; /* ptr to output is[0] array */ 404dc008846SHong Zhang for (i=0; i<is_max; i++) { /* for each is */ 405dc008846SHong Zhang isz = 0; 406fc70d14eSHong Zhang n = data[1+i]; /* size of input is[i] */ 407dc008846SHong Zhang 40876f244e2SHong Zhang /* initialize and set table_i(mark idx and nidx) and table0(only mark idx) */ 409bfc6803cSHong Zhang if (whose == MINE){ /* process this processor's is[] */ 410bfc6803cSHong Zhang table_i = table[i]; 4110472cc68SHong Zhang nidx_i = nidx + 1+ is_max + Mbs*i; 412bfc6803cSHong Zhang } else { /* process other processor's is[] - only use one temp table */ 413430a0127SHong Zhang table_i = table[0]; 414bfc6803cSHong Zhang } 415bfc6803cSHong Zhang ierr = PetscBTMemzero(Mbs,table_i);CHKERRQ(ierr); 416bfc6803cSHong Zhang ierr = PetscBTMemzero(Mbs,table0);CHKERRQ(ierr); 41776f244e2SHong Zhang if (n==0) { 41876f244e2SHong Zhang nidx[1+i] = 0; /* size of new is[i] */ 41976f244e2SHong Zhang continue; 42076f244e2SHong Zhang } 42176f244e2SHong Zhang 42276f244e2SHong Zhang isz0 = 0; col_max = 0; 423dc008846SHong Zhang for (j=0; j<n; j++){ 424dc008846SHong Zhang col = idx_i[j]; 42577431f27SBarry Smith if (col >= Mbs) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"index col %D >= Mbs %D",col,Mbs); 426bfc6803cSHong Zhang if(!PetscBTLookupSet(table_i,col)) { 427bfc6803cSHong Zhang ierr = PetscBTSet(table0,col);CHKERRQ(ierr); 4280472cc68SHong Zhang if (whose == MINE) {nidx_i[isz0] = col;} 429dbe03f88SHong Zhang if (col_max < col) col_max = col; 430bfc6803cSHong Zhang isz0++; 431bfc6803cSHong Zhang } 432632d0f97SHong Zhang } 433dc008846SHong Zhang 4340472cc68SHong Zhang if (whose == MINE) {isz = isz0;} 435fc70d14eSHong Zhang k = 0; /* no. of indices from input is[i] that have been examined */ 436dc008846SHong Zhang for (row=0; row<mbs; row++){ 437dc008846SHong Zhang a_start = ai[row]; a_end = ai[row+1]; 438dc008846SHong Zhang b_start = bi[row]; b_end = bi[row+1]; 4390472cc68SHong Zhang if (PetscBTLookup(table0,row+rstart)){ /* row is on input is[i]: 4400472cc68SHong Zhang do row search: collect all col in this row */ 441dc008846SHong Zhang for (l = a_start; l<a_end ; l++){ /* Amat */ 442dc008846SHong Zhang col = aj[l] + rstart; 443fc70d14eSHong Zhang if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;} 444dc008846SHong Zhang } 445dc008846SHong Zhang for (l = b_start; l<b_end ; l++){ /* Bmat */ 446dc008846SHong Zhang col = garray[bj[l]]; 447fc70d14eSHong Zhang if (!PetscBTLookupSet(table_i,col)) {nidx_i[isz++] = col;} 448dc008846SHong Zhang } 449dc008846SHong Zhang k++; 450dc008846SHong Zhang if (k >= isz0) break; /* for (row=0; row<mbs; row++) */ 4510472cc68SHong Zhang } else { /* row is not on input is[i]: 4520472cc68SHong Zhang do col serach: add row onto nidx_i if there is a col in nidx_i */ 453dc008846SHong Zhang for (l = a_start; l<a_end ; l++){ /* Amat */ 454dc008846SHong Zhang col = aj[l] + rstart; 45576f244e2SHong Zhang if (col > col_max) break; 456dc008846SHong Zhang if (PetscBTLookup(table0,col)){ 457fc70d14eSHong Zhang if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;} 458dc008846SHong Zhang break; /* for l = start; l<end ; l++) */ 459632d0f97SHong Zhang } 460632d0f97SHong Zhang } 461dc008846SHong Zhang for (l = b_start; l<b_end ; l++){ /* Bmat */ 462dc008846SHong Zhang col = garray[bj[l]]; 46376f244e2SHong Zhang if (col > col_max) break; 464dc008846SHong Zhang if (PetscBTLookup(table0,col)){ 465fc70d14eSHong Zhang if (!PetscBTLookupSet(table_i,row+rstart)) {nidx_i[isz++] = row+rstart;} 466dc008846SHong Zhang break; /* for l = start; l<end ; l++) */ 467632d0f97SHong Zhang } 468dc008846SHong Zhang } 469dc008846SHong Zhang } 470bfc6803cSHong Zhang } 471dc008846SHong Zhang 472dc008846SHong Zhang if (i < is_max - 1){ 473fc70d14eSHong Zhang idx_i += n; /* ptr to input is[i+1] array */ 474bfc6803cSHong Zhang nidx_i += isz; /* ptr to output is[i+1] array */ 475dc008846SHong Zhang } 476fc70d14eSHong Zhang nidx[1+i] = isz; /* size of new is[i] */ 4771ab97a2aSSatish Balay } /* for each is */ 478dc008846SHong Zhang ierr = PetscBTDestroy(table0);CHKERRQ(ierr); 479632d0f97SHong Zhang 480632d0f97SHong Zhang PetscFunctionReturn(0); 481632d0f97SHong Zhang } 482632d0f97SHong Zhang 483632d0f97SHong Zhang 484