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_m3.c                                **/
35 /**                                                        **/
36 /**   AUTHOR     : Francois PELLEGRINI                     **/
37 /**                                                        **/
38 /**   FUNCTION   : Creates the source graph for            **/
39 /**                tridimensional mesh source graphs.      **/
40 /**                                                        **/
41 /**   DATES      : # Version 4.0  : from : 11 feb 2002     **/
42 /**                                 to   : 18 may 2004     **/
43 /**                # Version 5.0  : from : 13 dec 2007     **/
44 /**                                 to   : 16 mar 2008     **/
45 /**                # Version 5.1  : from : 01 jul 2010     **/
46 /**                                 to   : 14 feb 2011     **/
47 /**                # Version 6.0  : from : 01 jan 2012     **/
48 /**                                 to   : 12 nov 2014     **/
49 /**                                                        **/
50 /**   NOTES      : # The vertices of the (dX,dY,dZ) mesh   **/
51 /**                  are numbered as terminals so that     **/
52 /**                  t(0,0,0) = 0, t(1,0,0) = 1,           **/
53 /**                  t(dX - 1, 0, 0) = dX - 1, t(0,1,0) =  **/
54 /**                  dX, t (0, 0, 1) = dX * dY - 1,        **/
55 /**                  and t(x,y,z) = (z * dY + y) * dX + x. **/
56 /**                                                        **/
57 /************************************************************/
58 
59 /*
60 **  The defines and includes.
61 */
62 
63 #define GMK_M3
64 
65 #include "module.h"
66 #include "common.h"
67 #include "scotch.h"
68 #include "gmk_m3.h"
69 
70 /*
71 **  The static definitions.
72 */
73 
74 static int                  C_paraNum = 0;        /* Number of parameters       */
75 static int                  C_fileNum = 0;        /* Number of file in arg list */
76 static File                 C_fileTab[C_FILENBR] = { /* The file array          */
77                               { "w" },
78                               { "w" } };
79 
80 static const char *         C_usageList[] = {
81   "gmk_m3 <dimX> [<dimY> [<dimZ> [<output source file>]]] <options>",
82   "  -b<val>   : Set base value for output (0 or 1)",
83   "  -g<file>  : Output the geometry to <file>",
84   "  -h        : Display this help",
85   "  -t        : Build a torus rather than a mesh",
86   "  -V        : Print program version and copyright",
87   NULL };
88 
89 /****************************************/
90 /*                                      */
91 /* The main routine, which computes the */
92 /* source graph description.            */
93 /*                                      */
94 /****************************************/
95 
96 int
main(int argc,char * argv[])97 main (
98 int                         argc,
99 char *                      argv[])
100 {
101   int                 flagval;                    /* Process flags      */
102   SCOTCH_Num          baseval;                    /* Base value         */
103   SCOTCH_Num          d[3] = { 1, 1, 1 };         /* Mesh dimensions    */
104   SCOTCH_Num          c[3];                       /* Vertex coordinates */
105   int                 i;
106 
107   errorProg ("gmk_m3");
108 
109   flagval = C_FLAGDEFAULT;                        /* Set default flags */
110   baseval = 0;
111 
112   if ((argc >= 2) && (argv[1][0] == '?')) {       /* If need for help */
113     usagePrint (stdout, C_usageList);
114     return     (0);
115   }
116 
117   fileBlockInit (C_fileTab, C_FILENBR);           /* Set default stream pointers */
118 
119   for (i = 1; i < argc; i ++) {                   /* Loop for all option codes                        */
120     if ((argv[i][0] != '-') || (argv[i][1] == '\0') || (argv[i][1] == '.')) { /* If found a file name */
121       if (C_paraNum < 3) {                        /* If number of parameters not reached              */
122         if ((d[C_paraNum ++] = atoi (argv[i])) < 1) { /* Get the dimension                            */
123           errorPrint ("main: invalid dimension '%s'", argv[i]);
124           return     (1);
125         }
126         continue;                                 /* Process the other parameters */
127       }
128       if (C_fileNum < C_FILEARGNBR)               /* A file name has been given */
129         fileBlockName (C_fileTab, C_fileNum ++) = argv[i];
130       else {
131         errorPrint ("main: too many file names given");
132         return     (1);
133       }
134     }
135     else {                                        /* If found an option name */
136       switch (argv[i][1]) {
137         case 'B' :                                /* Set base value */
138         case 'b' :
139           baseval = (SCOTCH_Num) atol (&argv[i][2]);
140           if ((baseval < 0) || (baseval > 1)) {
141             errorPrint ("main: invalid base value '" SCOTCH_NUMSTRING "'", (SCOTCH_Num) baseval);
142           }
143           break;
144         case 'G' :                                /* Output the geometry */
145         case 'g' :
146           flagval |= C_FLAGGEOOUT;
147           if (argv[i][2] != '\0')
148             C_filenamegeoout = &argv[i][2];
149           break;
150         case 'H' :                                /* Give the usage message */
151         case 'h' :
152           usagePrint (stdout, C_usageList);
153           return     (0);
154         case 'T' :                                /* Build a torus */
155         case 't' :
156           flagval |= C_FLAGTORUS;
157           break;
158         case 'V' :
159           fprintf (stderr, "gmk_m3, version " SCOTCH_VERSION_STRING "\n");
160           fprintf (stderr, "Copyright 2004,2007,2008,2010-2012,2014 IPB, Universite de Bordeaux, INRIA & CNRS, France\n");
161           fprintf (stderr, "This software is libre/free software under CeCILL-C -- see the user's manual for more information\n");
162           return  (0);
163         default :
164           errorPrint ("main: unprocessed option '%s'", argv[i]);
165           return     (1);
166       }
167     }
168   }
169 
170   fileBlockOpen (C_fileTab, C_FILENBR);           /* Open all files */
171 
172   if (flagval & C_FLAGTORUS) {                    /* Build a torus */
173     fprintf (C_filepntrsrcout, "0\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t000\n",
174              (SCOTCH_Num) (d[0] * d[1] * d[2]),   /* Print number of vertices              */
175              (SCOTCH_Num) ((6 * d[0] * d[1] * d[2])      - /* Print number of edges (arcs) */
176                            ((d[0] < 3) ? (2 * d[1] * d[2]) : 0) -
177                            ((d[1] < 3) ? (2 * d[0] * d[2]) : 0) -
178                            ((d[2] < 3) ? (2 * d[0] * d[1]) : 0)),
179              (SCOTCH_Num) baseval);
180 
181     for (c[2] = 0; c[2] < d[2]; c[2] ++) {        /* Output neighbor list */
182       for (c[1] = 0; c[1] < d[1]; c[1] ++) {
183         for (c[0] = 0; c[0] < d[0]; c[0] ++) {
184           fprintf (C_filepntrsrcout, SCOTCH_NUMSTRING,
185                    (SCOTCH_Num) (((d[0] > 2) ? 3 : d[0]) + /* Output number of neighbors */
186                                  ((d[1] > 2) ? 3 : d[1]) +
187                                  ((d[2] > 2) ? 3 : d[2]) - 3));
188           if (d[2] > 2)
189             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING, /* Output the neighbors */
190                      (SCOTCH_Num) ((((c[2] + d[2] - 1) % d[2]) * d[1] + c[1]) * d[0] + c[0] + baseval));
191           if (d[1] > 2)
192             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
193                      (SCOTCH_Num) ((c[2] * d[1] + ((c[1] + d[1] - 1) % d[1])) * d[0] + c[0] + baseval));
194           if (d[0] > 2)
195             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
196                      (SCOTCH_Num) (((c[2] * d[1] + c[1]) * d[0] + (c[0] + d[0] - 1) % d[0])) + baseval);
197           if (d[0] > 1)
198             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
199                      (SCOTCH_Num) ((c[2] * d[1] + c[1]) * d[0] + ((c[0] + 1) % d[0]) + baseval));
200           if (d[1] > 1)
201             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
202                      (SCOTCH_Num) ((c[2] * d[1] + ((c[1] + 1) % d[1])) * d[0] + c[0] + baseval));
203           if (d[2] > 1)
204             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
205                      (SCOTCH_Num) ((((c[2] + 1) % d[2]) * d[1] + c[1]) * d[0] + c[0] + baseval));
206           fprintf (C_filepntrsrcout, "\n");
207         }
208       }
209     }
210   }
211   else {                                          /* Build a mesh */
212     fprintf (C_filepntrsrcout, "0\n" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n" SCOTCH_NUMSTRING "\t000\n",
213              (SCOTCH_Num) (d[0] * d[1] * d[2]),
214              (SCOTCH_Num) ((d[0] * d[1] * d[2] * 3 - (d[0] * d[1] + d[0] * d[2] + d[1] * d[2])) * 2),
215              (SCOTCH_Num) baseval);
216 
217     for (c[2] = 0; c[2] < d[2]; c[2] ++) {        /* Output neighbor list */
218       for (c[1] = 0; c[1] < d[1]; c[1] ++) {
219         for (c[0] = 0; c[0] < d[0]; c[0] ++) {
220           fprintf (C_filepntrsrcout, "%d",
221                    ((c[0] == 0)          ? 0 : 1) + /* Output number of neighbors */
222                    ((c[0] == (d[0] - 1)) ? 0 : 1) +
223                    ((c[1] == 0)          ? 0 : 1) +
224                    ((c[1] == (d[1] - 1)) ? 0 : 1) +
225                    ((c[2] == 0)          ? 0 : 1) +
226                    ((c[2] == (d[2] - 1)) ? 0 : 1));
227           if (c[2] != 0)                          /* Output the neighbors */
228             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
229                      (SCOTCH_Num) (((c[2] - 1) * d[1] + c[1]) * d[0] + c[0] + baseval));
230           if (c[1] != 0)
231             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
232                      (SCOTCH_Num) ((c[2] * d[1] + c[1] - 1) * d[0] + c[0] + baseval));
233           if (c[0] != 0)
234             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
235                      (SCOTCH_Num) ((c[2] * d[1] + c[1]) * d[0] + c[0] - 1 + baseval));
236           if (c[0] != (d[0] - 1))
237             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
238                      (SCOTCH_Num) ((c[2] * d[1] + c[1]) * d[0] + c[0] + 1 + baseval));
239           if (c[1] != (d[1] - 1))
240             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
241                      (SCOTCH_Num) ((c[2] * d[1] + c[1] + 1) * d[0] + c[0] + baseval));
242           if (c[2] != (d[2] - 1))
243             fprintf (C_filepntrsrcout, "\t" SCOTCH_NUMSTRING,
244                      (SCOTCH_Num) (((c[2] + 1) * d[1] + c[1]) * d[0] + c[0] + baseval));
245           fprintf (C_filepntrsrcout, "\n");
246         }
247       }
248     }
249   }
250 
251   if (flagval & C_FLAGGEOOUT) {                   /* If geometry is wanted                */
252    fprintf (C_filepntrgeoout, "3\n" SCOTCH_NUMSTRING "\n", /* Output geometry file header */
253             (SCOTCH_Num) (d[0] * d[1] * d[2]));
254 
255     for (c[2] = 0; c[2] < d[2]; c[2] ++) {        /* Output mesh coordinates */
256       for (c[1] = 0; c[1] < d[1]; c[1] ++) {
257         for (c[0] = 0; c[0] < d[0]; c[0] ++)
258           fprintf (C_filepntrgeoout, SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\t" SCOTCH_NUMSTRING "\n",
259                    (SCOTCH_Num) ((c[2] * d[1] + c[1]) * d[0] + c[0] + baseval),
260                    (SCOTCH_Num) c[0],
261                    (SCOTCH_Num) (d[1] - 1 - c[1]),
262                    (SCOTCH_Num) c[2]);
263       }
264     }
265   }
266 
267   fileBlockClose (C_fileTab, C_FILENBR);          /* Always close explicitely to end eventual (un)compression tasks */
268 
269 #ifdef COMMON_PTHREAD
270   pthread_exit ((void *) 0);                      /* Allow potential (un)compression tasks to complete */
271 #endif /* COMMON_PTHREAD */
272   return (0);
273 }
274