Skip to content

Commit eb53088

Browse files
authored
Add files via upload
0 parents  commit eb53088

File tree

9 files changed

+1679
-0
lines changed

9 files changed

+1679
-0
lines changed

add.cu

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <cuda.h>
4+
5+
// CUDA Kernel for vector addition
6+
__global__ void vectorAdd(float *A, float *B, float *C, int n) {
7+
int id = blockIdx.x * blockDim.x + threadIdx.x;
8+
if (id < n) C[id] = A[id] + B[id];
9+
}
10+
11+
int main() {
12+
int n;
13+
std::cout << "Enter the number of elements: ";
14+
std::cin >> n;
15+
16+
// Allocate host memory
17+
std::vector<float> h_A(n), h_B(n), h_C(n);
18+
19+
// Take user inputs for vectors
20+
std::cout << "Enter elements for vector A:\n";
21+
for (int i = 0; i < n; i++) std::cin >> h_A[i];
22+
std::cout << "Enter elements for vector B:\n";
23+
for (int i = 0; i < n; i++) std::cin >> h_B[i];
24+
25+
// Allocate device memory
26+
float *d_A, *d_B, *d_C;
27+
cudaMalloc(&d_A, n * sizeof(float));
28+
cudaMalloc(&d_B, n * sizeof(float));
29+
cudaMalloc(&d_C, n * sizeof(float));
30+
31+
// Copy data to device
32+
cudaMemcpy(d_A, h_A.data(), n * sizeof(float), cudaMemcpyHostToDevice);
33+
cudaMemcpy(d_B, h_B.data(), n * sizeof(float), cudaMemcpyHostToDevice);
34+
35+
// Launch kernel
36+
vectorAdd<<<(n + 255) / 256, 256>>>(d_A, d_B, d_C, n);
37+
38+
// Copy result back
39+
cudaMemcpy(h_C.data(), d_C, n * sizeof(float), cudaMemcpyDeviceToHost);
40+
41+
// Print the result
42+
std::cout << "Result vector C:\n";
43+
for (int i = 0; i < n; i++) std::cout << h_C[i] << " ";
44+
std::cout << "\n";
45+
46+
// Free memory
47+
cudaFree(d_A);
48+
cudaFree(d_B);
49+
cudaFree(d_C);
50+
51+
return 0;
52+
}

bfsdfs.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
#include <stack>
5+
#include <omp.h>
6+
7+
using namespace std;
8+
9+
void parallelBFS(const vector<vector<int>>& graph, int startNode) {
10+
int n = graph.size();
11+
vector<bool> visited(n, false);
12+
queue<int> q;
13+
14+
q.push(startNode);
15+
visited[startNode] = true;
16+
17+
cout << "\nParallel BFS: ";
18+
while (!q.empty()) {
19+
int node = q.front();
20+
q.pop();
21+
cout << node << " ";
22+
23+
#pragma omp parallel for
24+
for (int i = 0; i < graph[node].size(); ++i) {
25+
int neighbor = graph[node][i];
26+
if (!visited[neighbor]) {
27+
#pragma omp critical
28+
{
29+
if (!visited[neighbor]) {
30+
visited[neighbor] = true;
31+
q.push(neighbor);
32+
}
33+
}
34+
}
35+
}
36+
}
37+
cout << endl;
38+
}
39+
40+
void parallelDFS(const vector<vector<int>>& graph, int startNode) {
41+
int n = graph.size();
42+
vector<bool> visited(n, false);
43+
stack<int> s;
44+
45+
s.push(startNode);
46+
visited[startNode] = true;
47+
48+
cout << "\nParallel DFS: ";
49+
while (!s.empty()) {
50+
int node = s.top();
51+
s.pop();
52+
cout << node << " ";
53+
54+
#pragma omp parallel for
55+
for (int i = 0; i < graph[node].size(); ++i) {
56+
int neighbor = graph[node][i];
57+
if (!visited[neighbor]) {
58+
#pragma omp critical
59+
{
60+
if (!visited[neighbor]) {
61+
visited[neighbor] = true;
62+
s.push(neighbor);
63+
}
64+
}
65+
}
66+
}
67+
}
68+
cout << endl;
69+
}
70+
71+
int main() {
72+
int n, m;
73+
cout << "Enter the number of nodes: ";
74+
cin >> n;
75+
cout << "Enter the number of edges: ";
76+
cin >> m;
77+
78+
vector<vector<int>> graph(n);
79+
cout << "Enter the edges (node1 node2):\n";
80+
for (int i = 0; i < m; ++i) {
81+
int u, v;
82+
cin >> u >> v;
83+
graph[u].push_back(v);
84+
graph[v].push_back(u); // For undirected graph
85+
}
86+
87+
int startNode;
88+
cout << "Enter the start node for BFS and DFS: ";
89+
cin >> startNode;
90+
91+
parallelBFS(graph, startNode);
92+
parallelDFS(graph, startNode);
93+
94+
return 0;
95+
}
96+
97+
// 5
98+
// 4
99+
// 0 1
100+
// 0 2
101+
// 1 3
102+
// 1 4
103+
// 0
104+
// Parallel BFS: 0 1 2 3 4
105+
// Parallel DFS: 0 2 1 4 3

boston.csv

Lines changed: 507 additions & 0 deletions
Large diffs are not rendered by default.

