| 
 | 1 | +/***************************************************************************  | 
 | 2 | + *  | 
 | 3 | + * Copyright (C) 2018 Codeplay Software Limited  | 
 | 4 | + * Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 5 | + * you may not use this file except in compliance with the License.  | 
 | 6 | + * You may obtain a copy of the License at  | 
 | 7 | + *  | 
 | 8 | + * http://www.apache.org/licenses/LICENSE-2.0  | 
 | 9 | + *  | 
 | 10 | + * For your convenience, a copy of the License has been included in this  | 
 | 11 | + * repository.  | 
 | 12 | + *  | 
 | 13 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 14 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 16 | + * See the License for the specific language governing permissions and  | 
 | 17 | + * limitations under the License.  | 
 | 18 | + *  | 
 | 19 | + * Codeplay's ComputeCpp SDK  | 
 | 20 | + *  | 
 | 21 | + * placeholder-accessors.cpp  | 
 | 22 | + *  | 
 | 23 | + * Description:  | 
 | 24 | + * Sample code that illustrates how to use placeholder accessors.  | 
 | 25 | + *  | 
 | 26 | + **************************************************************************/  | 
 | 27 | + | 
 | 28 | +#include <CL/sycl.hpp>  | 
 | 29 | + | 
 | 30 | +#include <iostream>  | 
 | 31 | + | 
 | 32 | +using namespace cl::sycl;  | 
 | 33 | + | 
 | 34 | +namespace {  | 
 | 35 | +class Worker;  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | +class Doubler {  | 
 | 39 | + /* These are placeholder accessors a class members so they are visible to both  | 
 | 40 | + * perform_doubling and operator(). As placeholder accessors, they do not have  | 
 | 41 | + * a buffer associated with them yet. It will be attached during the command  | 
 | 42 | + * group */  | 
 | 43 | + accessor<const uint8_t, 1, access::mode::read, access::target::global_buffer,  | 
 | 44 | + access::placeholder::true_t>  | 
 | 45 | + in_accessor;  | 
 | 46 | + accessor<uint8_t, 1, access::mode::discard_write,  | 
 | 47 | + access::target::global_buffer, access::placeholder::true_t>  | 
 | 48 | + out_accessor;  | 
 | 49 | + | 
 | 50 | + public:  | 
 | 51 | + void perform_doubling(const uint8_t* input, uint8_t* output, size_t items) {  | 
 | 52 | + try {  | 
 | 53 | + queue myQueue;  | 
 | 54 | + | 
 | 55 | + /* Create buffers from the provided pointers */  | 
 | 56 | + auto in_buffer = buffer<const uint8_t, 1>(input, items);  | 
 | 57 | + auto out_buffer = buffer<uint8_t, 1>(output, items);  | 
 | 58 | + | 
 | 59 | + myQueue.submit([&](handler& cgh) {  | 
 | 60 | + /* Associate buffers with the accessors */  | 
 | 61 | + cgh.require(in_buffer, in_accessor);  | 
 | 62 | + cgh.require(out_buffer, out_accessor);  | 
 | 63 | + | 
 | 64 | + /* The accessors can now be used to access the buffers created above */  | 
 | 65 | + cgh.parallel_for<Worker>(range<1>(items), *this);  | 
 | 66 | + });  | 
 | 67 | + } catch (const exception& e) {  | 
 | 68 | + std::cerr << "SYCL exception caught: " << e.what() << "\n";  | 
 | 69 | + throw;  | 
 | 70 | + }  | 
 | 71 | + }  | 
 | 72 | + | 
 | 73 | + void operator()(item<1> item) { out_accessor[item] = in_accessor[item] * 2; }  | 
 | 74 | +};  | 
 | 75 | + | 
 | 76 | +/* Input data, and array length */  | 
 | 77 | +constexpr size_t num_items = 10;  | 
 | 78 | +const std::array<uint8_t, num_items> values = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};  | 
 | 79 | + | 
 | 80 | +int main() {  | 
 | 81 | + Doubler doubler{};  | 
 | 82 | + | 
 | 83 | + std::array<uint8_t, num_items> output{};  | 
 | 84 | + doubler.perform_doubling(values.data(), output.data(), num_items);  | 
 | 85 | + | 
 | 86 | + /* We check that the result is correct. */  | 
 | 87 | + if (!std::equal(  | 
 | 88 | + output.begin(), output.end(), values.begin(),  | 
 | 89 | + [](const auto out, const auto in) { return out == in * 2; })) {  | 
 | 90 | + std::cerr << "Resulting output is wrong!\n";  | 
 | 91 | + return 1;  | 
 | 92 | + }  | 
 | 93 | + return 0;  | 
 | 94 | +}  | 
0 commit comments