Compressed Sparse Rows (CSR) Accessor#

The csr_accessor class provides read-only access to the rows of the csr table as data arrays in CSR storage format.

Usage Example#

#include <sycl/sycl.hpp>
#include <iostream>

#include "oneapi/dal/table/csr.hpp"
#include "oneapi/dal/table/csr_accessor.hpp"

namespace dal = oneapi::dal;

int main() {
   sycl::queue queue { sycl::default_selector() };

   // Create arrays of data, column indices and row offsets of the table
   // in sparse CSR storage format with one-based indexing on host
   const float host_data[] = { 1.0f, 2.0f, 3.0f, 4.0f, 1.0f, 11.0f, 8.0f };
   const std::int64_t host_column_indices[] = { 1, 2, 4, 3, 2, 4, 2 };
   const std::int64_t host_row_offsets[] = { 1, 4, 5, 7, 8 };

   constexpr std::int64_t row_count = 4;
   constexpr std::int64_t column_count = 4;
   constexpr std::int64_t element_count = 7;

   // Allocate SYCL shared memory for storing data, column indices and row offsets arrays
   auto shared_data = sycl::malloc_shared<float>(element_count, queue);
   auto shared_column_indices = sycl::malloc_shared<std::int64_t>(element_count, queue);
   auto shared_row_offsets = sycl::malloc_shared<std::int64_t>(row_count + 1, queue);

   // Copy data, column indices and row offsets arrays from host to SYCL shared memory
   auto data_event = queue.memcpy(shared_data, host_data, sizeof(float) * element_count);
   auto column_indices_event = queue.memcpy(shared_column_indices,
                                            host_column_indices,
                                            sizeof(std::int64_t) * element_count);
   auto row_offsets_event =
      queue.memcpy(shared_row_offsets, host_row_offsets, sizeof(std::int64_t) * (row_count + 1));

   auto table = dal::csr_table::wrap(queue,
                                     shared_data,
                                     shared_column_indices,
                                     shared_row_offsets,
                                     row_count,
                                     column_count,
                                     dal::sparse_indexing::one_based,
                                     { data_event, column_indices_event, row_offsets_event });

   // Accessing second and third rows of the table
   dal::csr_accessor<const float> acc{ table };

   const auto [block_data, block_column_indices, block_row_offsets] = acc.pull(queue, { 1, 3 });

   for (std::int64_t i = 0; i < block_data.get_count(); i++) {
      std::cout << block_data[i] << ", ";
   }
   std::cout << std::endl;

   for (std::int64_t i = 0; i < block_column_indices.get_count(); i++) {
      std::cout << block_column_indices[i] << ", ";
   }
   std::cout << std::endl;

   for (std::int64_t i = 0; i < block_row_offsets.get_count(); i++) {
      std::cout << block_row_offsets[i] << ", ";
   }
   std::cout << std::endl;

   sycl::free(shared_data, queue);
   sycl::free(shared_column_indices, queue);
   sycl::free(shared_row_offsets, queue);
   return 0;
}

Programming Interface#

All types and functions in this section are declared in the oneapi::dal namespace and available via the inclusion of the oneapi/dal/table/csr_accessor.hpp header file.

template<typename T>
class csr_accessor#
Template Parameters

T – The type of data values in blocks returned by the accessor. Should be const-qualified for read-only access. An accessor supports at least float, double, and std::int32_t types of T.

Constructors

template<typename U = T, std::enable_if_t<std::is_const_v<U>, int> = 0>
csr_accessor(const csr_table &table)#

Creates a read-only accessor object from the csr table. Available only for const-qualified T.

Template Parameters

U – The type of data values in blocks returned by the accessor. Should be const-qualified for read-only access. An accessor supports at least float, double, and std::int32_t types of U.

Parameters

table – Input CSR table.

Public Methods

std::tuple<array_d, array_i, array_i> pull(const range &row_range = {0, -1}, const sparse_indexing indexing = sparse_indexing::one_based) const#

Provides access to the rows of the table in CSR format The method returns arrays that directly point to the memory within the table if it is possible. In that case, the arrays refer to the memory as immutable data. Otherwise, new memory blocks are allocated, and the data from the table rows is converted and copied into those blocks. In this case, arrays refer to the blocks as mutable data.

Parameters
  • row_range – The range of rows that data is returned from the accessor.

  • indexing – The indexing scheme used to access the data in the returned arrays in CSR layout. Should be sparse_indexing::zero_based or sparse_indexing::one_based.

std::tuple<T*, I*, I*> pull(array_d &data, array_i &column_indices, array_i &row_offsets, const range &row_range = {0, -1}, const sparse_indexing indexing = sparse_indexing::one_based) const#

Provides access to the rows of the table in CSR format The method returns arrays that directly point to the memory within the table if it is possible. In that case, the arrays refer to the memory as immutable data. Otherwise, new memory blocks are allocated, and the data from the table rows is converted and copied into those blocks. In this case, arrays refer to the blocks as mutable data. The method updates data, column_indices, and row_offsets arrays.

Parameters
  • data – The block in which memory is reused (if it is possible) to obtain the values from the table.

  • column_indices – The block in which memory is reused (if it is possible) to obtain the column indices from the table.

  • row_offsets – The block in which memory is reused (if it is possible) to obtain the row offsets from the table. The memory of data, column_indices and row_offsets blocks are reset either when their size is not big enough, or when the blocks contain immutable data, or when direct memory from the table can be used. If the blocks are reset to use direct memory pointers from the object, they refer to those pointers as immutable memory blocks.

  • row_range – The range of rows that data is returned from the accessor.

  • indexing – The indexing scheme used to access the data in the returned arrays in CSR layout. Should be sparse_indexing::zero_based or sparse_indexing::one_based.