forked from intel/llvm
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include <iostream> | ||
#include <CL/sycl.hpp> | ||
|
||
#include <sycl/ext/oneapi/experimental/graph.hpp> | ||
|
||
const size_t n = 10; | ||
|
||
float host_gold_result() { | ||
float alpha = 1.0f; | ||
float beta = 2.0f; | ||
float gamma = 3.0f; | ||
|
||
float sum = 0.0f; | ||
|
||
for(size_t i = 0; i < n; ++i) { | ||
sum += (alpha * 1.0f + beta * 2.0f) | ||
* (gamma * 3.0f + beta * 2.0f); | ||
} | ||
|
||
return sum; | ||
} | ||
|
||
int main() { | ||
float alpha = 1.0f; | ||
float beta = 2.0f; | ||
float gamma = 3.0f; | ||
|
||
float *x , *y, *z; | ||
|
||
sycl::property_list properties{ | ||
sycl::property::queue::in_order(), | ||
sycl::ext::oneapi::property::queue::lazy_execution{} | ||
}; | ||
|
||
sycl::gpu_selector device_selector; | ||
|
||
sycl::queue q{device_selector, properties}; | ||
|
||
sycl::ext::oneapi::experimental::command_graph g; | ||
|
||
float *dotp = sycl::malloc_shared<float>(1, q); | ||
|
||
x = sycl::malloc_shared<float>(n, q); | ||
y = sycl::malloc_shared<float>(n, q); | ||
z = sycl::malloc_shared<float>(n, q); | ||
|
||
/* init data on the device */ | ||
auto n_i = g.add([&](sycl::handler &h) { | ||
h.parallel_for(n, [=](sycl::id<1> it){ | ||
const size_t i = it[0]; | ||
x[i] = 1.0f; | ||
y[i] = 2.0f; | ||
z[i] = 3.0f; | ||
}); | ||
}); | ||
|
||
auto node_a = g.add([&](sycl::handler &h) { | ||
h.parallel_for(sycl::range<1>{n}, [=](sycl::id<1> it) { | ||
const size_t i = it[0]; | ||
x[i] = alpha * x[i] + beta * y[i]; | ||
}); | ||
}, {n_i}); | ||
|
||
auto node_b = g.add([&](sycl::handler &h) { | ||
h.parallel_for(sycl::range<1>{n}, [=](sycl::id<1> it) { | ||
const size_t i = it[0]; | ||
z[i] = gamma * z[i] + beta * y[i]; | ||
}); | ||
}, {n_i}); | ||
|
||
auto node_c = g.add([&](sycl::handler &h) { | ||
h.parallel_for(sycl::range<1>{n}, | ||
sycl::reduction(dotp, 0.0f, std::plus()), | ||
[=](sycl::id<1> it, auto &sum) { | ||
const size_t i = it[0]; | ||
sum += x[i] * z[i]; | ||
}); | ||
}, {node_a, node_b}); | ||
|
||
auto exec_graph = g.finalize(q.get_context()); | ||
|
||
exec_graph.exec_and_wait(q); | ||
|
||
sycl::free(dotp, q); | ||
sycl::free(x, q); | ||
sycl::free(y, q); | ||
sycl::free(z, q); | ||
|
||
std::cout << "done.\n"; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#include <iostream> | ||
#include <CL/sycl.hpp> | ||
|
||
#include <sycl/ext/oneapi/experimental/graph.hpp> | ||
|
||
const size_t n = 10; | ||
|
||
int main() { | ||
|
||
sycl::property_list properties{ | ||
sycl::property::queue::in_order(), | ||
sycl::ext::oneapi::property::queue::lazy_execution{} | ||
}; | ||
|
||
//sycl::gpu_selector device_selector; | ||
|
||
sycl::queue q{sycl::gpu_selector_v, properties}; | ||
|
||
//sycl::queue copy_q{}; | ||
|
||
sycl::ext::oneapi::experimental::command_graph g; | ||
|
||
float *arr = sycl::malloc_shared<float>(n, q); | ||
|
||
g.add( | ||
[&](sycl::handler& h){ | ||
h.parallel_for(sycl::range<1>{n}, [=](sycl::id<1> idx){size_t i = idx; arr[i]=1; }); | ||
}); | ||
|
||
auto result_before_exec1 = arr[0]; | ||
|
||
auto exec_graph = g.finalize(q.get_context()); | ||
|
||
auto result_before_exec2 = arr[0]; | ||
|
||
exec_graph.exec_and_wait(q); | ||
|
||
auto result = arr[0]; | ||
|
||
std::cout << "done.\n"; | ||
|
||
return 0; | ||
} |