Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
uva
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
uva
Commits
0a09169c
Commit
0a09169c
authored
May 12, 2011
by
Taddeüs Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test program to familiarize with MPI.
parent
ae636604
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
10 deletions
+72
-10
modsim/ass4_taddeus/ring.c
modsim/ass4_taddeus/ring.c
+18
-10
modsim/ass4_taddeus/ring_test.c
modsim/ass4_taddeus/ring_test.c
+54
-0
No files found.
modsim/ass4_taddeus/ring.c
View file @
0a09169c
...
@@ -2,14 +2,17 @@
...
@@ -2,14 +2,17 @@
#include <stdio.h>
#include <stdio.h>
#include <mpi.h>
#include <mpi.h>
double
*
data
=
{.
0
};
double
data
[
1
]
=
{.
0
};
void
initialize_ring
(
void
)
{
void
initialize_ring
(
int
tasks
)
{
double
received
;
double
received
;
MPI_Status
status
;
puts
(
"Sending first data"
);
printf
(
"Sending first data..."
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
2
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
1
,
0
,
MPI_COMM_WORLD
);
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
tasks
-
1
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
);
puts
(
"sent"
);
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
tasks
-
1
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
,
&
status
);
if
(
received
!=
*
data
)
if
(
received
!=
*
data
)
printf
(
"Received data does not match (sent: %f, received: %f)
\n
"
,
printf
(
"Received data does not match (sent: %f, received: %f)
\n
"
,
...
@@ -20,10 +23,15 @@ void initialize_ring(void) {
...
@@ -20,10 +23,15 @@ void initialize_ring(void) {
void
pass_data
(
int
tasks
,
int
rank
)
{
void
pass_data
(
int
tasks
,
int
rank
)
{
double
received
;
double
received
;
MPI_Status
status
;
printf
(
"Task %d sending data
\n
"
,
task
);
int
target
=
(
rank
+
1
)
%
tasks
,
source
=
(
rank
-
1
+
tasks
)
%
tasks
;
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
(
rank
-
1
+
tasks
)
%
tasks
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
(
rank
+
1
)
%
tasks
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
);
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
source
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
,
&
status
);
printf
(
"Task %d received data from %d, sending to %d..."
,
rank
,
source
,
target
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
target
,
0
,
MPI_COMM_WORLD
);
puts
(
"sent"
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
int
main
(
int
argc
,
char
**
argv
)
{
...
@@ -38,7 +46,7 @@ int main(int argc, char **argv) {
...
@@ -38,7 +46,7 @@ int main(int argc, char **argv) {
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
rank
);
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
rank
);
if
(
!
rank
)
if
(
!
rank
)
initialize_ring
();
initialize_ring
(
tasks
);
else
else
pass_data
(
tasks
,
rank
);
pass_data
(
tasks
,
rank
);
...
...
modsim/ass4_taddeus/ring_test.c
0 → 100644
View file @
0a09169c
#include <stdlib.h>
#include <stdio.h>
#include <mpi.h>
double
data
[
1
]
=
{.
0
};
void
initialize_ring
(
int
tasks
)
{
double
received
;
MPI_Status
status
;
printf
(
"Sending first data..."
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
1
,
0
,
MPI_COMM_WORLD
);
puts
(
"sent"
);
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
tasks
-
1
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
,
&
status
);
if
(
received
!=
*
data
)
printf
(
"Received data does not match (sent: %f, received: %f)
\n
"
,
*
data
,
received
);
else
puts
(
"Data received succesfully"
);
}
void
pass_data
(
int
tasks
,
int
rank
)
{
double
received
;
MPI_Status
status
;
int
target
=
(
rank
+
1
)
%
tasks
,
source
=
(
rank
-
1
+
tasks
)
%
tasks
;
MPI_Recv
(
&
received
,
1
,
MPI_DOUBLE
,
source
,
MPI_ANY_TAG
,
MPI_COMM_WORLD
,
&
status
);
printf
(
"Task %d received data from %d, sending to %d..."
,
rank
,
source
,
target
);
MPI_Send
(
data
,
1
,
MPI_DOUBLE
,
target
,
0
,
MPI_COMM_WORLD
);
puts
(
"sent"
);
}
int
main
(
int
argc
,
char
**
argv
)
{
int
tasks
,
rank
,
error
;
if
(
(
error
=
MPI_Init
(
&
argc
,
&
argv
))
!=
MPI_SUCCESS
)
{
printf
(
"MPI_init failed (error %d)
\n
"
,
error
);
MPI_Abort
(
MPI_COMM_WORLD
,
error
);
}
MPI_Comm_size
(
MPI_COMM_WORLD
,
&
tasks
);
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
rank
);
if
(
!
rank
)
initialize_ring
(
tasks
);
else
pass_data
(
tasks
,
rank
);
MPI_Finalize
();
}
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