bsms.cpp

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <omp.h>
4+
#include <chrono>
5+
#include <cstdlib>
6+
7+
using namespace std;
8+
using namespace std::chrono;
9+
10+
// Sequential Bubble Sort
11+
void bubbleSortSequential(vector<int> &arr)
12+
{
13+
int n = arr.size();
14+
for (int i = 0; i < n - 1; i++)
15+
{
16+
for (int j = 0; j < n - i - 1; j++)
17+
{
18+
if (arr[j] > arr[j + 1])
19+
{
20+
swap(arr[j], arr[j + 1]);
21+
}
22+
}
23+
}
24+
}
25+
26+
// Parallel Bubble Sort using OpenMP
27+
void bubbleSortParallel(vector<int> &arr)
28+
{
29+
int n = arr.size();
30+
#pragma omp parallel for
31+
for (int i = 0; i < n - 1; i++)
32+
{
33+
for (int j = 0; j < n - i - 1; j++)
34+
{
35+
if (arr[j] > arr[j + 1])
36+
{
37+
swap(arr[j], arr[j + 1]);
38+
}
39+
}
40+
}
41+
}
42+
43+
// Merge function for Merge Sort
44+
void merge(vector<int> &arr, int left, int mid, int right)
45+
{
46+
int n1 = mid - left + 1;
47+
int n2 = right - mid;
48+
49+
vector<int> L(n1), R(n2);
50+
51+
for (int i = 0; i < n1; i++)
52+
L[i] = arr[left + i];
53+
for (int i = 0; i < n2; i++)
54+
R[i] = arr[mid + 1 + i];
55+
56+
int i = 0, j = 0, k = left;
57+
while (i < n1 && j < n2)
58+
{
59+
if (L[i] <= R[j])
60+
arr[k++] = L[i++];
61+
else
62+
arr[k++] = R[j++];
63+
}
64+
65+
while (i < n1)
66+
arr[k++] = L[i++];
67+
while (j < n2)
68+
arr[k++] = R[j++];
69+
}
70+
71+
// Sequential Merge Sort
72+
void mergeSortSequential(vector<int> &arr, int left, int right)
73+
{
74+
if (left < right)
75+
{
76+
int mid = left + (right - left) / 2;
77+
mergeSortSequential(arr, left, mid);
78+
mergeSortSequential(arr, mid + 1, right);
79+
merge(arr, left, mid, right);
80+
}
81+
}
82+
83+
// Parallel Merge Sort using OpenMP
84+
void mergeSortParallel(vector<int> &arr, int left, int right)
85+
{
86+
if (left < right)
87+
{
88+
int mid = left + (right - left) / 2;
89+
90+
#pragma omp parallel sections
91+
{
92+
#pragma omp section
93+
mergeSortParallel(arr, left, mid);
94+
95+
#pragma omp section
96+
mergeSortParallel(arr, mid + 1, right);
97+
}
98+
merge(arr, left, mid, right);
99+
}
100+
}
101+
102+
// Generate a random array of a given size
103+
vector<int> generateRandomArray(int size)
104+
{
105+
vector<int> arr(size);
106+
for (int i = 0; i < size; i++)
107+
arr[i] = rand() % 100000;
108+
return arr;
109+
}
110+
111+
int main()
112+
{
113+
srand(time(0));
114+
115+
// int arraySize = 100;
116+
// vector<int> arr = generateRandomArray(arraySize);
117+
118+
int arraySize = 7;
119+
vector<int> arr = {2, 6, 7, 32, 3, 1, 9};
120+
for (int i = 0; i < arraySize; i++)
121+
{
122+
cout << arr[i] << " ";
123+
}
124+
cout << endl;
125+
126+
// Time Sequential Bubble Sort
127+
vector<int> arrCopy = arr;
128+
auto start = high_resolution_clock::now();
129+
bubbleSortSequential(arrCopy);
130+
auto stop = high_resolution_clock::now();
131+
auto duration = duration_cast<microseconds>(stop - start);
132+
cout << "Sequential Bubble Sort: " << duration.count() << " microseconds" << endl;
133+
134+
// Time Parallel Bubble Sort
135+
arrCopy = arr;
136+
start = high_resolution_clock::now();
137+
bubbleSortParallel(arrCopy);
138+
stop = high_resolution_clock::now();
139+
duration = duration_cast<microseconds>(stop - start);
140+
cout << "Parallel Bubble Sort: " << duration.count() << " microseconds" << endl;
141+
142+
// Time Sequential Merge Sort
143+
arrCopy = arr;
144+
start = high_resolution_clock::now();
145+
mergeSortSequential(arrCopy, 0, arrCopy.size() - 1);
146+
stop = high_resolution_clock::now();
147+
duration = duration_cast<microseconds>(stop - start);
148+
cout << "Sequential Merge Sort: " << duration.count() << " microseconds" << endl;
149+
150+
// Time Parallel Merge Sort
151+
arrCopy = arr;
152+
start = high_resolution_clock::now();
153+
mergeSortParallel(arrCopy, 0, arrCopy.size() - 1);
154+
stop = high_resolution_clock::now();
155+
duration = duration_cast<microseconds>(stop - start);
156+
cout << "Parallel Merge Sort: " << duration.count() << " microseconds" << endl;
157+
158+
for (int i = 0; i < arraySize; i++)
159+
{
160+
cout << arrCopy[i] << " ";
161+
}
162+
cout << endl;
163+
164+
return 0;
165+
}

0 commit comments

Comments
 (0)