summaryrefslogtreecommitdiffstats
path: root/dis/Matrix/ver2/matrix.c
blob: 957d7c5e99a3190bc0f8caf64ca76a43f35323a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/* Please note:
 * This code is the optimized version of the first version of Matrix 
 * Stressmark. It uses less temporary vectors and vsariables, thus reduce 
 * memory allocation/deallocation overhead. the simulation is faster 
 */
/*
 *  Sample code for the DIS Matrix Stressmark
 *
 * This source code is the completely correct source code based on 
 * the example codes provided by Atlantic Aerospace Division, Titan 
 * Systems Corporation, 2000.
 * 
 * If you just compile and generate the executables from this source 
 * code, this code would be enough. However, if you wish to get a complete 
 * understanding of this stressmark, it is strongly suggested that you
 * read the Benchmark Analysis and Specifications Document Version 1.0
 * before going on since the detailed comments are given in this documents.
 * the comments are not repeated here.
 */

/*
 *  The Sparse Matrix Storage is implemented by Compact Row Storage Scheme
 *  In the code, the data is first generated by randomNonzeroFloat()
 *  the data is first stored in a full-space matrix with size of dim*dim
 *  then the data is transfered to the Compact Row Matrix, 
 *  the data value is kept in *value,
 *  the columns corresponding to the value are stored in *col_ind,
 *  the start element of each row is stored in *row_start.
 */
 
/* 
 * Please note: 
 * the total number of data is numberNonzero +dim
 * among which, NumberNonzero because this is symmetric matrix
 * dim because the diagonal elements
 */

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include "DISstressmarkRNG.h"
#include "extra.h"

#define MIN_SEED -2147483647
#define MAX_SEED -1
#define MIN_DIM  1
#define MAX_DIM  32768
#define MAX_ITERATIONS 65536
#define MIN_TOLERANCE 0.000007
#define MAX_TOLERANCE 0.5
#define MIN_NUMBER   -3.4e10/dim
#define MAX_NUMBER 3.4e10/dim
#define EPSI   1.0e-10
#define MIN_DIG_NUMBER 1.0e-10
#define MAX_DIG_NUMBER 3.4e10

/*
 *  External variable, dimension
 */

static int dim;
int argc;
char** argv;

/*      
 *  matrix * vector     
 */

void matrixMulvector(double *value, 
		     int *col_ind, 
		     int *row_start,
		     double  *vector,
		     double *out)
{  
  int l, ll;
  double  sum;
  int tmp_rs, tmp_re;
 
  for (l=0; l<dim; l++){  
     *(out + l) = 0;
     tmp_rs = row_start[l];
    
     if (tmp_rs != -1){
      tmp_re = row_start[l+1];   /*
				  *get the start and ending elements of 
				  *  each row
				 */
      for (ll=tmp_rs; ll<tmp_re; ll++){
	*(out + l) += value[ll]*vector[col_ind[ll]];
      }
    }
  }
  return; 
}


/*
 *    vector1 - vector2
 */

void  vectorSub(double  *vector1, double  *vector2, double *vector){

  int l;

  for (l=0; l<dim; l++){
    *(vector + l) = *(vector1 + l) - *(vector2 + l);
  }
  return; 
}


/*
 * vector1 + vector2
 */

void vectorAdd(double  *vector1, double  *vector2, double *vector){

  int l;

  for (l=0; l<dim; l++){
    *(vector + l) = *(vector1 + l) + *(vector2 + l);
  }
  return; 
} 

/* 
 * vector1 * vector2
 */

double  vectorMul(double  *vector1, double  *vector2){

  int l;
  double  product;

  product = 0;

  for (l=0; l<dim; l++){
    product += (*(vector1 + l))*(*(vector2 + l));

  }
  return product;
} 

/*
 * /vector/
 */

double  vectorValue(double  *vector){

  double  value;
  int l;

  value = 0;

  for (l=0; l<dim; l++){
    value += (*(vector + l)) * (*(vector + l));
  }

  return (sqrt(value));
}

/*
 * transpose(vector)
 * In fact, we return the original vector here
 */

void  transpose(double  *vector, double *vect){

  int l;

  for (l=0; l<dim; l++){
    *(vect+l) = *(vector+l);
  }
  return; 
}

/*
 * value * <vector>
 */
void valueMulvector(double  value, double  *vector, double *vect){

  int l;
  int lll, i;
  double tmp;

  for (l=0; l<dim; l++){
    *(vect + l) = *(vector + l) * value;
  }
  return;
}
  
/*
 * generate the data distributed sparsely in matrix
 */

