Skip to content

Commit

Permalink
[DAPHNE-#669] Properly handle views in WriteCsv kernel (#678)
Browse files Browse the repository at this point in the history
- Now WriteCsv takes the rowSkip argument of the matrix it's iterating over into account.
- Added script-level test cases that triggered the bug in the old version of the code.
- Fixes #669.
  • Loading branch information
AlexRTer authored Apr 17, 2024
1 parent f3bd4d1 commit e7649ba
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/runtime/local/io/WriteCsv.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,17 @@ struct WriteCsv<DenseMatrix<VT>> {
static void apply(const DenseMatrix<VT> *arg, File* file) {
assert(file != nullptr && "File required");
const VT * valuesArg = arg->getValues();
size_t cell = 0;
const size_t rowSkip = arg->getRowSkip();
const size_t argNumCols = arg->getNumCols();

for (size_t i = 0; i < arg->getNumRows(); ++i)
{
for(size_t j = 0; j < arg->getNumCols(); ++j)
for(size_t j = 0; j < argNumCols; ++j)
{
fprintf(
file->identifier,
std::is_floating_point<VT>::value ? "%f" : (std::is_same<VT, long int>::value ? "%ld" : "%d"),
valuesArg[cell++]
valuesArg[i*rowSkip + j]
);
if(j < (arg->getNumCols() - 1))
fprintf(file->identifier, ",");
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set(TEST_SOURCES
api/cli/functions/FunctionsTest.cpp
api/cli/functions/RecursiveFunctionsTest.cpp
api/cli/io/ReadTest.cpp
api/cli/io/WriteTest.cpp
api/cli/import/ImportTest.cpp
api/cli/indexing/IndexingTest.cpp
api/cli/inference/InferenceTest.cpp
Expand Down
4 changes: 4 additions & 0 deletions test/api/cli/io/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
matrix_full.csv
matrix_full.csv.meta
matrix_view.csv
matrix_view.csv.meta
40 changes: 40 additions & 0 deletions test/api/cli/io/WriteTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2024 The DAPHNE Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <api/cli/Utils.h>

#include <tags.h>

#include <catch.hpp>

#include <filesystem>
#include <string>

const std::string dirPath = "test/api/cli/io/";

TEST_CASE("writeMatrixCSV_Full", TAG_IO) {
std::string csvPath = dirPath + "matrix_full.csv";
std::filesystem::remove(csvPath); // remove old file if it still exists
checkDaphneStatusCode(StatusCode::SUCCESS, dirPath + "writeMatrix_full.daphne", "--args", std::string("outPath=\"" + csvPath + "\"").c_str());
compareDaphneToRef(dirPath + "matrix_full_ref.csv", dirPath + "readMatrix.daphne", "--args", std::string("inPath=\"" + csvPath + "\"").c_str());
}

TEST_CASE("writeMatrixCSV_View", TAG_IO) {
std::string csvPath = dirPath + "matrix_view.csv";
std::filesystem::remove(csvPath); // remove old file if it still exists
checkDaphneStatusCode(StatusCode::SUCCESS, dirPath + "writeMatrix_view.daphne", "--args", std::string("outPath=\"" + csvPath + "\"").c_str());
compareDaphneToRef(dirPath + "matrix_view_ref.csv", dirPath + "readMatrix.daphne", "--args", std::string("inPath=\"" + csvPath + "\"").c_str());
}
4 changes: 4 additions & 0 deletions test/api/cli/io/matrix_full_ref.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DenseMatrix(3x2, int64_t)
1 2
3 4
5 6
4 changes: 4 additions & 0 deletions test/api/cli/io/matrix_view_ref.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DenseMatrix(3x1, int64_t)
2
4
6
4 changes: 4 additions & 0 deletions test/api/cli/io/readMatrix.daphne
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Read a matrix from the file $inPath and print it.

X = readMatrix($inPath);
print(X);
4 changes: 4 additions & 0 deletions test/api/cli/io/writeMatrix_full.daphne
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Write an entire small matrix to the file $outPath.

X = reshape(seq(1, 6, 1), 3, 2);
write(X, $outPath);
5 changes: 5 additions & 0 deletions test/api/cli/io/writeMatrix_view.daphne
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Write a view into a small matrix to the file $outPath.

X = reshape(seq(1, 6, 1), 3, 2);
X = X[, 1]; # only the 2nd column
write(X, $outPath);

0 comments on commit e7649ba

Please sign in to comment.