SCALAPACK 2.2.2
LAPACK: Linear Algebra PACKage
Loading...
Searching...
No Matches
pctrmrdrv.c
Go to the documentation of this file.
1#include "redist.h"
2/* $Id: pctrmrdrv.c,v 1.1.1.1 2000/02/15 18:04:11 susan Exp $
3 *
4 * pctrmrdrv.c :
5 *
6 *
7 * PURPOSE:
8 *
9 * this driver is testing the PCTRMR2D routine. It calls it to obtain a new
10 * scattered block data decomposition of a distributed COMPLEX (block
11 * scattered) matrix. Then it calls PCTRMR2D for the inverse redistribution
12 * and checks the results with the initial data.
13 *
14 * Data are going from a Block Scattered nbrow0 x nbcol0 decomposition on the
15 * processor grid p0 x q0, to data distributed in a BS nbrow1 x nbcol1 on the
16 * processor grid p1 x q1, then back to the BS nbrow0 x nbcol0 decomposition
17 * on the processor grid p0 x q0.
18 *
19 * See pctrmr.c file for detailed info on the PCTRMR2D function.
20 *
21 *
22 * The testing parameters are read from the file TRMR2D.dat, see the file in the
23 * distribution to have an example.
24 *
25 * created by Bernard Tourancheau in April 1994.
26 *
27 * modifications : see sccs history
28 *
29 * ===================================
30 *
31 *
32 * NOTE :
33 *
34 * - the matrix elements are COMPLEX
35 *
36 * - memory requirements : this procedure requires approximately 3 times the
37 * memory space of the initial data block in grid 0 (initial block, copy for
38 * test and second redistribution result) and 1 time the memory space of the
39 * result data block in grid 1. with the element size = sizeof(complex)
40 * bytes,
41 *
42 *
43 * - use the procedures of the files:
44 *
45 * pctrmr.o pctrmr2.o pctrmraux.o
46 *
47 *
48 * ======================================
49 *
50 * WARNING ASSUMPTIONS :
51 *
52 *
53 * ========================================
54 *
55 *
56 * Planned changes:
57 *
58 *
59 *
60 * ========================================= */
61#define static2 static
62#if defined(Add_) || defined(f77IsF2C)
63#define fortran_mr2d pctrmr2do_
64#define fortran_mr2dnew pctrmr2d_
65#elif defined(UpCase)
66#define fortran_mr2dnew PCTRMR2D
67#define fortran_mr2d PCTRMR2DO
68#define ccopy_ CCOPY
69#define clacpy_ CLACPY
70#else
71#define fortran_mr2d pctrmr2do
72#define fortran_mr2dnew pctrmr2d
73#define ccopy_ ccopy
74#define clacpy_ clacpy
75#endif
76#define Clacpy Cctrlacpy
77void Clacpy();
78typedef struct {
79 float r, i;
80} complex;
81typedef struct {
82 Int desctype;
83 Int ctxt;
84 Int m;
85 Int n;
86 Int nbrow;
87 Int nbcol;
88 Int sprow;
89 Int spcol;
90 Int lda;
91} MDESC;
92#define BLOCK_CYCLIC_2D 1
93typedef struct {
94 Int gstart;
95 Int len;
96} IDESC;
97#define SHIFT(row,sprow,nbrow) ((row)-(sprow)+ ((row) >= (sprow) ? 0 : (nbrow)))
98#define max(A,B) ((A)>(B)?(A):(B))
99#define min(A,B) ((A)>(B)?(B):(A))
100#define DIVUP(a,b) ( ((a)-1) /(b)+1)
101#define ROUNDUP(a,b) (DIVUP(a,b)*(b))
102#ifdef MALLOCDEBUG
103#define malloc mymalloc
104#define free myfree
105#define realloc myrealloc
106#endif
107/* Cblacs */
108extern void Cblacs_pcoord();
110extern void Csetpvmtids();
111extern void Cblacs_get();
112extern void Cblacs_pinfo();
113extern void Cblacs_gridinfo();
114extern void Cblacs_gridinit();
115extern void Cblacs_exit();
116extern void Cblacs_gridexit();
117extern void Cblacs_setup();
118extern void Cigebs2d();
119extern void Cigebr2d();
120extern void Cigesd2d();
121extern void Cigerv2d();
122extern void Cigsum2d();
123extern void Cigamn2d();
124extern void Cigamx2d();
125extern void Ccgesd2d();
126extern void Ccgerv2d();
127/* lapack */
128void clacpy_();
129/* aux fonctions */
131extern void *mr2d_malloc();
132extern Int ppcm();
133extern Int localsize();
136extern void paramcheck();
137/* tools and others function */
138#define scanD0 ctrscanD0
139#define dispmat ctrdispmat
140#define setmemory ctrsetmemory
141#define freememory ctrfreememory
142#define scan_intervals ctrscan_intervals
143extern void scanD0();
144extern void dispmat();
145extern void setmemory();
146extern void freememory();
147extern Int scan_intervals();
148extern void Cpctrmr2do();
149extern void Cpctrmr2d();
150/* some defines for Cpctrmr2do */
151#define SENDBUFF 0
152#define RECVBUFF 1
153#define SIZEBUFF 2
154#if 0
155#define DEBUG
156#endif
157#ifndef DEBUG
158#define NDEBUG
159#endif
160#include <stdio.h>
161#include <stdlib.h>
162#include <string.h>
163#include <ctype.h>
164#include <assert.h>
165/* initblock: intialize the local part of a matrix with random data (well,
166 * not very random) */
167static2 void
169{
170 complex *pdata;
171 Int i;
172 pdata = block;
173 for (i = 0; i < m * n; i++, pdata++) {
174 (*pdata).r = i;
175 };
176}
177/* getparam:read from a file a list of integer parameters, the end of the
178 * parameters to read is given by a NULL at the end of the args list */
179#ifdef __STDC__
180#include <stdarg.h>
181static void
182getparam(FILE * f,...)
183{
184#else
185#include <varargs.h>
186static void
187getparam(va_alist)
188va_dcl
189{
190 FILE *f;
191#endif
192 va_list ap;
193 Int i;
194 static Int nbline;
195 char *ptr, *next;
196 Int *var;
197 static char buffer[200];
198#ifdef __STDC__
199 va_start(ap, f);
200#else
201 va_start(ap);
202 f = va_arg(ap, FILE *);
203#endif
204 do {
205 next = fgets(buffer, 200, f);
206 if (next == NULL) {
207 fprintf(stderr, "bad configuration driver file:after line %d\n", nbline);
208 exit(1);
209 }
210 nbline += 1;
211 } while (buffer[0] == '#');
212 ptr = buffer;
213 var = va_arg(ap, Int *);
214 while (var != NULL) {
215 *var = strtol(ptr, &next, 10);
216 if (ptr == next) {
217 fprintf(stderr, "bad configuration driver file:error line %d\n", nbline);
218 exit(1);
219 }
220 ptr = next;
221 var = va_arg(ap, Int *);
222 }
223 va_end(ap);
224}
225void
226initforpvm(Int argc, char *argv[])
227{
228 Int pnum, nproc;
229 Cblacs_pinfo(&pnum, &nproc);
230 if (nproc < 1) { /* we are with PVM */
231 if (pnum == 0) {
232 if (argc < 2) {
233 fprintf(stderr, "usage with PVM:xctrmr nbproc\n\
234\t where nbproc is the number of nodes to initialize\n");
235 exit(1);
236 }
237 nproc = atoi(argv[1]);
238 }
239 Cblacs_setup(&pnum, &nproc);
240 }
241}
242int
243main(int argc, char *argv[])
244{
245 /* We initialize the data-block on the current processor, then redistribute
246 * it, and perform the inverse redistribution to compare the local memory
247 * with the initial one. */
248 /* Data file */
249 FILE *fp;
250 Int nbre, nbremax;
251 /* Data distribution 0 parameters */
252 Int p0, /* # of rows in the processor grid */
253 q0; /* # of columns in the processor grid */
254 /* Data distribution 1 parameters */
255 Int p1, q1;
256 /* # of parameter to be read on the keyboard */
257#define nbparameter 24
258 /* General variables */
259 Int blocksize0;
260 Int mypnum, nprocs;
261 Int parameters[nbparameter], nberrors;
262 Int i;
263 Int ia, ja, ib, jb, m, n;
264 Int gcontext, context0, context1;
265 Int myprow1, myprow0, mypcol0, mypcol1;
266 Int dummy;
267 MDESC ma, mb;
268 char *uplo, *diag;
269 complex *ptrmyblock, *ptrsavemyblock, *ptrmyblockcopy, *ptrmyblockvide;
270#ifdef UsingMpiBlacs
271 MPI_Init(&argc, &argv);
272#endif
273 setvbuf(stdout, NULL, _IOLBF, 0);
274 setvbuf(stderr, NULL, _IOLBF, 0);
275#ifdef T3D
276 free(malloc(14000000));
277#endif
278 initforpvm(argc, argv);
279 /* Read physical parameters */
280 Cblacs_pinfo(&mypnum, &nprocs);
281 /* initialize BLACS for the parameter communication */
282 Cblacs_get((Int)0, (Int)0, &gcontext);
283 Cblacs_gridinit(&gcontext, "R", nprocs, (Int)1);
284 Cblacs_gridinfo(gcontext, &dummy, &dummy, &mypnum, &dummy);
285 if (mypnum == 0) {
286 if ((fp = fopen("TRMR2D.dat", "r")) == NULL) {
287 fprintf(stderr, "Can't open TRMR2D.dat\n");
288 exit(1);
289 };
290 printf("\n// CTRMR2D TESTER for COMPLEX //\n");
291 getparam(fp, &nbre, NULL);
292 printf("////////// %d tests \n\n", nbre);
293 parameters[0] = nbre;
294 Cigebs2d(gcontext, "All", "H", (Int)1, (Int)1, parameters, (Int)1);
295 } else {
296 Cigebr2d(gcontext, "All", "H", (Int)1, (Int)1, parameters, (Int)1, (Int)0, (Int)0);
297 nbre = parameters[0];
298 };
299 if (mypnum == 0) {
300 printf("\n m n m0 n0 sr0 sc0 i0 j0 p0 q0 nbr0 nbc0 \
301m1 n1 sr1 sc1 i1 j1 p1 q1 nbr1 nbc1\n\n");
302 };
303 /****** TEST LOOP *****/
304 /* Here we are in grip 1xnprocs */
305 nbremax = nbre;
306#ifdef DEBUG
307 fprintf(stderr, "bonjour,je suis le noeud %d\n", mypnum);
308#endif
309 while (nbre-- != 0) { /* Loop on the serie of tests */
310 /* All the processors read the parameters so we have to be in a 1xnprocs
311 * grid at each iteration */
312 /* Read processors grid and matrices parameters */
313 if (mypnum == 0) {
314 Int u, d;
315 getparam(fp,
316 &m, &n,
317 &ma.m, &ma.n, &ma.sprow, &ma.spcol,
318 &ia, &ja, &p0, &q0, &ma.nbrow, &ma.nbcol,
319 &mb.m, &mb.n, &mb.sprow, &mb.spcol,
320 &ib, &jb, &p1, &q1, &mb.nbrow, &mb.nbcol,
321 &u, &d,
322 NULL);
323 uplo = u ? "UPPER" : "LOWER";
324 diag = d ? "UNIT" : "NONUNIT";
325 printf("\t\t************* TEST # %d **********\n",
326 nbremax - nbre);
327 printf(" %3d %3d %3d %3d %3d %3d %3d %3d \
328%3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d %3d",
329 m, n,
330 ma.m, ma.n, ma.sprow, ma.spcol,
331 ia, ja, p0, q0, ma.nbrow, ma.nbcol,
332 mb.m, mb.n, mb.sprow, mb.spcol,
333 ib, jb, p1, q1, mb.nbrow, mb.nbcol);
334 printf(" %s %s", toupper(*uplo) == 'U' ? "up" : "low",
335 toupper(*diag) == 'U' ? "unit" : "nonunit");
336 printf("\n");
337 if (p0 * q0 > nprocs || p1 * q1 > nprocs) {
338 fprintf(stderr, "not enough nodes:%d processors required\n",
339 max(p0 * q0, p1 * q1));
340 exit(1);
341 }
342 parameters[0] = p0;
343 parameters[1] = q0;
344 parameters[2] = ma.nbrow;
345 parameters[3] = ma.nbcol;
346 parameters[4] = p1;
347 parameters[5] = q1;
348 parameters[6] = mb.nbrow;
349 parameters[7] = mb.nbcol;
350 parameters[8] = ma.m;
351 parameters[9] = ma.n;
352 parameters[10] = ma.sprow;
353 parameters[11] = ma.spcol;
354 parameters[12] = mb.sprow;
355 parameters[13] = mb.spcol;
356 parameters[14] = ia;
357 parameters[15] = ja;
358 parameters[16] = ib;
359 parameters[17] = jb;
360 parameters[18] = m;
361 parameters[19] = n;
362 parameters[20] = mb.m;
363 parameters[21] = mb.n;
364 parameters[22] = *uplo == 'U';
365 parameters[23] = *diag == 'U';
366 Cigebs2d(gcontext, "All", "H", (Int)1, nbparameter, parameters, (Int)1);
367 } else {
368 Cigebr2d(gcontext, "All", "H", (Int)1, nbparameter, parameters, (Int)1, (Int)0, (Int)0);
369 p0 = parameters[0];
370 q0 = parameters[1];
371 ma.nbrow = parameters[2];
372 ma.nbcol = parameters[3];
373 p1 = parameters[4];
374 q1 = parameters[5];
375 mb.nbrow = parameters[6];
376 mb.nbcol = parameters[7];
377 ma.m = parameters[8];
378 ma.n = parameters[9];
379 ma.sprow = parameters[10];
380 ma.spcol = parameters[11];
381 mb.sprow = parameters[12];
382 mb.spcol = parameters[13];
383 ia = parameters[14];
384 ja = parameters[15];
385 ib = parameters[16];
386 jb = parameters[17];
387 m = parameters[18];
388 n = parameters[19];
389 mb.m = parameters[20];
390 mb.n = parameters[21];
393 uplo = parameters[22] ? "UPPER" : "LOWER";
394 diag = parameters[23] ? "UNIT" : "NONUNIT";
395 };
396 Cblacs_get((Int)0, (Int)0, &context0);
397 Cblacs_gridinit(&context0, "R", p0, q0);
398 Cblacs_get((Int)0, (Int)0, &context1);
399 Cblacs_gridinit(&context1, "R", p1, q1);
400 Cblacs_gridinfo(context0, &dummy, &dummy, &myprow0, &mypcol0);
401 if (myprow0 >= p0 || mypcol0 >= q0)
402 myprow0 = mypcol0 = -1;
403 Cblacs_gridinfo(context1, &dummy, &dummy, &myprow1, &mypcol1);
404 if (myprow1 >= p1 || mypcol1 >= q1)
405 myprow1 = mypcol1 = -1;
406 assert((myprow0 < p0 && mypcol0 < q0) || (myprow0 == -1 && mypcol0 == -1));
407 assert((myprow1 < p1 && mypcol1 < q1) || (myprow1 == -1 && mypcol1 == -1));
408 ma.ctxt = context0;
409 mb.ctxt = context1;
410 /* From here, we are not assuming that only the processors working in the
411 * redistribution are calling xxMR2D, but the ones not concerned will do
412 * nothing. */
413 /* We compute the exact size of the local memory block for the memory
414 * allocations */
415 if (myprow0 >= 0 && mypcol0 >= 0) {
416 blocksize0 = memoryblocksize(&ma);
417 ma.lda = localsize(SHIFT(myprow0, ma.sprow, p0), p0, ma.nbrow, ma.m);
418 setmemory(&ptrmyblock, blocksize0);
419 initblock(ptrmyblock, 1, blocksize0);
420 setmemory(&ptrmyblockcopy, blocksize0);
421 memcpy((char *) ptrmyblockcopy, (char *) ptrmyblock,
422 blocksize0 * sizeof(complex));
423 setmemory(&ptrmyblockvide, blocksize0);
424 for (i = 0; i < blocksize0; i++)
425 ptrmyblockvide[i].r = -1;
426 }; /* if (mypnum < p0 * q0) */
427 if (myprow1 >= 0 && mypcol1 >= 0) {
428 setmemory(&ptrsavemyblock, memoryblocksize(&mb));
429 mb.lda = localsize(SHIFT(myprow1, mb.sprow, p1), p1, mb.nbrow, mb.m);
430 }; /* if (mypnum < p1 * q1) */
431 /* Redistribute the matrix from grid 0 to grid 1 (memory location
432 * ptrmyblock to ptrsavemyblock) */
433 Cpctrmr2d(uplo, diag, m, n,
434 ptrmyblock, ia, ja, &ma,
435 ptrsavemyblock, ib, jb, &mb, gcontext);
436 /* Perform the inverse redistribution of the matrix from grid 1 to grid 0
437 * (memory location ptrsavemyblock to ptrmyblockvide) */
438 Cpctrmr2d(uplo, diag, m, n,
439 ptrsavemyblock, ib, jb, &mb,
440 ptrmyblockvide, ia, ja, &ma, gcontext);
441 /* Check the differences */
442 nberrors = 0;
443 if (myprow0 >= 0 && mypcol0 >= 0) {
444 /* only for the processors that do have data at the begining */
445 for (i = 0; i < blocksize0; i++) {
446 Int li, lj, gi, gj;
447 Int in;
448 in = 1;
449 li = i % ma.lda;
450 lj = i / ma.lda;
451 gi = (li / ma.nbrow) * p0 * ma.nbrow +
452 SHIFT(myprow0, ma.sprow, p0) * ma.nbrow + li % ma.nbrow;
453 gj = (lj / ma.nbcol) * q0 * ma.nbcol +
454 SHIFT(mypcol0, ma.spcol, q0) * ma.nbcol + lj % ma.nbcol;
455 assert(gi < ma.m && gj < ma.n);
456 gi -= (ia - 1);
457 gj -= (ja - 1);
458 if (gi < 0 || gj < 0 || gi >= m || gj >= n)
459 in = 0;
460 else if (toupper(*uplo) == 'U')
461 in = (gi <= gj + max(0, m - n) - (toupper(*diag) == 'U'));
462 else
463 in = (gi >= gj - max(0, n - m) + (toupper(*diag) == 'U'));
464 if (!in) {
465 ptrmyblockcopy[i].r = -1;
466 }
467 if (ptrmyblockvide[i].r != ptrmyblockcopy[i].r) {
468 nberrors++;
469 printf("Proc %d : Error element number %d, value = %f , initvalue =%f \n"
470 ,mypnum, i,
471 ptrmyblockvide[i].r, ptrmyblockcopy[i].r);
472 };
473 };
474 if (nberrors > 0) {
475 printf("Processor %d, has tested %d COMPLEX elements,\
476Number of redistribution errors = %d \n",
477 mypnum, blocksize0, nberrors);
478 }
479 }
480 /* Look at the errors on all the processors at this point. */
481 Cigsum2d(gcontext, "All", "H", (Int)1, (Int)1, &nberrors, (Int)1, (Int)0, (Int)0);
482 if (mypnum == 0)
483 if (nberrors)
484 printf(" => Total number of redistribution errors = %d \n",
485 nberrors);
486 else
487 printf("TEST PASSED OK\n");
488 /* release memory for the next iteration */
489 if (myprow0 >= 0 && mypcol0 >= 0) {
490 freememory((char *) ptrmyblock);
491 freememory((char *) ptrmyblockvide);
492 freememory((char *) ptrmyblockcopy);
493 }; /* if (mypnum < p0 * q0) */
494 /* release memory for the next iteration */
495 if (myprow1 >= 0 && mypcol1 >= 0) {
496 freememory((char *) ptrsavemyblock);
497 };
498 if (myprow0 >= 0)
499 Cblacs_gridexit(context0);
500 if (myprow1 >= 0)
501 Cblacs_gridexit(context1);
502 }; /* while nbre != 0 */
503 if (mypnum == 0) {
504 fclose(fp);
505 };
506 Cblacs_exit((Int)0);
507 return 0;
508}/* main */
#define Int
Definition Bconfig.h:22
Int memoryblocksize()
static2 void initblock(complex *block, Int m, Int n)
Definition pctrmrdrv.c:168
Int changeorigin()
#define freememory
Definition pctrmrdrv.c:141
void Ccgesd2d()
#define scan_intervals
Definition pctrmrdrv.c:142
#define SHIFT(row, sprow, nbrow)
Definition pctrmrdrv.c:97
void Cblacs_gridexit()
#define max(A, B)
Definition pctrmrdrv.c:98
void Cigsum2d()
#define static2
Definition pctrmrdrv.c:61
#define scanD0
Definition pctrmrdrv.c:138
Int Cblacs_pnum()
void Cpctrmr2d()
#define Clacpy
Definition pctrmrdrv.c:76
void Cigamx2d()
void Cblacs_pinfo()
void Cpctrmr2do()
void Cblacs_pcoord()
void Cigamn2d()
Int localsize()
#define setmemory
Definition pctrmrdrv.c:140
void Cblacs_get()
void Ccgerv2d()
void Cigerv2d()
void Cigebs2d()
#define dispmat
Definition pctrmrdrv.c:139
void paramcheck()
void Cblacs_setup()
#define BLOCK_CYCLIC_2D
Definition pctrmrdrv.c:92
void Cblacs_gridinit()
void Cblacs_gridinfo()
void Csetpvmtids()
Int localindice()
void Cigesd2d()
void initforpvm(Int argc, char *argv[])
Definition pctrmrdrv.c:226
Int ppcm()
void Cigebr2d()
#define clacpy_
Definition pctrmrdrv.c:74
void Cblacs_exit()
void * mr2d_malloc()
#define nbparameter
main()
Definition size.c:2
Int m
Definition pcgemr.c:166
Int spcol
Definition pcgemr.c:171
Int nbcol
Definition pcgemr.c:169
Int sprow
Definition pcgemr.c:170
Int nbrow
Definition pcgemr.c:168
Int ctxt
Definition pcgemr.c:165
Int desctype
Definition pcgemr.c:164
Int n
Definition pcgemr.c:167
Int lda
Definition pcgemr.c:172
float r
Definition pcgemr.c:161