Skip to content

Commit 1b60d39

Browse files
author
AlirezaAK2000
committed
First commit
1 parent 94fe7bb commit 1b60d39

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3502
-0
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"*.tcc": "cpp"
4+
}
5+
}

HW1/9731107_HW01.pdf

1010 KB
Binary file not shown.

HW1/HW01.pdf

448 KB
Binary file not shown.

HW2/9731107_HW02.pdf

1.91 MB
Binary file not shown.

HW2/HW02.pdf

485 KB
Binary file not shown.

HW3/HW3_9731107.pdf

694 KB
Binary file not shown.

HW3/matmul3d.cpp

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
#include<stdio.h>
5+
#include<stdlib.h>
6+
#include<omp.h>
7+
#include<time.h>
8+
9+
#define MATRIX_SIZE 512
10+
#define THREADS_NUM 1
11+
#define EXP_NUM 5
12+
13+
typedef struct {
14+
short data[MATRIX_SIZE][MATRIX_SIZE][MATRIX_SIZE];
15+
} Tensor3D;
16+
17+
18+
Tensor3D A;
19+
Tensor3D B;
20+
Tensor3D C;
21+
22+
void fill_with_rand_nums(Tensor3D* A);
23+
void fill_with_zero(Tensor3D* A);
24+
void fill_matrix(Tensor3D* A, Tensor3D* B, Tensor3D* C);
25+
void print_matrix(Tensor3D* A);
26+
27+
void matmul3d_block_p(Tensor3D* A, Tensor3D* B, Tensor3D* C);
28+
void matmul3d_row_p(Tensor3D* A, Tensor3D* B, Tensor3D* C);
29+
void matmul3d_col_p(Tensor3D* A, Tensor3D* B, Tensor3D* C);
30+
31+
void free_matrix(Tensor3D* A);
32+
33+
int main()
34+
{
35+
36+
// checks if openMP is available
37+
#ifndef _OPENMP
38+
printf("OpenMP is not supported, sorry!\n");
39+
getchar();
40+
return 0;
41+
#endif
42+
43+
omp_set_num_threads(THREADS_NUM);
44+
printf("number of threads : %d\n" , THREADS_NUM);
45+
46+
double exp_times_sum = 0;
47+
fill_matrix(&A, &B, &C);
48+
49+
for (int i = 0; i < EXP_NUM; i++)
50+
{
51+
double start = omp_get_wtime();
52+
matmul3d_col_p(&A, &B, &C);
53+
double end = omp_get_wtime() - start;
54+
printf("elapsed time : %f exp : %d\n", end, i + 1);
55+
exp_times_sum += end;
56+
}
57+
58+
printf("average elapsed time for block parallelism: %f\n", exp_times_sum / EXP_NUM);
59+
60+
61+
// free_matrix(&A);
62+
// free_matrix(&B);
63+
// free_matrix(&C);
64+
65+
return 0;
66+
67+
}
68+
69+
70+
71+
void fill_with_rand_nums(Tensor3D* A) {
72+
for (int i = 0; i < MATRIX_SIZE; i++) {
73+
for (int j = 0; j < MATRIX_SIZE; j++) {
74+
for (int k = 0; k < MATRIX_SIZE; k++) {
75+
A->data[i][j][k] = rand() % 10;
76+
}
77+
}
78+
}
79+
}
80+
81+
void fill_with_zero(Tensor3D* A) {
82+
for (int i = 0; i < MATRIX_SIZE; i++) {
83+
for (int j = 0; j < MATRIX_SIZE; j++) {
84+
for (int k = 0; k < MATRIX_SIZE; k++) {
85+
A->data[i][j][k] = 0;
86+
}
87+
}
88+
}
89+
}
90+
91+
92+
void fill_matrix(Tensor3D* A, Tensor3D* B, Tensor3D* C) {
93+
double int_size = sizeof(short);
94+
double size_mb = ((double)(MATRIX_SIZE * MATRIX_SIZE * MATRIX_SIZE)) * int_size / (1024.0 * 1024.0);
95+
printf(" size of matrixes : %lf MB\n", size_mb);
96+
fill_with_rand_nums(A);
97+
fill_with_rand_nums(B);
98+
fill_with_zero(C);
99+
}
100+
101+
void free_matrix(Tensor3D* A) {
102+
free(A->data);
103+
}
104+
105+
void print_matrix(Tensor3D* A) {
106+
printf("[");
107+
for (int i = 0; i < MATRIX_SIZE; i++) {
108+
printf("[\n");
109+
for (int j = 0; j < MATRIX_SIZE; j++) {
110+
for (int k = 0; k < MATRIX_SIZE; k++) {
111+
printf("%d ", A->data[i][j][k]);
112+
}
113+
printf("\n");
114+
}
115+
printf("]\n");
116+
}
117+
printf("]\n");
118+
}
119+
120+
121+
void matmul3d_block_p(Tensor3D* A, Tensor3D* B, Tensor3D* C) {
122+
#pragma omp parallel for
123+
for (int i = 0; i < MATRIX_SIZE; i++) {
124+
for (int j = 0; j < MATRIX_SIZE; j++) {
125+
for (int k = 0; k < MATRIX_SIZE; k++) {
126+
for (int p = 0; p < MATRIX_SIZE; p++) {
127+
C->data[i][j][k] += A->data[i][j][p] * B->data[i][p][k];
128+
}
129+
}
130+
}
131+
}
132+
}
133+
134+
void matmul3d_row_p(Tensor3D* A, Tensor3D* B, Tensor3D* C) {
135+
#pragma omp parallel for collapse(2)
136+
for (int i = 0; i < MATRIX_SIZE; i++) {
137+
for (int j = 0; j < MATRIX_SIZE; j++) {
138+
for (int k = 0; k < MATRIX_SIZE; k++) {
139+
for (int p = 0; p < MATRIX_SIZE; p++) {
140+
C->data[i][j][k] += A->data[i][j][p] * B->data[i][p][k];
141+
}
142+
}
143+
}
144+
}
145+
}
146+
147+
148+
149+
void matmul3d_col_p(Tensor3D* A, Tensor3D* B, Tensor3D* C) {
150+
#pragma omp parallel for collapse(3)
151+
for (int i = 0; i < MATRIX_SIZE; i++) {
152+
for (int j = 0; j < MATRIX_SIZE; j++) {
153+
for (int k = 0; k < MATRIX_SIZE; k++) {
154+
for (int p = 0; p < MATRIX_SIZE; p++) {
155+
C->data[i][j][k] += A->data[i][j][p] * B->data[i][p][k];
156+
}
157+
}
158+
}
159+
}
160+
}

