From 80024d4155f34758837bbb59fd987b9a2d0b4056 Mon Sep 17 00:00:00 2001 From: lykos98 Date: Tue, 5 Mar 2024 16:57:10 +0100 Subject: [PATCH] implemented point exchange still preliminarly --- src/tree/tree.c | 79 +++++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 23 deletions(-) diff --git a/src/tree/tree.c b/src/tree/tree.c index a71dc06..509a69a 100644 --- a/src/tree/tree.c +++ b/src/tree/tree.c @@ -958,7 +958,7 @@ void build_top_kdtree(global_context_t *ctx, pointset_t *og_pointset, top_kdtree MPI_DB_PRINT("Root is %p\n", tree -> root); if(I_AM_MASTER) { - tree_print(ctx, tree -> root); + //tree_print(ctx, tree -> root); write_nodes_to_file(ctx, tree, "bb/nodes_50_blobs_more_var.csv"); } @@ -1031,8 +1031,9 @@ int partition_data_around_key(int* key, float_t *val, int vec_len, int ref_key , void exchange_points(global_context_t* ctx, top_kdtree_t* tree) { - size_t* points_per_proc = (size_t*)malloc(ctx -> world_size * sizeof(size_t)); - int* points_owners = (int*) malloc(ctx -> dims * ctx -> local_n_points * sizeof(float_t)); + int* points_per_proc = (int*)malloc(ctx -> world_size * sizeof(int)); + int* points_owners = (int*)malloc(ctx -> dims * ctx -> local_n_points * sizeof(float_t)); + int* partition_offset = (int*)malloc(ctx -> world_size * sizeof(int)); /* compute owner */ for(size_t i = 0; i < ctx -> local_n_points; ++i) { @@ -1044,9 +1045,12 @@ void exchange_points(global_context_t* ctx, top_kdtree_t* tree) int last_idx = 0; int len = ctx -> local_n_points; float_t* curr_data = ctx -> local_data; + + partition_offset[0] = 0; for(int owner = 1; owner < ctx -> world_size; ++owner) { last_idx = partition_data_around_key(points_owners, ctx -> local_data, ctx -> dims, owner, last_idx, ctx -> local_n_points); + partition_offset[owner] = last_idx; points_per_proc[owner - 1] = last_idx; } @@ -1058,41 +1062,70 @@ void exchange_points(global_context_t* ctx, top_kdtree_t* tree) points_per_proc[i] = points_per_proc[i] - points_per_proc[i - 1]; } - - /* - MPI_DB_PRINT("Points per proc begin: "); - for(int i = 0; i < ctx -> world_size; ++i) - { - MPI_DB_PRINT("%lu ", points_per_proc[i]); - } - MPI_DB_PRINT("\n"); - */ - MPI_Allreduce(MPI_IN_PLACE, points_per_proc, ctx -> world_size, MPI_UINT64_T,MPI_SUM, ctx -> mpi_communicator); + /* + int* points_per_proc_all = (int*)malloc(ctx -> world_size * sizeof(int)); + MPI_Allreduce(MPI_IN_PLACE, points_per_proc_all, ctx -> world_size, MPI_INT,MPI_SUM, ctx -> mpi_communicator); size_t test_num = 0; - for(int i = 0; i < ctx -> world_size; ++i) test_num += points_per_proc[i]; + for(int i = 0; i < ctx -> world_size; ++i) test_num += points_per_proc_all[i]; MPI_DB_PRINT("Master has n_points %lu and in node population has %lu points\n", ctx -> n_points, test_num); - /* + + + MPI_DB_PRINT("Points per proc after: "); for(int i = 0; i < ctx -> world_size; ++i) { - MPI_DB_PRINT("%lu ", points_per_proc[i]); + MPI_DB_PRINT("%lu ", points_per_proc_all[i]); } MPI_DB_PRINT("\n"); - */ + free(points_per_proc_all); + */ - /* - for(int i = 0; i < ctx -> local_n_points; ++i) + int* rcvcount = (int*)malloc(ctx -> world_size * sizeof(int)); + int* displs = (int*)malloc(ctx -> world_size * sizeof(int)); + float_t* rcvbuffer = NULL; + int tot_count = 0; + for(int rcv = 0; rcv < ctx -> world_size; ++rcv) { - MPI_DB_PRINT("%d ", points_owners[i]); - if(i % 10 == 0) MPI_DB_PRINT("\n"); + /* recieve the number of points to recieve from each proc */ + MPI_Gather(&(points_per_proc[rcv]), 1, MPI_INT, rcvcount, 1, MPI_INT, rcv, ctx -> mpi_communicator); + float_t* send_buffer = ctx -> local_data + (ctx -> dims * partition_offset[rcv]); + + + /* if I am the reciever recieve */ + if(rcv == ctx -> mpi_rank) + { + displs[0] = 0; + for(int i = 1; i < ctx -> world_size; ++i) displs[i] = displs[i - 1] + rcvcount[i - 1]; + + /*multiply for number of elements */ + for(int i = 0; i < ctx -> world_size; ++i) + { + displs[i] = displs[i] * ctx -> dims; + rcvcount[i] = rcvcount[i] * ctx -> dims; + tot_count += rcvcount[i]; + } + //DB_PRINT("[RANK %d] is recieving %d elements %d points\n", rcv, tot_count, tot_count / ctx -> dims); + rcvbuffer = (float_t*)malloc(tot_count * sizeof(float_t)); + } + MPI_Gatherv(send_buffer, points_per_proc[rcv], MPI_MY_FLOAT, rcvbuffer, rcvcount, displs, MPI_MY_FLOAT, rcv, ctx -> mpi_communicator); } - */ + + ctx -> local_n_points = tot_count; + + /* free prv pointer */ + free(ctx -> local_data); + ctx -> local_data = rcvbuffer; + + free(points_owners); free(points_per_proc); + free(partition_offset); + free(rcvcount); + free(displs); } void simulate_master_read_and_scatter(int dims, size_t n, global_context_t *ctx) @@ -1184,7 +1217,6 @@ void simulate_master_read_and_scatter(int dims, size_t n, global_context_t *ctx) original_ps.lb_box = (float_t*)malloc(ctx -> dims * sizeof(float_t)); original_ps.ub_box = (float_t*)malloc(ctx -> dims * sizeof(float_t)); - float_t incr = 0.05; float_t tol = 0.002; top_kdtree_t tree; @@ -1196,6 +1228,7 @@ void simulate_master_read_and_scatter(int dims, size_t n, global_context_t *ctx) + free(send_counts); free(displacements); -- GitLab