-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.cpp
More file actions
126 lines (94 loc) · 3.33 KB
/
c.cpp
File metadata and controls
126 lines (94 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include<bits/stdc++.h>
#include<omp.h>
using namespace std;
#define ll long long
void disp(vector<vector<int>> m){
for(auto it: m){
for(auto i : it){
cout<<i<<" ";
}cout<<endl;
}
}
void storeMatrixToFile(const std::vector<std::vector<int>>& matrix, const std::string& filename) {
std::ofstream outputFile(filename);
if (!outputFile.is_open()) {
std::cerr << "Unable to open file: " << filename << std::endl;
return;
}
for (const auto& row : matrix) {
for (int element : row) {
outputFile << element << " ";
}
outputFile << std::endl;
}
outputFile.close();
}
std::vector<std::vector<int>> readMatrixFromFile(const std::string& filename) {
std::ifstream inputFile(filename);
if (!inputFile.is_open()) {
std::cerr << "Unable to open file: " << filename << std::endl;
return {};
}
std::vector<std::vector<int>> matrix;
int num;
while (inputFile >> num) {
matrix.push_back({num});
while (inputFile.peek() == ' ') {
inputFile.get();
inputFile >> num;
matrix.back().push_back(num);
}
}
inputFile.close();
return matrix;
}
/// ////////////////////////////////////
/// //////////////////////////////
#include <vector>
// Function to transpose a submatrix
void transpose_submatrix(std::vector<std::vector<int>>& matrix, std::vector<std::vector<int>>& dest, int start_row, int start_col, int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
dest[start_col + j][start_row + i] = matrix[start_row + i][start_col + j];
}
}
}
// Function to transpose a matrix by iterating through blocks
void transpose_by_blocks(std::vector<std::vector<int>>& src, std::vector<std::vector<int>>& dest, int block_size) {
int rows = src.size();
int cols = src[0].size();
for (int i = 0; i < rows; i += block_size) {
for (int j = 0; j < cols; j += block_size) {
int block_rows = std::min(block_size, rows - i);
int block_cols = std::min(block_size, cols - j);
transpose_submatrix(src, dest, i, j, block_rows, block_cols);
}
}
}
int main(){
std::string filenam = "in.txt";
/////////////////
vector<vector<int>> src=readMatrixFromFile(filenam);
ll r=src.size(),c=src[0].size();
cout<<"c.cpp "<<"r "<<r<<" c "<<c<<endl;
// disp(src);
cout<<" Reading above\n";
ll r_t=c,c_t=r;
vector<vector<int>> dest(r_t,vector<int>(c_t)); //transpose(src);
// Get the current time point
cout<<"calling transpose\n";
auto start = std::chrono::high_resolution_clock::now();
/////////////////////////////////////////////////
// src r,c ; dest r_t,c_t
// dest=transpose()
transpose_by_blocks(src,dest,286); //(81290)^0.5
/////////////////////
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
// Convert the duration to milliseconds and output
std::cout << "Time taken: " << duration.count()*1000 << " miliseconds" << std::endl;
// disp(dest);
std::string filename = "out.txt";
storeMatrixToFile(dest, filename);
std::cout << "Matrix has been stored in the file: " << filename << std::endl;
}