Fakultas Ilmu Komputer UI

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

add hpc stuff

parent 9efe8323
#include <stdio.h>
#include <math.h>
int main () {
int lower, upper,total,i,j;
int isprime;
total = 0;
lower = 2;
upper = 1234567;
for (i = lower; i<= upper; i++) {
isprime = 1;
for ( j=2; j<i; j++) {
if (i % j == 0) {
isprime = 0;
break;
}
}
total+=isprime;
}
printf("Number of Prime Numbers from %d to %d=%d\n",lower,upper,total);
}
#include <stdio.h>
#include "mpi.h"
#include <stdlib.h>
#include <assert.h>
int main(int argc, char *argv[]) {
int lower,upper,total,i,j,isprime;
int rank, size;
int local_total = 0;
int global_total = 0;
double time_initial,time_current,time;
MPI_Init(&argc, &argv);
time_initial = MPI_Wtime();
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
local_total = 0;
lower = 2;
upper = 1234567;
MPI_Bcast ( &upper, 1, MPI_INT, 0, MPI_COMM_WORLD );
for (i = lower + rank; i<= upper; i=i+size) {
isprime = 1;
for ( j=2; j<i; j++) {
if (i % j == 0) {
isprime = 0;
break;
}
}
local_total+=isprime;
}
MPI_Reduce(&local_total,&global_total,1,MPI_INT,MPI_SUM,0,MPI_COMM_WORLD);
time_current=MPI_Wtime();
time = time_current - time_initial;
if (rank == 0) {
printf("Total Prime Numbers = %d\n",global_total);
printf("ElapsedTime=%.3f\n",time);
}
MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize();
return 0;
}
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