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

COOMatrix implementation #689

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/runtime/local/datagen/GenGivenVals.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <runtime/local/datastructures/CSRMatrix.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/COOMatrix.h>

#include <algorithm>
#include <stdexcept>
Expand Down Expand Up @@ -160,6 +161,50 @@ struct GenGivenVals<CSRMatrix<VT>> {
}
};

// ----------------------------------------------------------------------------
// COOMatrix
// ----------------------------------------------------------------------------

template<typename VT>
struct GenGivenVals<COOMatrix<VT>> {
static COOMatrix<VT> * generate(size_t numRows, const std::vector<VT> & elements, size_t minNumNonZeros = 0) {
const size_t numCells = elements.size();
if (numCells % numRows != 0)
throw std::runtime_error("genGivenVals: number of given data elements must be divisible by given number of rows");
const size_t numCols = numCells / numRows;
size_t numNonZeros = 0;
for(VT v : elements)
if(v != VT(0))
numNonZeros++;
auto res = DataObjectFactory::create<COOMatrix<VT>>(numRows, numCols, std::max(numNonZeros, minNumNonZeros), false);
VT * values = res->getValues();
size_t * colIdxs = res->getColIdxs();
size_t * rowIdxs = res->getRowIdxs();
size_t pos = 0;
size_t colIdx = -1;
size_t rowIdx = -1;
for(size_t i = 0; i < numCells; i++) {
if (i % numCols == 0) {
rowIdx ++;
colIdx = 0;
} else {
colIdx ++;
}
VT v = elements[i];
if(v != VT(0)) {
values[pos] = v;
colIdxs[pos] = colIdx;
rowIdxs[pos] = rowIdx;
pos++;
}
}
values[pos] = VT(0);
colIdxs[pos] = size_t(-1);
rowIdxs[pos] = size_t(-1);
return res;
}
};

// ----------------------------------------------------------------------------
// Matrix
// ----------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/runtime/local/datastructures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ add_library(DataStructures
DataPlacement.cpp
DenseMatrix.cpp
CSRMatrix.cpp
COOMatrix.cpp
Frame.cpp
Structure.cpp
IAllocationDescriptor.h
Expand Down
37 changes: 37 additions & 0 deletions src/runtime/local/datastructures/COOMatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 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 <runtime/local/io/DaphneSerializer.h>

#include "COOMatrix.h"


/* TODO COO serializer */
template<typename ValueType>
size_t COOMatrix<ValueType>::serialize(std::vector<char> &buf) const {
throw std::runtime_error("COOMatrix does not support serialization yet");
// return DaphneSerializer<COOMatrix<ValueType>>::serialize(this, buf);
}

// explicitly instantiate to satisfy linker
template class COOMatrix<double>;
template class COOMatrix<float>;
template class COOMatrix<int>;
template class COOMatrix<long>;
template class COOMatrix<signed char>;
template class COOMatrix<unsigned char>;
template class COOMatrix<unsigned int>;
template class COOMatrix<unsigned long>;
Loading