Guest User

D&C Zavolskov/Zemlyanskiy

a guest
Nov 21st, 2016
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <omp.h>
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include<math.h>
  8. #include <fstream>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11.  
  12. using namespace std;
  13.  
  14. void foo (const char *in, const char *out)
  15. {
  16.     int t;
  17.   printf ("%s -> %s: start\n", in, out);
  18.   for( int i = 0; i < 1000000;i++){
  19.     t +=1;
  20.     t -=1;
  21.   }
  22.  
  23.   /* Эмуляция того, что данная функция работает долго. На системах, отличных от linux,
  24.    * данная функция может называться по другому */
  25.   //sleep (1);
  26.  
  27.   printf ("%s -> %s: finish\n", in, out);
  28. }
  29.  
  30. int copys(double ** t_or,double ** t_out,int count,int shift)
  31. {
  32.     for( int i = shift; i < count + shift; i++)
  33.     {
  34.         for( int j = shift; j < count + shift; j++)
  35.         {
  36.             t_out[i - shift][j-shift]=t_or[i][j];
  37.         }
  38.     }
  39.  
  40.     return 0;
  41.     //из большой в маленькую.
  42. }
  43.  
  44. double** create_array(int rows, int cols)
  45. {
  46.     double** array=new double*[rows];
  47.     for (int i=0; i<rows; i++)
  48.         array[i]=new double[cols];
  49.  
  50.     for( int i = 0 ; i < rows; i++){
  51.         for( int j = 0; j < cols; j++)
  52.             array[i][j] = 0;
  53.     }
  54.     return array;
  55. }
  56.  
  57. void input_array(double ** array,int rows, int cols)
  58. {
  59.     srand(time(NULL));
  60.     for (int i=0; i<rows; i++)
  61.         for (int j=0; j<cols; j++)
  62.             array[i][j]=rand() % 300;
  63. }
  64.  
  65. void input_array_s(double ** array,int rows)
  66. {
  67.     srand(time(NULL));
  68.     array[0][0]=rand() % 200;
  69.     array[1][0]=rand() % 100;
  70.     array[0][1]=array[1][0];
  71.     for (int i=1; i<rows - 1; i++){
  72.         array[i][i]=array[i-1][i-1] - rand() % 200;
  73.         array[i+1][i]=rand() % 100;
  74.         array[i][i+1]=array[i+1][i];
  75.     }
  76.     array[rows-1][rows-1] = array[rows-2][rows-2] - rand() % 200;
  77. }
  78.  
  79. void fresh_array(double ** array,int rows, int cols)
  80. {
  81.     srand(time(NULL));
  82.     for (int i=0; i<rows; i++)
  83.         for (int j=0; j<cols; j++)
  84.             array[i][j]=0.0;
  85. }
  86.  
  87. void print_array(double ** array, int rows, int cols)
  88. {
  89.  
  90.     /*ofstream fout("cppstudio2.txt",ios::app);
  91.     fout << "=";
  92.     fout << rows;
  93.     fout << "/";
  94.     fout.close();*/
  95.     for (int i=0; i<rows; i++)
  96.     {
  97.         for (int j=0; j<cols; j++)
  98.         printf("%ff ", array[i][j]);
  99.         printf("\n");
  100.     }
  101.  
  102. }
  103.  
  104. void delete_array(double **array,int rows)
  105. {
  106.     for (int i=0; i<rows; i++) delete [] array[i];
  107.     delete [] array;
  108. }
  109.  
  110. void MultiplyWithOutAMP(double ** aMatrix,double ** bMatrix,double ** product, int x,int y,int z)
  111. {
  112.     for (int row = 0; row < x; row++)
  113.     {
  114.         for (int col = 0; col < z; col++)
  115.         {
  116.             for (int inner = 0; inner < y; inner++)
  117.             {
  118.                 product[row][col] += aMatrix[row][inner] * bMatrix[inner][col];
  119.             }
  120.         }
  121.     }
  122. }
  123.  
  124. float f(float x, double ** v, double ** u, double bm, int n)
  125. {
  126.     double res = 0;
  127.     for( int i = 0; i < n; i++){
  128.         res += u[i][0]*u[i][0]/(v[i][i] - x);
  129.     }
  130.     return res*bm + 1;
  131. }
  132.  
  133. float df (float x, double ** v, double ** u, double bm, int n)
  134. {
  135.     double res = 0;
  136.     for( int i = 0; i < n; i++){
  137.         res += u[i][0]*u[i][0]/((v[i][i] - x)*(v[i][i] - x));
  138.     }
  139.     return res*bm;
  140. }
  141.  
  142. double newton( double x0,double err, int maxmitr, double ** v, double ** u, double bm, int n)
  143. {
  144.     int itr;
  145.     double t = x0 + 0.135;
  146.     double h,x1;
  147.     //x0+=0.001;
  148.     //printf("\nEnter x0, allowed error and maximum iterations\n");
  149.     //scanf("%f %f %d", &x0, &allerr, &maxmitr);
  150.     for (itr=1; itr<=maxmitr; itr++)
  151.     {
  152.         //printf("-");
  153.         h=0.0001*f(x0,v,u,bm,n)/df(x0,v,u,bm,n);
  154.         x1=x0-h;
  155.         //printf(" At Iteration no. %3d, x = %9.6f\n", itr, x1);
  156.         if (fabs(x1) > 100000){
  157.             return t;
  158.         }
  159.         if (fabs(h) < err )
  160.         {
  161.             //printf("After %3d iterations, root = %8.6f\n", itr, x1);
  162.             return x1;
  163.         }
  164.         x0=x1;
  165.     }
  166.     //printf(" The required solution does not converge or iterations are insufficient\n");
  167.     return t;
  168. }
  169.  
  170. /*
  171.  
  172. double newton( double x0,double err, int maxmitr, double ** v, double ** u, double bm, int n) {
  173.   x0+=0.001;
  174.   double x1  = x0 - f(x0,v,u,bm,n)/df(x0,v,u,bm,n);
  175.   while (fabs(x1-x0)>err) {
  176.     x0 = x1;
  177.     x1 = x1 - f(x1,v,u,bm,n)/df(x1,v,u,bm,n);
  178.   }
  179.   return x1;
  180. }
  181. */
  182.  
  183. void eugenVal(double ** u,double ** vt,double ** v, double bm,int n){
  184.     //print_array(u,2,1);
  185.     //printf("\n");
  186.     //print_array(vt,2,1);
  187.     //printf("\n");
  188.     //printf("+");
  189.     for(int i = 0;i < n; i++){
  190.         v[i][i] = newton(vt[i][i] + 0.1,0.001,80,vt,u,bm,n);
  191.     }
  192.     //v[n-1][n-1] = newton(vt[n-1][n-1] +0.01,0.0001,80,vt,u,bm,n);
  193.     return;
  194. }
  195. void uf(double ** q1, double ** q2, double ** u,int n){
  196.  
  197.  
  198.     for( int i = 0; i < n/2; i++){
  199.         //printf("%d - %d = %ff\n", n/2 - 1, i,u[i][0]);
  200.         u[i][0]=q1[n/2 - 1][i];
  201.     }
  202.  
  203.     for( int i = n/2; i < n; i++){
  204.         u[i][0] = q2[0][i - n/2];
  205.     }
  206.  
  207. }
  208.  
  209. int vf(double ** v1, double ** v2, double ** v,int n){
  210.  
  211.  
  212.     for( int i = 0; i < n/2; i++){
  213.         v[i][i]=v1[i][i];
  214.     }
  215.  
  216.     for( int i = n/2; i < n; i++){
  217.         v[i][i] = v2[i-n/2][i-n/2];
  218.     }
  219.     return 0;
  220. }
  221.  
  222. void trans(double ** u, double ** ut, int n){
  223.     for(int i = 0; i < n; i++)
  224.         ut[0][i] = u[i][0];
  225.     return;
  226. }
  227.  
  228. void di(double ** d,double ** q1,double ** q2,double ** vt,double ** u,double bm, int n){
  229.     double ** ut;
  230.     ut = create_array(1,n);
  231.     trans(u,ut,n);
  232.     MultiplyWithOutAMP(u,ut,d,n,n,n);
  233.     for(int i = 0; i < n; i++){
  234.         d[i][i] += vt[i][i];
  235.     }
  236.     delete_array(ut,n);
  237.     return;
  238. }
  239.  
  240. void eugenVecTemp(double ** qt,double ** vt,double ** v,double ** u,int n){
  241.     double ** temp = create_array(n,n);
  242.     double ** temp2 = create_array(n,1);
  243.     for(int i = 0; i < n; i++){
  244.         fresh_array(temp,n,n);
  245.         fresh_array(temp2,n,n);
  246.         for(int j = 0; j < n ; j++){
  247.             temp[j][j] = 1/(vt[j][j] - v[i][i]);
  248.         }
  249.             MultiplyWithOutAMP(temp,u,temp2,n,n,1);
  250.         for(int k = 0; k < n ; k++){
  251.             qt[k][i]=temp2[k][0];
  252.         }
  253.     }
  254.     delete_array(temp,n);
  255.     delete_array(temp2,n);
  256.     return;
  257. }
  258.  
  259. void eugenVec(double ** q,double **  qt,double ** q1,double ** q2, int n){
  260.     double ** temp = create_array(n,n);
  261.  
  262.     for( int i = 0; i < n/2; i++){
  263.         temp[i][i]=q1[i][i];
  264.     }
  265.  
  266.     for( int i = n/2; i < n; i++){
  267.         temp[i][i] = q2[i-n/2][i-n/2];
  268.     }
  269.  
  270.     MultiplyWithOutAMP(temp,qt,q,n,n,n);
  271.     delete_array(temp,n);
  272.     return;
  273. }
  274.  
  275. int proc(double ** t,double ** q, double ** v, int n)
  276. {
  277.  
  278.     if( n == 1)
  279.     {
  280.         **q = 1;
  281.         **v = **t;
  282.         return 1;
  283.     }
  284.     else
  285.     {
  286.  
  287.         double bm = t[n/2][n/2 - 1];
  288.  
  289.         double ** t1;
  290.         t1 = create_array(n/2,n/2);
  291.  
  292.         double ** q1;
  293.         q1 = create_array(n/2,n/2);
  294.  
  295.         double ** v1;
  296.         v1 = create_array(n/2,n/2);
  297.  
  298.         copys(t,t1,n/2,0);
  299.         t1[n/2-1][n/2-1] -= bm;
  300.  
  301.         double ** t2;
  302.         t2 = create_array(n/2,n/2);
  303.  
  304.         double ** q2;
  305.         q2 = create_array(n/2,n/2);
  306.  
  307.  
  308.         double ** v2;
  309.         v2 = create_array(n/2,n/2);
  310.  
  311.  
  312.         copys(t,t2,n/2,n/2);
  313.  
  314.         t2[0][0] -= bm;
  315.  
  316.         omp_set_num_threads(1);
  317.  
  318.         #pragma omp parallel sections
  319.             {
  320.             #pragma omp section
  321.                   {
  322.                     proc(t1,q1,v1,n/2);
  323.                   }
  324.             #pragma omp section
  325.                   {
  326.                     proc(t2,q2,v2,n/2);
  327.                   }
  328.  
  329.               }
  330.  
  331.         double ** u = create_array(n,1);
  332.         uf(q1,q2,u,n);
  333.         double ** vt = create_array(n,n);
  334.         vf(v1,v2,vt,n);
  335.  
  336.         eugenVal(u,vt,v,bm,n);
  337.  
  338.  
  339.         double ** qt = create_array(n,n);
  340.  
  341.         eugenVecTemp(qt,vt,v,u,n);
  342.  
  343.         eugenVec(q, qt,q1,q2,n);
  344.  
  345.         delete_array(q1,n/2);
  346.         delete_array(q2,n/2);
  347.         delete_array(v1,n/2);
  348.         delete_array(v2,n/2);
  349.         delete_array(t1,n/2);
  350.         delete_array(t2,n/2);
  351.  
  352.         return 0;
  353.     }
  354. }
  355.  
  356.  
  357. int main()
  358. {
  359.     double ** t;
  360.     struct timeval start, end;
  361.     gettimeofday(&start, NULL);
  362.     int cout = 8;
  363.     t = create_array(cout,cout);
  364.     input_array_s(t,cout);
  365.  
  366.  
  367.     print_array(t,cout,cout);
  368.     double ** v;
  369.     v = create_array(cout,cout);
  370.  
  371.  
  372.     double ** q;
  373.     q = create_array(cout,cout);
  374.  
  375.  
  376.     proc(t,q,v,cout);
  377. gettimeofday(&end, NULL);
  378.     print_array(v,cout,cout);
  379.     printf("\n-----------\n");
  380.     print_array(q,cout,cout);
  381.  
  382. double delta = ((end.tv_sec  - start.tv_sec) * 1000000u +
  383.          end.tv_usec - start.tv_usec) / 1.e6;
  384.     printf("\n cout - %d, proc - 1, time - %ff",cout,delta);
  385.  
  386.    
  387.     return 0;
  388. }
Advertisement
Add Comment
Please, Sign In to add comment