Sorting [Paralel 2 Komputer]

#include <mpi.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

int i,j,k,Temp,rank,jmlhproses,namakom,batas;
char nama_prosesor[MPI_MAX_PROCESSOR_NAME];

MPI_Status status;
MPI_Datatype type;

int data1[9000000];
int data2[9000000];
int data[9000000];
int terima[9000000];
int A[9000000];
int B[9000000];
int C[9000000];
long jmlhdata,jumlah,index, index2, counter;
double start, finish, salin, total;

//=======[ buat Prosedur insertion ]===========
int insertion(int index)
{
for(i=1; i<=(jumlah-1); i++)
{
Temp = data[i];
j = i-1;

if (data[i]<=Temp)
{
index ++;   //counter untuk kasus terbaik ketika data telah terurut ascending.
// kalo lebih dari 2M
if (index>=batas){
index=0;
counter ++;}
}
else
{index ++;
if (index>=batas){
index=0;
counter ++;}
}
while(data[j]>Temp && j>=0)
{
data[j+1] = data[j];
j=j-1;
index ++;  //menghitung banyaknya perbandingan
if (index>=batas){
index=0;
counter ++;}
}
data[j+1] = Temp;
}
return (index);
}

//=======[ buat Prosedur Merge ]===========
int merge(int index)
{
i=0; j=0; k=0;    index2=0;
while (i!=jumlah && j!=jumlah)
{
if (A[i]<=B[j])
{C[k]=A[i];
k++;
i++;
index ++;}
else
{C[k]=B[j];
k++;
j++;
index ++;}
}

while(i!=jumlah)
{C[k]=A[i];
k++;
i++;}
while(j!=jumlah)
{C[k]=B[j];
k++;
j++;}
return index;
}
//========= [ program Utama ]========================
int main(int argc, char *argv[])
{
time_t t;
srand((unsigned) time(&t));

MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);

MPI_Type_contiguous(jmlhdata, MPI_LONG, &type);
MPI_Type_commit(&type);

MPI_Comm_size(MPI_COMM_WORLD,&jmlhproses);
MPI_Get_processor_name(nama_prosesor, &namakom);

//jmlhdata = strtol(argv[1], argv, 10);

jmlhdata=1000;
jumlah=jmlhdata/jmlhproses;
batas=1000000;
//==============proses nul (0)==================
if (rank == 0)
{
//printf(“\nProses Pada komputer:%s\r\n”,nama_prosesor);
//printf(“\nmengerjakan proses 0 \n”);

//data = (int *)malloc(jmlhdata * sizeof(int));
for(i=0; i<jmlhdata;i++)
{
data[i]=rand()%100;
//printf(“%d “,data[i]);
}
//printf(“\r\n”);

//edit——————-
j=0;
//printf(“\n data yang akan dikirim \n”);
for(i=jumlah; i<jmlhdata;i++)
{
data1[j]=data[i];
//printf(“%d “,data1[j]);
j++;
}

//for(i=0; i<jumlah;i++)
//{
//printf(“%d “,data[i]);
//}
//printf(“\r\n”);

//==========[ kirim data keproses 0 ]===========
MPI_Send(data1, jumlah, MPI_LONG, 1, 99, MPI_COMM_WORLD);

start = MPI_Wtime();
index=insertion(index);
finish = MPI_Wtime();
salin = finish – start;
//total=salin;
//printf(“\nData terurut proses 0:\n”);
for(j=0; j<jumlah; j++)
{
data2[j]=data[j];
//printf(“%d “,data[j]);
}
//printf(“\r\n”);

//============ambil data dari proses 1===============================
//printf(“\nRank %d menerima data dari proses 1: \n”,rank);
MPI_Recv(terima, jumlah, MPI_LONG, 1, 99, MPI_COMM_WORLD, &status);
/*for(i=0;i<jumlah;i++)
{
printf(“%d “,terima[i]);
}
printf(“\r\n”);*/

// ===========penggabungan proses 0 da proses (1+2)====================

for(i=0;i<jumlah;i++)
{
A[i]=data2[i];
B[i]=terima[i];
}

start = MPI_Wtime();
index2=merge(index2);
finish = MPI_Wtime();
total = finish – start;
//total=total+salin;

printf(“\n=================================Data pada 2 prosesor=========================:\n”);
/*for(k=0;k<jmlhdata;k++)
{
printf(“%d   “,C[k]);
}
printf(“\r\n”); */

printf(“\nJumlah Data             = %d “,jmlhdata);
//printf(“\nJumlah proses insertion         = %d “,index);
printf(“\nJumlah proses    insertion     = %d X %d + %d “,counter,batas,index);
//printf(“\nJumlah proses insertion total    = %d “,insert);
printf(“\nJumlah proses merge         = %d “,index2);
printf(“\nLama proses pada insertion     = %f   “,salin);
printf(“\nLama proses pada Merge         = %f   “,total);
printf(“\n==================================selesai======================================:\n”);

}

//============Proses 1 (satu)======================================
if (rank == 1)
{
//printf(“\nProses Pada komputer:%s\r\n”,nama_prosesor);
//printf(“\mengerjakan proses 1 dgn data awal:\n”);
MPI_Recv(terima, jumlah, MPI_LONG, 0, 99, MPI_COMM_WORLD, &status);
for(i=0;i<jumlah;i++)
{
data[i]=terima[i];
//printf(“%d “,data[i]);
}
//printf(“\r\n”);

insertion(index);
/*printf(“\nData terurut proses 1:\n”);
for(i=0;i<jumlah;i++)
{
printf(“%d “, data[i]);
}*/

//==========[ kirim data keproses 0 ]===========

MPI_Send(data, jumlah, MPI_LONG, 0, 99, MPI_COMM_WORLD);

}
MPI_Type_free(&type);

MPI_Finalize();
return 0;
}

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s