1 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,MPIU_SUM,((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,MPIU_SUM,((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 PetscReal sum,work = 0.0; 52 const PetscScalar *xx; 53 PetscErrorCode ierr; 54 PetscInt n = xin->map->n; 55 PetscBLASInt one = 1,bn; 56 57 PetscFunctionBegin; 58 ierr = PetscBLASIntCast(n,&bn);CHKERRQ(ierr); 59 if (type == NORM_2 || type == NORM_FROBENIUS) { 60 ierr = VecGetArrayRead(xin,&xx);CHKERRQ(ierr); 61 work = PetscRealPart(BLASdot_(&bn,xx,&one,xx,&one)); 62 ierr = VecRestoreArrayRead(xin,&xx);CHKERRQ(ierr); 63 ierr = MPI_Allreduce(&work,&sum,1,MPIU_REAL,MPIU_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 64 *z = PetscSqrtReal(sum); 65 ierr = PetscLogFlops(2.0*xin->map->n);CHKERRQ(ierr); 66 } else if (type == NORM_1) { 67 /* Find the local part */ 68 ierr = VecNorm_Seq(xin,NORM_1,&work);CHKERRQ(ierr); 69 /* Find the global max */ 70 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPIU_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 71 } else if (type == NORM_INFINITY) { 72 /* Find the local max */ 73 ierr = VecNorm_Seq(xin,NORM_INFINITY,&work);CHKERRQ(ierr); 74 /* Find the global max */ 75 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPIU_MAX,((PetscObject)xin)->comm);CHKERRQ(ierr); 76 } else if (type == NORM_1_AND_2) { 77 PetscReal temp[2]; 78 ierr = VecNorm_Seq(xin,NORM_1,temp);CHKERRQ(ierr); 79 ierr = VecNorm_Seq(xin,NORM_2,temp+1);CHKERRQ(ierr); 80 temp[1] = temp[1]*temp[1]; 81 ierr = MPI_Allreduce(temp,z,2,MPIU_REAL,MPIU_SUM,((PetscObject)xin)->comm);CHKERRQ(ierr); 82 z[1] = PetscSqrtReal(z[1]); 83 } 84 PetscFunctionReturn(0); 85 } 86 87 /* 88 These two functions are the MPI reduction operation used for max and min with index 89 The call below to MPI_Op_create() converts the function Vec[Max,Min]_Local() to the 90 MPI operator Vec[Max,Min]_Local_Op. 91 */ 92 MPI_Op VecMax_Local_Op = 0; 93 MPI_Op VecMin_Local_Op = 0; 94 95 EXTERN_C_BEGIN 96 #undef __FUNCT__ 97 #define __FUNCT__ "VecMax_Local" 98 void MPIAPI VecMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 99 { 100 PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out; 101 102 PetscFunctionBegin; 103 if (*datatype != MPIU_REAL) { 104 (*PetscErrorPrintf)("Can only handle MPIU_REAL data types"); 105 MPI_Abort(MPI_COMM_SELF,1); 106 } 107 if (xin[0] > xout[0]) { 108 xout[0] = xin[0]; 109 xout[1] = xin[1]; 110 } else if (xin[0] == xout[0]) { 111 xout[1] = PetscMin(xin[1],xout[1]); 112 } 113 PetscFunctionReturnVoid(); /* cannot return a value */ 114 } 115 EXTERN_C_END 116 117 EXTERN_C_BEGIN 118 #undef __FUNCT__ 119 #define __FUNCT__ "VecMin_Local" 120 void MPIAPI VecMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype) 121 { 122 PetscReal *xin = (PetscReal*)in,*xout = (PetscReal*)out; 123 124 PetscFunctionBegin; 125 if (*datatype != MPIU_REAL) { 126 (*PetscErrorPrintf)("Can only handle MPIU_REAL data types"); 127 MPI_Abort(MPI_COMM_SELF,1); 128 } 129 if (xin[0] < xout[0]) { 130 xout[0] = xin[0]; 131 xout[1] = xin[1]; 132 } else if (xin[0] == xout[0]) { 133 xout[1] = PetscMin(xin[1],xout[1]); 134 } 135 PetscFunctionReturnVoid(); 136 } 137 EXTERN_C_END 138 139 #undef __FUNCT__ 140 #define __FUNCT__ "VecMax_MPI" 141 PetscErrorCode VecMax_MPI(Vec xin,PetscInt *idx,PetscReal *z) 142 { 143 PetscErrorCode ierr; 144 PetscReal work; 145 146 PetscFunctionBegin; 147 /* Find the local max */ 148 ierr = VecMax_Seq(xin,idx,&work);CHKERRQ(ierr); 149 150 /* Find the global max */ 151 if (!idx) { 152 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPIU_MAX,((PetscObject)xin)->comm);CHKERRQ(ierr); 153 } else { 154 PetscReal work2[2],z2[2]; 155 PetscInt rstart; 156 rstart = xin->map->rstart; 157 work2[0] = work; 158 work2[1] = *idx + rstart; 159 ierr = MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMax_Local_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 160 *z = z2[0]; 161 *idx = (PetscInt)z2[1]; 162 } 163 PetscFunctionReturn(0); 164 } 165 166 #undef __FUNCT__ 167 #define __FUNCT__ "VecMin_MPI" 168 PetscErrorCode VecMin_MPI(Vec xin,PetscInt *idx,PetscReal *z) 169 { 170 PetscErrorCode ierr; 171 PetscReal work; 172 173 PetscFunctionBegin; 174 /* Find the local Min */ 175 ierr = VecMin_Seq(xin,idx,&work);CHKERRQ(ierr); 176 177 /* Find the global Min */ 178 if (!idx) { 179 ierr = MPI_Allreduce(&work,z,1,MPIU_REAL,MPIU_MIN,((PetscObject)xin)->comm);CHKERRQ(ierr); 180 } else { 181 PetscReal work2[2],z2[2]; 182 PetscInt rstart; 183 184 ierr = VecGetOwnershipRange(xin,&rstart,PETSC_NULL);CHKERRQ(ierr); 185 work2[0] = work; 186 work2[1] = *idx + rstart; 187 ierr = MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMin_Local_Op,((PetscObject)xin)->comm);CHKERRQ(ierr); 188 *z = z2[0]; 189 *idx = (PetscInt)z2[1]; 190 } 191 PetscFunctionReturn(0); 192 } 193 194 195 196 197 198 199 200 201