Fakultas Ilmu Komputer UI
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Ardhi Putra Pratama
teaching
Commits
b80ebac2
Commit
b80ebac2
authored
Jan 26, 2018
by
Ardhi Putra Pratama
Browse files
Add collective comp code
parent
e1b16819
Changes
3
Hide whitespace changes
Inline
Side-by-side
hpc/hello-world/mpi_bcred.c
0 → 100644
View file @
b80ebac2
#include
"mpi.h"
#include
<stdio.h>
#include
<math.h>
#define DATASIZE 10
int
main
(
int
argc
,
char
**
argv
)
{
int
myid
,
numprocs
;
int
i
,
x
,
low
,
high
,
myresult
=
0
,
result
;
int
data
[
DATASIZE
]
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
}
MPI_Init
(
&
argc
,
&
argv
);
MPI_Comm_size
(
MPI_COMM_WORLD
,
&
numprocs
);
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
myid
);
MPI_Bcast
(
data
,
DATASIZE
,
MPI_INT
,
0
,
MPI_COMM_WORLD
);
/* add portion of data */
x
=
DATASIZE
/
numprocs
;
/* must be an integer */
low
=
myid
*
x
;
high
=
low
+
x
;
for
(
i
=
low
;
i
<
high
;
i
++
)
{
myresult
+=
data
[
i
];
}
printf
(
"%d has %d
\n
"
,
myresult
,
myid
);
MPI_Reduce
(
&
myresult
,
&
result
,
1
,
MPI_INT
,
MPI_SUM
,
0
,
MPI_COMM_WORLD
);
if
(
0
==
myid
)
{
printf
(
"The sum is %d.
\n
"
,
result
);
}
MPI_Finalize
();
}
hpc/hello-world/mpi_scatter.c
0 → 100644
View file @
b80ebac2
#include
"mpi.h"
#include
<stdio.h>
#define SIZE 4
int
main
(
int
argc
,
char
**
argv
)
{
int
numtasks
,
rank
,
sendcount
,
recvcount
,
source
;
float
sendbuf
[
SIZE
][
SIZE
]
=
{
{
1
.
0
,
2
.
0
,
3
.
0
,
4
.
0
},
{
5
.
0
,
6
.
0
,
7
.
0
,
8
.
0
},
{
9
.
0
,
10
.
0
,
11
.
0
,
12
.
0
},
{
13
.
0
,
14
.
0
,
15
.
0
,
16
.
0
}
};
float
recvbuf
[
SIZE
];
MPI_Init
(
&
argc
,
&
argv
);
MPI_Comm_rank
(
MPI_COMM_WORLD
,
&
rank
);
MPI_Comm_size
(
MPI_COMM_WORLD
,
&
numtasks
);
if
(
numtasks
==
SIZE
)
{
source
=
1
;
sendcount
=
SIZE
;
recvcount
=
SIZE
;
MPI_Scatter
(
sendbuf
,
sendcount
,
MPI_FLOAT
,
recvbuf
,
recvcount
,
MPI_FLOAT
,
source
,
MPI_COMM_WORLD
);
printf
(
"rank= %d Results: %f %f %f %f
\n
"
,
rank
,
recvbuf
[
0
],
recvbuf
[
1
],
recvbuf
[
2
],
recvbuf
[
3
]);
}
else
printf
(
"Must specify %d processors. Terminating.
\n
"
,
SIZE
);
MPI_Finalize
();
}
hpc/hello-world/mpi_send.c
View file @
b80ebac2
#include
<
mpi.h
>
#include
"
mpi.h
"
#include
<stdio.h>
int
main
(
int
argc
,
char
**
argv
)
{
...
...
Write
Preview
Supports
Markdown
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