Skip to content
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

<!-- ---------------------
v1.5.1
--------------------- -->
## v1.5.1 - 29-11-2024

### Fixed

- Set precision in `DTensor::saveToFile` properly
- `DTensor<T>::parseFromTextFile` throws `std::invalid_argument` if `T` is unsupported

<!-- ---------------------
v1.5.0
--------------------- -->
Expand Down
6 changes: 3 additions & 3 deletions include/tensor.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,9 @@ data_t<T> vectorFromFile(std::string path_to_file) {
vecDataFromFile[i] = std::stoull(line.c_str());
} else if constexpr (std::is_same_v<T, size_t>) {
sscanf(line.c_str(), "%zu", &vecDataFromFile[i]);
} else {
throw std::invalid_argument("data type not supported");
}
// todo

if (++i == numElements) break;
}
Expand All @@ -653,8 +654,7 @@ void DTensor<T>::saveToFile(std::string pathToFile) {
file << numRows() << std::endl << numCols() << std::endl << numMats() << std::endl;
std::vector<T> myData(numEl()); download(myData);
if constexpr (std::is_floating_point<T>::value) {
int prec = std::numeric_limits<T>::max_digits10 - 1;
file << std::setprecision(prec);
file << std::setprecision(std::numeric_limits<T>::max_digits10);
}
for(const T& el : myData) file << el << std::endl;
}
Expand Down
33 changes: 22 additions & 11 deletions test/testTensor.cu
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,35 @@ TEST_F(TensorTest, randomTensorCreation) {

TEMPLATE_WITH_TYPE_T
void parseTensorFromFile() {
size_t nR = 20, nC = 40, nM = 60;
auto r = DTensor<T>::createRandomTensor(nR, nC, nM, -1, 1);
std::string fName = "myTest.dtensor";
r.saveToFile(fName);
auto a = DTensor<T>::parseFromTextFile(fName);
EXPECT_EQ(nR, a.numRows());
EXPECT_EQ(nC, a.numCols());
EXPECT_EQ(nM, a.numMats());
auto diff = a - r;
T err = diff.maxAbs();
EXPECT_LT(err, 2*std::numeric_limits<T>::epsilon());
size_t n_runs = 10;
for (size_t i = 0; i < n_runs; i++) {
size_t nR = 20, nC = 40, nM = 6;
auto r = DTensor<T>::createRandomTensor(nR, nC, nM, -1, 1);
std::string fName = "myTest.dtensor";
r.saveToFile(fName);
auto a = DTensor<T>::parseFromTextFile(fName);
EXPECT_EQ(nR, a.numRows());
EXPECT_EQ(nC, a.numCols());
EXPECT_EQ(nM, a.numMats());
auto diff = a - r;
T err = diff.maxAbs();
EXPECT_LT(err, 2 * std::numeric_limits<T>::epsilon());
}
}

TEST_F(TensorTest, parseTensorFromFile) {
parseTensorFromFile<float>();
parseTensorFromFile<double>();
}

TEST_F(TensorTest, parseTensorUnsupportedDataType) {
size_t nR = 20, nC = 40, nM = 60;
auto r = DTensor<double>::createRandomTensor(nR, nC, nM, -1, 1);
std::string fName = "myTest.dtensor";
r.saveToFile(fName);
EXPECT_THROW(DTensor<char>::parseFromTextFile(fName), std::invalid_argument);
}

/* ---------------------------------------
* Move constructor
* --------------------------------------- */
Expand Down