Skip to content
Snippets Groups Projects
Commit 956c4c12 authored by 13513046's avatar 13513046
Browse files

segmentation fault fixed

parent 4c6bf547
No related merge requests found
...@@ -9,15 +9,17 @@ int *create_rand_nums(int num_elements) { ...@@ -9,15 +9,17 @@ int *create_rand_nums(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() % 10;//(rand() / (float)RAND_MAX); rand_nums[i] = rand();
} }
return rand_nums; return rand_nums;
} }
int main() { int main(int argc, char** argv) {
MPI_Status Stat; if (argc != 2) {
fprintf(stderr, "Usage: avg num_elements\n");
int num_elements_per_proc = 4; }
MPI_Status Stat;
srand(time(NULL)); srand(time(NULL));
MPI_Init(NULL, NULL); MPI_Init(NULL, NULL);
...@@ -26,33 +28,36 @@ int main() { ...@@ -26,33 +28,36 @@ int main() {
MPI_Comm_rank(MPI_COMM_WORLD, &world_rank); MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);
int world_size; int world_size;
MPI_Comm_size(MPI_COMM_WORLD, &world_size); MPI_Comm_size(MPI_COMM_WORLD, &world_size);
int i, j, k, l, t, rc;
int total_elements = num_elements_per_proc * world_size; int i, j, k, l, t, rc, max = -RAND_MAX, min = RAND_MAX;
int range = 10 / world_size + (10/world_size > 0);//(RAND_MAX / world_size) + (RAND_MAX % world_size > 0); int total_elements = atoi(argv[1]);
int range;
int inmsg[total_elements]; int inmsg[total_elements];
int bucket[world_size][total_elements]; int bucket[world_size][total_elements];
printf("Ini proses ke %d\n", world_rank);
int *rand_nums = NULL; int *rand_nums = NULL;
if (world_rank == 0) { if (world_rank == 0) {
rand_nums = create_rand_nums(total_elements); rand_nums = create_rand_nums(total_elements);
for (i = 0; i < total_elements; i++) { for (i = 0; i < total_elements; i++) {
printf("%d, ", rand_nums[i]); if (rand_nums[i] > max)
max = rand_nums[i];
if (rand_nums[i] < min)
min = rand_nums[i];
} }
range = (max - min) / world_size + ((max - min) % world_size > 0);
for (i = 0; i < world_size; i++) { for (i = 0; i < world_size; i++) {
k = 0; k = 0;
for (j = 0; j < total_elements; j++) { for (j = 0; j < total_elements; j++) {
if ((rand_nums[j] < i*range+range) && (rand_nums[j] >= i*range)) { if ((rand_nums[j] < i*range+range) && (rand_nums[j] >= i*range)) {
bucket[i][k] = rand_nums[j]; bucket[i][k] = rand_nums[j];
printf("%d di %d", bucket[i][k], i);
k++; k++;
} }
} }
for (l = k; l < total_elements; l++) { for (l = k; l < total_elements; l++) {
bucket[i][l] = -1; bucket[i][l] = -1;
} }
rc = MPI_Send(bucket[i], total_elements+1, MPI_FLOAT, i, 1, MPI_COMM_WORLD); rc = MPI_Send(bucket[i], total_elements+1, MPI_INT, i, 1, MPI_COMM_WORLD);
} }
for (i = 1 ; i < total_elements; i++) { for (i = 1 ; i < total_elements; i++) {
j = i; j = i;
...@@ -66,8 +71,8 @@ int main() { ...@@ -66,8 +71,8 @@ int main() {
} }
} }
} else { } else {
rc = MPI_Recv(inmsg, total_elements+1, MPI_FLOAT, 0, 1, MPI_COMM_WORLD, &Stat); rc = MPI_Recv(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD, &Stat);
//Bucket Sort //Sort
for (i = 1 ; i < total_elements; i++) { for (i = 1 ; i < total_elements; i++) {
j = i; j = i;
...@@ -79,41 +84,32 @@ int main() { ...@@ -79,41 +84,32 @@ int main() {
j--; j--;
} }
} }
rc = MPI_Send(inmsg, total_elements+1, MPI_FLOAT, 0, 1, MPI_COMM_WORLD); rc = MPI_Send(inmsg, total_elements+1, MPI_INT, 0, 1, MPI_COMM_WORLD);
} }
printf("Ini proses ke %d selesai\n", world_rank);
printf("LOL LMFAO\n");
if (world_rank == 0) { if (world_rank == 0) {
k = 0; k = 0;
for (j = 0; j < total_elements; j++) { for (j = 0; j < total_elements; j++) {
if (bucket[0][j] >= 0) { if (bucket[0][j] >= 0) {
rand_nums[k] = bucket[0][j]; rand_nums[k] = bucket[0][j];
k++; k++;
printf("%d",bucket[0][j]);
} }
} }
for (i = 1; i < world_size; i++) { for (i = 1; i < world_size; i++) {
rc = MPI_Recv(bucket[i], total_elements+1, MPI_FLOAT, i, 1, MPI_COMM_WORLD, &Stat); rc = MPI_Recv(bucket[i], total_elements+1, MPI_INT, i, 1, MPI_COMM_WORLD, &Stat);
for (j = 0; j < total_elements; j++) { for (j = 0; j < total_elements; j++) {
if (bucket[i][j] >= 0) { if (bucket[i][j] >= 0) {
rand_nums[k] = bucket[i][j]; rand_nums[k] = bucket[i][j];
k++; k++;
printf("%d",bucket[i][j]);
} }
} }
} }
for (i = 0; i < total_elements; i++) {
printf("%d, ", rand_nums[i]);
}
printf("aaa\n");
} }
printf("baa\n");
if (world_rank == 0) { if (world_rank == 0) {
free(rand_nums); free(rand_nums);
free(bucket);
} }
free(inmsg);
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