Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <omp.h>
  6.  
  7. using namespace std;
  8. #ifndef ROWS
  9. #define ROWS 1024
  10. #endif
  11. #ifndef COLS
  12. #define COLS 1024
  13. #endif
  14.  
  15. void gramSchmidt(float Q[][COLS], const int rows, const int cols)
  16. {
  17.     for(int k=0; k < cols; k++)
  18.     {
  19.         double tmp = 0.;
  20.         #pragma omp parallel for reduction(+:tmp)
  21.         for(int i=0; i < rows; i++) tmp +=  (Q[i][k] * Q[i][k]);
  22.         tmp = sqrt(tmp);
  23.  
  24.         #pragma omp parallel for
  25.         for(int i=0; i < rows; i++) Q[i][k] /= tmp;
  26.  
  27.         #pragma omp parallel for reduction(+:tmp)
  28.         for(int j=k+1; j < cols; j++)
  29.         {
  30.             tmp=0.;
  31.             for(int i=0; i < rows; i++) tmp += Q[i][k] * Q[i][j];
  32.             for(int i=0; i < rows; i++) Q[i][j] -= tmp * Q[i][k];
  33.         }
  34.   }
  35. }
  36.  
  37. void gramSchmidtSeq(float Q[][COLS], const int rows, const int cols)
  38. {
  39.     for(int k=0; k < cols; k++)
  40.     {
  41.         double tmp = 0.;
  42.  
  43.         for(int i=0; i < rows; i++) tmp +=  (Q[i][k] * Q[i][k]);
  44.         tmp = sqrt(tmp);
  45.  
  46.  
  47.         for(int i=0; i < rows; i++) Q[i][k] /= tmp;
  48.  
  49.  
  50.         for(int j=k+1; j < cols; j++)
  51.         {
  52.             tmp=0.;
  53.             for(int i=0; i < rows; i++) tmp += Q[i][k] * Q[i][j];
  54.             for(int i=0; i < rows; i++) Q[i][j] -= tmp * Q[i][k];
  55.         }
  56.   }
  57. }
  58. void checkOctave(float A[][COLS], int rows, int cols)
  59. {
  60.   int found_error=0;
  61.   for(int c1=0; c1 < cols; c1++)
  62.     for(int c2=c1; c2 < cols; c2++) {
  63.       double sum=0.;
  64.       for(int i=0; i < rows; i++) sum += A[i][c1] * A[i][c2];
  65.       if(c1 == c2) { // should be near 1 (unit length)
  66.     if(sum < 0.9) {
  67.       printf("Failed unit length: %d %d %g\n", c1,c2,sum);
  68.       found_error = 1;
  69.       exit(1);
  70.     }
  71.       } else { // should be very small (orthogonal)
  72.     if(sum > 0.1) {
  73.       printf("Failed orthogonal  %d %d %g\n", c1,c2,sum);
  74.       found_error = 1;
  75.       exit(1);
  76.     }
  77.       }
  78.   }
  79.   if(!found_error) printf("Check OK!\n");
  80. }
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84.   int rows=ROWS;
  85.   int cols=COLS;
  86.   float (*A)[COLS] = (float(*)[COLS])malloc(sizeof(float)*rows*cols);
  87.   float (*B)[COLS] = (float(*)[COLS])malloc(sizeof(float)*rows*cols);
  88.   // fill matrix A with random numbers
  89.   for(int i=0; i < rows; i++)
  90.     for(int j=0; j < cols; j++){
  91.       A[i][j] = (float)rand()/ (float)RAND_MAX;
  92.       B[i][j] = A[i][j];
  93.     }
  94.   printf("Done with init!\n");
  95.   double startTime;
  96.   double endTime;
  97.  
  98.  /* startTime = omp_get_wtime();
  99.   gramSchmidtSeq(B, rows, cols);
  100.   endTime = omp_get_wtime();
  101.  
  102.   printf("runtime %d %d matrix %g\n",rows, cols, (endTime-startTime));*/
  103.   startTime = omp_get_wtime();
  104.   gramSchmidt(A, rows, cols);
  105.   endTime = omp_get_wtime();
  106.  
  107.   printf("runtime %d %d matrix %g\n",rows, cols, (endTime-startTime));
  108.  
  109.   startTime = omp_get_wtime();
  110.   gramSchmidtSeq(B, rows, cols);
  111.   endTime = omp_get_wtime();
  112.  
  113.   printf("runtime %d %d matrix %g\n",rows, cols, (endTime-startTime));
  114.   checkOctave(A, rows, cols);
  115.   free(A);
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement