vakho

Temporary Cpp Code

Mar 13th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.02 KB | None | 0 0
  1. #define USE_EXISTING_YS true
  2. #define MIN_VALUE -100
  3. #define MAX_VALUE +100
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8. #include <string>
  9.  
  10. #include <chrono>
  11. #include <random>
  12.  
  13. #include "boost\filesystem.hpp"
  14. #include "boost\regex.hpp"
  15. #include "boost\algorithm\string\predicate.hpp"
  16.  
  17. #include "mmio.c"
  18.  
  19. double* cholesky(double *A, int n)
  20. {
  21.     double *L = (double*)calloc(n * n, sizeof(double));
  22.     if (L == NULL) {
  23.         exit(EXIT_FAILURE);
  24.     }
  25.  
  26.     for (int i = 0; i < n; i++) {
  27.         for (int j = 0; j < (i + 1); j++) {
  28.             double s = 0;
  29.             for (int k = 0; k < j; k++)
  30.                 s += L[i * n + k] * L[j * n + k];
  31.             L[i * n + j] = (i == j) ?
  32.                 sqrt(A[i * n + i] - s) :
  33.                 (1.0 / L[j * n + j] * (A[i * n + j] - s));
  34.         }
  35.     }
  36.     return L;
  37. }
  38.  
  39. void show_matrix(double *A, int n) {
  40.     for (int i = 0; i < n; i++) {
  41.         for (int j = 0; j < n; j++)
  42.             printf("%2.5f ", A[i * n + j]);
  43.         printf("\n");
  44.     }
  45. }
  46.  
  47. void show_vector(double *v, int n) {
  48.     for (int i = 0; i < n; i++)    
  49.         printf("%2.5f ", v[i]);
  50.  
  51.     printf("\n");
  52. }
  53.  
  54. double* forwardSubstitution(double *L, double *b, int n) {
  55.  
  56.     double *y = (double*)calloc(n, sizeof(double));
  57.     if (y == NULL)
  58.         exit(EXIT_FAILURE);
  59.  
  60.     for (int i = 0; i < n; i++) {
  61.         y[i] = b[i];
  62.  
  63.         for (int j = 0; j <= i - 1; j++)
  64.             y[i] -= L[i + j * n] * y[j];
  65.  
  66.         y[i] /= L[i + i * n];
  67.     }
  68.     return y;
  69. }
  70.  
  71. double* backwardSubstitution(double *U, double *b, int n, bool isTransposed = true) {
  72.  
  73.     double *x = (double*)calloc(n, sizeof(double));
  74.     if (x == NULL)
  75.         exit(EXIT_FAILURE);
  76.  
  77.     // Same codes but with i and j swapped for matrix U
  78.     if (isTransposed) {
  79.         for (int i = n - 1; i >= 0; i--) {
  80.             x[i] = b[i];
  81.  
  82.             for (int j = i + 1; j < n; j++)
  83.                 x[i] -= U[i + j * n] * x[j];
  84.  
  85.             x[i] /= U[i + i * n];
  86.         }
  87.     }
  88.     else {
  89.         for (int i = n - 1; i >= 0; i--) {
  90.             x[i] = b[i];
  91.  
  92.             for (int j = i + 1; j < n; j++)
  93.                 x[i] -= U[j + i * n] * x[j];
  94.  
  95.             x[i] /= U[i + i * n];
  96.         }
  97.     }  
  98.     return x;
  99. }
  100.  
  101. int main()
  102. {
  103.     using namespace std;
  104.     using namespace boost;
  105.     using namespace boost::filesystem;
  106.     using namespace boost::algorithm;
  107.     using namespace std::chrono;
  108.  
  109.     time_point<high_resolution_clock> start, finish;
  110.    
  111.     // reusable time variable
  112.     long long time;
  113.  
  114.     // Random number generator for Ys
  115.     random_device rd; // Random at each execution
  116.     default_random_engine dre(rd());
  117.  
  118.     // Get the files with extension .mtx (specify full or relative path)
  119.     path current_dir("..\\SparseMatrixProject\\matrices\\small");
  120.     regex pattern("(.*\\.mtx)");
  121.  
  122.     // Iterate and read matrices
  123.     for (recursive_directory_iterator iter(current_dir), end; iter != end; ++iter)
  124.     {
  125.         string name = iter->path().filename().string();
  126.  
  127.         if (regex_match(name, pattern))
  128.         {
  129.             // Or if it ends with 'b.mtx'!
  130.             if (ends_with(name, "b.mtx") || ends_with(name, "coord.mtx")) {
  131.                 continue;
  132.             }
  133.  
  134.             // Ignore this matrix
  135.             if (starts_with(name, "bibd")) {
  136.                 continue;
  137.             }
  138.  
  139.             FILE *f;
  140.             int ret_code;
  141.             MM_typecode matcode;
  142.             int M, N, nz;
  143.             int i, *I, *J;
  144.             double *val, *inlineMat;
  145.  
  146.             string filePath = iter->path().string();
  147.  
  148.             // Open file
  149.             if ((f = fopen(filePath.c_str(), "r")) == NULL)
  150.                 exit(1);
  151.  
  152.             if (mm_read_banner(f, &matcode) != 0)
  153.             {
  154.                 printf("Could not process Matrix Market banner.\n");
  155.                 exit(1);
  156.             }
  157.  
  158.             /*  This is how one can screen matrix types if their application */
  159.             /*  only supports a subset of the Matrix Market data types.      */
  160.             /*if (mm_is_complex(matcode) && mm_is_matrix(matcode) &&
  161.                 mm_is_sparse(matcode))
  162.             {
  163.                 printf("Sorry, this application does not support ");
  164.                 printf("Market Market type: [%s]\n", mm_typecode_to_str(matcode));
  165.                 exit(1);
  166.             }*/
  167.  
  168.             /* find out size of sparse matrix .... */
  169.             if ((ret_code = mm_read_mtx_crd_size(f, &M, &N, &nz)) != 0)
  170.                 exit(1);
  171.            
  172.             // Ys
  173.             double *b;
  174.             b = (double *)malloc(sizeof(double)*N);
  175.             //x = (double *)calloc(N, sizeof(double)); // allocates memory and fills with zeroes.
  176.  
  177.             if (USE_EXISTING_YS)
  178.             {
  179.                 string ysPath = filePath.substr(0, filePath.find(".mtx", 0)) + "_Ys.txt";
  180.                
  181.                 // Open Ys file
  182.                 FILE *ysFile;
  183.                 if ((ysFile = fopen(ysPath.c_str(), "r")) == NULL)
  184.                     exit(1);
  185.  
  186.                 for (size_t i = 0; i < N; i++)
  187.                     fscanf(ysFile, "%lg\n", &b[i]);
  188.                
  189.                 fclose(ysFile);
  190.             }
  191.             else {
  192.                 uniform_real_distribution<double> di(MIN_VALUE, MAX_VALUE);
  193.                 for (size_t i = 0; i < N; i++)
  194.                     b[i] = di(dre);            
  195.             }
  196.  
  197.             /* reseve memory for matrices */
  198.             I = (int *)malloc(nz * sizeof(int));
  199.             J = (int *)malloc(nz * sizeof(int));
  200.             val = (double *)malloc(nz * sizeof(double));
  201.             inlineMat = (double *)malloc(N * N * sizeof(double));
  202.  
  203.             /* NOTE: when reading in doubles, ANSI C requires the use of the "l"  */
  204.             /*   specifier as in "%lg", "%lf", "%le", otherwise errors will occur */
  205.             /*  (ANSI C X3.159-1989, Sec. 4.9.6.2, p. 136 lines 13-15)            */
  206.             for (i = 0; i < nz; i++)
  207.             {
  208.                 fscanf(f, "%d %d %lg\n", &I[i], &J[i], &val[i]);
  209.                 I[i]--;  /* adjust from 1-based to 0-based */
  210.                 J[i]--;
  211.  
  212.                 inlineMat[I[i] + J[i] * N] = val[i];
  213.             }
  214.  
  215.             if (f != stdin) fclose(f);
  216.  
  217.             /************************/
  218.             /* now write out matrix */
  219.             /************************/
  220.             mm_write_banner(stdout, matcode);
  221.             mm_write_mtx_crd_size(stdout, M, N, nz);
  222.             /*for (i = 0; i < nz; i++) {
  223.                 fprintf(stdout, "%d %d %20.19g\n", I[i] + 1, J[i] + 1, val[i]);
  224.             }*/
  225.  
  226.             // [1] Solve with "Cholevsky Decomposition"
  227.             start = high_resolution_clock::now();
  228.            
  229.             // TODO compute cholevsky
  230.            
  231.             finish = high_resolution_clock::now();
  232.             time = duration_cast<milliseconds>(finish - start).count();
  233.             fprintf(stdout, "Cholevsky: %d milliseconds. \n", time);
  234.  
  235.             // [2] Solve with "Conjugate Gradient"
  236.             start = high_resolution_clock::now();
  237.  
  238.             // TODO compute cg
  239.  
  240.             finish = high_resolution_clock::now();
  241.             time = duration_cast<milliseconds>(finish - start).count();
  242.             fprintf(stdout, "CG: %d milliseconds.\n", time);
  243.  
  244.             free(I);
  245.             free(J);
  246.             free(val);
  247.  
  248.             free(b);
  249.  
  250.             printf("\n");
  251.         }
  252.     }
  253. }
Advertisement
Add Comment
Please, Sign In to add comment