Skip to content
Snippets Groups Projects
Commit 86ef4a75 authored by 13513096's avatar 13513096
Browse files

basic scatter and gather - unfinished

parent 938f5102
Branches master
No related merge requests found
No preview for this file type
...@@ -16,7 +16,7 @@ float *create_rand_nums(int num_elements) { ...@@ -16,7 +16,7 @@ float *create_rand_nums(int num_elements) {
// n adalah jumlah element // n adalah jumlah element
float *insertion_sort(float array[], int n) { float *insertion_sort(float array[], int n) {
float tarray[n]; float *tarray = (float *)malloc(sizeof(float) * n);
int d,c,t; int d,c,t;
for (c = 1 ; c <= n - 1; c++) { for (c = 1 ; c <= n - 1; c++) {
d = c; d = c;
...@@ -29,6 +29,7 @@ float *insertion_sort(float array[], int n) { ...@@ -29,6 +29,7 @@ float *insertion_sort(float array[], int n) {
d--; d--;
} }
} }
int i; int i;
for (i = 0 ; i < n; i++) { for (i = 0 ; i < n; i++) {
tarray[i] = array[i]; tarray[i] = array[i];
...@@ -52,12 +53,13 @@ int main(int argc, char** argv) { ...@@ -52,12 +53,13 @@ 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);
int num_elements_per_proc = world_size; int num_elements_per_proc = num_elements / world_size;
printf("%d\n",world_size);
float *rand_nums = NULL; float *rand_nums = NULL;
if (world_rank == 0) { if (world_rank == 0) {
rand_nums = create_rand_nums(num_elements); rand_nums = create_rand_nums(num_elements);
} }
int i; int i;
for (i = 0; i < num_elements; i++) { for (i = 0; i < num_elements; i++) {
printf("BBB %f \n", rand_nums[i]); printf("BBB %f \n", rand_nums[i]);
...@@ -66,9 +68,19 @@ int main(int argc, char** argv) { ...@@ -66,9 +68,19 @@ int main(int argc, char** argv) {
float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc); float *sub_rand_nums = (float *)malloc(sizeof(float) * num_elements_per_proc);
assert(sub_rand_nums != NULL); assert(sub_rand_nums != NULL);
MPI_Scatter(rand_nums, num_elements_per_proc, MPI_FLOAT, sub_rand_nums, num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD); MPI_Scatter(rand_nums, num_elements, MPI_FLOAT, sub_rand_nums, num_elements_per_proc, MPI_FLOAT, 0, MPI_COMM_WORLD);
for (i = 0; i < num_elements; i++) {
printf("CCC %f\n",sub_rand_nums[i]);
}
printf("AADADADAD\n");
float *sub_rand_numss = insertion_sort(sub_rand_nums, num_elements_per_proc); float *sub_rand_numss = insertion_sort(sub_rand_nums, num_elements_per_proc);
for (i = 0; i < 3; i++) {
printf("AAAA %f\n",sub_rand_numss[i]);
}
float *sub_avgs = NULL; float *sub_avgs = NULL;
if (world_rank == 0) { if (world_rank == 0) {
...@@ -76,9 +88,7 @@ int main(int argc, char** argv) { ...@@ -76,9 +88,7 @@ int main(int argc, char** argv) {
assert(sub_avgs != NULL); assert(sub_avgs != NULL);
} }
for (i = 0; i < 3; i++) {
printf("AAAA %f\n",sub_rand_numss[i]);
}
//MPI_Gather(&sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT, 0, MPI_COMM_WORLD); //MPI_Gather(&sub_avg, 1, MPI_FLOAT, sub_avgs, 1, MPI_FLOAT, 0, MPI_COMM_WORLD);
//Concat //Concat
......
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