LCOV - code coverage report
Current view: top level - src/dbm - dbm_multiply_comm.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 97.3 % 259 252
Test Date: 2026-11-24 01:22:08 Functions: 100.0 % 18 18

            Line data    Source code
       1              : /*----------------------------------------------------------------------------*/
       2              : /*  CP2K: A general program to perform molecular dynamics simulations         */
       3              : /*  Copyright 2000-2026 CP2K developers group <https://cp2k.org>              */
       4              : /*                                                                            */
       5              : /*  SPDX-License-Identifier: BSD-3-Clause                                     */
       6              : /*----------------------------------------------------------------------------*/
       7              : #include "dbm_multiply_comm.h"
       8              : #include "../mpiwrap/cp_mpi.h"
       9              : #include "../offload/offload_mempool.h"
      10              : 
      11              : #include "../base/cp_assert.h"
      12              : #include <limits.h>
      13              : #include <omp.h>
      14              : #include <stdlib.h>
      15              : #include <string.h>
      16              : 
      17              : #if 1
      18              : #define DBM_MULTIPLY_COMM_MEMPOOL
      19              : #endif
      20              : 
      21              : /*******************************************************************************
      22              :  * \brief Private routine for allocating a communication buffer, which is taken
      23              :  *        from the memory pool (DBM_MULTIPLY_COMM_MEMPOOL) rather than mapping
      24              :  *        fresh pages for every multiplication.
      25              :  * \author Hans Pabst
      26              :  ******************************************************************************/
      27      1832498 : static void *allocate_comm_buffer(const size_t size) {
      28              : #if defined(DBM_MULTIPLY_COMM_MEMPOOL)
      29      1832498 :   return offload_mempool_host_malloc(size);
      30              : #else
      31              :   return cp_mpi_alloc_mem(size);
      32              : #endif
      33              : }
      34              : 
      35              : /*******************************************************************************
      36              :  * \brief Private routine for releasing a buffer from allocate_comm_buffer.
      37              :  * \author Hans Pabst
      38              :  ******************************************************************************/
      39      3285360 : static void free_comm_buffer(void *mem) {
      40              : #if defined(DBM_MULTIPLY_COMM_MEMPOOL)
      41      3285360 :   offload_mempool_host_free(mem);
      42              : #else
      43              :   cp_mpi_free_mem(mem);
      44              : #endif
      45      3285360 : }
      46              : 
      47              : /*******************************************************************************
      48              :  * \brief Private routine for computing greatest common divisor of two numbers.
      49              :  * \author Ole Schuett
      50              :  ******************************************************************************/
      51       803498 : static int gcd(const int a, const int b) {
      52       803498 :   if (a == 0) {
      53              :     return b;
      54              :   }
      55       419468 :   return gcd(b % a, a); // Euclid's algorithm.
      56              : }
      57              : 
      58              : /*******************************************************************************
      59              :  * \brief Private routine for computing least common multiple of two numbers.
      60              :  * \author Ole Schuett
      61              :  ******************************************************************************/
      62       384030 : static int lcm(const int a, const int b) { return (a * b) / gcd(a, b); }
      63              : 
      64              : /*******************************************************************************
      65              :  * \brief Private routine for converting element counts to byte counts.
      66              :  * \author Hans Pabst
      67              :  ******************************************************************************/
      68       923520 : static int checked_byte_count(const int nelements, const size_t element_size) {
      69              :   // MPI displacements/counts are int; unchecked multiplication could overflow
      70              :   // silently and corrupt the alltoallv layout.
      71       923520 :   cp_assert(0 <= nelements);
      72       923520 :   cp_assert(element_size <= INT_MAX);
      73       923520 :   cp_assert(nelements <= INT_MAX / (int)element_size);
      74       923520 :   return nelements * (int)element_size;
      75              : }
      76              : 
      77              : /*******************************************************************************
      78              :  * \brief Private routine for computing the sum of the given integers.
      79              :  * \author Ole Schuett
      80              :  ******************************************************************************/
      81              : static inline int isum(const int n, const int input[n]) {
      82              :   int output = 0;
      83      1065600 :   for (int i = 0; i < n; i++) {
      84       426240 :     output += input[i];
      85              :   }
      86       213120 :   return output;
      87              : }
      88              : 
      89              : /*******************************************************************************
      90              :  * \brief Private routine for computing the cumulative sums of given numbers.
      91              :  * \author Ole Schuett and Hans Pabst
      92              :  ******************************************************************************/
      93       213120 : static inline void icumsum(const int n, const int input[n], int output[n]) {
      94              :   // Exclusive prefix sum using carried accumulators to avoid a load from
      95              :   // output[i-1] each iteration (removes loop-carried memory dependency).
      96       213120 :   int oval = output[0] = 0, ival = input[0];
      97       426240 :   for (int i = 1; i < n; i++) {
      98       213120 :     output[i] = (oval += ival);
      99       213120 :     ival = input[i];
     100              :   }
     101              : }
     102              : 
     103              : /*******************************************************************************
     104              :  * \brief Private routine computing received data counts from block metadata.
     105              :  * \author Hans Pabst
     106              :  ******************************************************************************/
     107       106560 : static void compute_data_recv_count(const int nranks,
     108              :                                     const int blks_recv_count[nranks],
     109              :                                     const int blks_recv_displ[nranks],
     110              :                                     const int free_index_sizes[],
     111              :                                     const int sum_index_sizes[],
     112              :                                     const dbm_pack_block_t blks_recv[],
     113              :                                     int data_recv_count[nranks]) {
     114              :   // Derives per-rank data counts locally from the already-received block
     115              :   // metadata, eliminating a collective alltoall for exchanging data amounts.
     116       106560 :   memset(data_recv_count, 0, nranks * sizeof(int));
     117       319680 :   for (int irank = 0; irank < nranks; irank++) {
     118      1688962 :     for (int i = 0; i < blks_recv_count[irank]; i++) {
     119      1475842 :       const dbm_pack_block_t *const blk =
     120      1475842 :           &blks_recv[blks_recv_displ[irank] + i];
     121      1475842 :       const int block_size =
     122      1475842 :           free_index_sizes[blk->free_index] * sum_index_sizes[blk->sum_index];
     123      1475842 :       cp_assert(block_size >= 0);
     124      1475842 :       cp_assert(data_recv_count[irank] <= INT_MAX - block_size);
     125      1475842 :       data_recv_count[irank] += block_size;
     126              :     }
     127              :   }
     128       106560 : }
     129              : 
     130              : /*******************************************************************************
     131              :  * \brief Private struct used for planing during pack_matrix.
     132              :  * \author Ole Schuett
     133              :  ******************************************************************************/
     134              : typedef struct {
     135              :   const dbm_block_t *blk; // source block
     136              :   int rank;               // target mpi rank
     137              :   int row_size;
     138              :   int col_size;
     139              : } plan_t;
     140              : 
     141              : /*******************************************************************************
     142              :  * \brief Private routine for calculating tick indices in pack plans.
     143              :  * \author Maximilian Graml
     144              :  ******************************************************************************/
     145              : static inline unsigned long long calculate_tick_index(int sum_index,
     146              :                                                       int nticks) {
     147              :   // 1021 is used as a random prime to scramble the index
     148              :   return ((unsigned long long)sum_index * 1021ULL) % (unsigned long long)nticks;
     149              : }
     150              : 
     151              : /*******************************************************************************
     152              :  * \brief Private routine for planing packs.
     153              :  * \author Ole Schuett
     154              :  ******************************************************************************/
     155        71040 : static void create_pack_plans(const bool trans_matrix, const bool trans_dist,
     156              :                               const dbm_matrix_t *matrix,
     157              :                               const cp_mpi_comm_t comm,
     158              :                               const dbm_dist_1d_t *dist_indices,
     159              :                               const dbm_dist_1d_t *dist_ticks, const int nticks,
     160              :                               const int npacks, plan_t *plans_per_pack[npacks],
     161              :                               int nblks_per_pack[npacks],
     162              :                               int ndata_per_pack[npacks]) {
     163        71040 :   memset(nblks_per_pack, 0, npacks * sizeof(int));
     164        71040 :   memset(ndata_per_pack, 0, npacks * sizeof(int));
     165              : 
     166        71040 : #pragma omp parallel
     167              :   {
     168              :     // 1st pass: Compute number of blocks that will be send in each pack.
     169              :     int nblks_mythread[npacks];
     170              :     memset(nblks_mythread, 0, npacks * sizeof(int));
     171              : #pragma omp for schedule(static) nowait
     172              :     for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
     173              :       dbm_shard_t *shard = &matrix->shards[ishard];
     174              :       for (int iblock = 0; iblock < shard->nblocks; iblock++) {
     175              :         const dbm_block_t *blk = &shard->blocks[iblock];
     176              :         const int sum_index = (trans_matrix) ? blk->row : blk->col;
     177              :         unsigned long long itick64 = calculate_tick_index(sum_index, nticks);
     178              :         const int ipack = itick64 / dist_ticks->nranks;
     179              :         nblks_mythread[ipack]++;
     180              :       }
     181              :     }
     182              : 
     183              :     // Sum nblocks across threads and allocate arrays for plans.
     184              : #pragma omp critical
     185              :     for (int ipack = 0; ipack < npacks; ipack++) {
     186              :       nblks_per_pack[ipack] += nblks_mythread[ipack];
     187              :       nblks_mythread[ipack] = nblks_per_pack[ipack];
     188              :     }
     189              : #pragma omp barrier
     190              : #pragma omp for
     191              :     for (int ipack = 0; ipack < npacks; ipack++) {
     192              :       const int nblks = nblks_per_pack[ipack];
     193              :       plans_per_pack[ipack] = malloc(nblks * sizeof(plan_t));
     194              :       cp_assert(plans_per_pack[ipack] != NULL || nblks == 0);
     195              :     }
     196              : 
     197              :     // 2nd pass: Plan where to send each block.
     198              :     int ndata_mythread[npacks];
     199              :     memset(ndata_mythread, 0, npacks * sizeof(int));
     200              : #pragma omp for schedule(static) nowait // Need static to match previous loop.
     201              :     for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
     202              :       dbm_shard_t *shard = &matrix->shards[ishard];
     203              :       for (int iblock = 0; iblock < shard->nblocks; iblock++) {
     204              :         const dbm_block_t *blk = &shard->blocks[iblock];
     205              :         const int free_index = (trans_matrix) ? blk->col : blk->row;
     206              :         const int sum_index = (trans_matrix) ? blk->row : blk->col;
     207              :         unsigned long long itick64 = calculate_tick_index(sum_index, nticks);
     208              :         const int ipack = itick64 / dist_ticks->nranks;
     209              :         // Compute rank to which this block should be sent.
     210              :         const int coord_free_idx = dist_indices->index2coord[free_index];
     211              :         const int coord_sum_idx = itick64 % dist_ticks->nranks;
     212              :         const int coords[2] = {(trans_dist) ? coord_sum_idx : coord_free_idx,
     213              :                                (trans_dist) ? coord_free_idx : coord_sum_idx};
     214              :         const int rank = cp_mpi_cart_rank(comm, coords);
     215              :         const int row_size = matrix->row_sizes[blk->row];
     216              :         const int col_size = matrix->col_sizes[blk->col];
     217              :         ndata_mythread[ipack] += row_size * col_size;
     218              :         // Create plan.
     219              :         const int iplan = --nblks_mythread[ipack];
     220              :         plans_per_pack[ipack][iplan].blk = blk;
     221              :         plans_per_pack[ipack][iplan].rank = rank;
     222              :         plans_per_pack[ipack][iplan].row_size = row_size;
     223              :         plans_per_pack[ipack][iplan].col_size = col_size;
     224              :       }
     225              :     }
     226              : #pragma omp critical
     227              :     for (int ipack = 0; ipack < npacks; ipack++) {
     228              :       ndata_per_pack[ipack] += ndata_mythread[ipack];
     229              :     }
     230              :   } // end of omp parallel region
     231        71040 : }
     232              : 
     233              : /*******************************************************************************
     234              :  * \brief Private routine for copying a block and computing its squared norm.
     235              :  ******************************************************************************/
     236     18204756 : static void copy_pack_block(const bool trans_matrix, const dbm_block_t *blk,
     237              :                             const int row_size, const int col_size,
     238              :                             const double shard_data[], double pack_data[],
     239              :                             const int offset, dbm_pack_block_t *pack_block) {
     240     18204756 :   double norm = 0.0; // Compute norm as double, store as float.
     241     18204756 :   if (trans_matrix) {
     242              :     // Transpose block to allow for outer-product style multiplication.
     243    129189159 :     for (int i = 0; i < row_size; i++) {
     244   1618306925 :       for (int j = 0; j < col_size; j++) {
     245   1495492492 :         const double element = shard_data[blk->offset + j * row_size + i];
     246   1495492492 :         pack_data[offset + i * col_size + j] = element;
     247   1495492492 :         norm += element * element;
     248              :       }
     249              :     }
     250      6374726 :     pack_block->free_index = blk->col;
     251      6374726 :     pack_block->sum_index = blk->row;
     252              :   } else {
     253   3070968445 :     for (int i = 0; i < row_size * col_size; i++) {
     254   3059138415 :       const double element = shard_data[blk->offset + i];
     255   3059138415 :       pack_data[offset + i] = element;
     256   3059138415 :       norm += element * element;
     257              :     }
     258     11830030 :     pack_block->free_index = blk->row;
     259     11830030 :     pack_block->sum_index = blk->col;
     260              :   }
     261     18204756 :   pack_block->norm = (float)norm;
     262     18204756 :   pack_block->offset = offset;
     263     18204756 : }
     264              : 
     265              : /*******************************************************************************
     266              :  * \brief Private routine for filling send buffers.
     267              :  * \author Ole Schuett
     268              :  ******************************************************************************/
     269       106560 : static void fill_send_buffers(
     270              :     const dbm_matrix_t *matrix, const bool trans_matrix, const int nblks_send,
     271              :     const int ndata_send, plan_t plans[nblks_send], const int nranks,
     272              :     int blks_send_count[nranks], int data_send_count[nranks],
     273              :     int blks_send_displ[nranks], int data_send_displ[nranks],
     274              :     dbm_pack_block_t blks_send[nblks_send], double data_send[ndata_send]) {
     275       106560 :   memset(blks_send_count, 0, nranks * sizeof(int));
     276       106560 :   memset(data_send_count, 0, nranks * sizeof(int));
     277              : 
     278       106560 : #pragma omp parallel
     279              :   {
     280              :     // 3th pass: Compute per rank nblks and ndata.
     281              :     int nblks_mythread[nranks], ndata_mythread[nranks];
     282              :     memset(nblks_mythread, 0, nranks * sizeof(int));
     283              :     memset(ndata_mythread, 0, nranks * sizeof(int));
     284              : #pragma omp for schedule(static) nowait
     285              :     for (int iblock = 0; iblock < nblks_send; iblock++) {
     286              :       const plan_t *plan = &plans[iblock];
     287              :       nblks_mythread[plan->rank] += 1;
     288              :       ndata_mythread[plan->rank] += plan->row_size * plan->col_size;
     289              :     }
     290              : 
     291              :     // Sum nblks and ndata across threads.
     292              : #pragma omp critical
     293              :     for (int irank = 0; irank < nranks; irank++) {
     294              :       blks_send_count[irank] += nblks_mythread[irank];
     295              :       data_send_count[irank] += ndata_mythread[irank];
     296              :       nblks_mythread[irank] = blks_send_count[irank];
     297              :       ndata_mythread[irank] = data_send_count[irank];
     298              :     }
     299              : #pragma omp barrier
     300              : 
     301              :     // Compute send displacements.
     302              : #pragma omp single
     303              :     {
     304              :       icumsum(nranks, blks_send_count, blks_send_displ);
     305              :       icumsum(nranks, data_send_count, data_send_displ);
     306              :       const int m = nranks - 1;
     307              :       cp_assert(nblks_send == blks_send_displ[m] + blks_send_count[m]);
     308              :       cp_assert(ndata_send == data_send_displ[m] + data_send_count[m]);
     309              :     }
     310              : 
     311              :     // 4th pass: Fill blks_send and data_send arrays.
     312              : #pragma omp for schedule(static) nowait // Match the previous loop.
     313              :     for (int iblock = 0; iblock < nblks_send; iblock++) {
     314              :       const plan_t *const plan = &plans[iblock];
     315              :       const dbm_block_t *const blk = plan->blk;
     316              :       const int ishard = dbm_get_shard_index(matrix, blk->row, blk->col);
     317              :       const dbm_shard_t *const shard = &matrix->shards[ishard];
     318              :       const int row_size = plan->row_size, col_size = plan->col_size;
     319              :       const int plan_size = row_size * col_size;
     320              :       const int irank = plan->rank;
     321              : 
     322              :       // The blk_send_data is ordered by rank, thread, and block.
     323              :       //   data_send_displ[irank]: Start of data for irank within blk_send_data.
     324              :       //   ndata_mythread[irank]: Current threads offset within data for irank.
     325              :       nblks_mythread[irank] -= 1;
     326              :       ndata_mythread[irank] -= plan_size;
     327              :       const int offset = data_send_displ[irank] + ndata_mythread[irank];
     328              :       const int jblock = blks_send_displ[irank] + nblks_mythread[irank];
     329              : 
     330              :       copy_pack_block(trans_matrix, blk, row_size, col_size, shard->data,
     331              :                       data_send, offset, &blks_send[jblock]);
     332              : 
     333              :       // After the block exchange data_recv_displ will be added to the offsets.
     334              :       blks_send[jblock].offset = offset - data_send_displ[irank];
     335              :     }
     336              :   } // end of omp parallel region
     337       106560 : }
     338              : 
     339              : /*******************************************************************************
     340              :  * \brief Private comperator passed to qsort to compare two blocks by sum_index.
     341              :  * \author Ole Schuett
     342              :  ******************************************************************************/
     343     91007267 : static int compare_pack_blocks_by_sum_index(const void *a, const void *b) {
     344     91007267 :   const dbm_pack_block_t *blk_a = (const dbm_pack_block_t *)a;
     345     91007267 :   const dbm_pack_block_t *blk_b = (const dbm_pack_block_t *)b;
     346     91007267 :   return blk_a->sum_index - blk_b->sum_index;
     347              : }
     348              : 
     349              : /*******************************************************************************
     350              :  * \brief Private routine for packing a local matrix directly by target shard.
     351              :  ******************************************************************************/
     352       697020 : static dbm_pack_t pack_local_matrix(const bool trans_matrix,
     353              :                                     const dbm_matrix_t *matrix,
     354       697020 :                                     const int nshards, double *seconds_sort) {
     355       697020 :   dbm_pack_t pack = {0};
     356       697020 :   if (dbm_get_num_blocks(matrix) == 0) {
     357        29411 :     return pack;
     358              :   }
     359       667609 :   const int nthreads = omp_get_max_threads();
     360       667609 :   const size_t count = (size_t)nthreads * nshards;
     361       667609 :   int *const workspace = malloc(2 * count * sizeof(int));
     362       667609 :   cp_assert(workspace != NULL);
     363       667609 :   int *const block_offsets = workspace;
     364       667609 :   int *const data_offsets = workspace + count;
     365       667609 :   int shard_start[nshards + 1];
     366       667609 :   double sort_start = 0.0;
     367              : 
     368       667609 : #pragma omp parallel
     369              :   {
     370              :     const size_t thread_start = (size_t)omp_get_thread_num() * nshards;
     371              :     int blocks[nshards], data[nshards];
     372              :     memset(blocks, 0, sizeof(blocks));
     373              :     memset(data, 0, sizeof(data));
     374              :     // Count directly by the output's free-index shard; input shards may differ.
     375              : #pragma omp for schedule(static) nowait
     376              :     for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
     377              :       const dbm_shard_t *const shard = &matrix->shards[ishard];
     378              :       for (int i = 0; i < shard->nblocks; i++) {
     379              :         const dbm_block_t *const blk = &shard->blocks[i];
     380              :         const int free_index = trans_matrix ? blk->col : blk->row;
     381              :         const int target = free_index % nshards;
     382              :         blocks[target]++;
     383              :         data[target] +=
     384              :             matrix->row_sizes[blk->row] * matrix->col_sizes[blk->col];
     385              :       }
     386              :     }
     387              :     memcpy(block_offsets + thread_start, blocks, sizeof(blocks));
     388              :     memcpy(data_offsets + thread_start, data, sizeof(data));
     389              : #pragma omp barrier
     390              : 
     391              : #pragma omp master
     392              :     {
     393              :       for (int ishard = 0; ishard < nshards; ishard++) {
     394              :         shard_start[ishard] = pack.nblocks;
     395              :         for (int ithread = 0; ithread < omp_get_num_threads(); ithread++) {
     396              :           const size_t i = (size_t)ithread * nshards + ishard;
     397              :           const int nblocks = block_offsets[i], ndata = data_offsets[i];
     398              :           block_offsets[i] = pack.nblocks;
     399              :           data_offsets[i] = pack.data_size;
     400              :           pack.nblocks += nblocks;
     401              :           pack.data_size += ndata;
     402              :         }
     403              :       }
     404              :       shard_start[nshards] = pack.nblocks;
     405              :       pack.blocks =
     406              :           allocate_comm_buffer(pack.nblocks * sizeof(dbm_pack_block_t));
     407              :       pack.data = allocate_comm_buffer(pack.data_size * sizeof(double));
     408              :     }
     409              : #pragma omp barrier
     410              :     // Keep the frequently updated cursors private to avoid false sharing.
     411              :     memcpy(blocks, block_offsets + thread_start, sizeof(blocks));
     412              :     memcpy(data, data_offsets + thread_start, sizeof(data));
     413              : 
     414              : #pragma omp for schedule(static) // Match the counting pass.
     415              :     for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
     416              :       const dbm_shard_t *const shard = &matrix->shards[ishard];
     417              :       for (int i = 0; i < shard->nblocks; i++) {
     418              :         const dbm_block_t *const blk = &shard->blocks[i];
     419              :         const int free_index = trans_matrix ? blk->col : blk->row;
     420              :         const int target = free_index % nshards;
     421              :         const int row_size = matrix->row_sizes[blk->row];
     422              :         const int col_size = matrix->col_sizes[blk->col];
     423              :         copy_pack_block(trans_matrix, blk, row_size, col_size, shard->data,
     424              :                         pack.data, data[target],
     425              :                         &pack.blocks[blocks[target]++]);
     426              :         data[target] += row_size * col_size;
     427              :       }
     428              :     }
     429              : 
     430              : #pragma omp master
     431              :     sort_start = omp_get_wtime();
     432              : #pragma omp barrier
     433              : #pragma omp for nowait
     434              :     for (int ishard = 0; ishard < nshards; ishard++) {
     435              :       const int nblocks = shard_start[ishard + 1] - shard_start[ishard];
     436              :       if (nblocks > 1) {
     437              :         qsort(&pack.blocks[shard_start[ishard]], nblocks,
     438              :               sizeof(dbm_pack_block_t), &compare_pack_blocks_by_sum_index);
     439              :       }
     440              :     }
     441              :   }
     442              : 
     443       667609 :   *seconds_sort += omp_get_wtime() - sort_start;
     444       667609 :   free(workspace);
     445       667609 :   return pack;
     446              : }
     447              : 
     448              : /*******************************************************************************
     449              :  * \brief Private routine for post-processing received blocks.
     450              :  * \author Ole Schuett
     451              :  ******************************************************************************/
     452       106560 : static void postprocess_received_blocks(
     453              :     const int nranks, const int nshards, const int nblocks_recv,
     454              :     const int blks_recv_count[nranks], const int blks_recv_displ[nranks],
     455              :     const int data_recv_displ[nranks],
     456       106560 :     dbm_pack_block_t blks_recv[nblocks_recv]) {
     457       106560 :   if (nblocks_recv == 0) {
     458       106560 :     return;
     459              :   }
     460       104802 :   if (nshards == 1) {
     461       314406 :     for (int irank = 0; irank < nranks; irank++) {
     462      1685446 :       for (int i = 0; i < blks_recv_count[irank]; i++) {
     463      1475842 :         blks_recv[blks_recv_displ[irank] + i].offset += data_recv_displ[irank];
     464              :       }
     465              :     }
     466       104802 :     if (nblocks_recv > 1) {
     467        53045 :       qsort(blks_recv, nblocks_recv, sizeof(dbm_pack_block_t),
     468              :             &compare_pack_blocks_by_sum_index);
     469              :     }
     470       104802 :     return;
     471              :   }
     472              : 
     473            0 :   int nblocks_per_shard[nshards], shard_start[nshards];
     474            0 :   memset(nblocks_per_shard, 0, nshards * sizeof(int));
     475            0 :   dbm_pack_block_t *blocks_tmp =
     476            0 :       malloc(nblocks_recv * sizeof(dbm_pack_block_t));
     477            0 :   cp_assert(blocks_tmp != NULL || nblocks_recv == 0);
     478              : 
     479            0 : #pragma omp parallel
     480              :   {
     481              :     // First use counting sort to group blocks by their free_index shard.
     482              :     int nblocks_mythread[nshards];
     483              :     memset(nblocks_mythread, 0, nshards * sizeof(int));
     484              :     int irank = 0;
     485              : #pragma omp for schedule(static) nowait
     486              :     for (int iblock = 0; iblock < nblocks_recv; iblock++) {
     487              :       while (iblock >= blks_recv_displ[irank] + blks_recv_count[irank]) {
     488              :         irank++;
     489              :       }
     490              :       blocks_tmp[iblock] = blks_recv[iblock];
     491              :       blocks_tmp[iblock].offset += data_recv_displ[irank];
     492              :       const int ishard = blks_recv[iblock].free_index % nshards;
     493              :       nblocks_mythread[ishard]++;
     494              :     }
     495              : #pragma omp critical
     496              :     for (int ishard = 0; ishard < nshards; ishard++) {
     497              :       nblocks_per_shard[ishard] += nblocks_mythread[ishard];
     498              :       nblocks_mythread[ishard] = nblocks_per_shard[ishard];
     499              :     }
     500              : #pragma omp barrier
     501              : #pragma omp single
     502              :     icumsum(nshards, nblocks_per_shard, shard_start);
     503              : #pragma omp for schedule(static) // Need static to match previous loop.
     504              :     for (int iblock = 0; iblock < nblocks_recv; iblock++) {
     505              :       const int ishard = blocks_tmp[iblock].free_index % nshards;
     506              :       const int jblock = --nblocks_mythread[ishard] + shard_start[ishard];
     507              :       blks_recv[jblock] = blocks_tmp[iblock];
     508              :     }
     509              : 
     510              :     // Then sort blocks within each shard by their sum_index.
     511              : #pragma omp for nowait
     512              :     for (int ishard = 0; ishard < nshards; ishard++) {
     513              :       if (nblocks_per_shard[ishard] > 1) {
     514              :         qsort(&blks_recv[shard_start[ishard]], nblocks_per_shard[ishard],
     515              :               sizeof(dbm_pack_block_t), &compare_pack_blocks_by_sum_index);
     516              :       }
     517              :     }
     518              :   } // end of omp parallel region
     519              : 
     520            0 :   free(blocks_tmp);
     521              : }
     522              : 
     523              : /*******************************************************************************
     524              :  * \brief Private routine for redistributing a matrix along selected dimensions.
     525              :  * \author Ole Schuett
     526              :  ******************************************************************************/
     527       768060 : static dbm_packed_matrix_t pack_matrix(const bool trans_matrix,
     528              :                                        const bool trans_dist,
     529              :                                        const dbm_matrix_t *restrict matrix,
     530              :                                        const dbm_distribution_t *restrict dist,
     531              :                                        const int nticks,
     532       768060 :                                        dbm_comm_iterator_t *iter) {
     533       768060 :   cp_assert(cp_mpi_comms_are_similar(matrix->dist->comm, dist->comm));
     534              : 
     535              :   // The row/col indicies are distributed along one cart dimension and the
     536              :   // ticks are distributed along the other cart dimension.
     537       768060 :   const dbm_dist_1d_t *dist_indices = (trans_dist) ? &dist->cols : &dist->rows;
     538       768060 :   const dbm_dist_1d_t *dist_ticks = (trans_dist) ? &dist->rows : &dist->cols;
     539       768060 :   const int *free_index_sizes =
     540              :       (trans_matrix) ? matrix->col_sizes : matrix->row_sizes;
     541       768060 :   const int *sum_index_sizes =
     542              :       (trans_matrix) ? matrix->row_sizes : matrix->col_sizes;
     543              : 
     544              :   // Allocate packed matrix.
     545       768060 :   const int nsend_packs = nticks / dist_ticks->nranks;
     546       768060 :   cp_assert(nsend_packs * dist_ticks->nranks == nticks);
     547       768060 :   dbm_packed_matrix_t packed;
     548       768060 :   packed.dist_indices = dist_indices;
     549       768060 :   packed.dist_ticks = dist_ticks;
     550       768060 :   packed.nsend_packs = nsend_packs;
     551       768060 :   packed.send_packs = malloc(nsend_packs * sizeof(dbm_pack_t));
     552       768060 :   cp_assert(packed.send_packs != NULL || nsend_packs == 0);
     553              : 
     554       768060 :   if (dist->nranks == 1) {
     555       697020 :     cp_assert(nsend_packs == 1);
     556       697020 :     packed.send_packs[0] = pack_local_matrix(
     557              :         trans_matrix, matrix, dist_indices->nshards, &iter->seconds_sort);
     558       697020 :     packed.recv_pack = (dbm_pack_t){0};
     559       697020 :     packed.max_nblocks = packed.send_packs[0].nblocks;
     560       697020 :     packed.max_data_size = packed.send_packs[0].data_size;
     561       697020 :     return packed;
     562              :   }
     563              : 
     564              :   // Plan all packs.
     565        71040 :   plan_t *plans_per_pack[nsend_packs];
     566        71040 :   int nblks_send_per_pack[nsend_packs], ndata_send_per_pack[nsend_packs];
     567        71040 :   create_pack_plans(trans_matrix, trans_dist, matrix, dist->comm, dist_indices,
     568              :                     dist_ticks, nticks, nsend_packs, plans_per_pack,
     569              :                     nblks_send_per_pack, ndata_send_per_pack);
     570              : 
     571              :   // Allocate send buffers for maximum number of blocks/data over all packs.
     572        71040 :   int nblks_send_max = 0, ndata_send_max = 0;
     573       177600 :   for (int ipack = 0; ipack < nsend_packs; ++ipack) {
     574       106560 :     nblks_send_max = imax(nblks_send_max, nblks_send_per_pack[ipack]);
     575       106560 :     ndata_send_max = imax(ndata_send_max, ndata_send_per_pack[ipack]);
     576              :   }
     577        71040 :   dbm_pack_block_t *blks_send =
     578        71040 :       allocate_comm_buffer(nblks_send_max * sizeof(dbm_pack_block_t));
     579        71040 :   double *data_send = allocate_comm_buffer(ndata_send_max * sizeof(double));
     580              : 
     581              :   // Cannot parallelize over packs (there might be too few of them).
     582       177600 :   for (int ipack = 0; ipack < nsend_packs; ipack++) {
     583              :     // Fill send buffers according to plans.
     584       106560 :     const int nranks = dist->nranks;
     585       106560 :     int blks_send_count[nranks], data_send_count[nranks];
     586       106560 :     int blks_send_displ[nranks], data_send_displ[nranks];
     587       106560 :     fill_send_buffers(matrix, trans_matrix, nblks_send_per_pack[ipack],
     588       106560 :                       ndata_send_per_pack[ipack], plans_per_pack[ipack], nranks,
     589              :                       blks_send_count, data_send_count, blks_send_displ,
     590              :                       data_send_displ, blks_send, data_send);
     591       106560 :     free(plans_per_pack[ipack]);
     592              : 
     593              :     // 1st communication: Exchange block counts.
     594       106560 :     double tick = omp_get_wtime();
     595       106560 :     int blks_recv_count[nranks], blks_recv_displ[nranks];
     596       106560 :     cp_mpi_alltoall_int(blks_send_count, 1, blks_recv_count, 1, dist->comm);
     597       426240 :     icumsum(nranks, blks_recv_count, blks_recv_displ);
     598       106560 :     const int nblocks_recv = isum(nranks, blks_recv_count);
     599              : 
     600              :     // 2nd communication: Exchange blocks.
     601       106560 :     dbm_pack_block_t *blks_recv =
     602       106560 :         allocate_comm_buffer(nblocks_recv * sizeof(dbm_pack_block_t));
     603       106560 :     int blks_send_count_byte[nranks], blks_send_displ_byte[nranks];
     604       106560 :     int blks_recv_count_byte[nranks], blks_recv_displ_byte[nranks];
     605       319680 :     for (int i = 0; i < nranks; i++) { // TODO: this is ugly!
     606       426240 :       blks_send_count_byte[i] =
     607       213120 :           checked_byte_count(blks_send_count[i], sizeof(dbm_pack_block_t));
     608       426240 :       blks_send_displ_byte[i] =
     609       213120 :           checked_byte_count(blks_send_displ[i], sizeof(dbm_pack_block_t));
     610       426240 :       blks_recv_count_byte[i] =
     611       213120 :           checked_byte_count(blks_recv_count[i], sizeof(dbm_pack_block_t));
     612       213120 :       blks_recv_displ_byte[i] =
     613       213120 :           checked_byte_count(blks_recv_displ[i], sizeof(dbm_pack_block_t));
     614              :     }
     615       319680 :     for (int i = 0; i < nranks; i++) {
     616       213120 :       iter->bytes_alltoall += blks_send_count_byte[i] +
     617       213120 :                               (int64_t)data_send_count[i] * sizeof(double);
     618              :     }
     619       106560 :     cp_mpi_alltoallv_byte(blks_send, blks_send_count_byte, blks_send_displ_byte,
     620              :                           blks_recv, blks_recv_count_byte, blks_recv_displ_byte,
     621              :                           dist->comm);
     622              : 
     623              :     // 3rd compute data counts from the received block metadata.
     624       106560 :     int data_recv_count[nranks], data_recv_displ[nranks];
     625       106560 :     compute_data_recv_count(nranks, blks_recv_count, blks_recv_displ,
     626              :                             free_index_sizes, sum_index_sizes, blks_recv,
     627              :                             data_recv_count);
     628       426240 :     icumsum(nranks, data_recv_count, data_recv_displ);
     629       106560 :     const int ndata_recv = isum(nranks, data_recv_count);
     630              : 
     631              :     // 4th communication: Exchange data.
     632       106560 :     double *data_recv = allocate_comm_buffer(ndata_recv * sizeof(double));
     633       106560 :     cp_mpi_alltoallv_double(data_send, data_send_count, data_send_displ,
     634              :                             data_recv, data_recv_count, data_recv_displ,
     635              :                             dist->comm);
     636              : 
     637              :     // 5th post-process received blocks and assemble them into a pack.
     638       106560 :     const double tock = omp_get_wtime();
     639       106560 :     iter->seconds_alltoall += tock - tick;
     640       106560 :     postprocess_received_blocks(nranks, dist_indices->nshards, nblocks_recv,
     641              :                                 blks_recv_count, blks_recv_displ,
     642              :                                 data_recv_displ, blks_recv);
     643       106560 :     iter->seconds_sort += omp_get_wtime() - tock;
     644       106560 :     packed.send_packs[ipack].nblocks = nblocks_recv;
     645       106560 :     packed.send_packs[ipack].data_size = ndata_recv;
     646       106560 :     packed.send_packs[ipack].blocks = blks_recv;
     647       106560 :     packed.send_packs[ipack].data = data_recv;
     648              :   }
     649              : 
     650              :   // Deallocate send buffers.
     651        71040 :   free_comm_buffer(blks_send);
     652        71040 :   free_comm_buffer(data_send);
     653              : 
     654              :   // Allocate pack_recv.
     655        71040 :   int max_nblocks = 0, max_data_size = 0;
     656       177600 :   for (int ipack = 0; ipack < packed.nsend_packs; ipack++) {
     657       106560 :     max_nblocks = imax(max_nblocks, packed.send_packs[ipack].nblocks);
     658       106560 :     max_data_size = imax(max_data_size, packed.send_packs[ipack].data_size);
     659              :   }
     660        71040 :   const double tick = omp_get_wtime();
     661        71040 :   cp_mpi_max_int(&max_nblocks, 1, packed.dist_ticks->comm);
     662        71040 :   cp_mpi_max_int(&max_data_size, 1, packed.dist_ticks->comm);
     663        71040 :   iter->seconds_alltoall += omp_get_wtime() - tick;
     664        71040 :   packed.max_nblocks = max_nblocks;
     665        71040 :   packed.max_data_size = max_data_size;
     666       142080 :   packed.recv_pack.blocks =
     667        71040 :       allocate_comm_buffer(packed.max_nblocks * sizeof(dbm_pack_block_t));
     668       142080 :   packed.recv_pack.data =
     669        71040 :       allocate_comm_buffer(packed.max_data_size * sizeof(double));
     670              : 
     671        71040 :   return packed; // Ownership of packed transfers to caller.
     672              : }
     673              : 
     674              : /*******************************************************************************
     675              :  * \brief Private routine for sending and receiving the pack for the given tick.
     676              :  * \author Ole Schuett
     677              :  ******************************************************************************/
     678       839100 : static dbm_pack_t *sendrecv_pack(const int itick, const int nticks,
     679              :                                  dbm_packed_matrix_t *packed, int64_t *bytes) {
     680       839100 :   const int nranks = packed->dist_ticks->nranks;
     681       839100 :   const int my_rank = packed->dist_ticks->my_rank;
     682              : 
     683              :   // Compute send rank and pack.
     684       839100 :   const int itick_of_rank0 = (itick + nticks - my_rank) % nticks;
     685       839100 :   const int send_rank = (my_rank + nticks - itick_of_rank0) % nranks;
     686       839100 :   const int send_itick = (itick_of_rank0 + send_rank) % nticks;
     687       839100 :   const int send_ipack = send_itick / nranks;
     688       839100 :   cp_assert(send_itick % nranks == my_rank);
     689              : 
     690              :   // Compute receive rank and pack.
     691       839100 :   const int recv_rank = itick % nranks;
     692       839100 :   const int recv_ipack = itick / nranks;
     693              : 
     694       839100 :   dbm_pack_t *send_pack = &packed->send_packs[send_ipack];
     695       839100 :   if (send_rank == my_rank) {
     696       803580 :     cp_assert(send_rank == recv_rank && send_ipack == recv_ipack);
     697              :     return send_pack; // Local pack, no mpi needed.
     698              :   } else {
     699        35520 :     *bytes += (int64_t)send_pack->nblocks * sizeof(dbm_pack_block_t) +
     700        35520 :               (int64_t)send_pack->data_size * sizeof(double);
     701              :     // Exchange blocks.
     702       106560 :     const int nblocks_in_bytes = cp_mpi_sendrecv_byte(
     703        35520 :         /*sendbuf=*/send_pack->blocks,
     704              :         /*sendcound=*/
     705        35520 :         checked_byte_count(send_pack->nblocks, sizeof(dbm_pack_block_t)),
     706              :         /*dest=*/send_rank,
     707              :         /*sendtag=*/send_ipack,
     708        35520 :         /*recvbuf=*/packed->recv_pack.blocks,
     709              :         /*recvcount=*/
     710        35520 :         checked_byte_count(packed->max_nblocks, sizeof(dbm_pack_block_t)),
     711              :         /*source=*/recv_rank,
     712              :         /*recvtag=*/recv_ipack,
     713        35520 :         /*comm=*/packed->dist_ticks->comm);
     714              : 
     715        35520 :     cp_assert(nblocks_in_bytes % sizeof(dbm_pack_block_t) == 0);
     716        35520 :     packed->recv_pack.nblocks = nblocks_in_bytes / sizeof(dbm_pack_block_t);
     717              : 
     718              :     // Exchange data.
     719        71040 :     packed->recv_pack.data_size = cp_mpi_sendrecv_double(
     720        35520 :         /*sendbuf=*/send_pack->data,
     721        35520 :         /*sendcound=*/send_pack->data_size,
     722              :         /*dest=*/send_rank,
     723              :         /*sendtag=*/send_ipack,
     724              :         /*recvbuf=*/packed->recv_pack.data,
     725        35520 :         /*recvcount=*/packed->max_data_size,
     726              :         /*source=*/recv_rank,
     727              :         /*recvtag=*/recv_ipack,
     728        35520 :         /*comm=*/packed->dist_ticks->comm);
     729              : 
     730        35520 :     return &packed->recv_pack;
     731              :   }
     732              : }
     733              : 
     734              : /*******************************************************************************
     735              :  * \brief Private routine for releasing a packed matrix.
     736              :  * \author Ole Schuett
     737              :  ******************************************************************************/
     738       768060 : static void free_packed_matrix(dbm_packed_matrix_t *packed) {
     739       768060 :   free_comm_buffer(packed->recv_pack.blocks);
     740       768060 :   free_comm_buffer(packed->recv_pack.data);
     741      1571640 :   for (int ipack = 0; ipack < packed->nsend_packs; ipack++) {
     742       803580 :     free_comm_buffer(packed->send_packs[ipack].blocks);
     743       803580 :     free_comm_buffer(packed->send_packs[ipack].data);
     744              :   }
     745       768060 :   free(packed->send_packs);
     746       768060 : }
     747              : 
     748              : /*******************************************************************************
     749              :  * \brief Internal routine for creating a communication iterator.
     750              :  * \author Ole Schuett
     751              :  ******************************************************************************/
     752       384030 : dbm_comm_iterator_t *dbm_comm_iterator_start(const bool transa,
     753              :                                              const bool transb,
     754              :                                              const dbm_matrix_t *matrix_a,
     755              :                                              const dbm_matrix_t *matrix_b,
     756              :                                              const dbm_matrix_t *matrix_c) {
     757       384030 :   dbm_comm_iterator_t *iter = malloc(sizeof(dbm_comm_iterator_t));
     758       384030 :   cp_assert(iter != NULL);
     759       384030 :   iter->dist = matrix_c->dist;
     760              : 
     761              :   // During each communication tick we'll fetch a pack_a and pack_b.
     762              :   // Since the cart might be non-squared, the number of communication ticks is
     763              :   // chosen as the least common multiple of the cart's dimensions.
     764       384030 :   iter->nticks = lcm(iter->dist->rows.nranks, iter->dist->cols.nranks);
     765       384030 :   iter->itick = 0;
     766       384030 :   iter->seconds_alltoall = iter->seconds_sort = 0.0;
     767       384030 :   iter->bytes_alltoall = iter->bytes_shift = 0;
     768              : 
     769              :   // 1.arg=source dimension, 2.arg=target dimension, false=rows, true=columns.
     770       384030 :   iter->packed_a =
     771       384030 :       pack_matrix(transa, false, matrix_a, iter->dist, iter->nticks, iter);
     772       384030 :   iter->packed_b =
     773       384030 :       pack_matrix(!transb, true, matrix_b, iter->dist, iter->nticks, iter);
     774              : 
     775       384030 :   return iter;
     776              : }
     777              : 
     778              : /*******************************************************************************
     779              :  * \brief Internal routine for retrieving next pair of packs of given iterator.
     780              :  * \author Ole Schuett
     781              :  ******************************************************************************/
     782       803580 : bool dbm_comm_iterator_next(dbm_comm_iterator_t *iter, dbm_pack_t **pack_a,
     783              :                             dbm_pack_t **pack_b) {
     784       803580 :   if (iter->itick >= iter->nticks) {
     785              :     return false; // end of iterator reached
     786              :   }
     787              : 
     788              :   // Start each rank at a different tick to spread the load on the sources.
     789       419550 :   const int shift = iter->dist->rows.my_rank + iter->dist->cols.my_rank;
     790       419550 :   const int itick = (iter->itick + shift) % iter->nticks;
     791       839100 :   *pack_a =
     792       419550 :       sendrecv_pack(itick, iter->nticks, &iter->packed_a, &iter->bytes_shift);
     793       839100 :   *pack_b =
     794       419550 :       sendrecv_pack(itick, iter->nticks, &iter->packed_b, &iter->bytes_shift);
     795              : 
     796       419550 :   ++iter->itick;
     797       419550 :   return true;
     798              : }
     799              : 
     800              : /*******************************************************************************
     801              :  * \brief Internal routine for releasing the given communication iterator.
     802              :  * \author Ole Schuett
     803              :  ******************************************************************************/
     804       384030 : void dbm_comm_iterator_stop(dbm_comm_iterator_t *iter) {
     805       384030 :   free_packed_matrix(&iter->packed_a);
     806       384030 :   free_packed_matrix(&iter->packed_b);
     807       384030 :   free(iter);
     808       384030 : }
     809              : 
     810              : // EOF
        

Generated by: LCOV version 2.0-1