1 #define PETSCVEC_DLL 2 /* 3 Code for some of the parallel vector primatives. 4 */ 5 #include "../src/vec/vec/impls/mpi/pvecimpl.h" 6 #include "petscblaslapack.h" 7 8 #undef __FUNCT__ 9 #define __FUNCT__ "VecMDot_MPI" 10 PetscErrorCode VecMDot_MPI(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z) 11 { 12 PetscScalar awork[128],*work = awork; 13 PetscErrorCode ierr; 14 15 PetscFunctionBegin; 16 if (nv > 128) { 17 ierr = PetscMalloc(nv*sizeof(PetscScalar),&work);CHKERRQ(ierr); 18 } 19 ierr = VecMDot_Seq(xin,nv,y,work);CHKERRQ(ierr); 20 ierr = MPI_Allreduce(work,z,nv,MPIU_SCALAR,PetscSum_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 21 if (nv > 128) { 22 ierr = PetscFree(work);CHKERRQ(ierr); 23 } 24 PetscFunctionReturn(0); 25 } 26 27 #undef __FUNCT__ 28 #define __FUNCT__ "VecMTDot_MPI" 29 PetscErrorCode VecMTDot_MPI(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z) 30 { 31 PetscScalar awork[128],*work = awork; 32 PetscErrorCode ierr; 33 34 PetscFunctionBegin; 35 if (nv > 128) { 36 ierr = PetscMalloc(nv*sizeof(PetscScalar),&work);CHKERRQ(ierr); 37 } 38 ierr = VecMTDot_Seq(xin,nv,y,work);CHKERRQ(ierr); 39 ierr = MPI_Allreduce(work,z,nv,MPIU_SCALAR,PetscSum_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 40 if (nv > 128) { 41 ierr = PetscFree(work);CHKERRQ(ierr); 42 } 43 PetscFunctionReturn(0); 44 } 45 46 #include "../src/vec/vec/impls/seq/ftn-kernels/fnorm.h" 47 #undef __FUNCT__ 48 #define __FUNCT__ "VecNorm_MPI" 49 PetscErrorCode VecNorm_MPI(Vec xin,NormType type,PetscReal *z) 50 { 51 Vec_MPI *x = (Vec_MPI*)xin->data; 52 PetscReal sum,work = 0.0; 53 PetscScalar *xx = x->array; 54 PetscErrorCode ierr; 55 PetscInt n = xin->map->n; 56 57 PetscFunctionBegin; 58 if (type == NORM_2 || type == NORM_FROBENIUS) { 59 60 #if defined(PETSC_HAVE_SLOW_BLAS_NORM2) 61 #if defined(PETSC_USE_FORTRAN_KERNEL_NORM) 62 fortrannormsqr_(xx,&n,&work); 63 #elif defined(PETSC_USE_UNROLLED_NORM) 64 switch (n & 0x3) { 65 case 3: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++; 66 case 2: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++; 67 case 1: work += PetscRealPart(xx[0]*PetscConj(xx[0])); xx++; n -= 4; 68 } 69 while (n>0) { 70 work += PetscRealPart(xx[0]*PetscConj(xx[0])+xx[1]*PetscConj(xx[1])+ 71 xx[2]*PetscConj(xx[2])+xx[3]*PetscConj(xx[3])); 72 xx += 4; n -= 4; 73 } 74 #else 75 {PetscInt i; for (i=0; i<n; i++) work += PetscRealPart((xx[i])*(PetscConj(xx[i])));} 76 #endif 77 #else 78 {PetscBLASInt one = 1,bn = PetscBLASIntCast(n); 79 work = BLASnrm2_(&bn,xx,&one); 80 work *= work; 81 } 82 #endif 83 ierr = MPI_Allreduce(&work,&sum,1,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 84 *z = sqrt(sum); 85 ierr = PetscLogFlops(2.0*xin->map->n);CHKERRQ(ierr); 86 } else if (type == NORM_1) { 87 /* Find the local part */ 88 ierr = VecNorm_Seq(xin,NORM_1,&work);CHKERRQ(ierr); 89 /* Find the global max */ 90 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 91 } else if (type == NORM_INFINITY) { 92 /* Find the local max */ 93 ierr = VecNorm_Seq(xin,NORM_INFINITY,&work);CHKERRQ(ierr); 94 /* Find the global max */ 95 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MAX,((PetscObject)xin)->comm);CHKERRQ(ierr); 96 } else if (type == NORM_1_AND_2) { 97 PetscReal temp[2]; 98 ierr = VecNorm_Seq(xin,NORM_1,temp);CHKERRQ(ierr); 99 ierr = VecNorm_Seq(xin,NORM_2,temp+1);CHKERRQ(ierr); 100 temp[1] = temp[1]*temp[1]; 101 ierr = MPI_Allreduce(temp,z,2,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 102 z[1] = sqrt(z[1]); 103 } 104 PetscFunctionReturn(0); 105 } 106 107 /* 108 These two functions are the MPI reduction operation used for max and min with index 109 The call below to MPI_Op_create() converts the function Vec[Max,Min]_Local() to the 110 MPI operator Vec[Max,Min]_Local_Op. 111 */ 112 MPI_Op VecMax_Local_Op = 0; 113 MPI_Op VecMin_Local_Op = 0; 114 115 EXTERN_C_BEGIN 116 #undef __FUNCT__ 117 #define __FUNCT__ "VecMax_Local" 118 void PETSCVEC_DLLEXPORT MPIAPI VecMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 119 { 120 PetscReal *xin = (PetscReal *)in,*xout = (PetscReal*)out; 121 122 PetscFunctionBegin; 123 if (*datatype != MPIU_REAL) { 124 (*PetscErrorPrintf)("Can only handle MPIU_REAL data types"); 125 MPI_Abort(MPI_COMM_WORLD,1); 126 } 127 if (xin[0] > xout[0]) { 128 xout[0] = xin[0]; 129 xout[1] = xin[1]; 130 } else if (xin[0] == xout[0]) { 131 xout[1] = PetscMin(xin[1],xout[1]); 132 } 133 PetscFunctionReturnVoid(); /* cannot return a value */ 134 } 135 EXTERN_C_END 136 137 EXTERN_C_BEGIN 138 #undef __FUNCT__ 139 #define __FUNCT__ "VecMin_Local" 140 void PETSCVEC_DLLEXPORT MPIAPI VecMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 141 { 142 PetscReal *xin = (PetscReal *)in,*xout = (PetscReal*)out; 143 144 PetscFunctionBegin; 145 if (*datatype != MPIU_REAL) { 146 (*PetscErrorPrintf)("Can only handle MPIU_REAL data types"); 147 MPI_Abort(MPI_COMM_WORLD,1); 148 } 149 if (xin[0] < xout[0]) { 150 xout[0] = xin[0]; 151 xout[1] = xin[1]; 152 } else if (xin[0] == xout[0]) { 153 xout[1] = PetscMin(xin[1],xout[1]); 154 } 155 PetscFunctionReturnVoid(); 156 } 157 EXTERN_C_END 158 159 #undef __FUNCT__ 160 #define __FUNCT__ "VecMax_MPI" 161 PetscErrorCode VecMax_MPI(Vec xin,PetscInt *idx,PetscReal *z) 162 { 163 PetscErrorCode ierr; 164 PetscReal work; 165 166 PetscFunctionBegin; 167 /* Find the local max */ 168 ierr = VecMax_Seq(xin,idx,&work);CHKERRQ(ierr); 169 170 /* Find the global max */ 171 if (!idx) { 172 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MAX,((PetscObject)xin)->comm);CHKERRQ(ierr); 173 } else { 174 PetscReal work2[2],z2[2]; 175 PetscInt rstart; 176 rstart = xin->map->rstart; 177 work2[0] = work; 178 work2[1] = *idx + rstart; 179 ierr = MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMax_Local_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 180 *z = z2[0]; 181 *idx = (PetscInt)z2[1]; 182 } 183 PetscFunctionReturn(0); 184 } 185 186 #undef __FUNCT__ 187 #define __FUNCT__ "VecMin_MPI" 188 PetscErrorCode VecMin_MPI(Vec xin,PetscInt *idx,PetscReal *z) 189 { 190 PetscErrorCode ierr; 191 PetscReal work; 192 193 PetscFunctionBegin; 194 /* Find the local Min */ 195 ierr = VecMin_Seq(xin,idx,&work);CHKERRQ(ierr); 196 197 /* Find the global Min */ 198 if (!idx) { 199 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MIN,((PetscObject)xin)->comm);CHKERRQ(ierr); 200 } else { 201 PetscReal work2[2],z2[2]; 202 PetscInt rstart; 203 204 ierr = VecGetOwnershipRange(xin,&rstart,PETSC_NULL);CHKERRQ(ierr); 205 work2[0] = work; 206 work2[1] = *idx + rstart; 207 ierr = MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMin_Local_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 208 *z = z2[0]; 209 *idx = (PetscInt)z2[1]; 210 } 211 PetscFunctionReturn(0); 212 } 213 214 215 216 217 218 219 220 221