Actual source code: pvec2.c
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 ../src/inline/dot.h
7: #include petscblaslapack.h
11: PetscErrorCode VecMDot_MPI(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z)
12: {
13: PetscScalar awork[128],*work = awork;
17: if (nv > 128) {
18: PetscMalloc(nv*sizeof(PetscScalar),&work);
19: }
20: VecMDot_Seq(xin,nv,y,work);
21: MPI_Allreduce(work,z,nv,MPIU_SCALAR,PetscSum_Op,((PetscObject)xin)->comm);
22: if (nv > 128) {
23: PetscFree(work);
24: }
25: return(0);
26: }
30: PetscErrorCode VecMTDot_MPI(Vec xin,PetscInt nv,const Vec y[],PetscScalar *z)
31: {
32: PetscScalar awork[128],*work = awork;
36: if (nv > 128) {
37: PetscMalloc(nv*sizeof(PetscScalar),&work);
38: }
39: VecMTDot_Seq(xin,nv,y,work);
40: MPI_Allreduce(work,z,nv,MPIU_SCALAR,PetscSum_Op,((PetscObject)xin)->comm);
41: if (nv > 128) {
42: PetscFree(work);
43: }
44: return(0);
45: }
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;
55: PetscInt n = xin->map->n;
58: if (type == NORM_2 || type == NORM_FROBENIUS) {
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: MPI_Allreduce(&work,&sum,1,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);
84: *z = sqrt(sum);
85: PetscLogFlops(2*xin->map->n);
86: } else if (type == NORM_1) {
87: /* Find the local part */
88: VecNorm_Seq(xin,NORM_1,&work);
89: /* Find the global max */
90: MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);
91: } else if (type == NORM_INFINITY) {
92: /* Find the local max */
93: VecNorm_Seq(xin,NORM_INFINITY,&work);
94: /* Find the global max */
95: MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MAX,((PetscObject)xin)->comm);
96: } else if (type == NORM_1_AND_2) {
97: PetscReal temp[2];
98: VecNorm_Seq(xin,NORM_1,temp);
99: VecNorm_Seq(xin,NORM_2,temp+1);
100: temp[1] = temp[1]*temp[1];
101: MPI_Allreduce(temp,z,2,MPIU_REAL,MPI_SUM,((PetscObject)xin)->comm);
102: z[1] = sqrt(z[1]);
103: }
104: return(0);
105: }
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;
118: void MPIAPI VecMax_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
119: {
120: PetscReal *xin = (PetscReal *)in,*xout = (PetscReal*)out;
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: }
131: PetscFunctionReturnVoid(); /* cannot return a value */
132: }
138: void MPIAPI VecMin_Local(void *in,void *out,PetscMPIInt *cnt,MPI_Datatype *datatype)
139: {
140: PetscReal *xin = (PetscReal *)in,*xout = (PetscReal*)out;
143: if (*datatype != MPIU_REAL) {
144: (*PetscErrorPrintf)("Can only handle MPIU_REAL data types");
145: MPI_Abort(MPI_COMM_WORLD,1);
146: }
147: if (xin[0] < xout[0]) {
148: xout[0] = xin[0];
149: xout[1] = xin[1];
150: }
151: PetscFunctionReturnVoid();
152: }
157: PetscErrorCode VecMax_MPI(Vec xin,PetscInt *idx,PetscReal *z)
158: {
160: PetscReal work;
163: /* Find the local max */
164: VecMax_Seq(xin,idx,&work);
166: /* Find the global max */
167: if (!idx) {
168: MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MAX,((PetscObject)xin)->comm);
169: } else {
170: PetscReal work2[2],z2[2];
171: PetscInt rstart;
172: rstart = xin->map->rstart;
173: work2[0] = work;
174: work2[1] = *idx + rstart;
175: MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMax_Local_Op,((PetscObject)xin)->comm);
176: *z = z2[0];
177: *idx = (PetscInt)z2[1];
178: }
179: return(0);
180: }
184: PetscErrorCode VecMin_MPI(Vec xin,PetscInt *idx,PetscReal *z)
185: {
187: PetscReal work;
190: /* Find the local Min */
191: VecMin_Seq(xin,idx,&work);
193: /* Find the global Min */
194: if (!idx) {
195: MPI_Allreduce(&work,z,1,MPIU_REAL,MPI_MIN,((PetscObject)xin)->comm);
196: } else {
197: PetscReal work2[2],z2[2];
198: PetscInt rstart;
200: VecGetOwnershipRange(xin,&rstart,PETSC_NULL);
201: work2[0] = work;
202: work2[1] = *idx + rstart;
203: MPI_Allreduce(work2,z2,2,MPIU_REAL,VecMin_Local_Op,((PetscObject)xin)->comm);
204: *z = z2[0];
205: *idx = (PetscInt)z2[1];
206: }
207: return(0);
208: }