Skip to content
Snippets Groups Projects
Commit 5b9fe622 authored by gazandi's avatar gazandi
Browse files

udahlah ya

parent cf5245ee
No related merge requests found
...@@ -2,55 +2,111 @@ ...@@ -2,55 +2,111 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <mpi.h> #include <mpi.h>
#include "mpi.h"
#include <assert.h> #include <assert.h>
#include <string.h>
float *create_rand_nums(int num_elements) { int *create_rand_nums(int num_elements) {
float *rand_nums = (float *)malloc(sizeof(float) * num_elements); int *rand_nums = (int *)malloc(sizeof(int) * num_elements);
assert(rand_nums != NULL); assert(rand_nums != NULL);
int i; int i;
for (i = 0; i < num_elements; i++) { for (i = 0; i < num_elements; i++) {
rand_nums[i] = (rand() / (float)RAND_MAX); rand_nums[i] = rand();
} }
return rand_nums; return rand_nums;
} }
float* bucket(float* array, int n, int batasbawah, int batasatas){
float* hasil = (float *)malloc(sizeof(float) * n); int* bucket(int* array, int n, int batasbawah, int batasatas){
float max =-30000, min=30000; int* hasil = (int *)malloc(sizeof(int) * n);
for(int i=0;i<n;i++){ for(int i=0;i<n;i++){
if(array[i]>=batasbawah&&array[i]<=batasatas){ if(array[i]>=batasbawah&&array[i]<=batasatas){
hasil[i] = array[i] hasil[i] = array[i];
} }
} }
return hasil; return hasil;
} }
float * bucketing(float* array,int n){
int** bucketing(int* array,int n, int num_elements){
int t;
int i,jumlaharray;
int* temp;
int** hasil;
int max=-65536, min=65535;
for( i=0;i < n;i++){
if(array[i]>max){
max = array[i];
}
if(array[i]<min){
min = array[i];
}
}
if (( hasil = malloc( num_elements*sizeof( int* ))) == NULL )
{ /* error */ }
for ( i = 0; i < num_elements; i++ )
{
if (( hasil[i] = malloc( n )) == NULL )
{ /* error */ }
}
for(int j=0;j<num_elements;j++){
temp = (int *)malloc(sizeof(int) * n);
jumlaharray = 0;
int ite=0;
for(i=0;i<n;i++){
if(array[i]>=(max*j/num_elements)&&array[i]<=(max*(j+1)/num_elements)){
temp[ite]= array[i];
jumlaharray++;
ite++;
}
}
hasil[j] = bucket(temp,jumlaharray,max*j/num_elements,max*(j+1)/num_elements);
}
return hasil;
} }
float* bucketSort(float* array, int n){ int* bucketSort(int* array, int n){
float* hasil = (float *)malloc(sizeof(float) * n); int* hasil = (int *)malloc(sizeof(int) * n);
int i,t,c,d; int t;
int i,c,d;
for( i=0;i < n;i++){ for( i=0;i < n;i++){
hasil[i] = array[i]; hasil[i] = array[i];
} }
for (c = 1 ; c <= n - 1; c++) { for (c = 1 ; c <= n-1 ; c++) {
d = c; d = c;
while ( d > 0 && hasil[d] < hasil[d-1]) { while ( d > 0 && hasil[d] < hasil[d-1]) {
t = hasil[d]; t = hasil[d];
array[d] = hasil[d-1]; hasil[d] = hasil[d-1];
array[d-1] = t; hasil[d-1] = t;
d--; d--;
} }
} }
return hasil; return hasil;
}
void demoBucketSort(int num_elements_per_proc ){
int *rand_nums = NULL;
rand_nums = create_rand_nums(num_elements_per_proc);
int **sub_avg = NULL;
sub_avg = bucketing(rand_nums, num_elements_per_proc,5);
printf("\n\n\n");
for(int i=0;i<5;i++){
printf("%d \n",i);
for(int k=0;k<((unsigned) strlen(sub_avg[i])) ;k++){
if((unsigned) strlen(sub_avg[i])>0){
printf("%d ",k);
printf("%d \n", sub_avg[i][k]);
}
}
}
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
MPI_Status Stat;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: avg num_elements_per_proc\n"); fprintf(stderr, "Usage: avg num_elements_per_proc\n");
exit(1); exit(1);
} }
int num_elements_per_proc = atoi(argv[1]); int num_elements_per_proc = atoi(argv[1]);
//demo(num_elements_per_proc);
srand(time(NULL)); srand(time(NULL));
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
...@@ -60,43 +116,38 @@ int main(int argc, char** argv) { ...@@ -60,43 +116,38 @@ int main(int argc, char** argv) {
int world_size; int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size); MPI_Comm_size(MPI_COMM_WORLD, &world_size);
float *rand_nums = NULL; int *rand_nums = NULL;
if (world_rank == 0) { if (world_rank == 0) {
rand_nums = create_rand_nums(num_elements_per_proc * world_size); rand_nums = create_rand_nums(num_elements_per_proc * world_size);
} }
float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc); int *sub_rand_nums = (int *)malloc(sizeof(int) * num_elements_per_proc);
assert(sub_rand_nums != NULL); assert(sub_rand_nums != NULL);
int **sub_avg = NULL;
sub_avg = bucketing(rand_nums, num_elements_per_proc,world_size);
MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums, int* hasil = (int *)malloc(sizeof(int) * num_elements_per_proc); ;
num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD); int ite=0;
int* arraybaru=NULL;
float *sub_avg = NULL; for(int i=0;i<world_size;i++){
float *sub_avgs = NULL; MPI_Send(&sub_avg[i],1,MPI_INT,i,i,MPI_COMM_WORLD);
if (world_rank == 0) { arraybaru = bucketSort(sub_avg[i],(unsigned) strlen(sub_avg[i]));
sub_avg = bucketSort(rand_nums, num_elements_per_proc); MPI_Recv(&arraybaru,1,MPI_INT,i,i,MPI_COMM_WORLD,&Stat);
sub_avgs = (float *)malloc(sizeof(float) * world_size); for(int j=0;j<(unsigned) strlen(sub_avg[i]);j++){
assert(sub_avgs != NULL); hasil[ite] = arraybaru[j];
} printf("%d \n",hasil[ite]);
ite++;
MPI_Gather(sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT, 0, }
MPI_COMM_WORLD); }
if (world_rank == 0) {
for(int i=0;i<world_size;i++)
printf("elements [%d] is %f\n",i, &sub_avgs[i]);
}
if (world_rank == 0) { if (world_rank == 0) {
free(rand_nums); free(rand_nums);
free(sub_avg); free(sub_avg);
free(sub_avgs); free(arraybaru);
free(hasil);
} }
free(sub_rand_nums);
MPI_Barrier(MPI_COMM_WORLD); MPI_Barrier(MPI_COMM_WORLD);
MPI_Finalize(); MPI_Finalize();
} }
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