Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This creates a kernel that evaluates the given expression. The most prominent example is application of atomic operations (see tests/eval.cpp for examples)
- Loading branch information
Showing
5 changed files
with
218 additions
and
1 deletion.
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
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,30 @@ | ||
#define BOOST_TEST_MODULE Eval | ||
#include <boost/test/unit_test.hpp> | ||
#include <vexcl/vector.hpp> | ||
#include <vexcl/element_index.hpp> | ||
#include <vexcl/function.hpp> | ||
#include <vexcl/vector_view.hpp> | ||
#include <vexcl/eval.hpp> | ||
#include "context_setup.hpp" | ||
|
||
BOOST_AUTO_TEST_CASE(eval_atomic) { | ||
const size_t M = 16; | ||
const size_t C = 64; | ||
const size_t N = M * C; | ||
|
||
std::vector<vex::command_queue> q(1, ctx.queue(0)); | ||
|
||
vex::vector<int> x(q, N); | ||
vex::vector<int> y(q, M); | ||
|
||
y = 0; | ||
x = vex::element_index() % M; | ||
|
||
vex::eval(atomic_add(&vex::permutation(x)(y), 1)); | ||
check_sample(y, [](size_t, int v) { BOOST_CHECK_EQUAL(v, C); }); | ||
|
||
vex::eval(atomic_sub(&vex::permutation(x)(y), 1)); | ||
check_sample(y, [](size_t, int v) { BOOST_CHECK_EQUAL(v, 0); }); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |
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,127 @@ | ||
#ifndef VEXCL_EVAL_HPP | ||
#define VEXCL_EVAL_HPP | ||
|
||
/* | ||
The MIT License | ||
Copyright (c) 2012-2016 Denis Demidov <dennis.demidov@gmail.com> | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. | ||
*/ | ||
|
||
/** | ||
* \file vexcl/eval.hpp | ||
* \author Denis Demidov <dennis.demidov@gmail.com> | ||
* \brief Evaluate vector expression. | ||
*/ | ||
|
||
#include <vexcl/operations.hpp> | ||
|
||
namespace vex { | ||
|
||
template <class Expr> | ||
void eval(const Expr &expr, | ||
const std::vector<backend::command_queue> &queue, | ||
const std::vector<size_t> &part | ||
) | ||
{ | ||
using namespace vex::detail; | ||
|
||
#if (VEXCL_CHECK_SIZES > 0) | ||
{ | ||
get_expression_properties prop; | ||
extract_terminals()(boost::proto::as_child(expr), prop); | ||
|
||
precondition( | ||
prop.queue.empty() || prop.queue.size() == queue.size(), | ||
"Incompatible queue lists" | ||
); | ||
|
||
precondition( | ||
prop.size == 0 || prop.size == part.back(), | ||
"Incompatible expression sizes" | ||
); | ||
} | ||
#endif | ||
static kernel_cache cache; | ||
|
||
for(unsigned d = 0; d < queue.size(); d++) { | ||
auto kernel = cache.find(queue[d]); | ||
|
||
backend::select_context(queue[d]); | ||
|
||
if (kernel == cache.end()) { | ||
backend::source_generator source(queue[d]); | ||
|
||
output_terminal_preamble termpream(source, queue[d], "prm", empty_state()); | ||
|
||
boost::proto::eval(boost::proto::as_child(expr), termpream); | ||
|
||
source.kernel("vexcl_eval_kernel") | ||
.open("(") | ||
.parameter<size_t>("n"); | ||
|
||
declare_expression_parameter declare(source, queue[d], "prm", empty_state()); | ||
extract_terminals()(boost::proto::as_child(expr), declare); | ||
|
||
source.close(")") | ||
.open("{") | ||
.grid_stride_loop() | ||
.open("{"); | ||
|
||
output_local_preamble loc_init(source, queue[d], "prm", empty_state()); | ||
boost::proto::eval(boost::proto::as_child(expr), loc_init); | ||
|
||
source.new_line(); | ||
vector_expr_context expr_ctx(source, queue[d], "prm", empty_state()); | ||
boost::proto::eval(boost::proto::as_child(expr), expr_ctx); | ||
source << ";"; | ||
source.close("}").close("}"); | ||
|
||
kernel = cache.insert(queue[d], backend::kernel( | ||
queue[d], source.str(), "vexcl_eval_kernel")); | ||
} | ||
|
||
if (size_t psize = part[d + 1] - part[d]) { | ||
auto &K = kernel->second; | ||
K.push_arg(psize); | ||
set_expression_argument setarg(K, d, part[d], empty_state()); | ||
extract_terminals()( boost::proto::as_child(expr), setarg); | ||
K(queue[d]); | ||
} | ||
} | ||
} | ||
|
||
template <class Expr> | ||
void eval(const Expr &expr) { | ||
using namespace vex::detail; | ||
|
||
get_expression_properties prop; | ||
extract_terminals()(boost::proto::as_child(expr), prop); | ||
|
||
precondition(!prop.queue.empty() && !prop.part.empty(), | ||
"Can not determine expression size and queue list" | ||
); | ||
|
||
eval(expr, prop.queue, prop.part); | ||
} | ||
|
||
} | ||
|
||
#endif |
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
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