summaryrefslogtreecommitdiffstats
path: root/dis/Matrix/ver2/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'dis/Matrix/ver2/matrix.c')
-rwxr-xr-xdis/Matrix/ver2/matrix.c79
1 files changed, 38 insertions, 41 deletions
diff --git a/dis/Matrix/ver2/matrix.c b/dis/Matrix/ver2/matrix.c
index 957d7c5..ffa7cb7 100755
--- a/dis/Matrix/ver2/matrix.c
+++ b/dis/Matrix/ver2/matrix.c
@@ -48,7 +48,7 @@
48#define MIN_DIM 1 48#define MIN_DIM 1
49#define MAX_DIM 32768 49#define MAX_DIM 32768
50#define MAX_ITERATIONS 65536 50#define MAX_ITERATIONS 65536
51#define MIN_TOLERANCE 0.000007 51#define MIN_TOLERANCE 1e-7//0.000007
52#define MAX_TOLERANCE 0.5 52#define MAX_TOLERANCE 0.5
53#define MIN_NUMBER -3.4e10/dim 53#define MIN_NUMBER -3.4e10/dim
54#define MAX_NUMBER 3.4e10/dim 54#define MAX_NUMBER 3.4e10/dim
@@ -61,8 +61,6 @@
61 */ 61 */
62 62
63static int dim; 63static int dim;
64int argc;
65char** argv;
66 64
67/* 65/*
68 * matrix * vector 66 * matrix * vector
@@ -312,7 +310,14 @@ void biConjugateGradient(double *value,
312 int maxIterations, 310 int maxIterations,
313 double *actualError, 311 double *actualError,
314 int *actualIteration, 312 int *actualIteration,
315 int dim) 313 int dim,
314 // Start internal matrixes
315 double *vectorP,
316 double *vectorR,
317 double *nextVectorR,
318 double *tmpVector1,
319 double *tmpVector2,
320 double *tmpVector3)
316 /* 321 /*
317 * in the code, we use a lot of temparary vectors and variables 322 * in the code, we use a lot of temparary vectors and variables
318 * this is just for simple and clear 323 * this is just for simple and clear
@@ -321,31 +326,18 @@ void biConjugateGradient(double *value,
321 * 326 *
322 */ 327 */
323{ 328{
324 double *vectorR;
325 double *vectorP, *matrixAvectorP, *nextVectorR;
326 double error; 329 double error;
327 int iteration; 330 int iteration;
328 double alpha, beta; 331 double alpha, beta;
329 332
330 double *tmpVector1, *tmpVector2, *tmpVector3;
331 double tmpValue1, tmpValue2; 333 double tmpValue1, tmpValue2;
332 int i; 334 int i;
333 int l; 335 int l;
334 int ll; 336 int ll;
335 SET_UP
336 337
337 alpha = 0; 338 alpha = 0;
338 beta = 0; 339 beta = 0;
339 340
340 vectorP = (double *)malloc(dim*sizeof(double));
341 vectorR = (double *)malloc(dim*sizeof(double));
342 nextVectorR = (double *)malloc(dim*sizeof(double));
343 vectorX = (double *)malloc(dim*sizeof(double));
344
345 tmpVector1 = (double *)malloc(dim*sizeof(double));
346 tmpVector2 = (double *)malloc(dim*sizeof(double));
347 tmpVector3 = (double *)malloc(dim*sizeof(double));
348
349 /* 341 /*
350 * vectorR = vectorB - matrixA*vectorX 342 * vectorR = vectorB - matrixA*vectorX
351 */ 343 */
@@ -369,7 +361,6 @@ void biConjugateGradient(double *value,
369 iteration = 0; 361 iteration = 0;
370 362
371 while ((iteration < maxIterations) && (error > errorTolerance)){ 363 while ((iteration < maxIterations) && (error > errorTolerance)){
372 START_LOOP
373 364
374 /* 365 /*
375 * alpha = (transpose(vectorR) * vectorR) / 366 * alpha = (transpose(vectorR) * vectorR) /
@@ -434,7 +425,6 @@ void biConjugateGradient(double *value,
434 error = vectorValue(tmpVector1)/vectorValue(vectorB); 425 error = vectorValue(tmpVector1)/vectorValue(vectorB);
435 426
436 iteration++; 427 iteration++;
437 STOP_LOOP
438 } 428 }
439 429
440 *actualError = error; 430 *actualError = error;
@@ -446,7 +436,6 @@ void biConjugateGradient(double *value,
446 436
447 free(vectorR); 437 free(vectorR);
448 free(vectorP); 438 free(vectorP);
449 WRITE_TO_FILE
450 439
451 return; 440 return;
452} 441}
@@ -515,10 +504,8 @@ void create_CRS(double *matrixA,
515} 504}
516 505
517 506
518int main(int _argc, char** _argv) 507int main(int argc, char** argv)
519{ 508{
520 argc = _argc;
521 argv = _argv;
522 int seed; 509 int seed;
523 int numberNonzero; 510 int numberNonzero;
524 int maxIterations; 511 int maxIterations;
@@ -536,18 +523,22 @@ int main(int _argc, char** _argv)
536 double *value; 523 double *value;
537 int *col_ind; 524 int *col_ind;
538 int *row_start; 525 int *row_start;
539 int sum; 526 double sum;
540 int k; 527 int k;
528 // Internal vects for biConj
529 double *vectorR, *vectorP, *nextVectorR;
530 double *tmpVector1, *tmpVector2, *tmpVector3;
531 SET_UP
541 532
542 fscanf(stdin, "%d %d %d %d %f", 533 assert(fscanf(stdin, "%d %d %d %d %f",
543 &seed, &dim, &numberNonzero,&maxIterations,&errorTolerance); 534 &seed, &dim, &numberNonzero,&maxIterations,&errorTolerance) == 5);
544 assert((seed > MIN_SEED) && (seed < MAX_SEED)); 535 assert((seed > MIN_SEED) && (seed < MAX_SEED));
545 assert((dim > MIN_DIM) && (dim < MAX_DIM)); 536 assert((dim > MIN_DIM) && (dim < MAX_DIM));
546 assert((numberNonzero > dim) && (numberNonzero < dim*dim)); 537 assert((numberNonzero > dim) && (numberNonzero < dim*dim));
547 assert((maxIterations > 0) && (maxIterations < MAX_ITERATIONS)); 538 assert((maxIterations > 0) && (maxIterations < MAX_ITERATIONS));
548 assert((errorTolerance > MIN_TOLERANCE) && (errorTolerance < MAX_TOLERANCE)); 539 assert((errorTolerance > MIN_TOLERANCE) && (errorTolerance < MAX_TOLERANCE));
549 540
550 matrixA = (double *)malloc(dim*dim*sizeof(double )); 541 matrixA = (double *)malloc(dim*dim*sizeof(double));
551 vectorB = (double *)malloc(dim*sizeof(double)); 542 vectorB = (double *)malloc(dim*sizeof(double));
552 vectorX = (double *)malloc(dim*sizeof(double)); 543 vectorX = (double *)malloc(dim*sizeof(double));
553 544
@@ -555,40 +546,46 @@ int main(int _argc, char** _argv)
555 col_ind = (int *)malloc((numberNonzero+dim)*sizeof(int)); 546 col_ind = (int *)malloc((numberNonzero+dim)*sizeof(int));
556 row_start = (int *)malloc((dim+1)*sizeof(int)); 547 row_start = (int *)malloc((dim+1)*sizeof(int));
557 548
549 // Internal matricies for biConj
550 vectorP = (double *)malloc(dim*sizeof(double));
551 vectorR = (double *)malloc(dim*sizeof(double));
552 nextVectorR = (double *)malloc(dim*sizeof(double));
553 tmpVector1 = (double *)malloc(dim*sizeof(double));
554 tmpVector2 = (double *)malloc(dim*sizeof(double));
555 tmpVector3 = (double *)malloc(dim*sizeof(double));
556
558 randInit(seed); 557 randInit(seed);
559 558
559 START_LOOP
560 initMatrix(matrixA, dim, numberNonzero); 560 initMatrix(matrixA, dim, numberNonzero);
561 561
562 create_CRS(matrixA, value, col_ind, row_start, dim, numberNonzero); 562 create_CRS(matrixA, value, col_ind, row_start, dim, numberNonzero);
563 563
564 initVector(vectorB, dim); 564 initVector(vectorB, dim);
565 zeroVector(vectorX, dim); 565 zeroVector(vectorX, dim);
566 printf(" after init\n");
567 566
568 beginTime = time(NULL); 567 beginTime = time(NULL);
569 568
570 actualError = 0; 569 actualError = 0;
571 actualIteration = 0; 570 actualIteration = 0;
572 571
573 biConjugateGradient(value, col_ind, row_start, vectorB, vectorX, errorTolerance, 572 biConjugateGradient(value, col_ind, row_start, vectorB, vectorX, errorTolerance,
574 maxIterations, 573 maxIterations,
575 &actualError, &actualIteration, dim); 574 &actualError, &actualIteration, dim,
576 575 vectorP, vectorR, nextVectorR, tmpVector1, tmpVector2, tmpVector3);
577
578
579 endTime = time(NULL) - beginTime;
580
581 576
577 STOP_LOOP
578 endTime = time(NULL);
582 579
583 sum = 0; 580 sum = 0;
584 for (k=1; k<dim; k++){ 581 for (k=1; k<dim; k++){
585 sum += sum + *(vectorX + k); 582 sum += sum + *(vectorX + k);
586 } 583 }
587 584
588 fprintf(stdout, "sum = %d, actualError = %e, actualIteration = %d\n", sum, actualError, actualIteration); 585 fprintf(stdout, "sum = %f, actualError = %e, actualIteration = %d\n", sum, actualError, actualIteration);
589 fprintf(stdout, "total time = %u sec. \n", (unsigned int)endTime); 586 fprintf(stderr, "time for matrix stressmark = %f seconds.\n", difftime(endTime, beginTime));
590 587
588 WRITE_TO_FILE
591 return(0); 589 return(0);
592 } 590}
593 591
594