Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
heat.c
Go to the documentation of this file.
1 
11 /*
12  * <license>
13  *
14  * Hitmap v1.2
15  *
16  * This software is provided to enhance knowledge and encourage progress in the scientific
17  * community. It should be used only for research and educational purposes. Any reproduction
18  * or use for commercial purpose, public redistribution, in source or binary forms, with or
19  * without modifications, is NOT ALLOWED without the previous authorization of the copyright
20  * holder. The origin of this software must not be misrepresented; you must not claim that you
21  * wrote the original software. If you use this software for any purpose (e.g. publication),
22  * a reference to the software package and the authors must be included.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND ANY
25  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * Copyright (c) 2007-2015, Trasgo Group, Universidad de Valladolid.
35  * All rights reserved.
36  *
37  * More information on http://trasgo.infor.uva.es/
38  *
39  * </license>
40 */
41 
42 #include <hitmap.h>
43 #include <unistd.h>
44 
48 #define SIMULATED_LOAD
49 
50 
51 /* OUTPUT FUNCIONS */
52 #define printfRootInternal(...) { if( hit_Rank == 0 ) { printf(__VA_ARGS__); fflush(stdout); }}
53 #define printfRoot(...) printfRootInternal(__VA_ARGS__)
54 
55 
56 /* HITMAP DOUBLE TILE */
57 hit_tileNewType(double);
58 
63 void print_help(char * name);
64 
70 void init_graph(HitTile_double graph, HitShape shape_global);
71 
78 HitTile_double heat_iteration(HitShape shape, HitTile_double graph);
79 
86 double result_check(HitShape shape, HitTile_double graph);
87 
88 
92 #define ITERATIONS 100
93 
97 #define INIT_VALUES 10
98 
102 HitTile_double graph_aux;
103 
108 
109 
110 #ifdef SIMULATED_LOAD
111  int simulated = 0;
112 #endif
113 
114 
115 
116 
120 int main(int argc, char ** argv){
121 
122  // Clock
123  HitClock timeclock;
124 
125  // Graph file name.
126  char * graph_file = NULL;
127 
128  // Iterations.
129  int iterations = ITERATIONS;
130 
131  // Argument.
132  char c;
133 
134  // Parse the command-line arguments.
135  while ((c = (char) getopt (argc, argv, "i:s:")) != -1)
136  switch (c) {
137  case 'i':
138  sscanf(optarg,"%d",&iterations);
139  break;
140 #ifdef SIMULATED_LOAD
141  case 's':
142  sscanf(optarg,"%d",&simulated);
143  break;
144 #endif
145  default:
146  abort ();
147  break;
148  }
149 
150  // Show help if there is no input file.
151  if(optind == argc){
152  print_help(argv[0]);
153  }
154 
155  // Get input file.
156  graph_file = argv[optind];
157 
158  /* Initialize hitmap communication */
159  hit_comInit(&argc,&argv);
160 
161  // Print info.
162  printfRoot("Graph: %s, Iter: %d\n",graph_file,iterations);
163  printfRoot("Nprocs: %d\n",hit_NProcs);
164 
165  hit_clockStart( timeclock );
166 
167  // Global graph
168  HitShape shape_global;
169  if(hit_Rank == 0) shape_global = hit_fileHBRead(graph_file);
170  printfRoot("Vertices: %d\n",hit_cShapeNvertices(shape_global));
171 
172  // Create the topology object.
173  HitTopology topo = hit_topology(plug_topPlain);
174 
175  // Distribute the graph among the processors.
176  lay = hit_layout(plug_layMetis,topo,&shape_global);
177 
178  // Get the shapes for the local and local extended graphs.
179  HitShape shape = hit_layShape(lay);
180  HitShape ext_shape = hit_cShapeExpand(shape,shape_global,1);
181 
182  // Allocate memory for the graphs.
183  HitTile_double graph;
184  hit_gcTileDomainShapeAlloc(&graph, double, ext_shape, HIT_VERTICES);
185  hit_gcTileDomainShapeAlloc(&graph_aux, double, ext_shape, HIT_VERTICES);
186 
187  // Init the local graph.
188  init_graph(graph, shape_global);
189 
190  // Create the communicator and send the initial values of the neighbor vertices.
191  HitCom com = hit_comSparseUpdate(lay, &graph, HIT_DOUBLE);
192  hit_comDo(&com);
193 
194  hit_clockStop( timeclock );
195  printfRoot("Time init: %f\n",hit_clockGetSeconds(timeclock));
196  hit_clockStart( timeclock );
197 
198  int i;
199  // Perform the equation.
200  for(i=0; i<iterations; i++){
201 
202  // Update the graph.
203  graph = heat_iteration(shape,graph);
204  // Update the communication object with the current graph.
205  hit_comUpdateOriginData(&com,&graph);
206  // Communication.
207  hit_comDo(&com);
208 
209  }
210 
211 
212  hit_clockStop( timeclock );
213 
214 
215  double result = result_check(shape,graph);
216 
217  printfRoot("Result: %f\n",result);
218  printfRoot("Time: %f\n",hit_clockGetSeconds(timeclock));
219 
220  // Free all the used resources.
221  hit_comFree(com);
222  hit_tileFree(graph);
224  hit_layFree(lay);
225  hit_topFree(topo);
226  hit_shapeFree(shape_global);
227 
228  hit_comFinalize();
229  return 0;
230 
231 }
232 
233 
234 void print_help(char * name){
235  printf("%s [-i ITERATIONS] FILE \n",name);
236 
237  printf(" -i ITERATIONS number of iterations (default 100)\n");
238  printf(" FILE input graph file\n");
239 
240  exit(0);
241 }
242 
243 
244 void init_graph(HitTile_double graph, HitShape shape_global){
245 
246  srandom(1);
247 
248  // Set all the vector to 0.
249  int i;
250  for(i=0;i<hit_cShapeNvertices(hit_tileShape(graph));i++)
251  hit_gcTileVertexAt(graph,i) = 0.0;
252 
253 
254  // If there are a few nodes, set only the first one.
255  if( hit_cShapeNvertices(shape_global) < (2*INIT_VALUES)){
256  int local = hit_cShapeVertexToLocal(hit_tileShape(graph),0);
257  if(local != -1) hit_gcTileVertexAt(graph,local) = 100.0;
258  return;
259  }
260 
261  // Init the nodes.
262  for(i=0;i<INIT_VALUES;i++){
263 
264  int first = (i * hit_cShapeNvertices(shape_global)) / INIT_VALUES;
265  int last = ((i+1) * hit_cShapeNvertices(shape_global)) / INIT_VALUES;
266 
267  int mod = last - first;
268 
269  int in = first + (int)random() % mod;
270 
271  int local = hit_cShapeVertexToLocal(hit_tileShape(graph),in);
272  if(local != -1) hit_gcTileVertexAt(graph,local) = 100.0;
273 
274  }
275 
276 }
277 
278 
279 
280 
281 HitTile_double heat_iteration(HitShape shape, HitTile_double graph){
282 
283  int vertex;
285 
286  // Iterate trough all the vertices.
287  hit_cShapeVertexIterator(vertex,shape) {
288 
289  int edge;
290 
291  // Set new value to 0.
292  double value = 0;
293 
294  hit_cShapeEdgeIterator(edge,ext_shape,vertex){
295 
296  // Get the neighbor.
297  int neighbor = hit_cShapeEdgeTarget(ext_shape,edge);
298 
299  // Add its contribution.
300  value += hit_gcTileVertexAt(graph,neighbor);
301 
302  }
303 
304 
305 #ifdef SIMULATED_LOAD
306  int i;
307  for(i=0;i<simulated;i++){
308  value = sin(value+1);
309  }
310 #endif
311 
312  int nedge = hit_cShapeNEdgesFromVertex(ext_shape,vertex);
313 
314  // Update the value of the vertex.
315  hit_gcTileVertexAt(graph_aux,vertex) = (value / nedge);
316  }
317 
318 
319  HitTile_double aux = graph_aux;
320  graph_aux = graph;
321  return aux;
322 
323 }
324 
325 
326 double result_check(HitShape shape, HitTile_double graph){
327 
328  HitTile_double sum, sum_all;
329 
330  hit_tileDomainAlloc(&sum, double, 1,1);
331  hit_tileDomainAlloc(&sum_all, double, 1,1);
332 
333  hit_tileElemAt(sum,1,0) = 0;
334 
335  int i;
336  for(i=0; i< hit_cShapeNvertices(shape); i++){
337  hit_tileElemAt(sum,1,0) += hit_gcTileVertexAt(graph,i);
338  }
339 
340  HitOp op_sum;
342  HitCom com_sum = hit_comReduce(lay, HIT_RANKS_NULL, &sum, &sum_all, HIT_DOUBLE, op_sum);
343 
344  hit_comDo(&com_sum);
345 
346  double res = hit_tileElemAt(sum_all,1,0);
347 
348  hit_comFree(com_sum);
349  hit_tileFree(sum);
350  hit_tileFree(sum_all);
351 
352  return res;
353 }
354 
MPI_Op HitOp
Definition: hit_com.h:118
void print_help(char *name)
Definition: heat.c:234
#define hit_layShape(lay)
Definition: hit_layout.h:650
#define hit_cShapeNEdgesFromVertex(s, vertex)
#define hit_tileNewType(baseType)
Definition: hit_tile.h:163
#define hit_cShapeEdgeTarget(s, edge)
HitCom hit_comSparseUpdate(HitLayout lay, const void *tileP, HitType baseType)
Definition: hit_com.c:1084
void hit_comOpSumDouble(void *, void *, int *, HitType *)
Definition: hit_com.c:2665
HitOp op_sum
Definition: mg.c:176
#define hit_Rank
Definition: hit_com.h:140
#define hit_cShapeVertexIterator(var, shape)
Definition: hit_cshape.h:392
#define hit_tileElemAt(var, ndims,...)
Definition: hit_tile.h:519
#define HIT_LAYOUT_NULL_STATIC
Definition: hit_layout.h:300
double result_check(HitShape shape, HitTile_double graph)
Definition: heat.c:326
void hit_comFree(HitCom issue)
Definition: hit_com.c:1995
void hit_comDo(HitCom *issue)
Definition: hit_com.c:2408
int simulated
Definition: heat.c:111
#define hit_tileDomainAlloc(newVarP, baseType, numDims,...)
Definition: hit_tile.h:336
#define hit_topology(name,...)
Definition: hit_topology.h:308
HitRanks HIT_RANKS_NULL
Definition: hit_topology.c:68
#define hit_cShapeEdgeIterator(var, shape, vertex)
Definition: hit_cshape.h:416
HitTile_Vector graph
#define ITERATIONS
Definition: heat.c:92
#define hit_NProcs
Definition: hit_com.h:142
#define hit_gcTileDomainShapeAlloc(var, baseType, shape, allocOpts)
Definition: hit_gctile.h:99
void hit_comUpdateOriginData(HitCom *com, const void *tileP)
Definition: hit_com.c:1985
#define INIT_VALUES
Definition: heat.c:97
#define hit_gcTileVertexAt(var, vertex)
Definition: hit_gctile.h:157
HitShape hit_cShapeExpand(HitShape shape, HitShape original, int amount)
Definition: hit_cshape.c:208
#define hit_cShapeNvertices(shape)
#define hit_cShapeVertexToLocal(s, vertex)
Definition: hit_cshape.h:243
void hit_topFree(HitTopology topo)
Definition: hit_topology.c:129
#define hit_comOp(function, operation)
Definition: hit_com.h:1036
HitShape ext_shape
HitTile_double graph_aux
Definition: heat.c:102
void hit_comInit(int *pargc, char **pargv[])
Definition: hit_com.c:111
HitShape shape
HitTile_double heat_iteration(HitShape shape, HitTile_double graph)
Definition: heat.c:281
#define hit_fileHBRead(hbfile)
Definition: hit_file.h:77
void hit_layFree(HitLayout lay)
Definition: hit_layout.c:2251
#define hit_clockStop(c)
Definition: hit_utils.h:109
int main(int argc, char *argv[])
Definition: cannonAsync.c:62
#define iterations
Definition: mg.c:119
#define hit_comReduce(lay, root, tilePSend, tilePRecv, baseType, operation)
Definition: hit_com.h:725
void init_graph(HitTile_double graph, HitShape shape_global)
Definition: heat.c:244
#define hit_layout(name, topo,...)
Definition: hit_layout.h:415
HitLayout lay
Definition: heat.c:107
#define hit_tileFree(var)
Definition: hit_tile.h:369
void hit_shapeFree(HitShape s)
Definition: hit_shape.c:84
#define HIT_DOUBLE
Definition: hit_com.h:78
#define printfRoot(...)
Definition: heat.c:53
#define hit_tileShape(var)
Definition: hit_tile.h:723
void hit_comFinalize()
Definition: hit_com.c:159
#define hit_clockGetSeconds(c)
Definition: hit_utils.h:190
#define HIT_VERTICES
Definition: hit_tile.h:208
#define hit_clockStart(c)
Definition: hit_utils.h:87