Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add range func #212

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions clic/src/tier1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1457,15 +1457,38 @@ range_func(const Device::Pointer & device,
int stop_z,
int step_z) -> Array::Pointer
{
start_x = (start_x < 0) ? 0 : start_x;
start_y = (start_y < 0) ? 0 : start_y;
start_z = (start_z < 0) ? 0 : start_z;

start_x = (start_x > src->width()) ? src->width() : start_x;
start_y = (start_y > src->height()) ? src->height() : start_y;
start_z = (start_z > src->depth()) ? src->depth() : start_z;

stop_x = (stop_x < 0) ? 0 : stop_x;
stop_y = (stop_y < 0) ? 0 : stop_y;
stop_z = (stop_z < 0) ? 0 : stop_z;

stop_x = (stop_x > src->width()) ? src->width() : stop_x;
stop_y = (stop_y > src->height()) ? src->height() : stop_y;
stop_z = (stop_z > src->depth()) ? src->depth() : stop_z;

tier0::create_dst(src,
dst,
abs(stop_x - start_x) / std::max(std::abs(step_x), 1),
abs(stop_y - start_y) / std::max(std::abs(step_y), 1),
abs(stop_z - start_z) / std::max(std::abs(step_z), 1),
src->dtype());

correct_range(&start_x, &stop_x, &step_x, static_cast<int>(src->width()));
correct_range(&start_y, &stop_y, &step_y, static_cast<int>(src->height()));
correct_range(&start_z, &stop_z, &step_z, static_cast<int>(src->depth()));
tier0::create_dst(src, dst, abs(start_x - stop_x), abs(start_y - stop_y), abs(start_z - stop_z), src->dtype());

const KernelInfo kernel = { "range", kernel::range };
const ParameterList params = { { "src", src }, { "dst", dst }, { "start_x", start_x },
{ "start_y", start_y }, { "start_z", start_z }, { "step_x", step_x },
{ "step_y", step_y }, { "step_z", step_z } };
const RangeArray range = { 1, 1, 1 };
{ "step_x", step_x }, { "start_y", start_y }, { "step_y", step_y },
{ "start_z", start_z }, { "step_z", step_z } };
const RangeArray range = { dst->width(), dst->height(), dst->depth() };
execute(device, kernel, params, range);
return dst;
}
Expand Down
93 changes: 93 additions & 0 deletions tests/tier1/test_range.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "cle.hpp"

#include <array>
#include <gtest/gtest.h>

class TestRange : public ::testing::TestWithParam<std::string>
{
protected:
std::array<float, 6 * 6 * 1> input = { 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6 };
};


TEST_P(TestRange, executeVertical)
{
std::array<float, 6 * 3 * 1> output;
std::array<float, 6 * 3 * 1> valid = { 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5 };

std::string param = GetParam();
cle::BackendManager::getInstance().setBackend(param);
auto device = cle::BackendManager::getInstance().getBackend().getDevice("", "all");
device->setWaitToFinish(true);

auto gpu_input = cle::Array::create(6, 6, 1, cle::dType::FLOAT, cle::mType::BUFFER, device);
gpu_input->write(input.data());

auto gpu_output = cle::tier1::range_func(device, gpu_input, nullptr, 0, 6, 1, 0, 6, 2, 1, 1, 1);

gpu_output->read(output.data());
for (int i = 0; i < output.size(); i++)
{
EXPECT_EQ(output[i], valid[i]);
}
}

TEST_P(TestRange, executeDiag)
{
std::array<float, 3 * 3 * 1> output;
std::array<float, 3 * 3 * 1> valid = { 1, 1, 1, 3, 3, 3, 5, 5, 5 };

std::string param = GetParam();
cle::BackendManager::getInstance().setBackend(param);
auto device = cle::BackendManager::getInstance().getBackend().getDevice("", "all");
device->setWaitToFinish(true);

auto gpu_input = cle::Array::create(6, 6, 1, cle::dType::FLOAT, cle::mType::BUFFER, device);
gpu_input->write(input.data());

auto gpu_output = cle::tier1::range_func(device, gpu_input, nullptr, 0, 6, 2, 0, 6, 2, 1, 1, 1);

gpu_output->read(output.data());
for (int i = 0; i < output.size(); i++)
{
EXPECT_EQ(output[i], valid[i]);
}
}

TEST_P(TestRange, executeNegative)
{
std::array<float, 6 * 3 * 1> output;
std::array<float, 6 * 3 * 1> valid = { 6, 6, 6, 6, 6, 6, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2 };

std::string param = GetParam();
cle::BackendManager::getInstance().setBackend(param);
auto device = cle::BackendManager::getInstance().getBackend().getDevice("", "all");
device->setWaitToFinish(true);

auto gpu_input = cle::Array::create(6, 6, 1, cle::dType::FLOAT, cle::mType::BUFFER, device);
gpu_input->write(input.data());

auto gpu_output = cle::tier1::range_func(device, gpu_input, nullptr, 0, 6, 1, 6, 0, -2, 1, 1, 1);

gpu_output->read(output.data());
for (int i = 0; i < output.size(); i++)
{
EXPECT_EQ(output[i], valid[i]);
}
}

std::vector<std::string>
getParameters()
{
std::vector<std::string> parameters;
#if USE_OPENCL
parameters.push_back("opencl");
#endif
#if USE_CUDA
parameters.push_back("cuda");
#endif
return parameters;
}

INSTANTIATE_TEST_SUITE_P(InstantiationName, TestRange, ::testing::ValuesIn(getParameters()));