Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
gaussSeidel2D.c
Go to the documentation of this file.
1 /*
2 * gaussSeidel2D.c
3 * Hitmap example
4 * Stencil code: Gauss-Seidel 2D for the heat equation.
5 *
6 * v1.1
7 * (c) 2013-2015, Arturo Gonzalez-Escribano
8 */
9 
10 /*
11  * <license>
12  *
13  * Hitmap v1.2
14  *
15  * This software is provided to enhance knowledge and encourage progress in the scientific
16  * community. It should be used only for research and educational purposes. Any reproduction
17  * or use for commercial purpose, public redistribution, in source or binary forms, with or
18  * without modifications, is NOT ALLOWED without the previous authorization of the copyright
19  * holder. The origin of this software must not be misrepresented; you must not claim that you
20  * wrote the original software. If you use this software for any purpose (e.g. publication),
21  * a reference to the software package and the authors must be included.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26  * THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Copyright (c) 2007-2015, Trasgo Group, Universidad de Valladolid.
34  * All rights reserved.
35  *
36  * More information on http://trasgo.infor.uva.es/
37  *
38  * </license>
39 */
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <hitmap.h>
44 
45 hit_tileNewType( double );
46 
50 
51 /* A. SEQUENTIAL: CELL UPDATE */
52 static inline void updateCell( double *myself, double up, double down, double left, double right ) {
53  *myself = ( up + down + left + right ) / 4;
54 }
55 
56 
57 /* B. INITIALIZE MATRIX */
58 void initMatrix( HitTile_double tileMat ) {
59 
60  /* 0. OPTIONAL COMPILATION: READING THE INPUT MATRIX FROM A FILE */
61 #ifdef READ_INPUT
62 #ifdef READ_BIN
63  hit_tileFileRead( &tileMat, "Matrix.in", HIT_FILE_ARRAY );
64 #else
65  hit_tileTextFileRead( &realTileA, "Matrix.in.dtxt", HIT_FILE_ARRAY, HIT_FILE_DOUBLE, 14, 4 );
66 #endif
67 #else
68  /* 1. INIT Mat = 0 */
69  double zero=0;
70  hit_tileFill( &tileMat, &zero );
71 
72  /* 2. INIT BORDERS UP(i)=1, DOWN(i)=2, LEFT(i)=3, RIGHT(i)=4 */
73  HitTile root = *hit_tileRoot( &tileMat );
74  int i,j;
75 
76  /* 2.1. FIRST COLUMN IS MINE */
77  if ( hit_sigIn( hit_tileDimSig( tileMat, 1 ), hit_tileDimBegin( root, 1 ) ) )
78  hit_tileForDimDomain( tileMat, 0, i )
79  hit_tileElemAt( tileMat, 2, i, 0 ) = 3;
80 
81  /* 2.2. LAST COLUMN IS MINE */
82  if ( hit_sigIn( hit_tileDimSig( tileMat, 1 ), hit_tileDimEnd( root, 1 ) ) )
83  hit_tileForDimDomain( tileMat, 0, i )
84  hit_tileElemAt( tileMat, 2, i, hit_tileDimCard( tileMat, 1 )-1 ) = 4;
85 
86  /* 2.3. FIRST ROW IS MINE */
87  if ( hit_sigIn( hit_tileDimSig( tileMat, 0 ), hit_tileDimBegin( root, 0 ) ) )
88  hit_tileForDimDomain( tileMat, 1, j )
89  hit_tileElemAt( tileMat, 2, 0, j ) = 1;
90 
91  /* 2.4. LAST ROW IS MINE */
92  if ( hit_sigIn( hit_tileDimSig( tileMat, 0 ), hit_tileDimEnd( root, 0 ) ) )
93  hit_tileForDimDomain( tileMat, 1, j )
94  hit_tileElemAt( tileMat, 2, hit_tileDimCard( tileMat, 0 )-1, j ) = 2;
95 #endif
96 
97 #ifdef WRITE_INPUT
98  /* 4. WRITE MAT ON A FILE */
99 #ifdef WRITE_BIN
100  hit_tileFileWrite( &tileMat, "Matrix.out", HIT_FILE_ARRAY );
101 #else
102 #ifdef WRITE_TILE_FILES
103  char name[8];
104  sprintf(name, "Matrix.%d", hit_Rank );
105  hit_tileTextFileWrite( &timeMat, name, HIT_FILE_TILE, HIT_FILE_DOUBLE, 14, 4 );
106 #endif
107  hit_tileTextFileWrite( &tileMat, "Matrix.out.dtxt", HIT_FILE_ARRAY, HIT_FILE_DOUBLE, 14, 4 );
108 #endif
109 #endif
110 }
111 
112 /* C. MAIN: GAUSS SEIDEL */
113 int main(int argc, char *argv[]) {
114 
115  hit_comInit( &argc, &argv );
116 
117  int rows, columns, numIter;
118 
119  if ( argc != 4 ) {
120  fprintf(stderr, "\nUsage: %s <numRows> <numColumns> <numIterations>\n", argv[0]);
121  exit(EXIT_FAILURE);
122  }
123  rows = atoi( argv[1] );
124  columns = atoi( argv[2] );
125  numIter = atoi( argv[3] );
126 
127  /* 0. INIT CLOCKS */
129  hit_clockStart( mainClock );
130  hit_clockStart( initClock );
131  hit_clockReset( sequentialClock );
132 
133  /* 1. CREATE VIRTUAL TOPOLOGY */
134  HitTopology topo = hit_topology( plug_topArray2DComplete );
135 
136  /* 2. DECLARE FULL MATRIX WITHOUT MEMORY */
137  HitTile_double matrix;
138  hit_tileDomain( &matrix, double, 2, rows, columns );
139 
140  /* 3. COMPUTE PARTITION */
141  HitShape parallelShape = hit_tileShape( matrix );
142  parallelShape = hit_shapeExpand( parallelShape, 2, -1 );
143  HitLayout matLayout = hit_layout( plug_layBlocks, topo, parallelShape );
144 
145  /* 4. ACTIVE PROCESSES */
146  if ( hit_layImActive( matLayout ) ) {
147 
148  /* 4.1. CREATE AND ALLOCATE LOCAL TILES */
149  /* 4.1.1. LOCAL TILE WITH SPACE FOR FOREIGN DATA */
150  HitTile_double tileMat;
151  HitShape expandedShape = hit_shapeDimExpand( hit_layShape(matLayout), 0, HIT_SHAPE_BEGIN, -1 );
152  expandedShape = hit_shapeDimExpand( expandedShape, 0, HIT_SHAPE_END, 1 );
153  expandedShape = hit_shapeDimExpand( expandedShape, 1, HIT_SHAPE_BEGIN, -1 );
154  expandedShape = hit_shapeDimExpand( expandedShape, 1, HIT_SHAPE_END, 1 );
155  hit_tileSelect( &tileMat, &matrix, expandedShape );
156  hit_tileAlloc( &tileMat );
157 
158 #ifdef DEBUG
159  hit_dumpTileFile( tileMat, "Tile", "Matrix tile" );
160 #endif
161 
162  /* 4.2. COMMUNICATION PATTERNS */
163  hit_comTagSet( TAG_UP, TAG_DOWN, TAG_LEFT, TAG_RIGHT );
164  HitPattern pattBeforeComp = hit_pattern( HIT_PAT_UNORDERED );
165  hit_patternAdd( &pattBeforeComp,
166  hit_comRecvSelectTag( matLayout,
167  hit_layNeighbor( matLayout, 0, -1 ) , &tileMat,
168  hit_shape( 2, hit_sigIndex(0), hit_sig( 1, hit_tileDimCard(tileMat,1)-2, 1) ),
170  HIT_DOUBLE, TAG_DOWN )
171  );
172  hit_patternAdd( &pattBeforeComp,
173  hit_comRecvSelectTag( matLayout,
174  hit_layNeighbor( matLayout, 1, -1 ) , &tileMat,
175  hit_shape( 2, hit_sig( 1, hit_tileDimCard(tileMat,0)-2, 1), hit_sigIndex(0) ),
177  HIT_DOUBLE, TAG_RIGHT )
178  );
179 
180  HitPattern pattAfterComp = hit_pattern( HIT_PAT_UNORDERED );
181  hit_patternAdd( &pattAfterComp,
182  hit_comSendRecvSelectTag( matLayout,
183  hit_layNeighbor( matLayout, 0, -1 ) , &tileMat,
184  hit_shape( 2, hit_sigIndex(1), hit_sig( 1, hit_tileDimCard(tileMat,1)-2, 1) ),
186  hit_layNeighbor( matLayout, 0, +1 ) , &tileMat,
187  hit_shape( 2, hit_sigIndex( hit_tileDimCard(tileMat,0)-1 ), hit_sig( 1, hit_tileDimCard(tileMat,1)-2, 1) ),
189  HIT_DOUBLE, TAG_UP )
190  );
191  hit_patternAdd( &pattAfterComp,
192  hit_comSendSelectTag( matLayout,
193  hit_layNeighbor( matLayout, 0, +1 ) , &tileMat,
194  hit_shape( 2, hit_sigIndex( hit_tileDimCard(tileMat,0)-2 ), hit_sig( 1, hit_tileDimCard(tileMat,1)-2, 1) ),
196  HIT_DOUBLE, TAG_DOWN )
197  );
198  hit_patternAdd( &pattAfterComp,
199  hit_comSendRecvSelectTag( matLayout,
200  hit_layNeighbor( matLayout, 1, -1 ) , &tileMat,
201  hit_shape( 2, hit_sig( 1, hit_tileDimCard(tileMat,0)-2, 1), hit_sigIndex(1) ),
203  hit_layNeighbor( matLayout, 1, +1 ) , &tileMat,
204  hit_shape( 2, hit_sig( 1, hit_tileDimCard(tileMat,0)-2, 1), hit_sigIndex( hit_tileDimCard(tileMat,1)-1 ) ),
206  HIT_DOUBLE, TAG_LEFT )
207  );
208  hit_patternAdd( &pattAfterComp,
209  hit_comSendSelectTag( matLayout,
210  hit_layNeighbor( matLayout, 1, +1 ) , &tileMat,
211  hit_shape( 2, hit_sig( 1, hit_tileDimCard(tileMat,0)-2, 1), hit_sigIndex( hit_tileDimCard(tileMat,1)-2 ) ),
213  HIT_DOUBLE, TAG_RIGHT )
214  );
215 
216  /* 4.3. INITIALIZE MATRIX (IN PARALLEL) */
217  initMatrix( tileMat );
218 
219  /* 4.4. COMPUTATION LOOP */
220  int i,j;
221  int loopIndex;
222  for (loopIndex = 0; loopIndex < numIter; loopIndex++) {
223  /* 4.4.1. COMMUNICATE BEFORE COMPUTE (Inner loop dependences) */
224  hit_patternDo( pattBeforeComp );
225 
226  // CLOCK: Measure secuential time
227  hit_clockContinue( sequentialClock );
228 
229  /* 4.4.2. SEQUENTIALIZED LOOP */
230  for ( i=1; i<hit_tileDimCard( tileMat, 0 )-1; i++ )
231  for ( j=1; j<hit_tileDimCard( tileMat, 1 )-1; j++ )
232  updateCell(
233  & hit_tileElemAt( tileMat, 2, i, j ),
234  hit_tileElemAt( tileMat, 2, i-1, j ),
235  hit_tileElemAt( tileMat, 2, i+1, j ),
236  hit_tileElemAt( tileMat, 2, i, j-1 ),
237  hit_tileElemAt( tileMat, 2, i, j+1 )
238  );
239  hit_clockStop( sequentialClock );
240 
241  /* 4.4.3. COMMUNICATE AFTER COMPUTE (External loop carried dependences) */
242  hit_patternDo( pattAfterComp );
243  }
244 
245  /* 4.5. CLOCK RESULTS */
246  hit_clockStop( mainClock );
247  hit_clockReduce( matLayout, mainClock );
248  hit_clockReduce( matLayout, sequentialClock );
249  hit_clockPrintMax( mainClock );
250  hit_clockPrintMax( sequentialClock );
251 
252  /* 4.6. WRITE RESULT MATRIX */
253 #ifdef WRITE_RESULT
254  HitTile root = *hit_tileRoot( &tileMat );
255  HitTile_double outputTile;
256  HitShape outputShape = hit_tileShape( tileMat );
257  /* FIRST ROW IS NOT MINE */
258  if ( ! hit_sigIn( hit_shapeSig( outputShape, 0 ), hit_tileDimBegin( root, 0 ) ) )
259  outputShape = hit_shapeDimExpand( outputShape, 0, HIT_SHAPE_BEGIN, +1 );
260  /* LAST ROW IS NOT MINE */
261  if ( ! hit_sigIn( hit_shapeSig( outputShape, 0 ), hit_tileDimEnd( root, 0 ) ) )
262  outputShape = hit_shapeDimExpand( outputShape, 0, HIT_SHAPE_END, -1 );
263  /* FIRST COLUMN IS NOT MINE */
264  if ( ! hit_sigIn( hit_shapeSig( outputShape, 1 ), hit_tileDimBegin( root, 1 ) ) )
265  outputShape = hit_shapeDimExpand( outputShape, 1, HIT_SHAPE_BEGIN, +1 );
266  /* LAST COLUMN IS NOT MINE */
267  if ( ! hit_sigIn( hit_shapeSig( outputShape, 1 ), hit_tileDimEnd( root, 1 ) ) )
268  outputShape = hit_shapeDimExpand( outputShape, 1, HIT_SHAPE_END, -1 );
269  hit_tileSelectArrayCoords( &outputTile, &tileMat, outputShape );
270 #ifdef WRITE_BIN
271  hit_tileFileWrite( &outputTile, "Result.out", HIT_FILE_ARRAY );
272 #else
273  hit_tileTextFileWrite( &outputTile, "Result.out.dtxt", HIT_FILE_ARRAY, HIT_FILE_DOUBLE, 14, 4 );
274 #endif
275 #endif
276  /* 4.7. FREE RESOURCES */
277  hit_tileFree( tileMat );
278  hit_patternFree( &pattBeforeComp );
279  hit_patternFree( &pattAfterComp );
280  }
281  /* 5. INACTIVE PROCESSES: ONLY COLLECTIVE CLOCK OPERATIONS */
282  else {
283  hit_clockStop( initClock );
284  hit_clockStop( mainClock );
285  hit_clockReduce( matLayout, mainClock );
286  hit_clockReduce( matLayout, sequentialClock );
287  hit_clockPrintMax( mainClock );
288  hit_clockPrintMax( sequentialClock );
289  }
290 
291  /* 6. FREE OTHER RESOURCES */
292  hit_layFree( matLayout );
293  hit_topFree( topo );
294  hit_comFinalize();
295  return 0;
296 }
#define hit_shape(nd,...)
Definition: hit_sshape.h:175
#define hit_layShape(lay)
Definition: hit_layout.h:650
#define hit_comTagSet(...)
Definition: hit_com.h:909
#define hit_tileNewType(baseType)
Definition: hit_tile.h:163
#define hit_tileFileWrite(var, file, coord)
Definition: hit_tile.h:947
#define hit_clockSynchronizeAll()
Definition: hit_utils.h:55
#define hit_tileFill(var, value)
Definition: hit_tile.h:390
HitShape hit_shapeExpand(HitShape shape, int dims, int offset)
Definition: hit_shape.c:210
#define hit_Rank
Definition: hit_com.h:140
#define hit_tileElemAt(var, ndims,...)
Definition: hit_tile.h:519
HitCom hit_comSendRecvSelectTag(HitLayout lay, HitRanks sendTo, const void *tilePSend, HitShape selectionSend, int modeSelectSend, HitRanks receiveFrom, const void *tilePRecv, HitShape selectionRecv, int modeSelectRecv, HitType baseType, int tag)
Definition: hit_com.c:450
#define hit_dumpTileFile(var, prefix, comment)
Definition: hit_dump.h:63
HitClock sequentialClock
Definition: gaussSeidel2D.c:49
#define hit_tileDimSig(var, dim)
Definition: hit_tile.h:776
#define hit_clockContinue(c)
Definition: hit_utils.h:121
void initMatrix(HitTile_double tileMat)
Definition: gaussSeidel2D.c:58
#define hit_tileAlloc(var)
Definition: hit_tile.h:319
#define hit_topology(name,...)
Definition: hit_topology.h:308
#define hit_sigIn(sig, ind)
Definition: hit_sig.h:186
#define HIT_FILE_DOUBLE
Definition: hit_tile.h:189
HitClock mainClock
Definition: cannonAsync.c:55
#define hit_tileSelect(newVar, oldVar, shape)
Definition: hit_tile.h:453
#define HIT_PAT_UNORDERED
Definition: hit_pattern.h:81
void hit_patternAdd(HitPattern *pattern, HitCom comm)
Definition: hit_pattern.c:61
#define hit_clockPrintMax(c)
Definition: hit_utils.h:175
#define hit_tileDimCard(var, dim)
Definition: hit_tile.h:750
#define hit_tileFileRead(var, file, coord)
Definition: hit_tile.h:974
#define hit_tileDimBegin(var, dim)
Definition: hit_tile.h:786
#define hit_clockReset(c)
Definition: hit_utils.h:78
#define hit_tileTextFileWrite(var, file, coord, datatype, s1, s2)
Definition: hit_tile.h:1011
void hit_topFree(HitTopology topo)
Definition: hit_topology.c:129
void hit_patternFree(HitPattern *pattern)
Definition: hit_pattern.c:94
void hit_comInit(int *pargc, char **pargv[])
Definition: hit_com.c:111
#define hit_layImActive(lay)
Definition: hit_layout.h:797
#define hit_tileTextFileRead(var, file, coord, datatype, s1, s2)
Definition: hit_tile.h:1045
#define hit_tileDomain(newVarP, baseType, numDims,...)
Definition: hit_tile.h:286
#define HIT_FILE_TILE
Definition: hit_tile.h:196
#define hit_tileDimEnd(var, dim)
Definition: hit_tile.h:796
void hit_layFree(HitLayout lay)
Definition: hit_layout.c:2251
#define hit_clockStop(c)
Definition: hit_utils.h:109
#define HIT_FILE_ARRAY
Definition: hit_tile.h:201
int main(int argc, char *argv[])
Definition: cannonAsync.c:62
#define hit_layNeighbor(lay, dim, shift)
Definition: hit_layout.h:722
#define hit_tileSelectArrayCoords(newVar, oldVar, shape)
Definition: hit_tile.h:494
HitShape hit_shapeDimExpand(HitShape shape, int dim, int position, int offset)
Definition: hit_shape.c:226
#define hit_tileForDimDomain(tile, dim, index)
Definition: hit_tile.h:587
#define hit_layout(name, topo,...)
Definition: hit_layout.h:415
#define hit_shapeSig(shape, dim)
Definition: hit_sshape.h:400
#define HIT_COM_TILECOORDS
Definition: hit_com.h:343
#define hit_comSendSelectTag(lay, sendTo, tileP, selection, modeSelect, baseType, tag)
Definition: hit_com.h:419
#define hit_tileFree(var)
Definition: hit_tile.h:369
#define HIT_DOUBLE
Definition: hit_com.h:78
#define hit_tileShape(var)
Definition: hit_tile.h:723
#define hit_patternDo(pattern)
Definition: hit_pattern.h:157
void hit_comFinalize()
Definition: hit_com.c:159
#define hit_comRecvSelectTag(lay, receiveFrom, tileP, selection, modeSelect, baseType, tag)
Definition: hit_com.h:466
#define HIT_SHAPE_END
Definition: hit_sshape.h:522
HitClock initClock
Definition: gaussSeidel2D.c:48
#define hit_clockReduce(lay, c)
Definition: hit_utils.h:139
#define hit_clockStart(c)
Definition: hit_utils.h:87
#define HIT_SHAPE_BEGIN
Definition: hit_sshape.h:515