Assigning an item element

In the SYCL 1.2.1 spec, the SYCL item class includes the following operator definition:

size_t &operator[](int dimension);

This is described as returning the constituent id l-value. In ComputeCpp 1.1.2’s item_base.h I can only see:

size_t operator[](int dimension) const;

This means the following (say) will not compile:

#include <CL/sycl.hpp>

namespace kernels { class K; }

int main(int argc, char *argv[])
{
  using namespace cl::sycl;
  queue q;

  q.submit([&](handler &cgh) {
    cgh.parallel_for<kernels::K>(range<3>{4,4,4}, [=](item<3> ix) {
      ix[0] = 0;
    });
  });

  return 0;
}

Indeed it’s unlikely that such an assignment would be a good idea, but it seems to be allowed in the specification. Do you think the spec will change on this point?

Hi Paul,

So the id and range classes do allow you to modify the elements via the subscript operator if the object is non-const. However, the intention of SYCL 1.2.1 is that the id and range objects retrieved via the kernel invocation API are read-only, so the item subscript operator and get_id member function return by value.

I believe the function you mentioned above is on the id and range class but not on the item class.

The example above could be changed to:

q.submit([&](handler &cgh) {
  cgh.parallel_for<kernels::K>(range<3>{4,4,4}, [=](item<3> ix) {
    auto id = ix.get_id();
    id[0] = 0;
  });
});

However this would not modify the id within the item object, just the local copy.

I hope this helps.

Gordon

Isn’t the operator[] method I mentioned included on listing line 13 on page 150 as a member of item in the current (1.2.1) spec?
Thanks,
Paul

Hi Paul,

The item class does have the subscript operator you mention, though it returns by-value, so you cannot assign to the result.

The subscript operator on the id class has the same operator but returns size_t & so you can modify it as you describe.

I noticed the page number you mentioned didn’t quite line up with what I have, perhaps you have a slightly older version of the spec, we have been releasing regular updates with errata fixes. You can find the latest in the Khronos registry here: https://www.khronos.org/registry/SYCL/specs/sycl-1.2.1.pdf.

I hope this helps,

Gordon

Hi Gordon,

Thanks. Yes, I was looking at SYCL 1.2.1 Revision 3 from July 19, 2018. I see that Revision 5 is available since April 18 this year, and the operator[] definition is changed as you describe.

Cheers,
Paul