• API
• FAQ
• Tools
• Trends
• Archive
SHARE
TWEET

# Fixing http://pastebin.com/HqHtFpq9 to use contiguous memory

Omnifarious Nov 26th, 2012 113 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <cstdlib>  // for random number generator
3. #include <ctime>
4. #include <fstream> // for writing to the file
5. using namespace std;
6.
7. class SquareMatrix {
8.  public:
9.    class Row;
10.    friend class Row;
11.
12.    SquareMatrix(unsigned int side)
13.         : side_(side), values_(new int[side*side])
14.    {
15.    }
16.    ~SquareMatrix() { delete [] values_; }
17.    SquareMatrix(const SquareMatrix &) = delete;
18.    SquareMatrix(SquareMatrix &&other)
19.         : side_(other.side_), values_(other.values_)
20.    {
21.       other.values_ = nullptr;
22.    }
23.
24.    const SquareMatrix &operator =(const SquareMatrix &) = delete;
25.    const SquareMatrix &operator =(SquareMatrix &&) = delete;
26.
27.    unsigned int size() const { return side_ ; }
28.
29.    inline Row operator [](unsigned int row);
30.    inline const Row operator [](unsigned int row) const;
31.
32.  private:
33.    const unsigned int side_;
34.    int *values_;
35. };
36.
37. class SquareMatrix::Row {
38.    friend class SquareMatrix;
39.
40.  public:
41.    const Row &operator =(const Row &) = delete;
42.    const Row &operator =(Row &&) = delete;
43.
44.    int &operator [](unsigned int col) {
45.       return matrix_.values_[row_ * matrix_.side_ + col];
46.    }
47.    int operator [](unsigned int col) const {
48.       return matrix_.values_[row_ * matrix_.side_ + col];
49.    }
50.
51.  private:
52.    SquareMatrix &matrix_;
53.    const unsigned int row_;
54.
55.    Row(Row &&) = default;
56.    Row(SquareMatrix &m, unsigned int row)
57.         : matrix_(m), row_(row)
58.    {
59.    }
60. };
61.
62. inline SquareMatrix::Row SquareMatrix::operator [](unsigned int row)
63. {
64.    return Row(*this, row);
65. }
66.
67. inline const SquareMatrix::Row SquareMatrix::operator [](unsigned int row) const
68. {
69.    return Row(*const_cast<SquareMatrix *>(this), row);
70. }
71.
72. int main()
73. {
74.
75.    int array_sizes[6] = {32,64,128,256,512,1024};
76.
77.    int m;
78.    ofstream results; //output the results
79.    results.open ("results.txt");
80.
81.    for(int i = 0; i < 6; i++){
82.       for (int k = 0; k < 3; k ++){
83.
84.          clock_t start = clock();
85.          m = array_sizes[i];
86.
87.          {
88.             SquareMatrix A(m), B(m), product(m);
89.
90.             // Initialize A[I][K]
91.             // I x K == row * inner
92.             for(int row = 0; row < m; row++) {
93.                for(int inner = 0; inner < m; inner++){
94.                   A[row][inner] = 5;
95.                }
96.             }
97.
98.
99.             for(int row = 0; row < m; row++) {
100.                for(int inner = 0; inner < m; inner++){
101.                   B[row][inner] = 3;
102.                   //cout << B[row][inner] << " ";
103.                }
104.                //cout << "\n";
105.             }
106.
107.             // main routine for multiplication
108.             for(int row = 0; row < m; row++) {
109.                for(int col = 0; col < m; col++) {
110.                   product[row][col] = 0;
111.                   for(int inner = 0; inner < m; inner++) {
112.                      product[row][col] += A[row][inner] * B[inner][col];
113.                   }
114.                   //cout << product[row][col] << " ";
115.                }
116.                //cout << "\n";
117.             }
118.          }
119.
120.          clock_t end = clock();
121.          double cpu_time = static_cast<double>(end - start)/CLOCKS_PER_SEC;
122.
123.          results << cpu_time << endl;
124.       }
125.       results << m << endl;
126.    }
127.
128.    results.close();
129.    return 0;
130. }
RAW Paste Data
Top