From ea410132c54a8b71a8a980a1aea59c758388859f Mon Sep 17 00:00:00 2001 From: Lars Hoffmann Date: Fri, 19 Jul 2024 07:58:44 +0200 Subject: [PATCH] Update MPI broadcasting code. --- src/mptrac.c | 29 ++++++++++++++--------------- src/mptrac.h | 28 ++++++++++++++++++---------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/mptrac.c b/src/mptrac.c index 73f9b1339..db6b6133a 100644 --- a/src/mptrac.c +++ b/src/mptrac.c @@ -42,30 +42,29 @@ static curandGenerator_t rng_curand; /*****************************************************************************/ #ifdef MPI -void broadcast_large_data(void* data, size_t N, int root, MPI_Comm comm) { - +void broadcast_large_data( + void *data, + size_t N) { + #define CHUNK_SIZE 2147483647 - - /* Get rank... */ - int rank; - MPI_Comm_rank(comm, &rank); - + /* Broadcast the size of the data first... */ - MPI_Bcast(&N, 1, MPI_UINT64_T, root, comm); - + MPI_Bcast(&N, 1, MPI_UINT64_T, 0, MPI_COMM_WORLD); + /* Calculate the number of chunks... */ size_t num_chunks = (N + CHUNK_SIZE - 1) / CHUNK_SIZE; - + /* Loop over chunks... */ for (size_t i = 0; i < num_chunks; i++) { - + /* Determine the start and end indices for the current chunk... */ size_t start = i * CHUNK_SIZE; size_t end = (start + CHUNK_SIZE > N) ? N : start + CHUNK_SIZE; size_t chunk_size = end - start; - + /* Broadcast the current chunk... */ - MPI_Bcast((char*)data + start, (int)chunk_size, MPI_BYTE, root, comm); + MPI_Bcast((char *) data + start, (int) chunk_size, MPI_BYTE, 0, + MPI_COMM_WORLD); } } #endif @@ -5803,10 +5802,10 @@ int read_met( LOG(2, "Broadcast data on rank %d...", rank); /* Broadcast... */ - broadcast_large_data(met, sizeof(met_t), 0, MPI_COMM_WORLD); + broadcast_large_data(met, sizeof(met_t)); } #endif - + /* Return success... */ return 1; } diff --git a/src/mptrac.h b/src/mptrac.h index 338421cc9..2e430478a 100644 --- a/src/mptrac.h +++ b/src/mptrac.h @@ -3421,22 +3421,30 @@ typedef struct { Functions... ------------------------------------------------------------ */ -#ifdef MPI /** - * @brief Broadcasts large data in chunks from the root process to all other processes in the given communicator. + * @brief Broadcasts large data across all processes in an MPI communicator. + * + * This function divides the data into manageable chunks and broadcasts each + * chunk sequentially. This approach is necessary because the data size may + * exceed the maximum allowable message size for a single MPI_Bcast operation. * - * This function splits the data into manageable chunks (up to 2 GB - 1 byte each) and broadcasts each chunk sequentially. - * This approach avoids issues with broadcasting very large data sizes that exceed the limits of a single MPI_Bcast call. + * @param data Pointer to the data to be broadcasted. + * @param N Size of the data in bytes. * - * @param data Pointer to the data to be broadcast. This pointer should point to a contiguous block of memory. - * @param N The size of the data in bytes. - * @param root The rank of the root process that holds the initial data. - * @param comm The MPI communicator. + * The function first broadcasts the total size of the data to all processes. + * Then, it calculates the number of chunks needed to broadcast the entire data. + * Each chunk is broadcasted in sequence until the entire data has been sent. + * + * The maximum chunk size is defined as CHUNK_SIZE (2147483647 bytes). + * + * @note The function assumes that the MPI environment has been initialized + * before calling this function and will be finalized afterward. * * @author Lars Hoffmann */ -void broadcast_large_data(void* data, size_t N, int root, MPI_Comm comm); -#endif +void broadcast_large_data( + void *data, + size_t N); /** * @brief Converts Cartesian coordinates to geographic coordinates.