Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
MPI_HW2
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
hasan.bahjat
MPI_HW2
Commits
45078d68
You need to sign in or sign up before continuing.
Commit
45078d68
authored
Jan 07, 2025
by
hasan khaddour
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ADD Second Question So,l.
parent
610f9345
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
68 additions
and
0 deletions
+68
-0
reduceTreee.py
reduceTreee.py
+68
-0
No files found.
reduceTreee.py
0 → 100644
View file @
45078d68
from
mpi4py
import
MPI
import
numpy
as
np
import
time
# Perform tree-based reduction on local_data using addition as the operation.
def
reduce_tree
(
local_data
,
rank
,
size
,
comm
):
num_processes
=
size
my_rank
=
rank
print
(
f
"[+] Process {my_rank} starting with local data: {local_data}"
)
step
=
1
while
step
<
num_processes
:
# XOR operation to find partner
partner
=
my_rank
^
step
if
partner
<
num_processes
:
# Exchange data with the partner
recv_data
=
np
.
zeros_like
(
local_data
)
comm
.
Sendrecv
(
local_data
,
dest
=
partner
,
recvbuf
=
recv_data
,
source
=
partner
)
print
(
f
"[+] Process {my_rank} received data from process {partner}: {recv_data}"
)
# Combine received data with the local data
local_data
+=
recv_data
print
(
f
"[+] Process {my_rank} updated local data: {local_data}"
)
# Move to the next step
step
*=
2
if
my_rank
==
0
:
return
local_data
else
:
return
None
def
main
():
comm
=
MPI
.
COMM_WORLD
rank
=
comm
.
Get_rank
()
size
=
comm
.
Get_size
()
# Initialize the local data for each process
local_data
=
np
.
array
([
rank
+
1
],
dtype
=
int
)
# Start timer
start_time
=
time
.
time
()
# Perform the tree based reduce operation
result
=
reduce_tree
(
local_data
,
rank
,
size
,
comm
)
# end timer
end_time
=
time
.
time
()
# Elapsed time
elapsed_time
=
end_time
-
start_time
if
rank
==
0
:
print
(
f
"[+] Final result after reduction: {result}"
)
print
(
f
"[+] Time taken for the reduction: {elapsed_time:.6f} seconds"
)
# Run the main function
if
__name__
==
"__main__"
:
main
()
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