code: def _coalesce_dense(row, col, val, nrows, ncols, op):
      # Sparse matrix coalescing on a dense matrix.
      #
      # It is done by stacking every non-zero entry on an individual slice
      # of an (nrows x ncols x nnz), that is, construct a tensor A with
      # shape (nrows, ncols, len(val)) where
      #
      #     A[row[i], col[i], i] = val[i]
      #
      # and then reducing on the third "nnz" dimension.
      #
      # The mask matrix M has the same sparsity pattern as A with 1 being
      # the non-zero entries.  This is used for division if the reduce
      # operator is mean.
      M = torch.zeros(NUM_ROWS, NUM_COLS, device=F.ctx())
      A = torch.full(
          (NUM_ROWS, NUM_COLS, 20) + val.shape[1:],
          default_entry[op],
          device=F.ctx(),
          dtype=val.dtype,
      )
      A = torch.index_put(A, (row, col, torch.arange(20)), val)
      for i in range(20):
          M[row[i], col[i]] += 1
      if op == "mean":
          A = A.sum(2)
      else:
          A = getattr(A, op)(2)
      M = M.view(NUM_ROWS, NUM_COLS, *([1] * (val.dim() - 1)))
      return A, M
  
file_path: dgl/tests/python/pytorch/sparse/test_reduction.py
imports: import doctest
  import operator
  import sys
  import backend
  import dgl.sparse
  import pytest
  import torch
label_desc: Use image node_id 1 for calling a global function with example usage: _coalesce_dense(row, col, val, nrows, ncols, op) and returns: A,  M