Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
seqJacobi2D.c
Go to the documentation of this file.
1 /*
2 * seqJacobi2D.c
3 * Stencil code: Jacobi 2D for the heat equation. Implemented as a 2D cellular automata.
4 * Sequential basic code
5 *
6 * v1.0
7 * (c) 2007-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 
44 int main(int argc, char *argv[]) {
45  if (argc < 4 ) {
46  fprintf( stderr, "Usage: %s <rows> <columns> <numIter>\n", argv[0] );
47  exit( EXIT_FAILURE );
48  }
49  int ROWS = atoi( argv[1] );
50  int COLS = atoi( argv[2] );
51  int STAGES = atoi( argv[3] );
52 
53  double mat[ROWS][COLS];
54  double copy[ROWS][COLS];
55 
56  int i,j;
57 
58  // Init
59  for ( i=0; i<ROWS; i++ )
60  for ( j=0; j<COLS; j++ )
61  mat[i][j] = 0.0;
62 
63  for ( i=0; i<ROWS; i++ ) {
64  mat[i][0] = 3;
65  mat[i][COLS-1] = 4;
66  }
67 
68  for ( j=0; j<COLS; j++ ) {
69  mat[0][j] = 1;
70  mat[ROWS-1][j] = 2;
71  }
72 
73  // Compute
74  int stage;
75  for ( stage=0; stage<STAGES; stage++ ) {
76  // Update copy
77  for ( i=0; i<ROWS; i++ )
78  for ( j=0; j<COLS; j++ )
79  copy[i][j] = mat[i][j];
80 
81  // Compute iteration
82  for ( i=1; i<ROWS-1; i++ )
83  for ( j=1; j<COLS-1; j++ )
84  mat[i][j] = ( copy[i-1][j] + copy[i+1][j] + copy[i][j-1] + copy[i][j+1] ) / 4;
85  }
86 
87  // Write
88  for ( i=0; i<ROWS; i++ )
89  for ( j=0; j<COLS; j++ )
90  printf("%014.4lf\n", mat[i][j] );
91 
92  // End
93  return 0;
94 }
int main(int argc, char *argv[])
Definition: cannonAsync.c:62