Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-04-MPI
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
mohamadbashar.disoki
PP-04-MPI
Commits
2e4f7c33
Commit
2e4f7c33
authored
Nov 29, 2023
by
mohamadbashar.disoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ring Example
parent
5a04e194
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
38 additions
and
0 deletions
+38
-0
ring.c
ring.c
+38
-0
No files found.
ring.c
0 → 100644
View file @
2e4f7c33
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
int
main
(
int
argc
,
char
**
argv
)
{
// Initialize the MPI environment
MPI_Init
(
NULL
,
NULL
);
// Find out rank, size
int
world_rank
;
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
world_rank
);
int
world_size
;
MPI_Comm_size
(
MPI_COMM_WORLD
,
&
world_size
);
int
token
;
// Receive from the lower process and send to the higher process. Take care
// of the special case when you are the first process to prevent deadlock.
if
(
world_rank
!=
0
)
{
MPI_Recv
(
&
token
,
1
,
MPI_INT
,
world_rank
-
1
,
0
,
MPI_COMM_WORLD
,
MPI_STATUS_IGNORE
);
printf
(
"Process %d received token %d from process %d
\n
"
,
world_rank
,
token
,
world_rank
-
1
);
}
else
{
// Set the token's value if you are process 0
token
=
-
1
;
}
MPI_Send
(
&
token
,
1
,
MPI_INT
,
(
world_rank
+
1
)
%
world_size
,
0
,
MPI_COMM_WORLD
);
// Now process 0 can receive from the last process. This makes sure that at
// least one MPI_Send is initialized before all MPI_Recvs (again, to prevent
// deadlock)
if
(
world_rank
==
0
)
{
MPI_Recv
(
&
token
,
1
,
MPI_INT
,
world_size
-
1
,
0
,
MPI_COMM_WORLD
,
MPI_STATUS_IGNORE
);
printf
(
"Process %d received token %d from process %d
\n
"
,
world_rank
,
token
,
world_size
-
1
);
}
MPI_Finalize
();
}
\ 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