diff --git a/bucket_sort.c b/bucket_sort.c index 151d166cd4220892cb4b20a4fce6226db1f33eb7..2cd6076187f38a4045ccfaa60a6412869f940771 100644 --- a/bucket_sort.c +++ b/bucket_sort.c @@ -19,38 +19,24 @@ int main(int argc, char** argv) { fprintf(stderr, "Usage: avg num_elements num_threads\n"); exit(1); } - //MPI_Status Stat; - srand(time(NULL)); - - //MPI_Init(NULL, NULL); - - //int world_rank; - //MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); - //int world_size; - //MPI_Comm_size(MPI_COMM_WORLD, &world_size); - int i, j, k, l, max = -RAND_MAX, min = RAND_MAX; int total_elements = atoi(argv[1]); int range; int world_size = atoi(argv[2]); - //int inmsg[total_elements]; int bucket[world_size][total_elements]; int *rand_nums = NULL; clock_t time_start, time_stop; time_start = clock(); - // if (world_rank == 0) { rand_nums = create_rand_nums(total_elements); for (i = 0; i < total_elements; i++) { if (rand_nums[i] > max) max = rand_nums[i]; if (rand_nums[i] < min) min = rand_nums[i]; - printf("%d ",rand_nums[i]); } printf("\n"); range = (max - min) / world_size + ((max - min) % world_size > 0); - //time_start = MPI_Wtime(); for (i = 0; i < world_size; i++) { k = 0; for (j = 0; j < total_elements; j++) { @@ -62,61 +48,30 @@ int main(int argc, char** argv) { for (l = k; l < total_elements; l++) { bucket[i][l] = -1; } - //rc = MPI_Send(bucket[i], total_elements+1, MPI_INT, i, 1, MPI_COMM_WORLD); } - for (i = 1 ; i < total_elements-1; i++)// { + for (i = 1 ; i < total_elements-1; i++) for (j = i; j < total_elements; j++) if (bucket[0][i] > bucket[0][j]) { int temp = bucket[0][i]; bucket[0][i] = bucket[0][j]; bucket[0][j] = temp; - } - /*j = i; - - while ( j > 0 && bucket[0][j] < bucket[0][j-1]) { - t = bucket[0][j]; - bucket[0][j] = bucket[0][j-1]; - bucket[0][j-1] = t; - - j--; - } - }*/ - // } else { - //rc = MPI_Recv(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD, &Stat); + } //Sort - int thread_count = strtol(argv[2], NULL, 10); - #pragma omp parallel for num_threads(thread_count) - for(k=0;k<world_size;k++) - for (i = 0; i < total_elements-1; i++) - for (j = i+1; j < total_elements; j++) - if (bucket[k][i] > bucket[k][j]) { - int temp = bucket[k][i]; - bucket[k][i] = bucket[k][j]; - bucket[k][j] = temp; - } - /* for (i = 1 ; i < total_elements; i++) { - j = i; - - while ( j > 0 && inmsg[j] < inmsg[j-1]) { - t = inmsg[j]; - inmsg[j] = inmsg[j-1]; - inmsg[j-1] = t; - - j--; - } - } */ - //rc = MPI_Send(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD); - // } - // if (world_rank == 0) { + int thread_count = strtol(argv[2],NULL,10); + #pragma omp parallel num_threads(thread_count) private(i,j,k) + #pragma omp for + for(k=0;k<world_size;k++) + for (i = 0; i < total_elements-1; i++) + for (j = i+1; j < total_elements; j++) + if (bucket[k][i] > bucket[k][j]) { + int temp = bucket[k][i]; + bucket[k][i] = bucket[k][j]; + bucket[k][j] = temp; + } + k = 0; - for (j = 0; j < total_elements; j++) { - if (bucket[0][j] >= 0) { - rand_nums[k] = bucket[0][j]; - k++; - } - } - for (i = 1; i < world_size; i++) { - // rc = MPI_Recv(bucket[i], total_elements+1, MPI_INT, i, 1, MPI_COMM_WORLD, &Stat); + #pragma omp for + for (i = 0; i < world_size; i++) { for (j = 0; j < total_elements; j++) { if (bucket[i][j] >= 0) { rand_nums[k] = bucket[i][j]; @@ -124,21 +79,22 @@ int main(int argc, char** argv) { } } } - // time_stop = MPI_Wtime() - time_start; - + + #pragma omp master for(i=0;i<total_elements;i++){ printf("%d ",rand_nums[i]); } + for(j=1;j<total_elements;j++){ + if(rand_nums[j]<rand_nums[j-1]){ + printf("sort tak berhasil"); + break; + } + } printf("\n"); time_stop = clock(); double waktu = (double) (time_stop - time_start) / CLOCKS_PER_SEC; - //if (world_rank == 0) { free(rand_nums); printf("%d processes, %d elements, %.2lf seconds", world_size, total_elements, waktu); - //} - - // MPI_Barrier(MPI_COMM_WORLD); - // MPI_Finalize(); }