1 /* Copyright 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       : test_scotch_dgraph_redist.c             **/
35 /**                                                        **/
36 /**   AUTHOR     : Francois PELLEGRINI                     **/
37 /**                                                        **/
38 /**   FUNCTION   : This module tests the operation of      **/
39 /**                the SCOTCH_dgraphBand() routine.        **/
40 /**                                                        **/
41 /**   DATES      : # Version 6.0  : from : 21 feb 2012     **/
42 /**                                 to     02 mar 2015     **/
43 /**                                                        **/
44 /************************************************************/
45 
46 /*
47 **  The defines and includes.
48 */
49 
50 #include <mpi.h>
51 #include <stdio.h>
52 #if (((defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined HAVE_STDINT_H))
53 #include <stdint.h>
54 #endif /* (((defined __STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)) || (defined HAVE_STDINT_H)) */
55 #include <stdlib.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <pthread.h>
59 #include <unistd.h>
60 
61 #include "ptscotch.h"
62 
63 #define errorProg                   SCOTCH_errorProg
64 #define errorPrint                  SCOTCH_errorPrint
65 
66 /*********************/
67 /*                   */
68 /* The main routine. */
69 /*                   */
70 /*********************/
71 
72 int
main(int argc,char * argv[])73 main (
74 
75 int                 argc,
76 char *              argv[])
77 {
78   MPI_Comm              proccomm;
79   int                   procglbnbr;               /* Number of processes sharing graph data */
80   int                   proclocnum;               /* Number of this process                 */
81   SCOTCH_Num            vertglbnbr;
82   SCOTCH_Num            vertlocnbr;
83   SCOTCH_Num            vertlocnum;
84   SCOTCH_Num *          partloctab;
85   SCOTCH_Num            baseval;
86   SCOTCH_Dgraph         srcgrafdat;
87   SCOTCH_Dgraph         dstgrafdat;
88   FILE *                file;
89   int                   procnum;
90 #ifdef SCOTCH_PTHREAD
91   int                 thrdlvlreqval;
92   int                 thrdlvlproval;
93 #endif /* SCOTCH_PTHREAD */
94 
95   errorProg (argv[0]);
96 
97 #ifdef SCOTCH_PTHREAD
98   thrdlvlreqval = MPI_THREAD_MULTIPLE;
99   if (MPI_Init_thread (&argc, &argv, thrdlvlreqval, &thrdlvlproval) != MPI_SUCCESS)
100     errorPrint ("main: Cannot initialize (1)");
101   if (thrdlvlreqval > thrdlvlproval)
102     errorPrint ("main: MPI implementation is not thread-safe: recompile without SCOTCH_PTHREAD");
103 #else /* SCOTCH_PTHREAD */
104   if (MPI_Init (&argc, &argv) != MPI_SUCCESS)
105     errorPrint ("main: Cannot initialize (2)");
106 #endif /* SCOTCH_PTHREAD */
107 
108   if (argc != 2) {
109     errorPrint ("main: invalid number of parameters");
110     exit       (1);
111   }
112 
113   proccomm = MPI_COMM_WORLD;
114   MPI_Comm_size (proccomm, &procglbnbr);          /* Get communicator data */
115   MPI_Comm_rank (proccomm, &proclocnum);
116 
117   fprintf (stderr, "Proc %2d of %2d, pid %d\n", proclocnum, procglbnbr, getpid ());
118 
119 #ifdef SCOTCH_CHECK_NOAUTO
120   if (proclocnum == 0) {                          /* Synchronize on keybord input */
121     char           c;
122 
123     printf ("Waiting for key press...\n");
124     scanf ("%c", &c);
125   }
126 #endif /* SCOTCH_CHECK_NOAUTO */
127 
128   if (MPI_Barrier (proccomm) != MPI_SUCCESS) {    /* Synchronize for debug */
129     errorPrint ("main: cannot communicate");
130     return     (1);
131   }
132 
133   if (SCOTCH_dgraphInit (&srcgrafdat, proccomm) != 0) { /* Initialize source graph */
134     errorPrint ("main: cannot initialize source graph");
135     return     (1);
136   }
137   if (SCOTCH_dgraphInit (&dstgrafdat, proccomm) != 0) { /* Initialize destination graph */
138     errorPrint ("main: cannot initialize destination graph");
139     return     (1);
140   }
141 
142   file = NULL;
143   if ((proclocnum == 0) &&
144       ((file = fopen (argv[1], "r")) == NULL)) {
145     errorPrint ("main: cannot open graph file");
146     return     (1);
147   }
148 
149   if (SCOTCH_dgraphLoad (&srcgrafdat, file, -1, 0) != 0) {
150     errorPrint ("main: cannot load source graph");
151     return     (1);
152   }
153 
154   if (file != NULL)
155     fclose (file);
156 
157   if (SCOTCH_dgraphCheck (&srcgrafdat) != 0) {
158     errorPrint ("main: invalid source graph");
159     return     (1);
160   }
161 
162   if (MPI_Barrier (proccomm) != MPI_SUCCESS) {    /* Synchronize for debug */
163     errorPrint ("main: cannot communicate");
164     return     (1);
165   }
166 
167   SCOTCH_dgraphData (&srcgrafdat, NULL, &vertglbnbr, &vertlocnbr, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
168 
169   if ((partloctab = malloc (vertlocnbr * sizeof (SCOTCH_Num))) == NULL) {
170     errorPrint ("main: cannot allocate frontier array");
171     return     (1);
172   }
173 
174   for (vertlocnum = 0; vertlocnum < vertlocnbr; vertlocnum ++) /* Create packs of 3 vertices each */
175     partloctab[vertlocnum] = (vertlocnum / 3) % procglbnbr;
176 
177   if (SCOTCH_dgraphRedist (&srcgrafdat, partloctab, NULL, -1, -1, &dstgrafdat) != 0) {
178     errorPrint ("main: cannot compute redistributed graph");
179     return     (1);
180   }
181 
182   SCOTCH_dgraphExit (&dstgrafdat);
183   SCOTCH_dgraphExit (&srcgrafdat);
184   free (partloctab);
185 
186   MPI_Finalize ();
187   exit         (0);
188 }
189