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

[DAPHNE-#224]: reshape-kernel for non-zero-copy cases #348

Merged
merged 3 commits into from
May 9, 2022
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
17 changes: 14 additions & 3 deletions src/runtime/local/kernels/Reshape.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,20 @@ struct Reshape<DenseMatrix<VT>, DenseMatrix<VT>> {

if(arg->getRowSkip() == arg->getNumCols() && res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numCols, arg->getValuesSharedPtr());
else
// TODO Support those cases. See #224.
throw std::runtime_error("reshape does not support cases which aren't pure meta data operations yet");
else {
if(res == nullptr)
res = DataObjectFactory::create<DenseMatrix<VT>>(numRows, numCols, false);

auto resVals = res->getValues();
auto argVals = arg->getValues();
size_t numArgRows = arg->getNumRows();
size_t numArgCols = arg->getNumCols();
for(size_t r = 0; r < numArgRows; r++) {
memcpy(resVals, argVals, numArgCols * sizeof(VT));
argVals += arg->getRowSkip();
resVals += numArgCols;
}
}
}
};

Expand Down
30 changes: 30 additions & 0 deletions test/runtime/local/kernels/ReshapeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,36 @@ TEMPLATE_PRODUCT_TEST_CASE("Reshape", TAG_KERNELS, (DenseMatrix), (double, uint3
checkReshape(arg, 3, 4, exp);
DataObjectFactory::destroy(exp);
}
SECTION("view 1") {
const DT * initial = genGivenVals<DT>(3, vals); // 3x4
const DT * view = DataObjectFactory::create<DT>(initial, 0, 3, 2, 4); // 3x2
const DT * exp = genGivenVals<DT>(2, {2,3,6, 7,10,11}); // 2x3
checkReshape(view, 2, 3, exp);

DataObjectFactory::destroy(exp);
DataObjectFactory::destroy(initial);
DataObjectFactory::destroy(view);
}
SECTION("view 2") {
const DT * initial = genGivenVals<DT>(2, vals); // 2x6
const DT * view = DataObjectFactory::create<DT>(initial, 1, 2, 0, 6); // 1x6
const DT * exp = genGivenVals<DT>(3, {6,7 ,8,9, 10,11}); // 3x2
checkReshape(view, 3, 2, exp);

DataObjectFactory::destroy(exp);
DataObjectFactory::destroy(initial);
DataObjectFactory::destroy(view);
}
SECTION("view 3") {
const DT * initial = genGivenVals<DT>(2, vals); // 2x6
const DT * view = DataObjectFactory::create<DT>(initial, 1, 2, 0, 4); // 1x4
const DT * exp = genGivenVals<DT>(2, {6,7 ,8,9}); // 2x2
checkReshape(view, 2, 2, exp);

DataObjectFactory::destroy(exp);
DataObjectFactory::destroy(initial);
DataObjectFactory::destroy(view);
}
SECTION("invalid reshape") {
DT * res = nullptr;
CHECK_THROWS(reshape<DT, DT>(res, arg, 5, 2, nullptr));
Expand Down