Bridge++  Version 1.4.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
physical_map.cpp
Go to the documentation of this file.
1 
14 #include "layout.h"
15 
16 #include <cstdarg>
17 #include <cstring>
18 
20 
21 static int find_coord_map(const char map_expr[], int ndim, int *map);
22 
23 // physical rank is assigned to physical grid coord in column major order.
24 // phys_coord[0] x phys_coord[1] x ... (fastest to slowest)
25 // other assignment may be chosen.
26 
27 //====================================================================
28 // logical grid coord to physical rank number
29 int Communicator_impl::Layout::grid_rank(int *rank, const int *gcoord)
30 {
31  int r = 0;
32 
33  for (int i = m_ndim - 1; i >= 0; --i) {
34  int k = m_physical_to_logical[i];
35  r = r * m_grid_dims[k] + gcoord[k];
36  }
37 
38  *rank = r;
39 
40  return EXIT_SUCCESS;
41 }
42 
43 
44 //====================================================================
45 //* physical rank to logical grid coord
46 int Communicator_impl::Layout::grid_coord(int *gcoord, const int rank)
47 {
48  int r = rank;
49 
50  for (int i = 0; i < m_ndim; ++i) {
51  int k = m_physical_to_logical[i];
52 
53  gcoord[k] = r % m_grid_dims[k];
54  r /= m_grid_dims[k];
55  }
56 
57  return EXIT_SUCCESS;
58 }
59 
60 
61 //====================================================================
63 {
64  strncpy(m_map_grid, CommonParameters::Grid_map(), sizeof(m_map_grid) / sizeof(char));
65 
66  // physical to logical map
67  m_physical_to_logical = new int [m_ndim];
68 
69  find_coord_map(m_map_grid, m_ndim, m_physical_to_logical);
70 
71  return EXIT_SUCCESS;
72 }
73 
74 
75 //====================================================================
77 {
78  delete [] m_physical_to_logical;
79 
80  return EXIT_SUCCESS;
81 }
82 
83 
84 //====================================================================
85 //* translate {x, y, z, t, (w)} into {0, 1, 2, 3, (4)}
86 static int find_coord_map(const char map_expr[], int ndim, int *map)
87 {
88  int len = strlen(map_expr);
89 
90  if (len != ndim) {
91  fprintf(stderr, "ERROR: find_map: length mismatch: %d, expected %d.\n", len, ndim);
92  return EXIT_FAILURE;
93  }
94 
95  for (int i = 0; i < len; ++i) {
96  char c = map_expr[i];
97  int idx = -1;
98 
99  switch (c)
100  {
101  case 'x':
102  idx = 0;
103  break;
104 
105  case 'y':
106  idx = 1;
107  break;
108 
109  case 'z':
110  idx = 2;
111  break;
112 
113  case 't':
114  idx = 3;
115  break;
116 
117  case 'w':
118  idx = 4;
119  break; // extra 5th dimension
120 
121  default:
122  fprintf(stderr, "ERROR: find_map: unknown symbol %c.\n", c);
123  return EXIT_FAILURE;
124  }
125 
126  map[i] = idx;
127  }
128 
129  return EXIT_SUCCESS;
130 }
131 
132 
133 //====================================================================
134 //============================================================END=====
static int grid_coord(int *gcoord, const int rank)
static int * m_physical_to_logical
map between physical and logical grid
Definition: layout.h:46
static int m_ndim
number of dimensions.
Definition: layout.h:34
static int grid_rank(int *rank, const int *gcoord)
static int find_coord_map(const char map_expr[], int ndim, int *map)
static int * m_grid_dims
grid dimensions in directions.
Definition: layout.h:38
static char * Grid_map()
static int physical_map_delete()
static int physical_map_setup()