|
| 1 | +//==---- prefetch.cpp - USM prefetch test ----------------------------------==// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// RUN: %clangxx -fsycl %s -o %t1.out -lOpenCL |
| 9 | +// RUN: env SYCL_DEVICE_TYPE=HOST %t1.out |
| 10 | +// RUN: %CPU_RUN_PLACEHOLDER %t1.out |
| 11 | + |
| 12 | +#include <CL/sycl.hpp> |
| 13 | + |
| 14 | +using namespace cl::sycl; |
| 15 | + |
| 16 | +static constexpr int count = 100; |
| 17 | + |
| 18 | +int main() { |
| 19 | + queue q([](exception_list el) { |
| 20 | + for (auto &e : el) |
| 21 | + throw e; |
| 22 | + }); |
| 23 | + float *src = (float*)malloc_shared(sizeof(float) * count, q.get_device(), |
| 24 | + q.get_context()); |
| 25 | + float *dest = (float*)malloc_shared(sizeof(float) * count, q.get_device(), |
| 26 | + q.get_context()); |
| 27 | + for (int i = 0; i < count; i++) |
| 28 | + src[i] = i; |
| 29 | + |
| 30 | + // Test handler::prefetch |
| 31 | + { |
| 32 | + event init_prefetch = q.submit([&](handler &cgh) { |
| 33 | + cgh.prefetch(src, sizeof(float) * count); |
| 34 | + }); |
| 35 | + |
| 36 | + q.submit([&](handler &cgh) { |
| 37 | + cgh.depends_on(init_prefetch); |
| 38 | + cgh.single_task<class double_dest>([=]() { |
| 39 | + for (int i = 0; i < count; i++) |
| 40 | + dest[i] = 2 * src[i]; |
| 41 | + }); |
| 42 | + }); |
| 43 | + q.wait_and_throw(); |
| 44 | + |
| 45 | + for (int i = 0; i < count; i++) { |
| 46 | + assert(dest[i] == i * 2); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Test queue::prefetch |
| 51 | + { |
| 52 | + event init_prefetch = q.prefetch(src, sizeof(float) * count); |
| 53 | + |
| 54 | + q.submit([&](handler &cgh) { |
| 55 | + cgh.depends_on(init_prefetch); |
| 56 | + cgh.single_task<class double_dest3>([=]() { |
| 57 | + for (int i = 0; i < count; i++) |
| 58 | + dest[i] = 3 * src[i]; |
| 59 | + }); |
| 60 | + }); |
| 61 | + q.wait_and_throw(); |
| 62 | + |
| 63 | + for (int i = 0; i < count; i++) { |
| 64 | + assert(dest[i] == i * 3); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + // Test ordered_queue::prefetch |
| 69 | + { |
| 70 | + ordered_queue oq([](exception_list el) { |
| 71 | + for (auto &e : el) |
| 72 | + throw e; |
| 73 | + }); |
| 74 | + event init_prefetch = oq.prefetch(src, sizeof(float) * count); |
| 75 | + |
| 76 | + oq.submit([&](handler &cgh) { |
| 77 | + cgh.depends_on(init_prefetch); |
| 78 | + cgh.single_task<class double_dest4>([=]() { |
| 79 | + for (int i = 0; i < count; i++) |
| 80 | + dest[i] = 4 * src[i]; |
| 81 | + }); |
| 82 | + }); |
| 83 | + oq.wait_and_throw(); |
| 84 | + |
| 85 | + for (int i = 0; i < count; i++) { |
| 86 | + assert(dest[i] == i * 4); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments