Skip to content
Snippets Groups Projects
Commit b92f7bb1 authored by omarcelh's avatar omarcelh
Browse files

Not yet done

parent 4be1e841
No related merge requests found
bucket 0 → 100755
File added
...@@ -2,8 +2,6 @@ ...@@ -2,8 +2,6 @@
#include "mpi.h" #include "mpi.h"
#include <stdlib.h> #include <stdlib.h>
#define BUCKET_SIZE 10
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;
MPI_Status stat; MPI_Status stat;
...@@ -11,72 +9,111 @@ int main(int argc, char** argv){ ...@@ -11,72 +9,111 @@ int main(int argc, char** argv){
MPI_Init(&argc, &argv); MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks); MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Comm_rank(MPI_COMM_WORLD, &rank);
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
int** buckets = (int**) malloc (sizeof(int*) * BUCKET_SIZE);
int* bucketsNEff = (int*) malloc (sizeof(int) * BUCKET_SIZE);
int* result = (int*) malloc (sizeof(int) * BUCKET_SIZE);
int j;
for(j=0; j<BUCKET_SIZE; j++){
bucketsNEff = 0;
}
if(rank == 0){ if(rank == 0){
int i; int i;
for(i=0; i<sizeof(array)/sizeof(array[0]); i++){ int n;
dest = array[i] / 10; srand(time(NULL));
bucketsNEff[dest] += 1; n = rand() % 10 + 1;
printf("%d number of elements\n", n);
MPI_Send(&array[i], 1, MPI_INT, dest, 1, MPI_COMM_WORLD); int input[n], result[n];
}
int c;
for (c = 0; c < n; c++) {
input[c] = abs(rand() % 100);
}
int* bucketsNEff = malloc (sizeof(int) * numtasks);
int j;
for(j=0; j<numtasks; j++){
bucketsNEff[j] = 0;
}
for(i=0; i<n; i++){
int inc = 100/(numtasks-1);
int iteration = 1;
int k, p = 0;
for(k=inc; k<100; k+=inc){
if(input[i] <= k){
bucketsNEff[iteration]++;
p = 1;
break;
}
iteration++;
}
if(!p){
bucketsNEff[iteration-1]++;
}
}
for(i=0; i<numtasks; i++){
MPI_Send(&input[i], 1, MPI_INT, i, 1, MPI_COMM_WORLD);
}
for(i=0; i<n; i++){
int inc = 100/(numtasks-1);
int iteration = 1;
int k, p = 0;
for(k=inc; k<100; k+=inc){
if(input[i] <= k){
MPI_Send(&input[i], 1, MPI_INT, iteration, 2, MPI_COMM_WORLD);
p = 1;
break;
}
iteration++;
}
if(!p){
MPI_Send(&input[i], 1, MPI_INT, iteration-1, 2, MPI_COMM_WORLD);
}
}
int lastIndex = 0;
int indexi = 0;
printf("A\n");
for(i=1; i<numtasks; i++){
int receiver[bucketsNEff[i]];
MPI_Recv(&receiver[0], bucketsNEff[i], MPI_INT, i, 3, MPI_COMM_WORLD, &stat);
if(lastIndex = 0){
lastIndex = bucketsNEff[i];
}
int k;
for(k=0; k<bucketsNEff[i]; k++){
input[indexi] = receiver[k];
indexi++;
}
}
printf("A\n");
} else { } else {
source = 0; int bucketNEff;
MPI_Recv(&buckets[rank][bucketsNEff[rank]], n, MPI_INT, source, 1, MPI_COMM_WORLD, &stat); MPI_Recv(&bucketNEff, 1, MPI_INT, 0, 1, MPI_COMM_WORLD, &stat);
printf("C\n");
int arr[bucketNEff];
int i;
for(i=0; i<bucketNEff; i++){
int temp = 0;
MPI_Recv(&temp, 1, MPI_INT, 0, 2, MPI_COMM_WORLD, &stat);
arr[i] = temp;
}
int c, d, t; int c, d, t;
for (c = 1 ; c <= bucketsNEff[rank] - 1; c++) { for (c = 1 ; c <= bucketNEff - 1; c++) {
d = c; d = c;
while ( d > 0 && buckets[rank][d] < buckets[rank][d-1]) { while ( d > 0 && arr[d] < arr[d-1]) {
t = buckets[rank][d]; t = arr[d];
buckets[rank][d] = buckets[rank][d-1]; arr[d] = arr[d-1];
buckets[rank][d-1] = t; arr[d-1] = t;
d--; d--;
} }
} }
MPI_Send(arr, bucketNEff, MPI_INT, 0, 3, MPI_COMM_WORLD);
} }
int a, b, elem;
for(a=0; a<BUCKET_SIZE; a++){
for(b=0; b<bucketsNEff[a]; b++){
result[elem] = buckets[a][b];
elem++;
printf("%d ", result[elem]);
}
}
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