Hitmap 1.3
 All Data Structures Namespaces Files Functions Variables Typedefs Friends Macros Groups Pages
mmult_csr.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 <stdio.h>
43 #include <stdlib.h>
44 #include <hitmap.h>
45 #include <hit_com.h>
46 #include <sys/time.h>
47 #include <unistd.h>
48 
53 
58 
62 hit_tileNewType(double);
63 
67 int iter = 100;
68 
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_cShapeEdgeTarget(hit_tileShape(M),j);
88 
89  // Multiply element M_ij * b_j
90  sum += hit_mcTileElemIteratorAt(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 
146 void print_help(char * name){
147  printf("%s [-n ITERATIONS] FILE \n",name);
148  printf(" -n number of iterations\n");
149  printf(" FILE input HB matrix file\n");
150 }
151 
152 
158 void init_matrix(int argc, char ** argv, HitShape * shape){
159 
160  if(argc > 1){
161 
162  // Argument.
163  char c;
164 
165  // Parse the command-line arguments.
166  while ((c = (char) getopt (argc, argv, "hn:")) != -1)
167  switch (c) {
168  case 'n':
169  sscanf(optarg,"%d",&iter);
170  break;
171  case 'h':
172  if(hit_Rank == 0) print_help(argv[0]);
173  hit_comFinalize();
174  exit(EXIT_SUCCESS);
175  default:
176  abort ();
177  break;
178  }
179 
180  char * matrix_file = argv[optind];
181 
182  if(hit_Rank != 0) return;
183 
184  printf("# Matrix: %s\n",matrix_file);
185  printf("# Iterations %d\n",iter);
186  *shape = hit_fileHBMatrixRead(matrix_file);
187 
188  if(hit_cShapeCard(*shape,0) != hit_cShapeCard(*shape,1)){
189  printf("Matrix must to be square");
190  exit(EXIT_FAILURE);
191  }
192 
193  } else {
194 
195  if(hit_Rank != 0) return;
196 
197  int i;
198  for(i=0; i<5; i++){
199  hit_cShapeAddElem(shape, i, i);
200  }
201  hit_cShapeAddElem(shape, 0, 3);
202 
203  }
204 }
205 
206 
207 
214 int main(int argc, char ** argv) {
215 
216  // Init Hitmap communications.
217  hit_comInit(&argc,&argv);
218 
219  // Timers.
220  HitClock init_time, comp_time;
221  hit_clockStart(init_time);
222 
223  // Load the global matrix.
224  HitShape shape_global = HIT_CSR_SHAPE_NULL;
225  init_matrix(argc, argv, &shape_global);
226 
227  // Create the topology object.
228  HitTopology topo = hit_topology(plug_topPlain);
229 
230  // Distribute the matrix among the processors.
231  lay = hit_layout(plug_laySparseRows,topo,&shape_global);
232 
233  // Get the shape for the local matrix.
234  HitShape shape = hit_layShape(lay);
235 
236  // Allocate the matrix.
237  HitTile_double M;
238  hit_mcTileDomainShapeAlloc(&M, double, shape);
239 
240  // Init the matrix values.
241  int i;
243  int j;
245  hit_mcTileElemIteratorAt(M,i,j) = 1.0 / (double) hit_cShapeNColsRow(shape,i);
246  }
247  }
248 
249  // Create the shape for the b vector.
250  //HitShape shape_b = hit_shape(1,hit_sig(0,hit_cShapeCard(shape_global,0)-1,1));
251  HitShape shape_b = hit_shape(1,hit_sig(0,hit_cShapeCard(shape,1)-1,1));
252 
253  // Distribute the vector with the result.
254  HitShape full_vector = hit_shape(1,hit_sig(0,hit_cShapeCard(shape_global,0)-1,1));
255  lay_v = hit_layout(plug_layBlocks,topo,full_vector);
256  HitShape shape_c = hit_layShape(lay_v);
257 
258  // Allocate the two vectors
259  HitTile_double b,c;
260 
261  hit_tileDomainShapeAlloc(&b,double,shape_b);
262  hit_tileDomainShapeAlloc(&c,double,shape_c);
263 
264  // Init the vector
265  for(i=0; i< hit_shapeSigCard(shape_c,0); i++){
266  hit_tileElemAt(c,1,i) = hit_shapeSig(shape_c,0).begin + i;
267  }
268 
269  // Create the communication to gather the result vector.
270  HitPattern pat = hit_pattern(HIT_PAT_UNORDERED);
271  hit_patMatMult(&pat,lay,shape_global,shape,&c,&b,HIT_DOUBLE);
272 
273  // Comunicate
274  hit_patternDo(pat);
275 
276  hit_clockStop(init_time);
277  hit_clockStart(comp_time);
278 
279  // Main loop
280  for(i=0; i<iter; i++){
281  mult(M, b, c);
282  hit_patternDo(pat);
283  }
284 
285  hit_clockStop(comp_time);
286  hit_clockWorldReduce(init_time);
287  hit_clockWorldReduce(comp_time);
288 
289  // Obtain the norm of the vector.
290  double norm = vector_norm(c);
291 
292  // Print times
293  if(hit_Rank == 0){
294  printf("# Result norm: %5.2f\n",norm);
295  printf("# Init time: %lf\n",hit_clockGetSeconds(init_time));
296  printf("# Comp time: %lf\n",hit_clockGetSeconds(comp_time));
297  printf("# Total time: %lf\n",hit_clockGetSeconds(init_time)+hit_clockGetSeconds(comp_time));
298 
299  }
300 
301  // Free resources
302  hit_layFree(lay);
303  hit_layFree(lay_v);
304  hit_topFree(topo);
305  hit_shapeFree(shape);
306  hit_tileFree(M);
307  hit_tileFree(b);
308  hit_tileFree(c);
309 
310  hit_comFinalize();
311 
312  return 0;
313 }
314 
315 
#define hit_shape(nd,...)
Definition: hit_sshape.h:175
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
HitLayout lay_v
Definition: mmult_bit.c:57
#define hit_tileNewType(baseType)
Definition: hit_tile.h:163
#define norm(vv)
#define hit_cShapeEdgeTarget(s, edge)
void hit_comOpSumDouble(void *, void *, int *, HitType *)
Definition: hit_com.c:2665
#define hit_cShapeColumnIterator(var, shape, row)
Definition: hit_cshape.h:403
HitOp op_sum
Definition: mg.c:176
#define hit_mcTileElemIteratorAt(var, iterX, iterY)
Definition: hit_mctile.h:159
#define hit_Rank
Definition: hit_com.h:140
int iter
Definition: mmult_bit.c:68
#define hit_tileElemAt(var, ndims,...)
Definition: hit_tile.h:519
#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
#define hit_cShapeNColsRow(s, row)
Definition: hit_cshape.h:210
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_cShapeCard(shape, dim)
Definition: hit_cshape.h:108
#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_patMatMult(pattern, lay, origin_matrix, matrix, tilePSend, tilePRecv, baseType)
Definition: hit_pattern.h:210
#define hit_clockWorldReduce(c)
Definition: hit_utils.h:156
#define hit_cShapeAddElem(shapep, x, y)
Definition: hit_cshape.h:435
#define hit_fileHBMatrixRead(hbfile)
Definition: hit_file.h:89
void hit_comInit(int *pargc, char **pargv[])
Definition: hit_com.c:111
HitShape shape
#define hit_cShapeRowIterator(var, shape)
Definition: hit_cshape.h:381
HitShape HIT_CSR_SHAPE_NULL
Definition: hit_shape.c:70
#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
#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_mcTileDomainShapeAlloc(var, baseType, shape)
Definition: hit_mctile.h:97
#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
double vector_norm(HitTile_double v)
Definition: mmult_bit.c:104