Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
pthread_Matrix Multiplication
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
younes.alkoja
pthread_Matrix Multiplication
Commits
7d601368
Commit
7d601368
authored
Jan 17, 2026
by
younes.alkoja
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add README.md
parents
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
README.md
README.md
+96
-0
No files found.
README.md
0 → 100644
View file @
7d601368
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;
const int DIM = 1000;
const int THREADS = 4;
vector
<vector
<
long
>
> a(DIM, vector
<long>
(DIM));
vector
<vector
<
long
>
> b(DIM, vector
<long>
(DIM));
vector
<vector
<
long
>
> c(DIM, vector
<long>
(DIM));
mutex mtx;
void init() {
for(int i=0; i<DIM; i++)
for(int j=0; j<DIM; j++) {
a
[
i
][
j
]
= i+j;
b
[
i
][
j
]
= i-j;
c
[
i
][
j
]
= 0;
}
}
void seq() {
for(int i=0; i<DIM; i++)
for(int j=0; j<DIM; j++) {
long s=0;
for(int k=0; k<DIM; k++)
s += a
[
i
][
k
]
*
b
[
k
][
j
]
;
c
[
i
][
j
]
=s;
}
}
void para_nosync(int id) {
int start=id
*
(DIM/THREADS);
int end=(id==THREADS-1)?DIM:start+(DIM/THREADS);
for(int i=start; i<end; i++)
for(int j=0; j<DIM; j++) {
long s=0;
for(int k=0; k<DIM; k++)
s += a
[
i
][
k
]
*
b
[
k
][
j
]
;
c
[
i
][
j
]
=s;
}
}
void para_sync(int id) {
int start=id
*
(DIM/THREADS);
int end=(id==THREADS-1)?DIM:start+(DIM/THREADS);
for(int i=start; i<end; i++)
for(int j=0; j<DIM; j++) {
long s=0;
for(int k=0; k<DIM; k++)
s += a
[
i
][
k
]
*
b
[
k
][
j
]
;
lock_guard
<mutex>
lock(mtx);
c
[
i
][
j
]
=s;
}
}
void reset() {
for(auto& row : c)
fill(row.begin(), row.end(), 0);
}
double time_func(void (
*
func)()) {
auto start = chrono::high_resolution_clock::now();
func();
auto end = chrono::high_resolution_clock::now();
return chrono::duration
<double>
(end-start).count();
}
int main() {
init();
reset(); double t1 = time_func(seq); cout << "Seq: " << t1 << "s
\n
";
reset(); double t2 = time_func(
[](
){
thread(para_nosync,0).join();
}); cout << "1 thread: " << t2 << "s
\n
";
reset(); double t3 = time_func(
[](
){
thread t
[
THREADS
]
;
for(int i=0;i<THREADS;i++) t
[
i
]
=thread(para_nosync,i);
for(int i=0;i<THREADS;i++) t
[
i
]
.join();
}); cout << THREADS << " threads: " << t3 << "s
\n
";
reset(); double t4 = time_func(
[](
){
thread t
[
THREADS
]
;
for(int i=0;i<THREADS;i++) t
[
i
]
=thread(para_sync,i);
for(int i=0;i<THREADS;i++) t
[
i
]
.join();
}); cout << THREADS << " threads sync: " << t4 << "s
\n
";
return 0;
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment