ScaLAPACK 2.1  2.1
ScaLAPACK: Scalable Linear Algebra PACKage
pdnrm2_.c
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------
2 *
3 * -- PBLAS routine (version 2.0) --
4 * University of Tennessee, Knoxville, Oak Ridge National Laboratory,
5 * and University of California, Berkeley.
6 * April 1, 1998
7 *
8 * ---------------------------------------------------------------------
9 */
10 /*
11 * Include files
12 */
13 #include "pblas.h"
14 #include "PBpblas.h"
15 #include "PBtools.h"
16 #include "PBblacs.h"
17 #include "PBblas.h"
18 
19 #ifdef __STDC__
20 void pdnrm2_( int * N, double * NORM2,
21  double * X, int * IX, int * JX, int * DESCX, int * INCX )
22 #else
23 void pdnrm2_( N, NORM2, X, IX, JX, DESCX, INCX )
24 /*
25 * .. Scalar Arguments ..
26 */
27  int * INCX, * IX, * JX, * N;
28  double * NORM2;
29 /*
30 * .. Array Arguments ..
31 */
32  int * DESCX;
33  double * X;
34 #endif
35 {
36 /*
37 * Purpose
38 * =======
39 *
40 * PDNRM2 computes the 2-norm of a subvector sub( X ),
41 *
42 * where
43 *
44 * sub( X ) denotes X(IX,JX:JX+N-1) if INCX = M_X,
45 * X(IX:IX+N-1,JX) if INCX = 1 and INCX <> M_X.
46 *
47 * Notes
48 * =====
49 *
50 * A description vector is associated with each 2D block-cyclicly dis-
51 * tributed matrix. This vector stores the information required to
52 * establish the mapping between a matrix entry and its corresponding
53 * process and memory location.
54 *
55 * In the following comments, the character _ should be read as
56 * "of the distributed matrix". Let A be a generic term for any 2D
57 * block cyclicly distributed matrix. Its description vector is DESC_A:
58 *
59 * NOTATION STORED IN EXPLANATION
60 * ---------------- --------------- ------------------------------------
61 * DTYPE_A (global) DESCA[ DTYPE_ ] The descriptor type.
62 * CTXT_A (global) DESCA[ CTXT_ ] The BLACS context handle, indicating
63 * the NPROW x NPCOL BLACS process grid
64 * A is distributed over. The context
65 * itself is global, but the handle
66 * (the integer value) may vary.
67 * M_A (global) DESCA[ M_ ] The number of rows in the distribu-
68 * ted matrix A, M_A >= 0.
69 * N_A (global) DESCA[ N_ ] The number of columns in the distri-
70 * buted matrix A, N_A >= 0.
71 * IMB_A (global) DESCA[ IMB_ ] The number of rows of the upper left
72 * block of the matrix A, IMB_A > 0.
73 * INB_A (global) DESCA[ INB_ ] The number of columns of the upper
74 * left block of the matrix A,
75 * INB_A > 0.
76 * MB_A (global) DESCA[ MB_ ] The blocking factor used to distri-
77 * bute the last M_A-IMB_A rows of A,
78 * MB_A > 0.
79 * NB_A (global) DESCA[ NB_ ] The blocking factor used to distri-
80 * bute the last N_A-INB_A columns of
81 * A, NB_A > 0.
82 * RSRC_A (global) DESCA[ RSRC_ ] The process row over which the first
83 * row of the matrix A is distributed,
84 * NPROW > RSRC_A >= 0.
85 * CSRC_A (global) DESCA[ CSRC_ ] The process column over which the
86 * first column of A is distributed.
87 * NPCOL > CSRC_A >= 0.
88 * LLD_A (local) DESCA[ LLD_ ] The leading dimension of the local
89 * array storing the local blocks of
90 * the distributed matrix A,
91 * IF( Lc( 1, N_A ) > 0 )
92 * LLD_A >= MAX( 1, Lr( 1, M_A ) )
93 * ELSE
94 * LLD_A >= 1.
95 *
96 * Let K be the number of rows of a matrix A starting at the global in-
97 * dex IA,i.e, A( IA:IA+K-1, : ). Lr( IA, K ) denotes the number of rows
98 * that the process of row coordinate MYROW ( 0 <= MYROW < NPROW ) would
99 * receive if these K rows were distributed over NPROW processes. If K
100 * is the number of columns of a matrix A starting at the global index
101 * JA, i.e, A( :, JA:JA+K-1, : ), Lc( JA, K ) denotes the number of co-
102 * lumns that the process MYCOL ( 0 <= MYCOL < NPCOL ) would receive if
103 * these K columns were distributed over NPCOL processes.
104 *
105 * The values of Lr() and Lc() may be determined via a call to the func-
106 * tion PB_Cnumroc:
107 * Lr( IA, K ) = PB_Cnumroc( K, IA, IMB_A, MB_A, MYROW, RSRC_A, NPROW )
108 * Lc( JA, K ) = PB_Cnumroc( K, JA, INB_A, NB_A, MYCOL, CSRC_A, NPCOL )
109 *
110 * Arguments
111 * =========
112 *
113 * N (global input) INTEGER
114 * On entry, N specifies the length of the subvector sub( X ).
115 * N must be at least zero.
116 *
117 * NORM2 (local output) DOUBLE PRECISION
118 * On exit, NORM2 specifies the 2-norm of the subvector sub( X )
119 * only in its scope (See below for further details).
120 *
121 * X (local input) DOUBLE PRECISION array
122 * On entry, X is an array of dimension (LLD_X, Kx), where LLD_X
123 * is at least MAX( 1, Lr( 1, IX ) ) when INCX = M_X and
124 * MAX( 1, Lr( 1, IX+N-1 ) ) otherwise, and, Kx is at least
125 * Lc( 1, JX+N-1 ) when INCX = M_X and Lc( 1, JX ) otherwise.
126 * Before entry, this array contains the local entries of the
127 * matrix X.
128 *
129 * IX (global input) INTEGER
130 * On entry, IX specifies X's global row index, which points to
131 * the beginning of the submatrix sub( X ).
132 *
133 * JX (global input) INTEGER
134 * On entry, JX specifies X's global column index, which points
135 * to the beginning of the submatrix sub( X ).
136 *
137 * DESCX (global and local input) INTEGER array
138 * On entry, DESCX is an integer array of dimension DLEN_. This
139 * is the array descriptor for the matrix X.
140 *
141 * INCX (global input) INTEGER
142 * On entry, INCX specifies the global increment for the
143 * elements of X. Only two values of INCX are supported in
144 * this version, namely 1 and M_X. INCX must not be zero.
145 *
146 * Further Details
147 * ===============
148 *
149 * When the result of a vector-oriented PBLAS call is a scalar, this
150 * scalar is set only within the process scope which owns the vector(s)
151 * being operated on. Let sub( X ) be a generic term for the input vec-
152 * tor(s). Then, the processes owning the correct the answer is determi-
153 * ned as follows: if an operation involves more than one vector, the
154 * processes receiving the result will be the union of the following set
155 * of processes for each vector:
156 *
157 * If N = 1, M_X = 1 and INCX = 1, then one cannot determine if a pro-
158 * cess row or process column owns the vector operand, therefore only
159 * the process owning sub( X ) receives the correct result;
160 *
161 * If INCX = M_X, then sub( X ) is a vector distributed over a process
162 * row. Each process in this row receives the result;
163 *
164 * If INCX = 1, then sub( X ) is a vector distributed over a process
165 * column. Each process in this column receives the result;
166 *
167 * -- Written on April 1, 1998 by
168 * Antoine Petitet, University of Tennessee, Knoxville 37996, USA.
169 *
170 * ---------------------------------------------------------------------
171 */
172 /*
173 * .. Local Scalars ..
174 */
175  char top;
176  int Xcol, Xi, Xii, Xj, Xjj, Xld, Xnp, Xnq, Xrow, ctxt, dst, dist,
177  info, k, mycol, mydist, myrow, npcol, nprow, src;
178  double scale, ssq, temp1, temp2;
179 /*
180 * .. Local Arrays ..
181 */
182  int Xd[DLEN_];
183  double * Xptr = NULL, work[4];
184 /* ..
185 * .. Executable Statements ..
186 *
187 */
188  PB_CargFtoC( *IX, *JX, DESCX, &Xi, &Xj, Xd );
189 #ifndef NO_ARGCHK
190 /*
191 * Test the input parameters
192 */
193  Cblacs_gridinfo( ( ctxt = Xd[CTXT_] ), &nprow, &npcol, &myrow, &mycol );
194  if( !( info = ( ( nprow == -1 ) ? -( 601 + CTXT_ ) : 0 ) ) )
195  PB_Cchkvec( ctxt, "PDNRM2", "X", *N, 1, Xi, Xj, Xd, *INCX, 6, &info );
196  if( info ) { PB_Cabort( ctxt, "PDNRM2", info ); return; }
197 #endif
198 /*
199 * Initialize NORM2
200 */
201  *NORM2 = ZERO;
202 /*
203 * Quick return if possible
204 */
205  if( *N == 0 ) return;
206 /*
207 * Retrieve process grid information
208 */
209 #ifdef NO_ARGCHK
210  Cblacs_gridinfo( ( ctxt = Xd[CTXT_] ), &nprow, &npcol, &myrow, &mycol );
211 #endif
212 /*
213 * Retrieve sub( X )'s local information: Xii, Xjj, Xrow, Xcol
214 */
215  PB_Cinfog2l( Xi, Xj, Xd, nprow, npcol, myrow, mycol, &Xii, &Xjj,
216  &Xrow, &Xcol );
217 /*
218 * Handle degenerate case separately, sub( X )'s scope is just one process
219 */
220  if( ( *N == 1 ) && ( *INCX == 1 ) && ( Xd[M_] == 1 ) )
221  {
222 /*
223 * Make sure I own some data and compute NORM2
224 */
225  if( ( ( myrow == Xrow ) || ( Xrow < 0 ) ) &&
226  ( ( mycol == Xcol ) || ( Xcol < 0 ) ) )
227  *NORM2 = ABS( X[Xii+Xjj*Xd[LLD_]] );
228  return;
229  }
230  else if( *INCX == Xd[M_] )
231  {
232 /*
233 * sub( X ) resides in (a) process row(s)
234 */
235  if( ( myrow == Xrow ) || ( Xrow < 0 ) )
236  {
237 /*
238 * Initialize SCALE and SSQ
239 */
240  scale = ZERO;
241  ssq = ONE;
242 /*
243 * Make sure I own some data and compute local sum of squares
244 */
245  Xnq = PB_Cnumroc( *N, Xj, Xd[INB_], Xd[NB_], mycol, Xd[CSRC_], npcol );
246  if( Xnq > 0 )
247  {
248  Xld = Xd[LLD_];
249  Xptr = X+(Xii+Xjj*Xld);
250 
251  for( k = 0; k < Xnq; k++ )
252  {
253  if( *Xptr != ZERO )
254  {
255  temp1 = ABS( *Xptr );
256  if( scale < temp1 )
257  {
258  temp2 = scale / temp1;
259  ssq = ONE + ssq * ( temp2 * temp2 );
260  scale = temp1;
261  }
262  else
263  {
264  temp2 = temp1 / scale;
265  ssq = ssq + ( temp2 * temp2 );
266  }
267  }
268  Xptr += Xld;
269  }
270  }
271 /*
272 * If Xnq <= 0, SCALE is zero and SSQ is one (see initialization above)
273 */
274  if( ( npcol >= 2 ) && ( Xcol >= 0 ) )
275  {
276 /*
277 * Combine the local sum of squares using a 1-tree topology within process row
278 * 0 if npcol > 1 and Xcol >= 0, i.e sub( X ) is distributed.
279 */
280  work[0] = scale;
281  work[1] = ssq;
282 
283  mydist = mycol;
284  k = 1;
285 l_10:
286  if( mydist & 1 )
287  {
288  dist = k * ( mydist - 1 );
289  dst = MPosMod( dist, npcol );
290  Cdgesd2d( ctxt, 2, 1, ((char*) work), 2, myrow, dst );
291  goto l_20;
292  }
293  else
294  {
295  dist = mycol + k;
296  src = MPosMod( dist, npcol );
297 
298  if( mycol < src )
299  {
300  Cdgerv2d( ctxt, 2, 1, ((char*)&work[2]), 2, myrow, src );
301  if( work[0] >= work[2] )
302  {
303  if( work[0] != ZERO )
304  {
305  temp1 = work[2] / work[0];
306  work[1] = work[1] + ( temp1 * temp1 ) * work[3];
307  }
308  }
309  else
310  {
311  temp1 = work[0] / work[2];
312  work[1] = work[3] + ( temp1 * temp1 ) * work[1];
313  work[0] = work[2];
314  }
315  }
316  mydist >>= 1;
317  }
318  k <<= 1;
319 
320  if( k < npcol ) goto l_10;
321 l_20:
322 /*
323 * Process column 0 broadcasts the combined values of SCALE and SSQ within their
324 * process row.
325 */
326  top = *PB_Ctop( &ctxt, BCAST, ROW, TOP_GET );
327  if( mycol == 0 )
328  {
329  Cdgebs2d( ctxt, ROW, &top, 2, 1, ((char*)work), 2 );
330  }
331  else
332  {
333  Cdgebr2d( ctxt, ROW, &top, 2, 1, ((char*)work), 2,
334  myrow, 0 );
335  }
336 /*
337 * Compute NORM2 redundantly NORM2 = WORK( 1 ) * SQRT( WORK( 2 ) )
338 */
339  dasqrtb_( &work[0], &work[1], NORM2 );
340  }
341  else
342  {
343 /*
344 * Compute NORM2 redundantly ( sub( X ) is not distributed )
345 */
346  dasqrtb_( &scale, &ssq, NORM2 );
347  }
348  }
349  return;
350  }
351  else
352  {
353 /*
354 * sub( X ) resides in (a) process column(s)
355 */
356  if( ( mycol == Xcol ) || ( Xcol < 0 ) )
357  {
358 /*
359 * Initialize SCALE and SSQ
360 */
361  scale = ZERO;
362  ssq = ONE;
363 /*
364 * Make sure I own some data and compute local sum of squares
365 */
366  Xnp = PB_Cnumroc( *N, Xi, Xd[IMB_], Xd[MB_], myrow, Xd[RSRC_], nprow );
367  if( Xnp > 0 )
368  {
369  Xptr = X+(Xii+Xjj*Xd[LLD_]);
370 
371  for( k = 0; k < Xnp; k++ )
372  {
373  if( *Xptr != ZERO )
374  {
375  temp1 = ABS( *Xptr );
376  if( scale < temp1 )
377  {
378  temp2 = scale / temp1;
379  ssq = ONE + ssq * ( temp2 * temp2 );
380  scale = temp1;
381  }
382  else
383  {
384  temp2 = temp1 / scale;
385  ssq = ssq + ( temp2 * temp2 );
386  }
387  }
388  Xptr++;
389  }
390  }
391 /*
392 * If Xnp <= 0, SCALE is zero and SSQ is one (see initialization above)
393 */
394  if( ( nprow >= 2 ) && ( Xrow >= 0 ) )
395  {
396 /*
397 * Combine the local sum of squares using a 1-tree topology within process
398 * column 0 if nprow > 1 and Xrow >= 0, i.e sub( X ) is distributed.
399 */
400  work[0] = scale;
401  work[1] = ssq;
402 
403  mydist = myrow;
404  k = 1;
405 l_30:
406  if( mydist & 1 )
407  {
408  dist = k * ( mydist - 1 );
409  dst = MPosMod( dist, nprow );
410  Cdgesd2d( ctxt, 2, 1, ((char*)work), 2, dst, mycol );
411  goto l_40;
412  }
413  else
414  {
415  dist = myrow + k;
416  src = MPosMod( dist, nprow );
417 
418  if( myrow < src )
419  {
420  Cdgerv2d( ctxt, 2, 1, ((char*)&work[2]), 2, src, mycol );
421  if( work[0] >= work[2] )
422  {
423  if( work[0] != ZERO )
424  {
425  temp1 = work[2] / work[0];
426  work[1] = work[1] + ( temp1 * temp1 ) * work[3];
427  }
428  }
429  else
430  {
431  temp1 = work[0] / work[2];
432  work[1] = work[3] + ( temp1 * temp1 ) * work[1];
433  work[0] = work[2];
434  }
435  }
436  mydist >>= 1;
437  }
438  k <<= 1;
439 
440  if( k < nprow ) goto l_30;
441 l_40:
442 /*
443 * Process column 0 broadcasts the combined values of SCALE and SSQ within their
444 * process column
445 */
446  top = *PB_Ctop( &ctxt, BCAST, COLUMN, TOP_GET );
447  if( myrow == 0 )
448  {
449  Cdgebs2d( ctxt, COLUMN, &top, 2, 1, ((char*)work), 2 );
450  }
451  else
452  {
453  Cdgebr2d( ctxt, COLUMN, &top, 2, 1, ((char*)work), 2,
454  0, mycol );
455  }
456 /*
457 * Compute NORM2 redundantly NORM2 = WORK[0] * SQRT( WORK[1] )
458 */
459  dasqrtb_( &work[0], &work[1], NORM2 );
460  }
461  else
462  {
463 /*
464 * Compute NORM2 redundantly ( sub( X ) is not distributed )
465 */
466  dasqrtb_( &scale, &ssq, NORM2 );
467  }
468  }
469  return;
470  }
471 /*
472 * End of PDNRM2
473 */
474 }
M_
#define M_
Definition: PBtools.h:39
ROW
#define ROW
Definition: PBblacs.h:46
MB_
#define MB_
Definition: PBtools.h:43
NB_
#define NB_
Definition: PBtools.h:44
COLUMN
#define COLUMN
Definition: PBblacs.h:45
CSRC_
#define CSRC_
Definition: PBtools.h:46
PBblacs.h
PBtools.h
PBblas.h
dasqrtb_
F_VOID_FCT dasqrtb_()
PBpblas.h
DLEN_
#define DLEN_
Definition: PBtools.h:48
MPosMod
#define MPosMod(I, d)
Definition: PBtools.h:95
Cdgebs2d
void Cdgebs2d()
LLD_
#define LLD_
Definition: PBtools.h:47
Cdgesd2d
void Cdgesd2d()
ZERO
#define ZERO
Definition: PBtools.h:66
PB_Cchkvec
void PB_Cchkvec()
IMB_
#define IMB_
Definition: PBtools.h:41
pdnrm2_
void pdnrm2_(int *N, double *NORM2, double *X, int *IX, int *JX, int *DESCX, int *INCX)
Definition: pdnrm2_.c:23
PB_Cabort
void PB_Cabort()
TOP_GET
#define TOP_GET
Definition: PBblacs.h:50
Cdgebr2d
void Cdgebr2d()
PB_Ctop
char * PB_Ctop()
ONE
#define ONE
Definition: PBtools.h:64
RSRC_
#define RSRC_
Definition: PBtools.h:45
PB_CargFtoC
void PB_CargFtoC()
BCAST
#define BCAST
Definition: PBblacs.h:48
PB_Cinfog2l
void PB_Cinfog2l()
PB_Cnumroc
int PB_Cnumroc()
ABS
#define ABS(a_)
Definition: PBtools.h:75
Cdgerv2d
void Cdgerv2d()
INB_
#define INB_
Definition: PBtools.h:42
Cblacs_gridinfo
void Cblacs_gridinfo()
pblas.h
CTXT_
#define CTXT_
Definition: PBtools.h:38