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

Test graph container #24

Open
wants to merge 10 commits into
base: testing
Choose a base branch
from
Open
14 changes: 14 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${GraphMLIR_BINARY_DIR})

set(GraphMLIR_EXAMPLES OFF CACHE BOOL "Build examples")

#-------------------------------------------------------------------------------
# GoogleTest
#-------------------------------------------------------------------------------
set(BUILD_TESTS ON CACHE BOOL "Build tests")
if (BUILD_TESTS)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip
)
FetchContent_MakeAvailable(googletest)
endif()

# Add MLIR and LLVM headers to the include path
include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
Expand All @@ -80,6 +93,7 @@ include_directories(${GraphMLIR_SOURCE_DIR}/lib)
add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(tools)
add_subdirectory(tests/unittests)

if(GraphMLIR_EXAMPLES)
add_subdirectory(examples)
Expand Down
2 changes: 0 additions & 2 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ BFS
PROPERTIES
LINKER_LANGUAGE C)



add_executable(bfsExample bfsExample.cpp)
add_dependencies(bfsExample graph-opt)
target_link_libraries(bfsExample BFS)
6 changes: 4 additions & 2 deletions include/Interface/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
// - The storage order is NCHW.
template <typename T, size_t N> class MemRef {
public:
// Default constructor.
MemRef(){};
// Constructor from shape.
MemRef(intptr_t sizes[N], T init = T(0));
// Constructor from data.
Expand Down Expand Up @@ -62,10 +64,10 @@ template <typename T, size_t N> class MemRef {
T &operator[](size_t index);
// release the pointer
T *release();
//comparision operator
bool operator==(const MemRef<T, N> &other);

protected:
// Default constructor.
MemRef(){};
// Set the strides.
// Computes the strides of the transposed tensor for transpose=true.
void setStrides();
Expand Down
1 change: 1 addition & 0 deletions lib/Interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_library(Container Container.cpp)
add_library(GraphContainer GraphContainer.cpp)
43 changes: 41 additions & 2 deletions lib/Interface/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#include <algorithm>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <memory>
#include <numeric>
#include <stdexcept>
Expand Down Expand Up @@ -177,8 +179,16 @@ template <typename T, std::size_t N>
MemRef<T, N> &MemRef<T, N>::operator=(MemRef<T, N> &&other) noexcept {
// Free the original aligned and allocated space.
delete[] allocated;
// Copy members of the original object.
MemRef<T, N>::MemRef(other);
// Steal members of the original object.
std::swap(strides, other.strides);
std::swap(offset, other.offset);
std::swap(sizes, other.sizes);
std::swap(size, other.size);
std::swap(allocated, other.allocated);
std::swap(aligned, other.aligned);
// Assign the NULL pointer to the original aligned and allocated members to
// avoid the double free error.
other.allocated = other.aligned = nullptr;
return *this;
}

Expand Down Expand Up @@ -277,4 +287,33 @@ template <typename T, size_t N> T *MemRef<T, N>::release() {
return temp;
}

template <typename T, size_t N>
bool MemRef<T, N>::operator==(const MemRef<T, N> &other) {
intptr_t x1 = this->sizes[0];
intptr_t y1 = this->sizes[1];
intptr_t x2 = other.sizes[0];
intptr_t y2 = other.sizes[1];

// compare the sizes array and size
if (x1 != x2 || y1 != y2 || this->size != other.size) {
return false;
}

// compare the strides
if (this->strides[0] != this->strides[0] ||
other.strides[1] != other.strides[1]) {
return false;
}

for (intptr_t i = 0; i < x1; i++) {
for (intptr_t j = 0; j < y1; j++) {
if (this->aligned[i * x1 + y1] != other.aligned[i * x1 + y1]) {
return false;
}
}
}

return true;
}

#endif // CORE_CONTAINER_DEF
6 changes: 3 additions & 3 deletions lib/Interface/GraphContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ void Graph<T, N>::addEdge(T Node1, T Node2, T EdgeWeight) {
this->incMat[Node2][edgeCount] = EdgeWeight;
break;
case graph::detail::GRAPH_INC_MATRIX_DIRECTED_WEIGHTED:
EdgeWeight = std::abs(sqrt(EdgeWeight));
EdgeWeight = EdgeWeight;
this->incMat[Node1][edgeCount] = EdgeWeight;
this->incMat[Node2][edgeCount] = -EdgeWeight;
this->edgeCount += 1;
Expand Down Expand Up @@ -461,9 +461,9 @@ template <typename T, size_t N> void Graph<T, N>::graph_to_MemRef_descriptor() {
for (k = flag + 1; k < this->incMat.size() && flag != -2; k++) {
if ((this->incMat[k][j] != 0) && flag != -1) {
if (this->incMat[k][j] < this->incMat[int(flag)][j])
linear[int(flag) * x + k] = pow(incMat[k][j], 2);
linear[int(flag) * x + k] = incMat[k][j];
else
linear[k * x + int(flag)] = pow(incMat[k][j], 2);
linear[k * x + int(flag)] = incMat[k][j];
flag = -1;
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(Interface)
12 changes: 12 additions & 0 deletions tests/unittests/Interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
add_executable(
InterfaceTests
GraphContainerTest.cpp
ContainerTest.cpp
)

target_link_libraries(InterfaceTests gtest gtest_main pthread BFS)

add_dependencies(InterfaceTests Container GraphContainer)

include(GoogleTest)
gtest_discover_tests(InterfaceTests)
80 changes: 80 additions & 0 deletions tests/unittests/Interface/ContainerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#include "Interface/Container.h"
#include <gtest/gtest.h>

template <typename T>
void ASSERT_ARRAY_EQ(const T *x, const T *y, const size_t n) {
if (std::is_integral<T>::value) {
for (size_t i = 0; i < n; i++) {
ASSERT_EQ(x[i], y[i]);
}
} else if (std::is_same<T, float>::value) {
for (size_t i = 0; i < n; i++) {
ASSERT_FLOAT_EQ(x[i], y[i]);
}
} else if (std::is_same<T, double>::value) {
for (size_t i = 0; i < n; i++) {
ASSERT_DOUBLE_EQ(x[i], y[i]);
}
}
}

class MemRefTest : public ::testing::Test {
protected:
void SetUp() override {}
void TearDown() override {}
};

// Copy constructor.
TEST_F(MemRefTest, CopyConstructor2DMemref) {
// new hard codede MemRef object.
float aligned[] = {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0};
intptr_t sizes[2] = {6, 6};
MemRef<float, 2> m(aligned, sizes, 0);
MemRef<float, 2> copy(m);
EXPECT_EQ(m == copy, true);
}

// Copy assignment operator.
TEST_F(MemRefTest, CopyAssignment2DMemref) {
// new hard codede MemRef object.
float aligned[] = {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0};
intptr_t sizes[2] = {6, 6};
MemRef<float, 2> m(aligned, sizes, 0);
MemRef<float, 2> copy = m;
EXPECT_EQ(m == copy, true);
}

// Move constructor.
TEST_F(MemRefTest, MoveConstructor2DMemref) {
// new hard codede MemRef object.
float aligned[] = {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0};
intptr_t sizes[2] = {6, 6};
MemRef<float, 2> m1(aligned, sizes, 0);
MemRef<float, 2> m2(aligned, sizes, 0);

// Construct using move constructor.
MemRef<float, 2> move = std::move(m1);

// test
EXPECT_EQ(m2 == move, true);
}

// Move assignment operator.
TEST_F(MemRefTest, MoveAssignment2DMemref) {
// new hard codede MemRef object.
float aligned[] = {0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,
0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0};
intptr_t sizes[2] = {6, 6};
MemRef<float, 2> m1(aligned, sizes, 0);
MemRef<float, 2> m2(aligned, sizes, 0);

MemRef<float, 2> move;
// Assignment using the move assignment operator.
move = std::move(m1);

// test
EXPECT_EQ(m2 == move, true);
}
Loading