Skip to content

Commit

Permalink
organized everything into a Test class so that I can add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh-s-sawant committed Aug 27, 2024
1 parent 2f2f388 commit 56b92ab
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 185 deletions.
7 changes: 7 additions & 0 deletions scripts/negf_cpp/math_libraries/Exec/input_test
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Make.package
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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
CEXE_headers += cudaErrorCheck.H

VPATH_LOCATIONS += $(CODE_HOME)/Source
INCLUDE_LOCATIONS += $(CODE_HOME)/Source

include $(CODE_HOME)/Source/Tests/Make.package
114 changes: 0 additions & 114 deletions scripts/negf_cpp/math_libraries/Source/Matrix_NotUsed.H

This file was deleted.

31 changes: 31 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Test.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef TEST_MATH_LIBRARY_H_
#define TEST_MATH_LIBRARY_H_

#include "Tests/All_Tests.H"

template <typename Test=Test_MM_Mul>
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
4 changes: 4 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include "Test.H"

//Explicit instantiation of the template
template class Test_MathLibrary<Test_MM_Mul>;
7 changes: 7 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Tests/All_Tests.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef ALL_TESTS_H_
#define ALL_TESTS_H_

#include "Test_MM_Mul.H"

#endif

7 changes: 7 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Tests/Make.package
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "MatrixDef.H"

class Test_MM_Mul
{
private:
int A_rows, A_cols, B_cols;
std::vector<int> 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<int>& flags);

void Define();
void Initialize();
void Perform_Test();
void Print_Input();
void Print_Output();

};
83 changes: 83 additions & 0 deletions scripts/negf_cpp/math_libraries/Source/Tests/Test_MM_Mul.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "Test_MM_Mul.H"
#include "GlobalFuncs.H"
#include "MathLib.H"
#include <AMReX_Gpu.H>

using namespace amrex;

Test_MM_Mul::Test_MM_Mul(int a_rows, int a_cols, int b_cols, const std::vector<int>& 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");
}
}
81 changes: 10 additions & 71 deletions scripts/negf_cpp/math_libraries/Source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include <AMReX_ParmParse.H>

#include "MathLib.H"
#include "MatrixDef.H"
#include "GlobalFuncs.H"
#include "Test.H"

using namespace amrex;

Expand All @@ -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<int> print_flags(2,0);
pp.query("print_input", print_flags[0]);
pp.query("print_output", print_flags[1]);

{
Test_MathLibrary<Test_MM_Mul>
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();
}

0 comments on commit 56b92ab

Please sign in to comment.