LCOV - code coverage report
Current view: top level - src/dbm - dbm_multiply.c (source / functions) Coverage Total Hit
Test: CP2K Regtests (git:1be8651) Lines: 79.7 % 128 102
Test Date: 2026-11-24 01:22:08 Functions: 87.5 % 8 7

            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.h"
       8              : #include "../offload/offload_mempool.h"
       9              : #include "../offload/offload_runtime.h"
      10              : #include "dbm_hyperparams.h"
      11              : #include "dbm_internal.h"
      12              : #include "dbm_library.h"
      13              : #include "dbm_multiply_comm.h"
      14              : #include "dbm_multiply_cpu.h"
      15              : #include "dbm_multiply_gpu.h"
      16              : #include "dbm_multiply_gpu_kernel.h"
      17              : 
      18              : #include "../base/cp_assert.h"
      19              : #include <limits.h>
      20              : #include <math.h>
      21              : #include <omp.h>
      22              : #include <stdio.h>
      23              : #include <stdlib.h>
      24              : #include <string.h>
      25              : 
      26              : /*******************************************************************************
      27              :  * \brief Private routine for computing the max filter threshold for each row.
      28              :  * \author Ole Schuett
      29              :  ******************************************************************************/
      30       384030 : static float *compute_rows_max_eps(const bool trans, const dbm_matrix_t *matrix,
      31              :                                    const double filter_eps) {
      32       384030 :   const int nrows = (trans) ? matrix->ncols : matrix->nrows;
      33       384030 :   int *nblocks_per_row = calloc(nrows, sizeof(int));
      34       384030 :   float *row_max_eps = malloc(nrows * sizeof(float));
      35       384030 :   cp_assert((nblocks_per_row != NULL && row_max_eps != NULL) || nrows == 0);
      36              : 
      37       384030 : #pragma omp parallel
      38              :   {
      39              : #pragma omp for
      40              :     for (int ishard = 0; ishard < dbm_get_num_shards(matrix); ishard++) {
      41              :       dbm_shard_t *shard = &matrix->shards[ishard];
      42              :       for (int iblock = 0; iblock < shard->nblocks; iblock++) {
      43              :         const dbm_block_t *blk = &shard->blocks[iblock];
      44              :         const int row = (trans) ? blk->col : blk->row;
      45              : #pragma omp atomic
      46              :         ++nblocks_per_row[row];
      47              :       }
      48              :     }
      49              : #pragma omp master
      50              :     cp_mpi_sum_int(nblocks_per_row, nrows, matrix->dist->comm);
      51              : #pragma omp barrier
      52              : #pragma omp for
      53              :     for (int i = 0; i < nrows; i++) {
      54              :       const float f =
      55              :           ((float)filter_eps) / ((float)imax(1, nblocks_per_row[i]));
      56              :       row_max_eps[i] = f * f;
      57              :     }
      58              :   } // end of omp parallel region
      59              : 
      60       384030 :   free(nblocks_per_row);
      61       384030 :   return row_max_eps; // Ownership of row_max_eps transfers to caller.
      62              : }
      63              : 
      64              : /*******************************************************************************
      65              :  * \brief Private struct for storing the context of the multiplication backend.
      66              :  * \author Ole Schuett
      67              :  ******************************************************************************/
      68              : typedef struct {
      69              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
      70              :   dbm_multiply_gpu_context_t gpu;
      71              : #endif
      72              :   int cpu_options; // Binary or'ed dbm_multiply_cpu_options (enum).
      73              : } backend_context_t;
      74              : 
      75              : /*******************************************************************************
      76              :  * \brief Private routine for initializing the multiplication backend.
      77              :  * \author Ole Schuett
      78              :  ******************************************************************************/
      79       384030 : static backend_context_t *backend_start(const dbm_matrix_t *matrix_c) {
      80       384030 :   backend_context_t *const ctx = calloc(1, sizeof(backend_context_t));
      81              :   // BLAS and LIBXS benefit in general from DBM_MULTIPLY_TASK_REORDER.
      82       384030 :   ctx->cpu_options = DBM_MULTIPLY_TASK_REORDER;
      83              : 
      84              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
      85              :   dbm_multiply_gpu_start(DBM_MAX_BATCH_SIZE, dbm_get_num_shards(matrix_c),
      86              :                          matrix_c->shards, &ctx->gpu);
      87              : #else
      88       384030 :   (void)matrix_c; // mark as used
      89              : #endif
      90              : 
      91       384030 :   return ctx;
      92              : }
      93              : 
      94              : /*******************************************************************************
      95              :  * \brief Private routine for handing newly arrived packs to the backend.
      96              :  * \author Ole Schuett
      97              :  ******************************************************************************/
      98            0 : static bool backend_upload_packs(const dbm_pack_t *pack_a,
      99              :                                  const dbm_pack_t *pack_b,
     100              :                                  backend_context_t *ctx) {
     101              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
     102              :   return dbm_multiply_gpu_upload_packs(pack_a, pack_b, &ctx->gpu);
     103              : #else
     104            0 :   (void)pack_a; // mark as used
     105            0 :   (void)pack_b;
     106            0 :   (void)ctx;
     107            0 :   return false;
     108              : #endif
     109              : }
     110              : 
     111              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
     112              : /*******************************************************************************
     113              :  * \brief Private routine for ordering a batch in place as the GPU backend asks.
     114              :  *        The batch is the page-locked array that is uploaded subsequently.
     115              :  * \author Hans Pabst
     116              :  ******************************************************************************/
     117              : static void backend_order_batch(dbm_batch_t *batch) {
     118              :   const int order_kind = dbm_multiply_gpu_task_order();
     119              :   const int ntasks = batch->ntasks;
     120              :   if (DBM_TASK_ORDER_NONE != order_kind && 1 < ntasks) {
     121              :     dbm_task_t *const copy =
     122              :         offload_mempool_host_malloc(sizeof(dbm_task_t) * ntasks);
     123              :     int *const order = malloc(sizeof(int) * ntasks);
     124              :     cp_assert(NULL != copy && NULL != order);
     125              :     memcpy(copy, batch->tasks, sizeof(dbm_task_t) * ntasks);
     126              :     dbm_multiply_cpu_task_order(ntasks, copy, order);
     127              :     for (int itask = 0; itask < ntasks; ++itask) {
     128              :       batch->tasks[itask] = copy[order[itask]];
     129              :     }
     130              :     free(order);
     131              :     offload_mempool_host_free(copy);
     132              :   }
     133              : }
     134              : #endif
     135              : 
     136              : /*******************************************************************************
     137              :  * \brief Private routine for sending a batch to the multiplication backend.
     138              :  *        The batch is left empty, i.e., ready to be filled again.
     139              :  * \author Ole Schuett
     140              :  ******************************************************************************/
     141       419628 : static void backend_process_batch(dbm_batch_t *batch, const double alpha,
     142              :                                   const dbm_pack_t *pack_a,
     143              :                                   const dbm_pack_t *pack_b, const int kshard,
     144              :                                   dbm_shard_t *shard_c, const bool finish,
     145              :                                   const bool force_cpu,
     146              :                                   backend_context_t *ctx) {
     147       419628 :   if (NULL != ctx) {
     148              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
     149              :     if (!force_cpu) {
     150              :       backend_order_batch(batch);
     151              :       dbm_multiply_gpu_process_batch(batch, alpha, shard_c, kshard, finish,
     152              :                                      &ctx->gpu);
     153              :     } else
     154              : #endif
     155              :     {
     156       419628 :       (void)kshard;
     157       419628 :       (void)finish;
     158       419628 :       (void)force_cpu;
     159       419628 :       dbm_multiply_cpu_process_batch(batch, alpha, pack_a, pack_b, shard_c,
     160              :                                      ctx->cpu_options);
     161              :     }
     162              :   } else { // Validate against host (aka CPU).
     163            0 :     dbm_multiply_cpu_process_batch(batch, alpha, pack_a, pack_b, shard_c,
     164              :                                    DBM_MULTIPLY_BLAS_LIBRARY);
     165              :   }
     166       419628 :   batch->max_m = batch->max_n = batch->max_k = 0;
     167       419628 :   batch->flops = 0;
     168       419628 :   batch->ntasks = 0;
     169       419628 : }
     170              : 
     171              : /*******************************************************************************
     172              :  * \brief Private routine for shutting down the multiplication backend.
     173              :  * \author Ole Schuett
     174              :  ******************************************************************************/
     175       384030 : static void backend_stop(backend_context_t *ctx) {
     176              : #if defined(__OFFLOAD) && !defined(__NO_OFFLOAD_DBM)
     177              :   dbm_multiply_gpu_stop(&ctx->gpu);
     178              : #endif
     179       384030 :   free(ctx);
     180       384030 : }
     181              : 
     182              : /*******************************************************************************
     183              :  * \brief Private routine returning the seconds since tick, which it resets.
     184              :  * \author Hans Pabst
     185              :  ******************************************************************************/
     186      3562830 : static double phase_lap(double *tick) {
     187      3562830 :   const double now = omp_get_wtime();
     188      3562830 :   const double result = now - *tick;
     189      3562830 :   *tick = now;
     190      3562830 :   return result;
     191              : }
     192              : 
     193              : /*******************************************************************************
     194              :  * \brief Private routine for multiplying two packs (C += alpha * A * B).
     195              :  *
     196              :  * Blocks in each pack are grouped by shard (free_index % nshards) and sorted
     197              :  * by sum_index within each group. The algorithm:
     198              :  *  1. Builds shard-boundary lookup tables for A (rows) and B (cols).
     199              :  *  2. For each (shard_row, shard_col) pair, determines the contiguous A and B
     200              :  *     block ranges belonging to that shard.
     201              :  *  3. Performs a merge-join over sum_index: advances A and B cursors in
     202              :  *lockstep, caching the B sub-range for each sum_index so that multiple A blocks
     203              :  *with the same sum_index reuse it without rescanning.
     204              :  *  4. Applies a norm-based filter (alpha^2 * norm_a * norm_b < eps) for early
     205              :  *     rejection before looking up or allocating the C block.
     206              :  *  5. Accumulates matching pairs into a batched GEMM task list, flushing to the
     207              :  *     backend (CPU or GPU) every DBM_MAX_BATCH_SIZE tasks.
     208              :  *
     209              :  * \author Ole Schuett and Hans Pabst
     210              :  ******************************************************************************/
     211       419550 : static void multiply_packs(const bool transa, const bool transb,
     212              :                            const double alpha, const dbm_pack_t *pack_a,
     213              :                            const dbm_pack_t *pack_b,
     214              :                            const dbm_matrix_t *matrix_a,
     215              :                            const dbm_matrix_t *matrix_b, dbm_matrix_t *matrix_c,
     216              :                            const float *rows_max_eps,
     217              :                            const bool retain_sparsity, const bool force_cpu,
     218              :                            int64_t *flop, backend_context_t *ctx) {
     219              :   // For validation, FLOPS do not count, and relying on ctx is not necessary.
     220       419550 :   backend_context_t *const context = (NULL != flop ? ctx : NULL);
     221       419550 :   const float alpha2 = (float)(alpha * alpha);
     222       419550 :   int64_t flop_sum = 0;
     223              : 
     224       419550 :   const int nshard_rows = matrix_c->dist->rows.nshards;
     225       419550 :   const int nshard_cols = matrix_c->dist->cols.nshards;
     226       419550 :   int *shard_row_start = calloc(nshard_rows, sizeof(int));
     227       419550 :   int *shard_col_start = calloc(nshard_cols, sizeof(int));
     228       419550 :   cp_assert(NULL != shard_row_start && NULL != shard_col_start);
     229              : 
     230       419550 :   const int *sum_index_sizes_a =
     231              :       (transa) ? matrix_a->row_sizes : matrix_a->col_sizes;
     232       419550 :   const int *sum_index_sizes_b =
     233              :       (transb) ? matrix_b->col_sizes : matrix_b->row_sizes;
     234       419550 :   const int *free_index_sizes_a =
     235              :       (transa) ? matrix_a->col_sizes : matrix_a->row_sizes;
     236       419550 :   const int *free_index_sizes_b =
     237              :       (transb) ? matrix_b->row_sizes : matrix_b->col_sizes;
     238              : 
     239       419550 : #pragma omp parallel reduction(+ : flop_sum)
     240              :   {
     241              :     // Thread-private batch covering given work in piece-wise fashion.
     242              :     dbm_batch_t batch = {0};
     243              :     batch.tasks =
     244              :         offload_mempool_host_malloc(sizeof(dbm_task_t) * DBM_MAX_BATCH_SIZE);
     245              :     // Thread-local stats, added to the library's counters once at the end.
     246              :     int64_t counters[DBM_NUM_COUNTERS] = {0};
     247              : 
     248              :     // Blocks are ordered first by shard. Creating lookup tables of boundaries.
     249              : #pragma omp for nowait
     250              :     for (int iblock = 1; iblock < pack_a->nblocks; iblock++) {
     251              :       const int shard_row = pack_a->blocks[iblock].free_index % nshard_rows;
     252              :       const int prev_shard_row =
     253              :           pack_a->blocks[iblock - 1].free_index % nshard_rows;
     254              :       if (prev_shard_row != shard_row) {
     255              :         shard_row_start[shard_row] = iblock;
     256              :       }
     257              :     }
     258              : #pragma omp for
     259              :     for (int jblock = 1; jblock < pack_b->nblocks; jblock++) {
     260              :       const int shard_col = pack_b->blocks[jblock].free_index % nshard_cols;
     261              :       const int prev_shard_col =
     262              :           pack_b->blocks[jblock - 1].free_index % nshard_cols;
     263              :       if (prev_shard_col != shard_col) {
     264              :         shard_col_start[shard_col] = jblock;
     265              :       }
     266              :     }
     267              : 
     268              : #pragma omp for collapse(2) DBM_OMP_SCHEDULE
     269              :     for (int shard_row = 0; shard_row < nshard_rows; shard_row++) {
     270              :       for (int shard_col = 0; shard_col < nshard_cols; shard_col++) {
     271              :         const int ishard = shard_row * nshard_cols + shard_col;
     272              :         dbm_shard_t *const shard_c = &matrix_c->shards[ishard];
     273              :         cp_assert(0 == batch.ntasks); // left empty by the previous shard
     274              : 
     275              :         // Determine contiguous block ranges for this shard in A and B.
     276              :         // Use a merge-join to find pairs of blocks with matching sum indices.
     277              :         // This utilizes that blocks within a shard are ordered by sum_index.
     278              :         const int iblock_start = shard_row_start[shard_row];
     279              :         int iblock_end = pack_a->nblocks;
     280              :         for (int t = iblock_start; t < pack_a->nblocks; ++t) {
     281              :           if (pack_a->blocks[t].free_index % nshard_rows != shard_row) {
     282              :             iblock_end = t;
     283              :             break;
     284              :           }
     285              :         }
     286              :         const int jblock_start = shard_col_start[shard_col];
     287              :         int jblock_end = pack_b->nblocks;
     288              :         for (int t = jblock_start; t < pack_b->nblocks; ++t) {
     289              :           if (pack_b->blocks[t].free_index % nshard_cols != shard_col) {
     290              :             jblock_end = t;
     291              :             break;
     292              :           }
     293              :         }
     294              :         if (iblock_start >= iblock_end || jblock_start >= jblock_end) {
     295              :           backend_process_batch(&batch, alpha, pack_a, pack_b, ishard, shard_c,
     296              :                                 true, force_cpu, context);
     297              :           continue;
     298              :         }
     299              : 
     300              :         // Merge over sum_index (both ranges sorted by sum_index).
     301              :         // Cache the B sub-range for each sum_index so that multiple A blocks
     302              :         // sharing the same sum_index reuse it without re-scanning B.
     303              :         int i = iblock_start, j = jblock_start, last_sum_index = -1;
     304              :         int b_range_start = -1, b_range_end = -1;
     305              : 
     306              :         while (i < iblock_end) {
     307              :           const dbm_pack_block_t *blk_a = &pack_a->blocks[i];
     308              :           const int sum_a = blk_a->sum_index;
     309              : 
     310              :           // Advance j until sum_b >= sum_a.
     311              :           while (j < jblock_end && pack_b->blocks[j].sum_index < sum_a) {
     312              :             ++j;
     313              :           }
     314              :           if (j >= jblock_end) {
     315              :             break; // No more matches possible.
     316              :           }
     317              : 
     318              :           const int sum_b = pack_b->blocks[j].sum_index;
     319              :           if (sum_b > sum_a) {
     320              :             ++i;
     321              :             continue; // Need next A block with higher sum_index.
     322              :           }
     323              : 
     324              :           // sum_a == sum_b: establish (or reuse) B range with this sum_index.
     325              :           if (sum_a != last_sum_index) {
     326              :             b_range_start = j;
     327              :             int t = j + 1;
     328              :             while (t < jblock_end && pack_b->blocks[t].sum_index == sum_a) {
     329              :               ++t;
     330              :             }
     331              :             b_range_end = t;
     332              :             last_sum_index = sum_a;
     333              :           }
     334              : 
     335              :           // Iterate over B blocks in current sum_index range.
     336              :           for (int jb = b_range_start; jb < b_range_end; ++jb) {
     337              :             const dbm_pack_block_t *const blk_b = &pack_b->blocks[jb];
     338              : 
     339              :             // Overlap the two dependent cache misses of upcoming lookups.
     340              :             if (jb + DBM_PREFETCH_SLOT < b_range_end) {
     341              :               dbm_shard_prefetch_slot(
     342              :                   shard_c, blk_a->free_index,
     343              :                   pack_b->blocks[jb + DBM_PREFETCH_SLOT].free_index);
     344              :             }
     345              :             if (jb + DBM_PREFETCH_BLOCK < b_range_end) {
     346              :               dbm_shard_prefetch_block(
     347              :                   shard_c, blk_a->free_index,
     348              :                   pack_b->blocks[jb + DBM_PREFETCH_BLOCK].free_index);
     349              :             }
     350              : 
     351              :             // Norm filter first (early reject).
     352              :             const float result_norm = alpha2 * blk_a->norm * blk_b->norm;
     353              :             if (result_norm < rows_max_eps[blk_a->free_index]) {
     354              :               continue;
     355              :             }
     356              : 
     357              :             // Check block sizes.
     358              :             const int m = free_index_sizes_a[blk_a->free_index];
     359              :             const int n = free_index_sizes_b[blk_b->free_index];
     360              :             const int k = sum_index_sizes_a[sum_a];
     361              :             cp_assert(m == matrix_c->row_sizes[blk_a->free_index]);
     362              :             cp_assert(n == matrix_c->col_sizes[blk_b->free_index]);
     363              :             cp_assert(k == sum_index_sizes_b[blk_b->sum_index]);
     364              : 
     365              :             if (m == 0 || n == 0 || k == 0) {
     366              :               continue;
     367              :             }
     368              : 
     369              :             // Get C block.
     370              :             const int row = blk_a->free_index, col = blk_b->free_index;
     371              :             dbm_block_t *blk_c = dbm_shard_lookup(shard_c, row, col);
     372              :             if (blk_c == NULL) {
     373              :               if (retain_sparsity) {
     374              :                 continue;
     375              :               }
     376              :               cp_assert(dbm_get_shard_index(matrix_c, row, col) == ishard);
     377              :               cp_assert(dbm_get_stored_coordinates(matrix_c, row, col) ==
     378              :                         matrix_c->dist->my_rank);
     379              :               blk_c = dbm_shard_promise_new_block(shard_c, row, col, m * n);
     380              :             }
     381              : 
     382              :             // Count flops.
     383              :             const int64_t task_flops = 2LL * m * n * k;
     384              :             flop_sum += task_flops;
     385              :             ++counters[dbm_library_counter_index(m, n, k)];
     386              : 
     387              :             // Track the shape of the batch.
     388              :             batch.max_m = imax(batch.max_m, m);
     389              :             batch.max_n = imax(batch.max_n, n);
     390              :             batch.max_k = imax(batch.max_k, k);
     391              :             batch.flops += task_flops;
     392              : 
     393              :             // Add block multiplication to batch.
     394              :             dbm_task_t *const tptr = &batch.tasks[batch.ntasks];
     395              :             tptr->offset_a = blk_a->offset;
     396              :             tptr->offset_b = blk_b->offset;
     397              :             tptr->offset_c = blk_c->offset;
     398              :             tptr->m = m;
     399              :             tptr->n = n;
     400              :             tptr->k = k;
     401              :             ++batch.ntasks;
     402              : 
     403              :             if (batch.ntasks == DBM_MAX_BATCH_SIZE) {
     404              :               backend_process_batch(&batch, alpha, pack_a, pack_b, ishard,
     405              :                                     shard_c, false, force_cpu, context);
     406              :             }
     407              :           }
     408              : 
     409              :           // Advance i; if next A block has same sum_index, B range is reused.
     410              :           ++i;
     411              :         }
     412              :         backend_process_batch(&batch, alpha, pack_a, pack_b, ishard, shard_c,
     413              :                               true, force_cpu, context);
     414              :       }
     415              :     }
     416              : 
     417              :     dbm_library_counters_add(counters);
     418              :     offload_mempool_host_free(batch.tasks);
     419              :   }
     420              : 
     421       419550 :   free(shard_row_start);
     422       419550 :   free(shard_col_start);
     423              : 
     424       419550 :   if (NULL != flop) {
     425       419550 :     *flop += flop_sum;
     426              :   }
     427       419550 : }
     428              : 
     429              : /*******************************************************************************
     430              :  * \brief Performs a multiplication of two dbm_matrix_t matrices.
     431              :  *        See dbm_matrix.h for details.
     432              :  * \author Ole Schuett
     433              :  ******************************************************************************/
     434       384030 : void dbm_multiply(const bool transa, const bool transb, const double alpha,
     435              :                   const dbm_matrix_t *matrix_a, const dbm_matrix_t *matrix_b,
     436              :                   const double beta, dbm_matrix_t *matrix_c,
     437              :                   const bool retain_sparsity, const double filter_eps,
     438              :                   int64_t *flop) {
     439       384030 :   cp_assert(omp_get_num_threads() == 1);
     440       384030 :   cp_assert(matrix_a != NULL && matrix_b != NULL && matrix_c != NULL);
     441       384030 :   double phase[DBM_NUM_PHASES] = {0}, tick = omp_get_wtime();
     442       384030 :   int64_t phase_bytes[DBM_NUM_PHASES] = {0};
     443              : 
     444              :   // Throughout the matrix multiplication code the "sum_index" and "free_index"
     445              :   // denote the summation (aka dummy) and free index from the Einstein notation.
     446       384030 :   const int num_sum_index_a = (transa) ? matrix_a->nrows : matrix_a->ncols;
     447       384030 :   const int num_sum_index_b = (transb) ? matrix_b->ncols : matrix_b->nrows;
     448       384030 :   const int num_free_index_a = (transa) ? matrix_a->ncols : matrix_a->nrows;
     449       384030 :   const int num_free_index_b = (transb) ? matrix_b->nrows : matrix_b->ncols;
     450              : 
     451              :   // Sanity check matrix dimensions.
     452       384030 :   cp_assert(num_sum_index_a == num_sum_index_b);
     453       384030 :   cp_assert(num_free_index_a == matrix_c->nrows);
     454       384030 :   cp_assert(num_free_index_b == matrix_c->ncols);
     455              : 
     456              :   // Prepare matrix_c (host).
     457       384030 :   dbm_scale(matrix_c, beta);
     458              : 
     459              :   // Determine if validation shall be performed.
     460       384030 :   const char *const maxeps_env = getenv("DBM_MULTIPLY_MAXEPS");
     461       384030 :   const char *const verify_env = getenv("DBM_MULTIPLY_VERIFY");
     462       384030 :   const double maxeps = (NULL == maxeps_env ? 1E-1 : fabs(atof(maxeps_env)));
     463       768060 :   const int verify =
     464       384030 :       (NULL == verify_env ? (NULL == maxeps_env ? 0 : 1) : atoi(verify_env));
     465       384030 :   dbm_matrix_t *matrix_d = NULL;
     466       384030 :   phase[DBM_PHASE_SETUP] += phase_lap(&tick);
     467       384030 :   if (0 != verify) {
     468            0 :     dbm_distribution_t *const dist_shared = matrix_c->dist;
     469            0 :     dbm_create(&matrix_d, dist_shared, matrix_c->name, matrix_c->nrows,
     470            0 :                matrix_c->ncols, matrix_c->row_sizes, matrix_c->col_sizes);
     471            0 :     dbm_copy(matrix_d, matrix_c);
     472            0 :     tick = omp_get_wtime(); // validation is not a phase
     473              :   }
     474              : 
     475              :   // Compute filter thresholds for each row.
     476       384030 :   float *rows_max_eps = compute_rows_max_eps(transa, matrix_a, filter_eps);
     477              : 
     478              :   // Start uploading matrix_c to the GPU.
     479       384030 :   backend_context_t *ctx = backend_start(matrix_c);
     480       384030 :   phase[DBM_PHASE_SETUP] += phase_lap(&tick);
     481              : 
     482              :   // Redistribute matrix_a and matrix_b across MPI ranks.
     483       384030 :   dbm_comm_iterator_t *iter =
     484       384030 :       dbm_comm_iterator_start(transa, transb, matrix_a, matrix_b, matrix_c);
     485       384030 :   const double redistribute = phase_lap(&tick);
     486       384030 :   phase[DBM_PHASE_ALLTOALL] += iter->seconds_alltoall;
     487       384030 :   phase[DBM_PHASE_SORT] += iter->seconds_sort;
     488       384030 :   phase_bytes[DBM_PHASE_ALLTOALL] += iter->bytes_alltoall;
     489       384030 :   phase[DBM_PHASE_PACK] +=
     490       384030 :       redistribute - iter->seconds_alltoall - iter->seconds_sort;
     491              : 
     492              :   // Count flops if requested.
     493       384030 :   if (NULL != flop) {
     494       384030 :     *flop = 0;
     495              :   }
     496              : 
     497              :   // Main loop.
     498              :   dbm_pack_t *pack_a, *pack_b;
     499       803580 :   while (dbm_comm_iterator_next(iter, &pack_a, &pack_b)) {
     500       419550 :     phase[DBM_PHASE_SHIFT] += phase_lap(&tick);
     501       419550 :     const bool uploaded = backend_upload_packs(pack_a, pack_b, ctx);
     502       419550 :     (void)uploaded; // mark used
     503       419550 :     phase[DBM_PHASE_UPLOAD] += phase_lap(&tick);
     504       419550 :     multiply_packs(transa, transb, alpha, pack_a, pack_b, matrix_a, matrix_b,
     505              :                    matrix_c, rows_max_eps, retain_sparsity, false /*!uploaded*/,
     506              :                    flop, ctx);
     507       419550 :     phase[DBM_PHASE_MULTIPLY] += phase_lap(&tick);
     508              :   }
     509              : 
     510              :   // Wait for all other MPI ranks to complete, then release ressources.
     511       384030 :   phase_bytes[DBM_PHASE_SHIFT] += iter->bytes_shift;
     512       384030 :   dbm_comm_iterator_stop(iter);
     513       384030 :   phase[DBM_PHASE_SHIFT] += phase_lap(&tick);
     514       384030 :   backend_stop(ctx);
     515       384030 :   phase[DBM_PHASE_FINISH] += phase_lap(&tick);
     516              : 
     517       384030 :   if (NULL != matrix_d) {
     518            0 :     ctx = backend_start(matrix_d);
     519            0 :     iter =
     520            0 :         dbm_comm_iterator_start(transa, transb, matrix_a, matrix_b, matrix_d);
     521            0 :     while (dbm_comm_iterator_next(iter, &pack_a, &pack_b)) {
     522            0 :       multiply_packs(transa, transb, alpha, pack_a, pack_b, matrix_a, matrix_b,
     523              :                      matrix_d, rows_max_eps, retain_sparsity, true, NULL, ctx);
     524              :     }
     525            0 :     dbm_comm_iterator_stop(iter);
     526            0 :     backend_stop(ctx);
     527            0 :     const double epsilon = dbm_maxeps(matrix_d, matrix_c);
     528            0 :     if (maxeps < epsilon) {
     529            0 :       if (1 == verify) {
     530            0 :         fprintf(stderr, "WARN ACC/LIBDBM: diff=%g\n", epsilon);
     531              :       } else {
     532            0 :         fprintf(stderr, "ERROR ACC/LIBDBM: diff=%g\n", epsilon);
     533            0 :         exit(EXIT_FAILURE);
     534              :       }
     535              :     }
     536            0 :     dbm_release(matrix_d);
     537            0 :     tick = omp_get_wtime(); // validation is not a phase
     538              :   }
     539              : 
     540              :   // Release filter thresholds.
     541       384030 :   free(rows_max_eps);
     542              : 
     543              :   // Final filter pass.
     544       384030 :   dbm_filter(matrix_c, filter_eps);
     545       384030 :   phase[DBM_PHASE_SETUP] += phase_lap(&tick);
     546       384030 :   dbm_library_phases_add(phase, phase_bytes);
     547       384030 : }
     548              : 
     549              : // EOF
        

Generated by: LCOV version 2.0-1