void initMatrix(double  *matrix, int dim, int numberNonzero){
  
  int k, l, ll;
  int i, j;

  int lll;
  double sum;

  for (k=0; k< dim*dim; k++){
    *(matrix + k) = 0;
  }

  for (l=0; l<numberNonzero/2; l++){

    i = randomUInt(1, dim-1);
    j = randomUInt(0, i-1);

    while (*(matrix + i*dim + j) != 0){
      
     i++;
       if (i == dim){
       j++;
       if (j == dim-1){
	 j = 0;
	 i = 1;
       }
       else{
	 i = j+1;
       }
     }
    }
  
    if (*(matrix + i*dim + j) == 0){
      *(matrix + i*dim + j) = (double )randomNonZeroFloat(MIN_NUMBER, 
							  MAX_NUMBER, 
							  EPSI);
      *(matrix + j*dim + i) = *(matrix + i*dim + j);
    }
  }
 
  for (ll=0; ll<dim; ll++){


    
    *(matrix + ll*dim + ll) = (double )randomNonZeroFloat(-MAX_DIG_NUMBER,
							  MAX_DIG_NUMBER, 
							  MIN_DIG_NUMBER);
    
    sum = 0;

    for (lll=0; lll<dim; lll++){
      if (lll != ll){
	sum += *(matrix + lll*dim + ll);
      }
    }
    
    if (*(matrix + ll*dim + ll) < sum ){
      *(matrix + ll*dim + ll) += sum;
   }
  }

  return;
}

/*
 * generate the data value in the vectors
 */
 
void initVector(double *vector, int dim){

  int l;
  
  for (l=0; l<dim; l++){
    *(vector + l) = (double )randomFloat (MIN_NUMBER, MAX_NUMBER);
  }

  return;
}

/*
 * make a vector contains value of zero
 */

void zeroVector(double *vector, int dim){
  int l;
  
  for (l=0; l<dim; l++){
    *(vector + l) = 0;
  }
  return;
}

/*
 * return a vector which is the copy of the vect
 */

void equalVector(double *vect, double *vect1){

  int l;

  for (l=0; l<dim; l++){
    *(vect1+l) = *(vect+l);
  }
  return; 
}



void biConjugateGradient(double *value,
			 int *col_ind,
			 int *row_start,
			 double *vectorB, 
			 double *vectorX,
			 double errorTolerance,
			 int maxIterations,
			 double *actualError,
			 int *actualIteration,
			 int dim)
     /* 
      * in the code, we use a lot of temparary vectors and variables
      * this is just for simple and clear
      * you can optimize these temporary variables and vectors 
      * based on your need
      *
      */
{
  double *vectorR;
  double *vectorP, *matrixAvectorP, *nextVectorR;
  double  error;
  int iteration;
  double  alpha, beta;

  double  *tmpVector1, *tmpVector2, *tmpVector3;
  double   tmpValue1, tmpValue2; 
  int i;
  int l;
  int ll;
  SET_UP

  alpha = 0;
  beta = 0;

  vectorP = (double *)malloc(dim*sizeof(double));
  vectorR = (double *)malloc(dim*sizeof(double));
  nextVectorR = (double *)malloc(dim*sizeof(double));
  vectorX = (double *)malloc(dim*sizeof(double));

  tmpVector1 = (double *)malloc(dim*sizeof(double));
  tmpVector2 = (double *)malloc(dim*sizeof(double));
  tmpVector3 = (double *)malloc(dim*sizeof(double));

  /*
   * vectorR = vectorB - matrixA*vectorX
   */
  matrixMulvector(value,col_ind, row_start, vectorX, tmpVector1);

  vectorSub(vectorB, tmpVector1, vectorR);

  /*
   * vectorP = vectorR
   */

  equalVector(vectorR, vectorP);

  /*
   * error = |matrixA * vectorX - vectorB| / |vectorB|
   */
  vectorSub(tmpVector1, vectorB, tmpVector1); 

  error = vectorValue(tmpVector1)/vectorValue(vectorB);

  iteration = 0;

  while ((iteration < maxIterations) && (error > errorTolerance)){
    START_LOOP
   
    /* 
     *   alpha = (transpose(vectorR) * vectorR) /
     *           (transpose(vectorP) * (matrixA * vectorP)
     */

    matrixMulvector(value, col_ind, row_start, vectorP, tmpVector1);  
    transpose(vectorR, tmpVector2); 
    transpose(vectorP, tmpVector3);
    tmpValue1 = vectorMul(tmpVector3, tmpVector1);
    tmpValue2 = vectorMul(tmpVector2, vectorR);
    alpha = tmpValue2/tmpValue1;
 
    /* 
     * nextVectorR = vectorR - alpha*(matrixA * vectorP)
     */

    valueMulvector(alpha, tmpVector1, tmpVector2);
    vectorSub(vectorR, tmpVector2, tmpVector1);
    equalVector(tmpVector1, nextVectorR);
 
    /* 
     * beta = (transpose(nextVectorR) * nextVectorR) /
     *           (transpose(vectorR) * vectorR)
     */

    transpose(nextVectorR, tmpVector3);
    tmpValue1 = vectorMul(tmpVector3, nextVectorR);
    transpose(vectorR, tmpVector2);
    tmpValue2 = vectorMul(tmpVector2, vectorR);
    beta = tmpValue1/tmpValue2;

    /*
     * vectorX = vectorX + alpha * vectorP
     */
    valueMulvector(alpha, vectorP, tmpVector1);       
    vectorAdd(vectorX,tmpVector1, vectorX);

    /* 
     *vectorP = nextVectorR + beta*vectorP
     */       
    valueMulvector(beta, vectorP, tmpVector1);   
    vectorAdd(nextVectorR, tmpVector1, tmpVector1);

    for (ll=0; ll<dim; ll++){
      *(vectorP + ll) = *(tmpVector1 + ll);
    }

    /*
     * vectorR = nextVectorR
     */
    
    for (l=0; l<dim; l++){
    *(vectorR+l) = *(nextVectorR+l);
    }

    /* 
     * error = |matrixA * vectorX - vectorB| / |vectorB|
     */
    matrixMulvector(value, col_ind,row_start, vectorX, tmpVector1);
    vectorSub(tmpVector1,vectorB,tmpVector1);
    error = vectorValue(tmpVector1)/vectorValue(vectorB);

    iteration++;
    STOP_LOOP
  }

  *actualError = error;
  *actualIteration = iteration;

  free(tmpVector1);
  free(tmpVector2);
  free(tmpVector3);

  free(vectorR);
  free(vectorP);
  WRITE_TO_FILE

  return;
}
  
