Guest User

Untitled

a guest
Dec 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1. #include <uC++.h>
  2.  
  3. #include <iostream>
  4. #include <iomanip>
  5. #include <fstream>
  6.  
  7. using namespace std;
  8. #include <cstdlib>
  9.  
  10. _Task RowMultiplier {
  11.     private:
  12.         int *xRow;
  13.         int **Y;
  14.         int *zRow;
  15.         int xcyr;
  16.         int ycols;
  17.  
  18.     void main() {
  19.         for (int c = 0; c < ycols; c++) {
  20.             zRow[c] = 0;
  21.             for (int i = 0; i < xcyr; i++) {
  22.                 zRow[c] += xRow[i] * Y[i][c];
  23.             }
  24.         }
  25.     }
  26.  
  27.     public:
  28.         RowMultiplier(int *xRow, int **Y, int*zRow, int xcyr, int ycols) :
  29.             xRow(xRow), Y(Y), zRow(zRow), xcyr(xcyr), ycols(ycols) {}
  30. };
  31.  
  32. void usage(char **argv) {
  33.     cerr << "Usage: " << argv[0] << " xrows xcols-yrows ycols "
  34.          << " [ X-matrix-file Y-matrix-file ]" << endl;
  35.     exit(EXIT_FAILURE);
  36. }
  37.  
  38. void matrixmultiply( int *Z[ ], int *X[ ], int xr, int xc, int *Y[ ], int yc ) {
  39.     RowMultiplier *multipliers[xr];
  40.     for (int r = 0; r < xr; r++) {
  41.         multipliers[r] = new RowMultiplier(X[r], Y, Z[r], xc, yc);
  42.     }
  43.  
  44.     for (int r = 0; r < xr; r++) {
  45.         delete multipliers[r];
  46.     }
  47. }
  48.  
  49. int** newMatrix(int rows, int cols) {
  50.     int ** matrix = new int*[rows];
  51.     for (int r = 0; r < rows; r++) {
  52.         matrix[r] = new int[cols];
  53.     }
  54.     return matrix;
  55. }
  56.  
  57. void freeMatrix(int **matrix, int rows) {
  58.     for (int r = 0; r < rows; r++) {
  59.         delete [] matrix[r];
  60.     }
  61.     delete [] matrix;
  62. }
  63.  
  64. int** generateMatrix(int rows, int cols) {
  65.     int **matrix = newMatrix(rows, cols);
  66.     for (int r = 0; r < rows; r++) {
  67.         for (int c = 0; c < cols; c++) {
  68.             matrix[r][c] = 37;
  69.         }
  70.     }
  71.     return matrix;
  72. }
  73.  
  74. int** readMatrix(ifstream &matrixFile, int rows, int cols) {
  75.     int **matrix = newMatrix(rows, cols);
  76.     for (int r = 0; r < rows; r++) {
  77.         for (int c = 0; c < cols; c++) {
  78.             matrixFile >> matrix[r][c];
  79.         }
  80.     }
  81.     return matrix;
  82. }
  83.  
  84. void printMatricies(int *X[], int *Y[], int *Z[], int xrows, int xcols, int ycols) {
  85.     const int cellSize = 8;
  86.  
  87.     int yrows = xcols;
  88.     int marginY = (cellSize + 1) * xcols + 4;
  89.     for (int r = 0; r < yrows; r++) {
  90.         cout << setw(marginY) << "|";
  91.         for (int c = 0; c < ycols; c++) {
  92.             cout << " " << setw(cellSize) << Y[r][c];
  93.         }
  94.         cout << endl;
  95.     }
  96.  
  97.     for (int i = 0; i < marginY - 1; i++) {
  98.         cout << '-';
  99.     }
  100.     cout << '*';
  101.     for (int i = 0; i < (cellSize + 1) * ycols; i++) {
  102.         cout << '-';
  103.     }
  104.     cout << endl;
  105.  
  106.     for (int r = 0; r < xrows; r++) {
  107.         for (int c = 0; c < xcols; c++) {
  108.             cout << setw(cellSize) << X[r][c] << " ";
  109.         }
  110.         cout << "   |";
  111.         for (int c = 0; c < ycols; c++) {
  112.             cout << " " << setw(cellSize) << Z[r][c];
  113.         }
  114.         cout << endl;
  115.     }
  116. }
  117.  
  118. void uMain::main() {
  119.     if (argc < 4 || argc > 6 || argc == 5) {
  120.         usage(argv);
  121.     } // if
  122.  
  123.     int xrows = atoi(argv[1]);
  124.     int xcols = atoi(argv[2]);
  125.     int yrows = xcols;
  126.     int ycols = atoi(argv[3]);
  127.  
  128.     if (xrows <= 0 || xcols <= 0 || ycols <= 0) {
  129.         usage(argv);
  130.     }
  131.  
  132.     int **X;
  133.     int **Y;
  134.     bool specifiedMatrixFiles;
  135.     if (argc == 6) {
  136.         specifiedMatrixFiles = true;
  137.         try {
  138.             ifstream matrixFile(argv[4]);
  139.             X = readMatrix(matrixFile, xrows, xcols);
  140.         } catch (uFile::Failure &e) {
  141.             cerr << "Error! Could not open file \"" << argv[4] << "\"" << endl;
  142.             usage(argv);
  143.         }
  144.  
  145.         try {
  146.             ifstream matrixFile(argv[5]);
  147.             Y = readMatrix(matrixFile, yrows, ycols);
  148.         } catch (uFile::Failure &e) {
  149.             cerr << "Error! Could not open file \"" << argv[4] << "\"" << endl;
  150.             usage(argv);
  151.         }
  152.     } else {
  153.         specifiedMatrixFiles = false;
  154.         X = generateMatrix(xrows, xcols);
  155.         Y = generateMatrix(yrows, ycols);
  156.     }
  157.  
  158. #ifdef PARALLEL
  159.     uProcessor p[xrows - 1] __attribute__((unused)); // number of CPUs
  160. #endif
  161.  
  162.     int **Z = newMatrix(xrows, ycols);
  163.     matrixmultiply(Z, X, xrows, xcols, Y, ycols);
  164.  
  165.     if (specifiedMatrixFiles) {
  166.         printMatricies(X, Y, Z, xrows, xcols, ycols);
  167.     }
  168.  
  169.     freeMatrix(X, xrows);
  170.     freeMatrix(Y, yrows);
  171.     freeMatrix(Z, xrows);
  172. }
Add Comment
Please, Sign In to add comment