diff --git "a/\\" "b/\\" new file mode 100644 index 0000000000000000000000000000000000000000..7189c8a490a739833b3f8cf7d07e7bf723ca533a --- /dev/null +++ "b/\\" @@ -0,0 +1,119 @@ +#include <stdio.h> +#include <stdlib.h> +#include <time.h> +#include <assert.h> +#include <mpi.h> + +using namespace std; + + +int main(int argc, char* argv[]){ + double starttime, endtime; + int proceso_id; + char processor_name[MPI_MAX_PROCESSOR_NAME]; + int namelen; + int numprocsused; + int SIZE; + unsigned int receivedElement; + unsigned int value; + unsigned int array[100000]; + int c, d, i; + unsigned int j; + unsigned int min = array[0]; + unsigned int max = array[0]; + int elementQtyArray[100000]; + int increaseOf, iteration, pridetas; + int issiunte; + int lastIndex, indexi; + unsigned int recvArray[100000]; + int elementQtyUsed; + unsigned int localArray[100000]; + int li; + + MPI_Status stat; + MPI::Init(); + + MPI_Comm_size(MPI_COMM_WORLD, &numprocsused); + proceso_id = MPI::COMM_WORLD.Get_rank(); + MPI_Get_processor_name(processor_name, &namelen); + + if(proceso_id == 0){ + starttime = MPI_Wtime(); + + for(i=0;i<SIZE;i++){ + if (array[i] < min) + min = array[i]; + if (array[i] > max) + max = array[i]; + } + + for(d=1;d<SIZE;d++){ + elementQtyArray[d] = 0; + } + + for(d=0;d<SIZE;d++){ + increaseOf = max/(numprocsused-1) + iteration = 1; + pridetas = 0; + for(j=increaseOf;j<=max;j=j+increaseOf){ + if (array[d] <= j){ + elementQtyArray[iteration++]; + pridetas = 1; + break; + } + iteration++; + } + if(pridetas = 9){ + elementQtyArray[iteration-1]++; + } + } + + for(i=1;i<numprocsused;i++){ + MPI_Send(&elementQtyArray[i], 1, MPI_INT, i, -2, MPI_COMM_WORLD); + } + + for(d=0;d<SIZE;d++){ + increaseOf = max/(numprocsused-1); + iteration = 1; + issiunte = 0; + for(j=increaseOf;j<=max;j=j+increaseOf){ + if (array[d] <= j){ + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration, -4, MPI_COMM_WORLD); + issunte = 1; + break; + } + iteration++; + } + if (issunte = 0){ + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration-1, -4, MPI_COMM_WORLD); + } + } + lastIndex = 0; + indexi = 0; + for(i=0;i<numprocsused;i++){ + MPI_Recv(&recvArray[0], elementQtyArray[i], MPI_UNSIGNED, i, 1000, MPI_COMM_WORLD, &stat); + if (lastIndex == 0){ + lastIndex = elementQtyArray[i]; + } + for(j=0;j<elementQtyArray[i];j++){ + array[indexi] = recvArray[j]; + indexi++; + } + } + + endtime = MPI_Wtime(); + } else { + MPI_Recv(&elementQtyUsed, 1, MPI_INT, 0, -2, MPI_COMM_WORLD, &stat); + + for(li=0;li<elementQtyUsed;li++){ + MPI_Recv(&receivedElement, 1, MPI_UNSIGNED, 0, -4, MPI_COMM_WORLD, &stat); + localArray[li] = receivedElement; + } + sort(localArray, localArray+elementQtyUsed); + + MPI_Send(localArray, elementQtyUsed, MPI_UNSIGNED, 0, 1000, MPI_COMM_WORLD); + } + MPI::Finalize(); + + return 0; +} diff --git a/bucket b/bucket new file mode 100755 index 0000000000000000000000000000000000000000..95d1dfa7861a3efe33c82da020a35a48acec31ac Binary files /dev/null and b/bucket differ diff --git a/bucketmpi.c b/bucketmpi.c new file mode 100644 index 0000000000000000000000000000000000000000..d079883f4f8631735a8bab965ec2d7545f484259 --- /dev/null +++ b/bucketmpi.c @@ -0,0 +1,142 @@ +#include "mpi.h" +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <time.h> +#include <assert.h> + +int main(int argc, char* argv[]){ + + double starttime, endtime; + int processor_id; + char processor_name[MPI_MAX_PROCESSOR_NAME]; + int namelen; + int num_process; + int t, d, i, j; + + MPI_Status stat; + MPI_Init(NULL,NULL); + + MPI_Comm_size(MPI_COMM_WORLD, &num_process); + MPI_Comm_rank(MPI_COMM_WORLD, &processor_id); + MPI_Get_processor_name(processor_name, &namelen); + unsigned int received_element; + srand(time(NULL)); + + if(processor_id == 0) { + int SIZE; + unsigned int value; + + scanf("%d", &SIZE); + unsigned int *array = malloc (SIZE * sizeof(unsigned int)); + for(i=0; i<SIZE; i++){ + array[i] = rand() % SIZE; + } + + starttime = MPI_Wtime(); + unsigned int min = array[0]; + unsigned int max = array[0]; + + for(i=0; i<SIZE; i++){ + if (array[i] < min){ + min = array[i]; + } + if (array[i] > max){ + max = array[i]; + } + } + + int *num_bucket = malloc (sizeof(int) *num_process); + for(d=1; d < num_process; d++){ + num_bucket[d] = 0; + } + if (num_process != 1){ + for(d=0; d < SIZE; d++){ + int increaseOf = max/(num_process-1); + int iteration = 1; + int annexed = 0; + for(j=increaseOf; j<=max; j=j+increaseOf){ + if(array[d] <= j){ + num_bucket[iteration]++; + annexed = 1; + break; + } + iteration++; + } + if (annexed == 0){ + num_bucket[iteration-1]++; + } + } + for(i=1; i<num_process; i++){ + MPI_Send(&num_bucket[i], 1, MPI_INT, i, 0, MPI_COMM_WORLD); + } + for(d=0; d < SIZE; d++){ + int increaseOf = max/(num_process-1); + int iteration = 1; + int message = 0; + for (j = increaseOf; j <= max; j = j + increaseOf) { + if(array[d] <= j) { + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration, 1, MPI_COMM_WORLD); + message = 1; + break; + } + iteration++; + } + if (message == 0) + MPI_Send(&array[d], 1, MPI_UNSIGNED, iteration-1, 1, MPI_COMM_WORLD); + } + + int lastIndex = 0; + int current_index = 0; + for(i=1; i<num_process; i++){ + unsigned int * recvArray = malloc(sizeof(unsigned int)*num_bucket[i]); + MPI_Recv(&recvArray[0], num_bucket[i], MPI_UNSIGNED, i, 2, MPI_COMM_WORLD, &stat); + if(lastIndex == 0){ + lastIndex = num_bucket[i]; + } + for(j=0; j<num_bucket[i]; j++){ + array[current_index] = recvArray[j]; + current_index++; + } + } + } + + endtime = MPI_Wtime(); + + int c; + for(c=0; c<SIZE; c++){ + printf("%d, ", array[c]); + } + + printf("\nIt took %f seconds \n", endtime-starttime); + printf("Numbers: %d \n", SIZE); + printf("Processes: %d \n", num_process); + + } else { + int element_used; + MPI_Recv(&element_used, 1, MPI_INT, 0, 0, MPI_COMM_WORLD, &stat); + unsigned int *localArray = malloc(sizeof(unsigned int)*element_used); + int li; + for(li = 0; li<element_used; li++){ + MPI_Recv(&received_element, 1, MPI_UNSIGNED, 0, 1, MPI_COMM_WORLD, &stat); + localArray[li] = received_element; + } + + int c; + for (c = 1 ; c <= element_used-1; c++) { + d = c; + + while ( d > 0 && localArray[d] < localArray[d-1]) { + t = localArray[d]; + localArray[d] = localArray[d-1]; + localArray[d-1] = t; + + d--; + } + } + MPI_Send(localArray, element_used, MPI_UNSIGNED, 0, 2, MPI_COMM_WORLD); + } + + MPI_Finalize(); + return 0; +}