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