Skip to content

Latest commit

 

History

History
91 lines (55 loc) · 2.18 KB

2024-11-27-rest.md

File metadata and controls

91 lines (55 loc) · 2.18 KB
marp theme headingDivider
true
ginkgo
2

Passing Data out of Ginkgo

Note: Only possible as views.

Examples for accessing the underlying pointer(s):

  • Array: arr.get_data()
  • Dense vector: vec.get_values()
  • CSR matrix: mtx.get_row_ptrs(), mtx.get_col_idxs(), mtx.get_values()

Example: Calling BLAS Routine with Ginkgo Data

auto x = gko::matrix::Dense<float>::create(exec, gko::dim<2>{n_row, 1});
auto y = gko::matrix::Dense<float>::create(exec, gko::dim<2>{n_row, 1});

cblas_srot(n_row, x->get_values(), x->get_stride(), y->get_values(), y->get_stride(),
           cos, sin);

Example: Passing Ginkgo Data to Kokkos

Special case for passing data to Kokkos via gko::ext::kokkos::map_data. Works for gko::array and gko::matrix::Dense.

auto g_vec = gko::matrix::Dense<float>::create(exec, gko::dim<2>{n_row, 1});

auto k_vec = gko::ext::kokkos::map_data(g_vec); // this is a 2D Kokkos::View
Kokkos::parallel_for(
  n_row, KOKKOS_LAMBDA(int i){           
    const float h = 1.0f / (discretization_points + 1);
    const float xi = ValueType(i + 1) * h;
    k_vec[i] = xi * xi;
  }
);

Advanced gko::array<T> Topics

Cross Executor Copy and Move

Scenario: copy construct (assign) arrays with different executors

auto cpu_exec = gko::OmpExecutor::create();
auto gpu_exec = gko::CudaExecutor::create(0, cpu_exec);

gko::array<int> arr_cpu(cpu_exec, ...);
gko::array<int> arr_gpu(gpu_exec);

arr_gpu = arr_cpu;

The data will be automatically copied between the CPU and GPU.

Using arr_gpu = std::move(arr_cpu) involves a copy instead of a move.

Note: gko::array<int> arr_gpu(arr_cpu) will create arr_gpu using the executor from arr_cpu.

Executor behavior

The executor doesn't change implicitly. After assignment arr_a = arr_b the executor of arr_a will be the same as before the assignment. The same holds for move assignments.

Explicitly change executor with arr.set_executor(other_exec)

Exceptions: When arr_a was default constructed, the assignment changes the executor.

Default constructed array has no executor Change executor with array.set_executor(other_exec)