Sample solution

Content

#include <cstddef>
#include <printf.hpp>
#include <hpc/cuda/check.hpp>
#include <hpc/cuda/copy.hpp>
#include <hpc/cuda/gematrix.hpp>
#include <hpc/matvec/gematrix.hpp>
#include <hpc/ulmblas/scal.hpp>

template<typename Alpha, typename TX>
__global__ void gescal(std::size_t m, std::size_t n, Alpha alpha,
      TX* X, std::ptrdiff_t incRowX, std::ptrdiff_t incColX) {
   std::size_t i = threadIdx.x + blockIdx.x * blockDim.x;
   std::size_t j = threadIdx.y + blockIdx.y * blockDim.y;
   if (i < m && j < n) {
      X[i*incRowX + j*incColX] *= alpha;
   }
}

int main() {
   hpc::matvec::GeMatrix<double> A(48, 4);
   for (std::size_t i = 0; i < A.numRows(); ++i) {
      for (std::size_t j = 0; j < A.numCols(); ++j) {
	 A(i, j) = 1000 * i + j;
      }
   }
   hpc::cuda::DeviceGeMatrix<double> B(A.numRows(), A.numCols());
   hpc::cuda::copy(A, B);

   std::size_t dim_per_block = 16;
   std::size_t num_blocks_per_row = (A.numRows() + dim_per_block - 1) /
      dim_per_block;
   std::size_t num_blocks_per_col = (A.numCols() + dim_per_block - 1) /
      dim_per_block;
   dim3 block(dim_per_block, dim_per_block);
   dim3 grid(num_blocks_per_row, num_blocks_per_col);

   gescal<<<grid, block>>>(B.numRows(), B.numCols(),
      2, B.data(), B.incRow(), B.incCol());

   hpc::cuda::copy(B, A);
   for (std::size_t i = 0; i < A.numRows(); ++i) {
      for (std::size_t j = 0; j < A.numCols(); ++j) {
	 fmt::printf(" %12lg", A(i, j));
      }
      fmt::printf("\n");
   }
}
livingstone$ cp /home/numerik/pub/hpc/ws19/session28/misc/Makefile .
livingstone$ make depend
gcc-makedepend -std=c++14 -I/home/numerik/pub/hpc/ws19/session28 -D__CUDACC__ -x c++ gescal.cu
livingstone$ make
nvcc -c -std=c++14 -I/home/numerik/pub/hpc/ws19/session28  gescal.cu
nvcc -w -o gescal -std=c++14  gescal.o
livingstone$ ./gescal
            0            2            4            6
         2000         2002         2004         2006
         4000         4002         4004         4006
         6000         6002         6004         6006
         8000         8002         8004         8006
        10000        10002        10004        10006
        12000        12002        12004        12006
        14000        14002        14004        14006
        16000        16002        16004        16006
        18000        18002        18004        18006
        20000        20002        20004        20006
        22000        22002        22004        22006
        24000        24002        24004        24006
        26000        26002        26004        26006
        28000        28002        28004        28006
        30000        30002        30004        30006
        32000        32002        32004        32006
        34000        34002        34004        34006
        36000        36002        36004        36006
        38000        38002        38004        38006
        40000        40002        40004        40006
        42000        42002        42004        42006
        44000        44002        44004        44006
        46000        46002        46004        46006
        48000        48002        48004        48006
        50000        50002        50004        50006
        52000        52002        52004        52006
        54000        54002        54004        54006
        56000        56002        56004        56006
        58000        58002        58004        58006
        60000        60002        60004        60006
        62000        62002        62004        62006
        64000        64002        64004        64006
        66000        66002        66004        66006
        68000        68002        68004        68006
        70000        70002        70004        70006
        72000        72002        72004        72006
        74000        74002        74004        74006
        76000        76002        76004        76006
        78000        78002        78004        78006
        80000        80002        80004        80006
        82000        82002        82004        82006
        84000        84002        84004        84006
        86000        86002        86004        86006
        88000        88002        88004        88006
        90000        90002        90004        90006
        92000        92002        92004        92006
        94000        94002        94004        94006
livingstone$