Fakultas Ilmu Komputer UI

Commit b80ebac2 authored by Ardhi Putra Pratama's avatar Ardhi Putra Pratama
Browse files

Add collective comp code

parent e1b16819
#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();
}
#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();
}
#include <mpi.h>
#include "mpi.h"
#include <stdio.h>
int main(int argc, char** argv) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment