Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
mmult_bit.c
Go to the documentation of this file.
1 
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 #include <hit_com.h>
45 #include <sys/time.h>
46 #include <unistd.h>
47 
48 
53 
58 
62 hit_tileNewType(double);
63 
64 
68 int iter = 100;
69 
70 
77 void mult(HitTile_double M, HitTile_double b, HitTile_double c){
78 
79  int i;
81 
82  int j;
83  double sum = 0.0;
85 
86  // Get the name of the column.
87  int index2 = hit_bShapeEdgeTarget(hit_tileShape(M),j);
88 
89  // Multiply element M_ij * b_j
90  sum += hit_mbTileElemIteratorAt(M,i,j) * hit_tileElemAtNoStride(b,1,index2);
91  }
92  // Set the elemetn c_i
93  hit_tileElemAtNoStride(c,1,i) = sum;
94 
95  }
96 }
97 
98 
104 double vector_norm(HitTile_double v){
105 
106  HitTile_double norm, sum_norm;
107 
108  hit_tileDomainAlloc(&norm, double, 1,1);
109  hit_tileDomainAlloc(&sum_norm, double, 1,1);
110 
111  hit_tileElemAt(norm,1,0) = 0.0;
112 
113  // Caculate the partial norm
114  int i;
115  for(i=0; i<hit_tileDimCard(v,0); i++){
116  hit_tileElemAt(norm,1,0) += pow(hit_tileElemAt(v,1,i),2);
117  }
118 
119  HitOp op_sum;
121 
122  // Reduce the norm to the root processor
123  HitRanks root = {{0,0,0,-1}};
124  HitCom com_sum = hit_comReduce(lay, root, &norm, &sum_norm, HIT_DOUBLE, op_sum);
125  hit_comDo(&com_sum);
126  hit_comFree(com_sum);
127 
128  double res = 0;
129 
130  if( hit_Rank == 0 ){
131  res = sqrt(hit_tileElemAt(sum_norm,1,0));
132  }
133 
134  // Free the tiles
135  hit_tileFree(norm);
136  hit_tileFree(sum_norm);
137 
138  return res;
139 }
140 
141 
142 
147 void print_help(char * name){
148  printf("%s [-n ITERATIONS] FILE \n",name);
149  printf(" -n number of iterations\n");
150  printf(" FILE input HB matrix file\n");
151 }
152 
153 
159 void init_matrix(int argc, char ** argv, HitShape * shape){
160 
161 
162 
163  if(argc > 1){
164 
165  // Argument.
166  char c;
167 
168  // Parse the command-line arguments.
169  while ((c = (char) getopt (argc, argv, "hn:")) != -1)
170  switch (c) {
171  case 'n':
172  sscanf(optarg,"%d",&iter);
173  break;
174  case 'h':
175  if(hit_Rank == 0) print_help(argv[0]);
176  hit_comFinalize();
177  exit(EXIT_SUCCESS);
178  default:
179  abort ();
180  break;
181  }
182 
183  char * matrix_file = argv[optind];
184 
185  if(hit_Rank != 0) return;
186 
187  printf("# Matrix: %s\n",matrix_file);
188  printf("# Iterations %d\n",iter);
189  *shape = hit_fileHBMatrixReadBitmap(matrix_file);
190 
191  if(hit_bShapeCard(*shape,0) != hit_bShapeCard(*shape,1)){
192  printf("Matrix must to be square");
193  exit(EXIT_FAILURE);
194  }
195 
196  } else {
197 
198  if(hit_Rank != 0) return;
199 
200  int i;
201  for(i=0; i<5; i++){
202  hit_bShapeAddElem(shape, i, i);
203  }
204  hit_bShapeAddElem(shape, 0, 3);
205 
206  }
207 }
208 
209 
210 
217 int main(int argc, char ** argv) {
218 
219 
220  // Init Hitmap communications.
221  hit_comInit(&argc,&argv);
222 
223  // Timers.
224  HitClock init_time, comp_time;
225  hit_clockStart(init_time);
226 
227  // Load the global matrix.
228  HitShape shape_global = HIT_BITMAP_SHAPE_NULL;
229  init_matrix(argc, argv, &shape_global);
230 
231 
232  // Create the topology object.
233  HitTopology topo = hit_topology(plug_topPlain);
234 
235  // Distribute the matrix among the processors.
236  lay = hit_layout(plug_laySparseBitmapRows,topo,&shape_global);
237 
238  // Get the shape for the local matrix.
239  HitShape shape = hit_layShape(lay);
240 
241  // Allocate the matrix.
242  HitTile_double M;
243  hit_mbTileDomainShapeAlloc(&M, double, shape);
244 
245  // Init the matrix values.
246  int i;
248  int nz = hit_bShapeNColsRow(shape,i);
249  int j;
251  hit_mbTileElemIteratorAt(M,i,j) = 1.0 / (double) nz;
252  }
253  }
254 
255  // Create the shape for the b vector.
256  HitShape shape_b = hit_shape(1,hit_sig(0,hit_bShapeCard(shape,1)-1,1));
257 
258  // Distribute the vector with the result.
259  HitShape full_vector = hit_shape(1,hit_sig(0,hit_bShapeCard(shape_global,0)-1,1));
260  lay_v = hit_layout(plug_layBlocks,topo,full_vector);
261  HitShape shape_c = hit_layShape(lay_v);
262 
263  // Allocate the two vectors
264  HitTile_double b,c;
265 
266  hit_tileDomainShapeAlloc(&b,double,shape_b);
267  hit_tileDomainShapeAlloc(&c,double,shape_c);
268 
269  // Init the vector
270  for(i=0; i< hit_shapeSigCard(shape_c,0); i++){
271  hit_tileElemAt(c,1,i) = hit_shapeSig(shape_c,0).begin + i;
272  }
273 
274  // Create the communication to gather the result vector.
275  HitPattern pat = hit_pattern(HIT_PAT_UNORDERED);
276  hit_patMatMultBitmap(&pat,lay,shape_global,shape,&c,&b,HIT_DOUBLE);
277 
278 
279  // Comunicate
280  hit_patternDo(pat);
281 
282  hit_clockStop(init_time);
283  hit_clockStart(comp_time);
284 
285  // Main loop
286  for(i=0; i<iter; i++){
287  mult(M, b, c);
288  hit_patternDo(pat);
289  }
290 
291 
292  hit_clockStop(comp_time);
293  hit_clockWorldReduce(init_time);
294  hit_clockWorldReduce(comp_time);
295 
296  // Obtain the norm of the vector.
297  double norm = vector_norm(c);
298 
299  // Print times
300  if(hit_Rank == 0){
301  printf("# Result norm: %5.2f\n",norm);
302  printf("# Init time: %lf\n",hit_clockGetSeconds(init_time));
303  printf("# Comp time: %lf\n",hit_clockGetSeconds(comp_time));
304  printf("# Total time: %lf\n",hit_clockGetSeconds(init_time)+hit_clockGetSeconds(comp_time));
305 
306  }
307 
308  // Free resources
309  hit_layFree(lay);
310  hit_layFree(lay_v);
311  hit_topFree(topo);
312  hit_shapeFree(shape);
313  hit_tileFree(M);
314  hit_tileFree(b);
315  hit_tileFree(c);
316 
317  hit_comFinalize();
318 
319  return 0;
320 }
321 
322 
#define hit_shape(nd,...)
Definition: hit_sshape.h:175
MPI_Op HitOp
Definition: hit_com.h:118
#define hit_bShapeColumnIterator(var, shape, row)
Definition: hit_bshape.h:466
void print_help(char *name)
Definition: heat.c:234
#define hit_layShape(lay)
Definition: hit_layout.h:650
HitLayout lay_v
Definition: mmult_bit.c:57
#define hit_tileNewType(baseType)
Definition: hit_tile.h:163
#define norm(vv)
#define hit_bShapeAddElem(shapep, x, y)
Definition: hit_bshape.h:612
void hit_comOpSumDouble(void *, void *, int *, HitType *)
Definition: hit_com.c:2665
HitOp op_sum
Definition: mg.c:176
#define hit_mbTileElemIteratorAt(var, iterX, iterY)
Definition: hit_mbtile.h:159
#define hit_Rank
Definition: hit_com.h:140
int hit_bShapeNColsRow(HitShape shape, int row)
Definition: hit_bshape.c:434
int iter
Definition: mmult_bit.c:68
#define hit_tileElemAt(var, ndims,...)
Definition: hit_tile.h:519
#define hit_fileHBMatrixReadBitmap(hbfile)
Definition: hit_file.h:149
#define hit_bShapeEdgeTarget(s, edge)
#define hit_bShapeCard(shape, dim)
Definition: hit_bshape.h:144
#define HIT_LAYOUT_NULL_STATIC
Definition: hit_layout.h:300
void hit_comFree(HitCom issue)
Definition: hit_com.c:1995
void hit_comDo(HitCom *issue)
Definition: hit_com.c:2408
void mult(HitTile_double M, HitTile_double b, HitTile_double c)
Definition: mmult_bit.c:77
#define hit_tileDomainAlloc(newVarP, baseType, numDims,...)
Definition: hit_tile.h:336
#define hit_topology(name,...)
Definition: hit_topology.h:308
#define hit_bShapeRowIterator(var, shape)
Definition: hit_bshape.h:444
#define hit_patMatMultBitmap(pattern, lay, origin_matrix, matrix, tilePSend, tilePRecv, baseType)
Definition: hit_pattern.h:225
#define HIT_PAT_UNORDERED
Definition: hit_pattern.h:81
#define hit_tileDimCard(var, dim)
Definition: hit_tile.h:750
#define hit_tileDomainShapeAlloc(newVarP, baseType, shape)
Definition: hit_tile.h:354
#define init_matrix(mm, a, b, c, d, e, f, g, h, i)
void hit_topFree(HitTopology topo)
Definition: hit_topology.c:129
#define hit_comOp(function, operation)
Definition: hit_com.h:1036
#define hit_clockWorldReduce(c)
Definition: hit_utils.h:156
void hit_comInit(int *pargc, char **pargv[])
Definition: hit_com.c:111
HitShape shape
#define hit_tileElemAtNoStride(var, ndims,...)
Definition: hit_tile.h:558
#define hit_shapeSigCard(shape, dim)
Definition: hit_sshape.h:412
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
HitShape HIT_BITMAP_SHAPE_NULL
Definition: hit_shape.c:71
#define hit_comReduce(lay, root, tilePSend, tilePRecv, baseType, operation)
Definition: hit_com.h:725
#define v(a, b, c)
#define hit_layout(name, topo,...)
Definition: hit_layout.h:415
HitLayout lay
Definition: heat.c:107
#define hit_shapeSig(shape, dim)
Definition: hit_sshape.h:400
#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 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_clockGetSeconds(c)
Definition: hit_utils.h:190
#define hit_clockStart(c)
Definition: hit_utils.h:87
#define hit_mbTileDomainShapeAlloc(var, baseType, shape)
Definition: hit_mbtile.h:101
double vector_norm(HitTile_double v)
Definition: mmult_bit.c:104