-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort_cpu.cpp
More file actions
93 lines (82 loc) · 3.37 KB
/
bubble_sort_cpu.cpp
File metadata and controls
93 lines (82 loc) · 3.37 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
#include <iostream>
#include <fstream>
#include <vector>
#include <chrono>
#include <omp.h>
#define MATRIX_SIZE 128
// Function for Bubble Sort on each row (Parallelized)
void bubbleSort_cpu(std::vector<int>& matrix, int n) {
#pragma omp parallel for
for (int i = 0; i < n; i++) {
bool swapped;
for (int j = 0; j < n - 1; j++) {
swapped = false;
for (int k = 0; k < n - j - 1; k++) {
if (matrix[i * n + k] > matrix[i * n + k + 1]) {
std::swap(matrix[i * n + k], matrix[i * n + k + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}
}
// Function to find min - max values (Parallelized)
void findMinMax_cpu(const std::vector<int>& matrix, int n, std::vector<int>& min_vals, std::vector<int>& max_vals) {
#pragma omp parallel for
for (int i = 0; i < n; i++) {
int minVal = matrix[i * n];
int maxVal = matrix[i * n];
for (int j = 1; j < n; j++) {
int val = matrix[i * n + j];
if (val < minVal) minVal = val;
if (val > maxVal) maxVal = val;
}
min_vals[i] = minVal;
max_vals[i] = maxVal;
}
}
int main() {
std::ifstream file("random_matrix_128x128.csv");
std::vector<int> host_matrix;
int value;
while (file >> value) {
host_matrix.push_back(value);
if (file.peek() == ',') file.ignore();
}
file.close();
std::vector<int> min_vals(MATRIX_SIZE);
std::vector<int> max_vals(MATRIX_SIZE);
//---------------------------------------------------------------------------------------------------------------------
// CPU Sorting
auto start = std::chrono::high_resolution_clock::now();
bubbleSort_cpu(host_matrix, MATRIX_SIZE);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> cpu_sort_duration = end - start;
//---------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------
// Find Min & Max
start = std::chrono::high_resolution_clock::now();
findMinMax_cpu(host_matrix, MATRIX_SIZE, min_vals, max_vals);
end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> cpu_minmax_duration = end - start;
//---------------------------------------------------------------------------------------------------------------------
// Save sorted matrix to a file
std::ofstream sorted_file("sorted_bubble_cpu.csv");
for (int i = 0; i < MATRIX_SIZE; i++) {
for (int j = 0; j < MATRIX_SIZE; j++) {
sorted_file << host_matrix[i * MATRIX_SIZE + j];
if (j < MATRIX_SIZE - 1) sorted_file << ",";
}
sorted_file << "\n";
}
sorted_file.close();
std::cout << "Row-wise Min/Max values:\n";
for (int i = 0; i < MATRIX_SIZE; i++) {
std::cout << "Row " << i + 1 << ": Min = " << min_vals[i] << ", Max = " << max_vals[i] << "\n";
}
std::cout << "--------------------------------" << std::endl;
std::cout << "CPU Sorting completed in: " << cpu_sort_duration.count() << " ms\n";
std::cout << "CPU Min/Max computation completed in: " << cpu_minmax_duration.count() << " ms\n";
return 0;
}