HW3/matmul3d.o

18.1 KB
Binary file not shown.

HW4/deadlock.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <omp.h>
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
5+
6+
7+
#define NUM_RESOURCES 8
8+
#define ATTEMPTS 4
9+
10+
11+
omp_lock_t locks[NUM_RESOURCES];
12+
13+
14+
void calc(int i , int j){
15+
int thread_num = omp_get_thread_num();
16+
17+
printf("threads %d wants %d and %d \n" , thread_num , i , j);
18+
19+
omp_set_lock(&locks[i]);
20+
omp_set_lock(&locks[j]);
21+
22+
int k = 0;
23+
24+
for(int p = 0 ; p < 1000000 ; p++){
25+
k++;
26+
}
27+
28+
omp_unset_lock(&locks[i]);
29+
omp_unset_lock(&locks[j]);
30+
31+
}
32+
33+
34+
int main(){
35+
#ifndef _OPENMP
36+
printf("OpenMP is not supported, sorry!\n");
37+
getchar();
38+
return 0;
39+
#endif
40+
41+
for (size_t i = 0; i < NUM_RESOURCES; i++)
42+
omp_init_lock(&locks[i]);
43+
44+
for (int k = 0; k < ATTEMPTS; k++)
45+
{
46+
printf("attempt %d : \n\n" , k + 1);
47+
#pragma omp parallel num_threads(NUM_RESOURCES)
48+
{
49+
int i = rand() % NUM_RESOURCES;
50+
int j = rand() % NUM_RESOURCES;
51+
if(i == j)
52+
j = (j + 1 ) % NUM_RESOURCES;
53+
54+
#pragma omp barrier
55+
calc(i , j);
56+
}
57+
}
58+
59+
for (size_t i = 0; i < NUM_RESOURCES; i++)
60+
omp_destroy_lock(&locks[i]);
61+
62+
return 0;
63+
}

HW4/deadlock.o

17.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)