|
| 1 | +// RUN: %{build} -o %t.out |
| 2 | +// RUN: %{run} %t.out |
| 3 | +//==-------- in_order_multi_queue_host_task.cpp ---------------------------==// |
| 4 | +// |
| 5 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | +// See https://llvm.org/LICENSE.txt for license information. |
| 7 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | +// |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +#include <condition_variable> |
| 11 | +#include <iostream> |
| 12 | + |
| 13 | +#include <sycl/detail/core.hpp> |
| 14 | +#include <sycl/properties/all_properties.hpp> |
| 15 | +#include <sycl/usm.hpp> |
| 16 | + |
| 17 | +using namespace sycl; |
| 18 | + |
| 19 | +const int dataSize = 1024; |
| 20 | + |
| 21 | +int main() { |
| 22 | + queue Queue1{property::queue::in_order()}; |
| 23 | + queue Queue2{property::queue::in_order()}; |
| 24 | + |
| 25 | + int *dataA = malloc_host<int>(dataSize, Queue1); |
| 26 | + int *dataB = malloc_host<int>(dataSize, Queue1); |
| 27 | + int *dataC = malloc_host<int>(dataSize, Queue1); |
| 28 | + |
| 29 | + bool ready = false; |
| 30 | + std::mutex host_task_mtx; |
| 31 | + std::condition_variable cv; |
| 32 | + |
| 33 | + auto Event1 = Queue1.submit([&](handler &cgh) { |
| 34 | + cgh.host_task([&] { |
| 35 | + std::unique_lock<std::mutex> lk(host_task_mtx); |
| 36 | + cv.wait(lk, [&] { return ready; }); |
| 37 | + for (size_t i = 0; i < dataSize; ++i) { |
| 38 | + dataA[i] = i; |
| 39 | + } |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + Queue2.submit([&](handler &cgh) { |
| 44 | + cgh.depends_on(Event1); |
| 45 | + cgh.parallel_for(range<1>(dataSize), |
| 46 | + [=](id<1> idx) { dataB[idx[0]] = dataA[idx[0]]; }); |
| 47 | + }); |
| 48 | + |
| 49 | + { |
| 50 | + std::unique_lock<std::mutex> lk(host_task_mtx); |
| 51 | + ready = true; |
| 52 | + } |
| 53 | + cv.notify_one(); |
| 54 | + |
| 55 | + Queue2.wait(); |
| 56 | + |
| 57 | + for (size_t i = 0; i != dataSize; ++i) { |
| 58 | + if (dataB[i] != i) { |
| 59 | + std::cout << "Result mismatches " << dataB[i] << " vs expected " << i |
| 60 | + << " for index " << i << std::endl; |
| 61 | + return 1; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + return 0; |
| 66 | +} |
0 commit comments