diff --git a/scripts/negf_cpp/math_libraries/Exec/input_test b/scripts/negf_cpp/math_libraries/Exec/input_test new file mode 100644 index 0000000..d45fe92 --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Exec/input_test @@ -0,0 +1,7 @@ +A_rows = 2 +A_cols = 4 +B_cols = 3 + +print_input=1 +print_output=1 +amrex.the_arena_is_managed=0 diff --git a/scripts/negf_cpp/math_libraries/Source/Make.package b/scripts/negf_cpp/math_libraries/Source/Make.package index 968edcd..5838b76 100644 --- a/scripts/negf_cpp/math_libraries/Source/Make.package +++ b/scripts/negf_cpp/math_libraries/Source/Make.package @@ -1,5 +1,8 @@ CEXE_sources += main.cpp CEXE_sources += MathLib.cpp +CEXE_sources += Test.cpp + +CEXE_headers += Test.H CEXE_headers += MathLib.H CEXE_headers += MatrixDef.H CEXE_headers += GlobalFuncs.H @@ -7,3 +10,5 @@ CEXE_headers += cudaErrorCheck.H VPATH_LOCATIONS += $(CODE_HOME)/Source INCLUDE_LOCATIONS += $(CODE_HOME)/Source + +include $(CODE_HOME)/Source/Tests/Make.package diff --git a/scripts/negf_cpp/math_libraries/Source/Matrix_NotUsed.H b/scripts/negf_cpp/math_libraries/Source/Matrix_NotUsed.H deleted file mode 100644 index 399f22c..0000000 --- a/scripts/negf_cpp/math_libraries/Source/Matrix_NotUsed.H +++ /dev/null @@ -1,114 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace amrex; -using MatrixDType = amrex::GpuComplex; -using Matrix1D = TableData; -using Matrix2D = TableData; - -//template -//class TD; - -enum class MemSpace : int {Arena, Host, Device, Managed}; - -class Matrix { - -public: - Matrix () {} - Matrix (int height, int width, MemSpace mem_space=MemSpace::Arena) - { - resize(height, width, mem_space); - } - - void resize(int height, int width, MemSpace mem_space) - { - _mem_space = mem_space; - #ifdef AMREX_USE_GPU - if(_mem_space == MemSpace::Arena) { - d_tab.resize({0, 0}, {height, width}, The_Arena()); - } - else if(_mem_space == MemSpace::Host) { - h_tab.resize({0, 0}, {height, width}, The_Pinned_Arena()); - } - else if(_mem_space == MemSpace::Device) { - d_tab.resize({0, 0}, {height, width}, The_Device_Arena()); - } - else if(_mem_space == MemSpace::Managed) { - d_tab.resize({0, 0}, {height, width}, The_Managed_Arena()); - } - #else - h_tab.resize({0, 0}, {height, width}); - #endif - } - - const auto& hi() const { - #ifdef AMREX_USE_GPU - return d_tab.hi(); - #else - return h_tab.hi(); - #endif - } - - MemSpace mem_space() const { - return _mem_space; - } - - const auto& host_table() const { - return h_tab.table(); - } - - const auto& host_const_table() const { - return h_tab.const_table(); - } - - #ifdef AMREX_USE_GPU - void copy_to_host() { - const auto& hi = d_tab.hi(); - h_tab.resize({0, 0}, {hi[0], hi[1]}, The_Pinned_Arena()); - - d_tab.copy(h_tab); - Gpu::streamSynchronize(); - } - void copy_to_device() { - const auto& hi = h_tab.hi(); - d_tab.resize({0, 0}, {hi[0], hi[1]}, The_Device_Arena()); - - h_tab.copy(d_tab); - Gpu::streamSynchronize(); - } - const auto& device_const_table() const { - return d_tab.const_table(); - } - const auto& device_table() const { - return d_tab.table(); - } - void clear_device() { - d_tab.clear(); - } - #endif - - void clear_host() { - h_tab.clear(); - } - void clear() { - #ifdef AMREX_USE_GPU - h_tab.clear(); - d_tab.clear(); - #else - h_tab.clear(); - #endif - } -private: - - MemSpace _mem_space; - #ifdef AMREX_USE_GPU - Matrix2D h_tab; - Matrix2D d_tab; - #else - Matrix2D h_tab; - #endif -}; diff --git a/scripts/negf_cpp/math_libraries/Source/Test.H b/scripts/negf_cpp/math_libraries/Source/Test.H new file mode 100644 index 0000000..4fc398f --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Test.H @@ -0,0 +1,31 @@ +#ifndef TEST_MATH_LIBRARY_H_ +#define TEST_MATH_LIBRARY_H_ + +#include "Tests/All_Tests.H" + +template +class Test_MathLibrary +{ + private: + Test m_test; + + public: + Test_MathLibrary(Test&& test_instance) : m_test(std::move(test_instance)) + { + m_test.Define(); + m_test.Initialize(); + m_test.Print_Input(); + } + + void Perform_Test() + { + m_test.Perform_Test(); + } + + void Print_Output() { + m_test.Print_Output(); + } + +}; + +#endif diff --git a/scripts/negf_cpp/math_libraries/Source/Test.cpp b/scripts/negf_cpp/math_libraries/Source/Test.cpp new file mode 100644 index 0000000..d54150e --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Test.cpp @@ -0,0 +1,4 @@ +#include "Test.H" + +//Explicit instantiation of the template +template class Test_MathLibrary; diff --git a/scripts/negf_cpp/math_libraries/Source/Tests/All_Tests.H b/scripts/negf_cpp/math_libraries/Source/Tests/All_Tests.H new file mode 100644 index 0000000..3454fab --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Tests/All_Tests.H @@ -0,0 +1,7 @@ +#ifndef ALL_TESTS_H_ +#define ALL_TESTS_H_ + +#include "Test_MM_Mul.H" + +#endif + diff --git a/scripts/negf_cpp/math_libraries/Source/Tests/Make.package b/scripts/negf_cpp/math_libraries/Source/Tests/Make.package new file mode 100644 index 0000000..15627fb --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Tests/Make.package @@ -0,0 +1,7 @@ +CEXE_sources += Test_MM_Mul.cpp + +CEXE_headers += Test_MM_Mul.H +CEXE_headers += All_Tests.H + +VPATH_LOCATIONS += $(CODE_HOME)/Source/Tests +INCLUDE_LOCATIONS += $(CODE_HOME)/Source/Tests diff --git a/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.H b/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.H new file mode 100644 index 0000000..2133edd --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.H @@ -0,0 +1,25 @@ +#include "MatrixDef.H" + +class Test_MM_Mul +{ + private: + int A_rows, A_cols, B_cols; + std::vector print_flags; + + Matrix2D h_A_data, h_B_data, h_C_data; + Matrix2D d_A_data, d_B_data, d_C_data; + + ComplexType A_init_val= ComplexType(1., 2.); + ComplexType B_init_val= ComplexType(-5., 3.); + + public: + Test_MM_Mul(int a_rows, int a_cols, int b_cols, + const std::vector& flags); + + void Define(); + void Initialize(); + void Perform_Test(); + void Print_Input(); + void Print_Output(); + +}; diff --git a/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.cpp b/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.cpp new file mode 100644 index 0000000..4b18673 --- /dev/null +++ b/scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.cpp @@ -0,0 +1,83 @@ +#include "Test_MM_Mul.H" +#include "GlobalFuncs.H" +#include "MathLib.H" +#include + +using namespace amrex; + +Test_MM_Mul::Test_MM_Mul(int a_rows, int a_cols, int b_cols, const std::vector& flags): + A_rows(a_rows), A_cols(a_cols), B_cols(b_cols), print_flags(flags) {} + + +void +Test_MM_Mul:: Define() +{ + h_A_data.resize({0,0},{A_rows, A_cols},The_Pinned_Arena()); + d_A_data.resize({0,0},{A_rows, A_cols},The_Arena()); + h_B_data.resize({0,0},{A_cols, B_cols},The_Pinned_Arena()); + d_B_data.resize({0,0},{A_cols, B_cols},The_Arena()); + h_C_data.resize({0,0},{A_rows, B_cols},The_Pinned_Arena()); + d_C_data.resize({0,0},{A_rows, B_cols},The_Arena()); +} + + +void +Test_MM_Mul:: Initialize() +{ + ComplexType zero(0., 0.); + + Define_Table2D(h_A_data, A_init_val); + Define_Table2D(h_B_data, B_init_val); + SetVal_Table2D(h_C_data, zero); + + d_A_data.copy(h_A_data); + d_B_data.copy(h_B_data); + d_C_data.copy(h_C_data); +} + + +void +Test_MM_Mul:: Print_Input() +{ + if(print_flags[0]) { + Print_Table2D(h_A_data, "A"); + const auto& h_A = h_A_data.const_table(); + const auto& h_B = h_B_data.const_table(); + + amrex::Print() << "\nPrinting h_A using h_A.p\n"; + + //Usage Error: In the forloop we should be using (i < dim_A[0]*dim_A[1]) + //But currently we need to add a buffer of 1 unit size to print properly! + for(int i=0; i<(A_rows+1)*A_cols; ++i) + { + amrex::Print() << i << " "<< *(h_A.p+i) << "\n"; + } + + Print_Table2D(h_B_data, "B"); + } +} + + +void +Test_MM_Mul:: Perform_Test() +{ + const auto& d_A = d_A_data.const_table(); + const auto& d_B = d_B_data.const_table(); + const auto& d_C = d_C_data.table(); + + MathLib::MatrixMatrixMultiply(d_C.p, d_A.p, d_B.p, A_rows, A_cols, B_cols); +} + + +void +Test_MM_Mul:: Print_Output() +{ + if(print_flags[1]) + { + //copy C from device to host and print + h_C_data.copy(d_C_data); + Gpu::streamSynchronize(); + + Print_Table2D(h_C_data, "C"); + } +} diff --git a/scripts/negf_cpp/math_libraries/Source/main.cpp b/scripts/negf_cpp/math_libraries/Source/main.cpp index 1e66068..97771a6 100644 --- a/scripts/negf_cpp/math_libraries/Source/main.cpp +++ b/scripts/negf_cpp/math_libraries/Source/main.cpp @@ -1,8 +1,6 @@ #include -#include "MathLib.H" -#include "MatrixDef.H" -#include "GlobalFuncs.H" +#include "Test.H" using namespace amrex; @@ -20,78 +18,19 @@ int main (int argc, char* argv[]) pp.query("A_rows", A_rows); pp.query("A_cols", A_cols); pp.query("B_cols", B_cols); - - int print_matrix_flag = false; - pp.query("print_matrix", print_matrix_flag); - //Create A, B, C matrices as 2D tables. We want to perform C = A * B. - Matrix2D h_A_data({0,0},{A_rows, A_cols},The_Pinned_Arena()); - Matrix2D d_A_data({0,0},{A_rows, A_cols},The_Arena()); - - Matrix2D h_B_data({0,0},{A_cols, B_cols},The_Pinned_Arena()); - Matrix2D d_B_data({0,0},{A_cols, B_cols},The_Arena()); - - Matrix2D h_C_data({0,0},{A_rows, B_cols},The_Pinned_Arena()); - Matrix2D d_C_data({0,0},{A_rows, B_cols},The_Arena()); - - //define matrices A & B - ComplexType num1(1.,2.); - ComplexType num2(-5.,3.); - ComplexType zero(0.,0.); - Define_Table2D(h_A_data, num1); - Define_Table2D(h_B_data, num2); - SetVal_Table2D(h_C_data, zero); - - //copy to A & B to device - d_A_data.copy(h_A_data); - d_B_data.copy(h_B_data); - d_C_data.copy(h_C_data); - - //get references to tables - const auto& dim_A = d_A_data.hi(); - const auto& dim_B = d_B_data.hi(); - const auto& d_A = d_A_data.const_table(); - const auto& d_B = d_B_data.const_table(); - const auto& d_C = d_C_data.table(); - - amrex::Print() << "dim_A (rows/cols): " << dim_A[0] << " " << dim_A[1] << "\n"; - amrex::Print() << "dim_B (rows/cols): " << dim_B[0] << " " << dim_B[1] << "\n"; + std::vector print_flags(2,0); + pp.query("print_input", print_flags[0]); + pp.query("print_output", print_flags[1]); + + { + Test_MathLibrary + mm_mul(Test_MM_Mul(A_rows, A_cols, B_cols, print_flags)); - //print A & B - Print_Table2D(h_A_data, "A"); - const auto& h_A = h_A_data.const_table(); - const auto& h_B = h_B_data.const_table(); + mm_mul.Perform_Test(); - amrex::Print() << "\nPrinting h_A using h_A.p\n"; - - //Usage Error: In the forloop we should be using (i < dim_A[0]*dim_A[1]) - //But currently we need to add a buffer of 1 unit size to print properly! - for(int i=0; i<(dim_A[0]+1)*dim_A[1]; ++i) - { - amrex::Print() << i << " "<< *(h_A.p+i) << "\n"; + mm_mul.Print_Output(); } - Print_Table2D(h_B_data, "B"); - - //Perform C = A * B - MathLib::MatrixMatrixMultiply(d_C.p, d_A.p, d_B.p, dim_A[0], dim_A[1], dim_B[1]); - - //copy C from device to host and print - h_C_data.copy(d_C_data); - Gpu::streamSynchronize(); - - Print_Table2D(h_C_data, "C"); - - h_A_data.clear(); - h_B_data.clear(); - h_C_data.clear(); - d_A_data.clear(); - d_B_data.clear(); - d_C_data.clear(); - - d_A_data.clear(); - d_B_data.clear(); - d_C_data.clear(); - amrex::Finalize(); }