/*
 * This is the function to transfer the data from the matrix of dense storage 
 * to Compact Row Storage
 */
void create_CRS(double *matrixA,
		double *value, 
		int *col_ind, 
		int *row_start,
		int dim,
		int numberNonzero)
{

  int i, j, k;
  int cnt;
  double tmp;

  /* 
   *initialize the row_start
   */
     
  for(k=0; k<dim; k++){
    row_start[k] = -1;
  }
  
  /* 
   * make the end of the last row to be numberNonzero + dim.
   */

  row_start[dim] = numberNonzero+dim;
  
  /*
   * initialize the col_ind
   */

  for (k=0; k<numberNonzero+dim; k++){
    col_ind[k] = -1;
  }


  cnt = 0;

  for (i=0;  (cnt<numberNonzero+dim)&&(i<dim); i++){
    for (j=0; (cnt<numberNonzero+dim)&&(j<dim); j++){
      
      tmp = *(matrixA + i*dim + j);

         if (tmp!=0){

	   value[cnt] = tmp;
	   col_ind[cnt] = j;
       
	    if (row_start[i] == -1)
	      row_start[i] = cnt;
	    
	    cnt += 1;
	 }	
    }
  }
  row_start[i] = cnt;

  return;
}


int main(int _argc, char** _argv)
{
  argc = _argc;
  argv = _argv;
  int seed;
  int numberNonzero;
  int maxIterations;
  float errorTolerance;
  double  actualError;
  int actualIteration;
  
  time_t beginTime;
  time_t endTime;

  double  *matrixA;
  double  *vectorB;
  double  *vectorX;

  double *value;
  int    *col_ind;
  int    *row_start;
  int sum;
  int k;

  fscanf(stdin, "%d %d %d %d %f", 
	 &seed, &dim, &numberNonzero,&maxIterations,&errorTolerance);
  assert((seed > MIN_SEED) && (seed < MAX_SEED));
  assert((dim > MIN_DIM) && (dim < MAX_DIM));
  assert((numberNonzero > dim) && (numberNonzero < dim*dim));
  assert((maxIterations > 0) && (maxIterations < MAX_ITERATIONS));
  assert((errorTolerance > MIN_TOLERANCE) && (errorTolerance < MAX_TOLERANCE));
  
  matrixA = (double  *)malloc(dim*dim*sizeof(double ));
  vectorB = (double *)malloc(dim*sizeof(double));
  vectorX = (double *)malloc(dim*sizeof(double));

  value = (double *)malloc((numberNonzero+dim)*sizeof(double));
  col_ind = (int *)malloc((numberNonzero+dim)*sizeof(int));
  row_start = (int *)malloc((dim+1)*sizeof(int));

  randInit(seed);

  initMatrix(matrixA, dim, numberNonzero);
  
  create_CRS(matrixA, value, col_ind, row_start, dim, numberNonzero);

  initVector(vectorB, dim);
  zeroVector(vectorX, dim);
  printf(" after init\n");

  beginTime = time(NULL);
  
  actualError = 0;
  actualIteration = 0;
   
  biConjugateGradient(value, col_ind, row_start, vectorB, vectorX, errorTolerance,
		      maxIterations,
		      &actualError, &actualIteration, dim);
  


  endTime = time(NULL) - beginTime;
  


  sum = 0;
  for (k=1; k<dim; k++){
    sum += sum + *(vectorX + k);
  }
  
  fprintf(stdout, "sum = %d, actualError = %e, actualIteration = %d\n", sum, actualError, actualIteration);
  fprintf(stdout, "total time = %u sec. \n", (unsigned int)endTime);

  return(0);
    }