KhronosGroup / SYCL-Docs

SYCL Open Source Specification
Other
114 stars 67 forks source link

Example from 4.12.1. Defining kernels as named function objects is misleading #198

Open keryell opened 3 years ago

keryell commented 3 years ago

The example

 class RandomFiller {
 public:
  RandomFiller(accessor<int> ptr)
      : ptr_ { ptr } {
    std::random_device hwRand;
    std::uniform_int_distribution<> r { 1, 100 };
    randomNum_ = r(hwRand);
  }
  void operator()(item<1> item) const { ptr_[item.get_id()] = get_random(); }
  int get_random() { return randomNum_; }

 private:
  accessor<int> ptr_;
  int randomNum_;
};

void workFunction(buffer<int, 1>& b, queue& q, const range<1> r) {
    myQueue.submit([&](handler& cgh) {
      accessor ptr { buf, cgh };
      RandomFiller filler { ptr };

      cgh.parallel_for(r, filler);
    });
}

is kind of misleading because of the useless call to get_random() instead of returning directly randomNum_, which might suggest with the call operator that a different random number is used for each work-item.

fraggamuffin commented 2 years ago

simplify with direct member