-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat
More file actions
335 lines (288 loc) · 8.64 KB
/
mat
File metadata and controls
335 lines (288 loc) · 8.64 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//Quadrant sum
#include<stdio.h>
#include<omp.h>
void main()
{
int N, i, j;
printf("Enter size of matrix:");
scanf("%d", &N);
int matrix[N][N];
printf("Enter matrix elements: \n");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
scanf("%d", &matrix[i][j]);
}
}
printf("Input matrix: \n");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
{
printf("%d", matrix[i][j]);
}
printf("\n");
}
int sumA = 0, sumB = 0, sumC = 0, sumD = 0;
#pragma omp parallel sections reduction(+:sumA, sumB, sumC, sumD)
{
#pragma omp section
{
for(i=0; i<N/2; i++)
{
for(j=0; j<N/2; j++)
{
sumA+=matrix[i][j];
}
}
}
#pragma omp section
{
for(i=0; i<N/2; i++)
{
for(j=N/2; j<N; j++)
{
sumB+=matrix[i][j];
}
}
}
#pragma omp section
{
for(i=N/2; i<N; i++)
{
for(j=0; j<N/2; j++)
{
sumC+=matrix[i][j];
}
}
}
#pragma omp section
{
for(i=N/2; i<N; i++)
{
for(j=N/2; j<N; j++)
{
sumD+=matrix[i][j];
}
}
}
}
printf("SumA= %d", sumA);
printf("SumB= %d", sumB);
printf("SumC= %d", sumC);
printf("SumD= %d", sumD);
}
//-------------------------------------
// Trace of a Matrix using OpenMP
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define SIZE 1000 // Define matrix size
// Function to calculate the trace sequentially
int trace_sequential(int matrix[SIZE][SIZE]) {
int trace = 0;
for (int i = 0; i < SIZE; i++) {
trace += matrix[i][i];
}
return trace;
}
// Function to calculate the trace using OpenMP
int trace_parallel(int matrix[SIZE][SIZE]) {
int trace = 0;
#pragma omp parallel for reduction(+:trace)
for (int i = 0; i < SIZE; i++) {
trace += matrix[i][i];
}
return trace;
}
int main() {
int matrix[SIZE][SIZE];
// Initialize matrix with random values
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
matrix[i][j] = rand() % 100;
}
}
double start, end;
// Sequential Trace Calculation
start = omp_get_wtime();
int trace_seq = trace_sequential(matrix);
end = omp_get_wtime();
printf("Sequential Trace: %d\n", trace_seq);
printf("Sequential Time: %f seconds\n", end - start);
// Parallel Trace Calculation
start = omp_get_wtime();
int trace_par = trace_parallel(matrix);
end = omp_get_wtime();
printf("Parallel Trace: %d\n", trace_par);
printf("Parallel Time: %f seconds\n", end - start);
// Correctness Check
printf("Correctness Check: %s\n", (trace_seq == trace_par) ? "PASS" : "FAIL");
return 0;
}
//--------------------------------------------
// Parallel vs Sequential Matrix Multiplication
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 500 // Matrix size
int A[N][N], B[N][N], C_seq[N][N], C_par[N][N];
int main() {
// Initialize matrices A and B
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = i + j;
B[i][j] = i - j;
C_seq[i][j] = 0;
C_par[i][j] = 0;
}
}
// Sequential Matrix Multiplication
double seq_start = omp_get_wtime();
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
C_seq[i][j] += A[i][k] * B[k][j];
}
}
}
double seq_end = omp_get_wtime();
// Parallel Matrix Multiplication
double par_start = omp_get_wtime();
#pragma omp parallel for collapse(2)
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
for (int k = 0; k < N; k++) {
C_par[i][j] += A[i][k] * B[k][j];
}
}
}
double par_end = omp_get_wtime();
// Verify correctness
int correct = 1;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (C_seq[i][j] != C_par[i][j]) {
correct = 0;
break;
}
}
if (!correct) break;
}
// Print results
printf("Sequential Time: %.6f seconds\n", seq_end - seq_start);
printf("Parallel Time: %.6f seconds\n", par_end - par_start);
printf("Correctness Check: %s\n", correct ? "PASS" : "FAIL");
return 0;
}
//Parallel Matrix Determinant Calculation
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 100
int determinant(int mat[N][N], int n) {
int det = 0;
if (n == 1) return mat[0][0];
int temp[N][N];
int sign = 1;
#pragma omp parallel for reduction(+:det) private(temp, sign)
for (int f = 0; f < n; f++) {
int i = 0, j = 0;
for (int row = 1; row < n; row++) {
for (int col = 0; col < n; col++) {
if (col == f) continue;
temp[i][j++] = mat[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
det += sign * mat[0][f] * determinant(temp, n - 1);
sign = -sign;
}
return det;
}
int main() {
int mat[N][N];
// Initialize matrix with random values
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
mat[i][j] = rand() % 10;
}
}
double start_time = omp_get_wtime();
int det = determinant(mat, N);
double end_time = omp_get_wtime();
printf("Determinant: %d\n", det);
printf("Time taken: %f seconds\n", end_time - start_time);
return 0;
}
// Parallel Convolution of Two Matrices
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 100
#define KERNEL_SIZE 3
void convolution(int input[N][N], int kernel[KERNEL_SIZE][KERNEL_SIZE], int output[N][N]) {
int pad = KERNEL_SIZE / 2;
#pragma omp parallel for
for (int i = pad; i < N - pad; i++) {
for (int j = pad; j < N - pad; j++) {
int sum = 0;
for (int ki = 0; ki < KERNEL_SIZE; ki++) {
for (int kj = 0; kj < KERNEL_SIZE; kj++) {
sum += input[i - pad + ki][j - pad + kj] * kernel[ki][kj];
}
}
output[i][j] = sum;
}
}
}
int main() {
int input[N][N], kernel[KERNEL_SIZE][KERNEL_SIZE], output[N][N];
// Initialize input matrix and kernel
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
input[i][j] = rand() % 10;
}
}
for (int i = 0; i < KERNEL_SIZE; i++) {
for (int j = 0; j < KERNEL_SIZE; j++) {
kernel[i][j] = rand() % 3 - 1; // Random kernel values between -1 and 1
}
}
double start_time = omp_get_wtime();
convolution(input, kernel, output);
double end_time = omp_get_wtime();
printf("Time taken: %f seconds\n", end_time - start_time);
return 0;
}
// Parallel Matrix Transposition
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#define N 1000
void transpose(int A[N][N], int B[N][N]) {
#pragma omp parallel for
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
B[j][i] = A[i][j];
}
}
}
int main() {
int A[N][N], B[N][N];
// Initialize matrix A with random values
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = rand() % 100;
}
}
double start_time = omp_get_wtime();
transpose(A, B);
double end_time = omp_get_wtime();
printf("Time taken: %f seconds\n", end_time - start_time);
return 0;
}