Skip to content

Commit

Permalink
Adds sum of duplicate inserted values
Browse files Browse the repository at this point in the history
This fixes #85
  • Loading branch information
fredrikbk committed May 23, 2017
1 parent 0ae4ea5 commit 0aaed99
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
39 changes: 34 additions & 5 deletions src/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,21 +374,50 @@ void TensorBase::pack() {
qsort(coordinatesPtr, numCoordinates, coordSize, lexicographicalCmp);


// Move coords into separate arrays
// Move coords into separate arrays and remove duplicates
std::vector<std::vector<int>> coordinates(order);
for (size_t i=0; i < order; ++i) {
coordinates[i] = std::vector<int>(numCoordinates);
}

std::vector<double> values(numCoordinates);
for (size_t i=0; i < numCoordinates; ++i) {
// Copy first coordinate-value pair
int* lastCoord = (int*)malloc(order * sizeof(int));
if (numCoordinates >= 1) {
int* coordComponent = (int*)coordinatesPtr;
for (size_t d=0; d < order; ++d) {
coordinates[d][0] = *coordComponent;
lastCoord[d] = *coordComponent;
coordComponent++;
}
values[0] = *((double*)coordComponent);
}
// Copy remaining coordinate-value pairs, removing duplicates
int j = 1;
int* coord = (int*)malloc(order * sizeof(int));
for (size_t i=1; i < numCoordinates; ++i) {
int* coordLoc = (int*)&coordinatesPtr[i*coordSize];
for (size_t d=0; d < order; ++d) {
coordinates[d][i] = *coordLoc;
coord[d] = *coordLoc;;
coordLoc++;
}
values[i] = *((double*)coordLoc);
double value = *((double*)coordLoc);
if (memcmp(coord, lastCoord, order*sizeof(int)) != 0) {
for (size_t d = 0; d < order; d++) {
coordinates[d][j] = coord[d];
}
values[j] = value;
j++;
}
else {
values[j-1] += value;
}
}
free(coord);
free(lastCoord);
for (size_t i=0; i < order; ++i) {
coordinates[i].resize(j);
}
values.resize(j);
taco_iassert(coordinates.size() > 0);
this->coordinateBuffer->clear();
this->coordinateBufferUsed = 0;
Expand Down
17 changes: 14 additions & 3 deletions test/tensor-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,12 @@ TEST(tensor, double_scalar) {
}

TEST(tensor, double_vector) {
map<vector<int>, double> vals = {{{0}, 1.0},
{{2}, 2.0}};

Tensor<double> a({5}, Sparse);
ASSERT_EQ(ComponentType::Double, a.getComponentType());
ASSERT_EQ(1u, a.getDimensions().size());
ASSERT_EQ(5, a.getDimensions()[0]);

map<vector<int>,double> vals = {{{0}, 1.0}, {{2}, 2.0}};
for (auto& val : vals) {
a.insert(val.first, val.second);
}
Expand All @@ -36,3 +34,16 @@ TEST(tensor, double_vector) {
ASSERT_EQ(vals.at(val.first), val.second);
}
}

TEST(tensor, duplicates) {
Tensor<double> a({5,5}, Sparse);
a.insert({1,2}, 42.0);
a.insert({2,2}, 10.0);
a.insert({1,2}, 1.0);
a.pack();
map<vector<int>,double> vals = {{{1,2}, 43.0}, {{2,2}, 10.0}};
for (auto& val : a) {
ASSERT_TRUE(util::contains(vals, val.first));
ASSERT_EQ(vals.at(val.first), val.second);
}
}

0 comments on commit 0aaed99

Please sign in to comment.