1 /* Copyright 2004,2007,2008,2010-2012,2014 IPB, Universite de Bordeaux, INRIA & CNRS
2 **
3 ** This file is part of the Scotch software package for static mapping,
4 ** graph partitioning and sparse matrix ordering.
5 **
6 ** This software is governed by the CeCILL-C license under French law
7 ** and abiding by the rules of distribution of free software. You can
8 ** use, modify and/or redistribute the software under the terms of the
9 ** CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
10 ** URL: "http://www.cecill.info".
11 **
12 ** As a counterpart to the access to the source code and rights to copy,
13 ** modify and redistribute granted by the license, users are provided
14 ** only with a limited warranty and the software's author, the holder of
15 ** the economic rights, and the successive licensors have only limited
16 ** liability.
17 **
18 ** In this respect, the user's attention is drawn to the risks associated
19 ** with loading, using, modifying and/or developing or reproducing the
20 ** software by the user in light of its specific status of free software,
21 ** that may mean that it is complicated to manipulate, and that also
22 ** therefore means that it is reserved for developers and experienced
23 ** professionals having in-depth computer knowledge. Users are therefore
24 ** encouraged to load and test the software's suitability as regards
25 ** their requirements in conditions enabling the security of their
26 ** systems and/or data to be ensured and, more generally, to use and
27 ** operate it in the same conditions as regards security.
28 **
29 ** The fact that you are presently reading this means that you have had
30 ** knowledge of the CeCILL-C license and that you accept its terms.
31 */
32 /************************************************************/
33 /**                                                        **/
34 /**   NAME       : gmk_m2.c                                **/
35 /**                                                        **/
36 /**   AUTHOR     : Francois PELLEGRINI                     **/
37 /**                                                        **/
38 /**   FUNCTION   : Creates the source graph for 2D mesh    **/
39 /**                graphs.                                 **/
40 /**                                                        **/
41 /**   DATES      : # Version 2.0  : from : 30 oct 1994     **/
42 /**                                 to     08 nov 1994     **/
43 /**                # Version 3.0  : from : 11 jul 1995     **/
44 /**                                 to     02 oct 1995     **/
45 /**                # Version 3.2  : from : 03 jun 1997     **/
46 /**                                 to   : 03 jun 1997     **/
47 /**                # Version 3.3  : from : 06 oct 1998     **/
48 /**                                 to   : 06 oct 1998     **/
49 /**                # Version 3.4  : from : 03 feb 2000     **/
50 /**                                 to   : 18 may 2004     **/
51 /**                # Version 5.0  : from : 13 dec 2007     **/
52 /**                                 to   : 16 mar 2008     **/
53 /**                # Version 5.1  : from : 01 jul 2010     **/
54 /**                                 to   : 14 feb 2011     **/
55 /**                # Version 6.0  : from : 01 jan 2012     **/
56 /**                                 to   : 12 nov 2014     **/
57 /**                                                        **/
58 /**   NOTES      : # The vertices of the (dX,dY) mesh are  **/
59 /**                  numbered as terminals so that         **/
60 /**                  t(0,0) = 0, t(1,0) = 1,               **/
61 /**                  t(dX - 1, 0) = dX - 1, t(0,1) = dX,   **/
62 /**                  and t(x,y) = (y * dX) + x.            **/
63 /**                                                        **/
64 /************************************************************/
65 
66 /*
67 **  The defines and includes.
68 */
69 
70 #define GMK_M2
71 
72 #include "module.h"
73 #include "common.h"
74 #include "scotch.h"
75 #include "gmk_m2.h"
76 
77 /*
78 **  The static definitions.
79 */
80 
81 static int                  C_paraNum = 0;        /* Number of parameters       */
82 static int                  C_fileNum = 0;        /* Number of file in arg list */
83 static File                 C_fileTab[C_FILENBR] = { /* The file array          */
84                               { "w" },
85                               { "w" } };
86 
87 static const char *         C_usageList[] = {
88   "gmk_m2 <dimX> [<dimY> [<output source file>]] <options>",
89   "  -b<val>   : Set base value for output (0 or 1)",
90   "  -e        : Build a 8-neighbor grid rather than a 4-neighbor one",
91   "  -g<file>  : Output the geometry to <file>",
92   "  -h        : Display this help",
93   "  -t        : Build a torus rather than a mesh",
94   "  -V        : Print program version and copyright",
95   NULL };
96 
97 /****************************************/
98 /*                                      */
99 /* The main routine, which computes the */
100 /* source graph description.            */
101 /*                                      */
102 /****************************************/
103 
104 int
main(int argc,char * argv[])105 main (
106 int                         argc,
107 char *                      argv[])
108 {
109   int                 flagval;                    /* Process flags      */
110   SCOTCH_Num          baseval;                    /* Base value         */
111   SCOTCH_Num          d[2] = { 1, 1 };            /* Mesh dimensions    */
112   SCOTCH_Num          c[2];                       /* Vertex coordinates */
113   int                 i;
114 
115   errorProg ("gmk_m2");
116 
117   flagval = C_FLAGDEFAULT;                        /* Set default flags */
118   baseval = 0;
119 
120   if ((argc >= 2) && (argv[1][0] == '?')) {       /* If need for help */
121     usagePrint (stdout, C_usageList);
122     return     (0);
123   }
124 
125   fileBlockInit (C_fileTab, C_FILENBR);           /* Set default stream pointers */
126 
127   for (i = 1; i < argc; i ++) {                   /* Loop for all option codes                        */
128     if ((argv[i][0] != '-') || (argv[i][1] == '\0') || (argv[i][1] == '.')) { /* If found a file name */
129       if (C_paraNum < 2) {                        /* If number of parameters not reached              */
130         if ((d[C_paraNum ++] = atoi (argv[i])) < 1) { /* Get the dimension                            */
131           errorPrint ("main: invalid dimension '%s'", argv[i]);
132           return     (1);
133         }
134         continue;                                 /* Process the other parameters */
135       }
136       if (C_fileNum < C_FILEARGNBR)               /* A file name has been given */
137         fileBlockName (C_fileTab, C_fileNum ++) = argv[i];
138       else {
139         errorPrint ("main: too many file names given");
140         return     (1);
141       }
142     }
143     else {                                        /* If found an option name */
144       switch (argv[i][1]) {
145         case 'B' :                                /* Set base value */
146         case 'b' :
147           baseval = (SCOTCH_Num) atol (&argv[i][2]);
148           if ((baseval < 0) || (baseval > 1)) {
149             errorPrint ("main: invalid base value '" SCOTCH_NUMSTRING "'", (SCOTCH_Num) baseval);
150           }
151           break;
152         case 'E' :                                /* Build a finite-element grid */
153         case 'e' :
154           flagval |= C_FLAGELEM;
155           break;
156         case 'G' :                                /* Output the geometry */
157         case 'g' :
158           flagval |= C_FLAGGEOOUT;
159           if (argv[i][2] != '\0')
160             C_filenamegeoout = &argv[i][2];
161           break;
162         case 'H' :                                /* Give the usage message */
163         case 'h' :
164           usagePrint (stdout, C_usageList);
165           return     (0);
166         case 'T' :                                /* Build a torus */
167         case 't' :
168           flagval |= C_FLAGTORUS;
169           break;
170         case 'V' :
171           fprintf (stderr, "gmk_m2, version " SCOTCH_VERSION_STRING "\n");
172           fprintf (stderr, "Copyright 2004,2007,2008,2010-2012,2014 IPB, Universite de Bordeaux, INRIA & CNRS, France\n");
173           fprintf (stderr, "This software is libre/free software under CeCILL-C -- see the user's manual for more information\n");
174           return  (0);
175         default :
176           errorPrint ("main: unprocessed option '%s'", argv[i]);
177           return     (1);
178       }
179     }
180   }
181 
182   fileBlockOpen (C_fileTab, C_FILENBR);           /* Open all files */
183 
184   if (flagval & C_FLAGELEM) {                     /* Build a 8-neighbor grid */
185     errorPrint ("main: elements not supported");
186     return     (1);
187   }
188 
189   if (flagval & C_FLAGTORUS) {                    /* Build a torus */
190     fprintf (C_filepntrsrcout, "0\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t000\n",
191              (SCOTCH_Num) (d[0] * d[1]),          /* Print number of vertices              */
192              (SCOTCH_Num) ((4 * d[0] * d[1])             - /* Print number of edges (arcs) */
193                            ((d[0] < 3) ? (2 * d[1]) : 0) -
194                            ((d[1] < 3) ? (2 * d[0]) : 0)),
195              (SCOTCH_Num) baseval);
196 
197     for (c[1] = 0; c[1] < d[1]; c[1] ++) {        /* Output neighbor list */
198       for (c[0] = 0; c[0] < d[0]; c[0] ++) {
199         fprintf (C_filepntrsrcout, SCOTCH_NUMSTRING,
200                  (SCOTCH_Num) (((d[0] > 2) ? 3 : d[0]) + /* Output number of neighbors */
201                                ((d[1] > 2) ? 3 : d[1]) - 2));
202         if (d[1] > 2)
203           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING, /* Output the neighbors */
204                    (SCOTCH_Num) (((c[1] + d[1] - 1) % d[1]) * d[0] + c[0] + baseval));
205         if (d[0] > 2)
206           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
207                    (SCOTCH_Num) ((c[1] * d[0] + (c[0] + d[0] - 1) % d[0]) + baseval));
208         if (d[0] > 1)
209           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
210                    (SCOTCH_Num) (c[1] * d[0] + ((c[0] + 1) % d[0]) + baseval));
211         if (d[1] > 1)
212           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
213                    (SCOTCH_Num) (((c[1] + 1) % d[1]) * d[0] + c[0] + baseval));
214         fprintf (C_filepntrsrcout, "\n");
215       }
216     }
217   }
218   else {                                          /* Build a mesh */
219     fprintf (C_filepntrsrcout, "0\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t000\n",
220              (SCOTCH_Num) (d[0] * d[1]),
221              (SCOTCH_Num) ((d[0] * d[1] * 2 - (d[0] + d[1])) * 2),
222              (SCOTCH_Num) baseval);
223 
224     for (c[1] = 0; c[1] < d[1]; c[1] ++) {        /* Output neighbor list */
225       for (c[0] = 0; c[0] < d[0]; c[0] ++) {
226         fprintf (C_filepntrsrcout, "%d",
227                  ((c[0] == 0)          ? 0 : 1) + /* Output number of neighbors */
228                  ((c[0] == (d[0] - 1)) ? 0 : 1) +
229                  ((c[1] == 0)          ? 0 : 1) +
230                  ((c[1] == (d[1] - 1)) ? 0 : 1));
231         if (c[1] != 0)                            /* Output the neighbors */
232           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
233                    (SCOTCH_Num) ((c[1] - 1) * d[0] + c[0] + baseval));
234         if (c[0] != 0)
235           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
236                    (SCOTCH_Num) (c[1] * d[0] + (c[0] - 1) + baseval));
237         if (c[0] != (d[0] - 1))
238           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
239                    (SCOTCH_Num) (c[1] * d[0] + (c[0] + 1) + baseval));
240         if (c[1] != (d[1] - 1))
241           fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
242                    (SCOTCH_Num) ((c[1] + 1) * d[0] + c[0] + baseval));
243         fprintf (C_filepntrsrcout, "\n");
244       }
245     }
246   }
247 
248   if (flagval & C_FLAGGEOOUT) {                   /* If geometry is wanted                */
249    fprintf (C_filepntrgeoout, "2\n" SCOTCH_NUMSTRING "\n", /* Output geometry file header */
250             (SCOTCH_Num) (d[0] * d[1]));
251 
252     for (c[1] = 0; c[1] < d[1]; c[1] ++) {        /* Output mesh coordinates */
253       for (c[0] = 0; c[0] < d[0]; c[0] ++)
254         fprintf (C_filepntrgeoout, SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n",
255                  (SCOTCH_Num) (c[1] * d[0] + c[0] + baseval),
256                  (SCOTCH_Num) c[0],
257                  (SCOTCH_Num) (d[1] - 1 - c[1]));
258     }
259   }
260 
261   fileBlockClose (C_fileTab, C_FILENBR);          /* Always close explicitely to end eventual (un)compression tasks */
262 
263 #ifdef COMMON_PTHREAD
264   pthread_exit ((void *) 0);                      /* Allow potential (un)compression tasks to complete */
265 #endif /* COMMON_PTHREAD */
266   return (0);
267 }
268