Skip to content

Commit

Permalink
Add placeholder for memory utils unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
pelesh committed Oct 26, 2023
1 parent 0983004 commit 201885b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 1 deletion.
2 changes: 1 addition & 1 deletion resolve/hip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ set_source_files_properties(${ReSolve_HIP_SRC} PROPERTIES LANGUAGE HIP)
# separate backend will be needed for HIP SDK)
add_library(resolve_backend_hip SHARED ${ReSolve_HIP_SRC})
target_link_libraries(resolve_backend_hip PRIVATE resolve_logger)
target_link_libraries(resolve_backend_hip PRIVATE resolve_hip)
target_link_libraries(resolve_backend_hip PUBLIC resolve_hip)
#target_include_directories(resolve_backend_hip PUBLIC ${hip_includes})
target_include_directories(resolve_backend_hip INTERFACE
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
Expand Down
1 change: 1 addition & 0 deletions tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
add_subdirectory(matrix)
add_subdirectory(vector)
add_subdirectory(utilities)
add_subdirectory(memory)
21 changes: 21 additions & 0 deletions tests/unit/memory/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#[[
@brief Build ReSolve memory utilities unit tests
@author Slaven Peles <[email protected]>
]]

# Build memory utilities tests
add_executable(runMemoryUtilsTests.exe runMemoryUtilsTests.cpp)
target_link_libraries(runMemoryUtilsTests.exe PRIVATE ReSolve resolve_backend_hip)
message(STATUS "Resolve libraries: ${resolve_backend_hip}")


# Install tests
set(installable_tests runMemoryUtilsTests.exe)
install(TARGETS ${installable_tests}
RUNTIME DESTINATION bin/resolve/tests/unit)

# Add tests to run
add_test(NAME memory_test COMMAND $<TARGET_FILE:runMemoryUtilsTests.exe>)
72 changes: 72 additions & 0 deletions tests/unit/memory/MemoryUtilsTests.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once
#include <string>
#include <vector>
#include <sstream>
#include <iterator>
#include <algorithm>
#include <resolve/MemoryUtils.hpp>
#include <tests/unit/TestBase.hpp>

namespace ReSolve { namespace tests {

/**
* @class Unit tests for memory handler class
*/
class MemoryUtilsTests : TestBase
{
public:
MemoryUtilsTests(std::string memspace) : memspace_(memspace)
{}
virtual ~MemoryUtilsTests()
{}

TestOutcome allocateAndDelete()
{
TestStatus status;
status = true;

MemoryHandler mh;

index_type n = 1000;
size_t m = 8000;
index_type* i = nullptr;
real_type* r = nullptr;

mh.allocateArrayOnDevice(&i, n);
mh.allocateBufferOnDevice((void**) &r, m);

status *= (i != nullptr);
status *= (r != nullptr);

mh.deleteOnDevice(i);
mh.deleteOnDevice(r);

return status.report(__func__);
}


private:
std::string memspace_{"cpu"};

// bool verifyAnswer(vector::Vector& x, real_type answer, std::string memspace)
// {
// bool status = true;
// if (memspace != "cpu") {
// x.copyData(memspace, "cpu");
// }

// for (index_type i = 0; i < x.getSize(); ++i) {
// // std::cout << x.getData("cpu")[i] << "\n";
// if (!isEqual(x.getData("cpu")[i], answer)) {
// status = false;
// std::cout << "Solution vector element x[" << i << "] = " << x.getData("cpu")[i]
// << ", expected: " << answer << "\n";
// break;
// }
// }
// return status;
// }

}; // class MemoryUtilsTests

}} // namespace ReSolve::tests
34 changes: 34 additions & 0 deletions tests/unit/memory/runMemoryUtilsTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <string>
#include <iostream>
#include <fstream>

#include "MemoryUtilsTests.hpp"

int main(int, char**)
{
ReSolve::tests::TestingResults result;

#ifdef RESOLVE_USE_HIP
{
std::cout << "Running memory tests with HIP backend:\n";
ReSolve::tests::MemoryUtilsTests test("hip");

result += test.allocateAndDelete();

std::cout << "\n";
}
#endif

#ifdef RESOLVE_USE_CUDA
{
std::cout << "Running memory tests with CUDA backend:\n";
ReSolve::tests::MemoryUtilsTests test("hip");

result += test.allocateAndDelete();

std::cout << "\n";
}
#endif

return result.summary();
}

0 comments on commit 201885b

Please sign in to comment.