Skip to content
Snippets Groups Projects
Commit bcc02283 authored by William Sentosa's avatar William Sentosa
Browse files

added clock features

parent cae1f2c8
Branches
Tags
No related merge requests found
No preview for this file type
...@@ -2,10 +2,13 @@ ...@@ -2,10 +2,13 @@
// Anggota : William Sentosa / 13513026 // Anggota : William Sentosa / 13513026
// Angela Lynn / 13513032 // Angela Lynn / 13513032
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "mpi.h" #include "mpi.h"
#define MAXELEMENT 10000 #define MAXELEMENT 40000
#define MAXPROCESS 100 #define MAXPROCESS 33
#define ELEMENT 40000
typedef struct { typedef struct {
int tabNumber[MAXELEMENT]; int tabNumber[MAXELEMENT];
...@@ -86,6 +89,21 @@ void divideArray(Numbers nums, int division, ArrNumbers *arr) { ...@@ -86,6 +89,21 @@ void divideArray(Numbers nums, int division, ArrNumbers *arr) {
} }
} }
void insertionSort(int* array, int size) {
int i, j, temp;
for (i = 1; i <= size - 1; i++) {
j = i;
while (j > 0 && array[j] < array[j-1]) {
temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;
j--;
}
}
}
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int numtasks, rank, dest, source, rc, count, tag=1; int numtasks, rank, dest, source, rc, count, tag=1;
int length; int length;
...@@ -101,10 +119,10 @@ int main(int argc, char *argv[]) { ...@@ -101,10 +119,10 @@ int main(int argc, char *argv[]) {
Numbers numbers; Numbers numbers;
if (rank == 0) { if (rank == 0) {
printf("halo from rank %d\n", rank); clock_t start = clock();
MakeNumbers(&numbers); MakeNumbers(&numbers);
for (i = 12; i >= 1; i--) { for (i = 0; i < ELEMENT; i++) {
addNumber(&numbers, i); addNumber(&numbers, rand()%ELEMENT+1);
} }
MakeArrNumbers(&arrNumbers); MakeArrNumbers(&arrNumbers);
divideArray(numbers, numtasks - 1, &arrNumbers); divideArray(numbers, numtasks - 1, &arrNumbers);
...@@ -114,7 +132,7 @@ int main(int argc, char *argv[]) { ...@@ -114,7 +132,7 @@ int main(int argc, char *argv[]) {
MPI_Send(&length, 1, MPI_INT, dest, tag, MPI_COMM_WORLD); MPI_Send(&length, 1, MPI_INT, dest, tag, MPI_COMM_WORLD);
rc = MPI_Send(arrNumbers.tabNumbers[i-1].tabNumber, length, MPI_INT, dest, tag, MPI_COMM_WORLD); rc = MPI_Send(arrNumbers.tabNumbers[i-1].tabNumber, length, MPI_INT, dest, tag, MPI_COMM_WORLD);
} }
printf("Hasil : ");
for(i=1; i<numtasks; i++) { for(i=1; i<numtasks; i++) {
source = i; source = i;
length = arrNumbers.tabNumbers[i-1].neffNumbers; length = arrNumbers.tabNumbers[i-1].neffNumbers;
...@@ -124,12 +142,16 @@ int main(int argc, char *argv[]) { ...@@ -124,12 +142,16 @@ int main(int argc, char *argv[]) {
printf("%d ", inmsg[j]); printf("%d ", inmsg[j]);
} }
} }
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC * 1000;
printf("\n");
printf("Elapsed Time : %.2f ms\n", seconds);
} }
else { else {
printf("halo from rank %d\n", rank);
source = 0; source = 0;
MPI_Recv(&length, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); MPI_Recv(&length, 1, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
rc = MPI_Recv(inmsg, length, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat); rc = MPI_Recv(inmsg, length, MPI_INT, source, tag, MPI_COMM_WORLD, &Stat);
insertionSort(inmsg, length);
dest = 0; dest = 0;
rc = MPI_Send(inmsg, length, MPI_INT, dest, tag, MPI_COMM_WORLD); rc = MPI_Send(inmsg, length, MPI_INT, dest, tag, MPI_COMM_WORLD);
......
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