Skip to content

Commit

Permalink
Add a pointer to the first element of the vector
Browse files Browse the repository at this point in the history
  • Loading branch information
aacostadiaz committed Aug 2, 2023
1 parent 57c2913 commit 02ea296
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions include/views/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ struct VectorView {
index_t size_;
increment_t strd_; // never size_t, because it could be negative

// Start of the vector.
// If stride is negative, start at the end of the vector and move backward.
container_t ptr_;

VectorView(view_container_t data, view_increment_t strd, view_index_t size);
VectorView(VectorView<view_container_t, view_index_t, view_increment_t> opV,
view_increment_t strd, view_index_t size);
Expand Down Expand Up @@ -97,17 +101,13 @@ struct VectorView {
template <bool use_as_ptr = false>
SYCL_BLAS_INLINE typename std::enable_if<!use_as_ptr, value_t &>::type eval(
index_t i) {
return (strd_ == 1) ? *(data_ + i)
: (strd_ > 0) ? *(data_ + i * strd_)
: *(data_ + (size_ * -strd_) + ((i + 1) * strd_));
return (strd_ == 1) ? *(ptr_ + i) : *(ptr_ + i * strd_);
}

template <bool use_as_ptr = false>
SYCL_BLAS_INLINE typename std::enable_if<!use_as_ptr, value_t>::type eval(
index_t i) const {
return (strd_ == 1) ? *(data_ + i)
: (strd_ > 0) ? *(data_ + i * strd_)
: *(data_ + (size_ * -strd_) + ((i + 1) * strd_));
return (strd_ == 1) ? *(ptr_ + i) : *(ptr_ + i * strd_);
}

SYCL_BLAS_INLINE value_t &eval(cl::sycl::nd_item<1> ndItem) {
Expand All @@ -121,13 +121,13 @@ struct VectorView {
template <bool use_as_ptr = false>
SYCL_BLAS_INLINE typename std::enable_if<use_as_ptr, value_t &>::type eval(
index_t indx) {
return *(data_ + indx);
return *(ptr_ + indx);
}

template <bool use_as_ptr = false>
SYCL_BLAS_INLINE typename std::enable_if<use_as_ptr, value_t>::type eval(
index_t indx) const noexcept {
return *(data_ + indx);
return *(ptr_ + indx);
}
};

Expand Down
4 changes: 2 additions & 2 deletions src/views/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ SYCL_BLAS_INLINE VectorView<_container_t, _IndexType,
_IncrementType>::VectorView(_container_t data,
_IncrementType strd,
_IndexType size)
: data_(data), size_(size), strd_(strd) {}
: data_(data), size_(size), strd_(strd), ptr_(strd > 0 ? data_ : data_ + (size_ - 1) * (-strd_)) {}

/*!
@brief Creates a view from an existing view.
Expand All @@ -55,7 +55,7 @@ SYCL_BLAS_INLINE
VectorView<_container_t, _IndexType, _IncrementType>::VectorView(
VectorView<_container_t, _IndexType, _IncrementType> opV,
_IncrementType strd, _IndexType size)
: data_(opV.get_data()), size_(size), strd_(strd) {}
: data_(opV.get_data()), size_(size), strd_(strd), ptr_(strd > 0 ? data_ : data_ + (size_ - 1) * (-strd_)) {}

/*!
* @brief Returns a reference to the container
Expand Down

0 comments on commit 02ea296

Please sign in to comment.