Unverified Commit 2efdbc1b authored by sschneiders's avatar sschneiders Committed by GitHub
Browse files

Merge pull request #24 from EmbeddedMontiArc/strepkov-model-test

fixed constant matrix generation and added tests to check correctness
parents 6570b6cf d53cae97
......@@ -64,11 +64,12 @@ public class MathConverter {
public static String getInstructionStringConstantVectorExpression(MathMatrixArithmeticValueSymbol mathExpressionSymbol, String matrixName, String typeName) {
String result = "";
int column = 0;
for (MathMatrixAccessOperatorSymbol symbol : mathExpressionSymbol.getVectors()) {
Log.debug(symbol.getTextualRepresentation(),"Symbol:");
Log.debug(symbol.getTextualRepresentation(), "Symbol:");
int row = 0;
for (MathMatrixAccessSymbol symbolAccess : symbol.getMathMatrixAccessSymbols()) {
Log.debug("symbolAccess: "+symbolAccess.getTextualRepresentation(),"MathConverter");
Log.debug("symbolAccess: " + symbolAccess.getTextualRepresentation(), "MathConverter");
result += matrixName + "(" + column + "," + row + ") = ";
result += symbolAccess.getTextualRepresentation();
result += ";\n";
......@@ -83,8 +84,8 @@ public class MathConverter {
firstPart += "(" + mathExpressionSymbol.getVectors().size() + ");\n";
} else if (typeName.equals(curBackend.getMatrixTypeName())) {
firstPart += curBackend.getMatrixInitString(mathExpressionSymbol.getVectors().get(0).getMathMatrixAccessSymbols().size(),
mathExpressionSymbol.getVectors().size());
firstPart += curBackend.getMatrixInitString(mathExpressionSymbol.getVectors().size(),
mathExpressionSymbol.getVectors().get(0).getMathMatrixAccessSymbols().size());
}
return firstPart + result;
}
......@@ -113,7 +114,7 @@ public class MathConverter {
}
public static String getConvertedUnitNumber(ASTUnitNumber unitNumber) {
if(!unitNumber.getNumber().isPresent()){
if (!unitNumber.getNumber().isPresent()) {
Log.error("Number should be present");
}
if (unitNumber.getNumber().get().getDivisor().intValue() == 1) {
......
......@@ -37,7 +37,7 @@ import static org.junit.Assert.*;
*/
public class GenerationTest extends AbstractSymtabTest {
@Ignore
@Test
public void testBasicConstantAssignment() throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
......@@ -46,7 +46,22 @@ public class GenerationTest extends AbstractSymtabTest {
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/testConstantAssignment");
generatorCPP.generateFiles(componentSymbol, symtab);
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "testConstantAssignment/";
testFilesAreEqual(files, restPath);
}
@Test
public void testBasicConstantAssignment2() throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("test.basicConstantAssignment2", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/testConstantAssignment2");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "testConstantAssignment2/";
testFilesAreEqual(files, restPath);
}
@Test
......
......@@ -19,6 +19,36 @@ import static org.junit.Assert.assertNotNull;
*/
public class BasicGenerationArmadilloTest extends AbstractSymtabTest {
@Test
public void testBasicConstantAssignment() throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("test.basicConstantAssignment", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/testConstantAssignment");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/testConstantAssignment/";
testFilesAreEqual(files, restPath);
}
@Test
public void testBasicConstantAssignment2() throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("test.basicConstantAssignment2", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/testConstantAssignment2");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/testConstantAssignment2/";
testFilesAreEqual(files, restPath);
}
@Test
public void testMathUnitOptimizations() throws IOException {
ThreadingOptimizer.resetID();
......@@ -96,8 +126,8 @@ public class BasicGenerationArmadilloTest extends AbstractSymtabTest {
}
@Test
public void testAllObjectDetectorInstances() throws IOException{
for(int i=1;i<=9;++i){
public void testAllObjectDetectorInstances() throws IOException {
for (int i = 1; i <= 9; ++i) {
testObjectDetectorInstancingL0(i);
testObjectDetectorInstancingL1(i);
testObjectDetectorInstancingL2(i);
......@@ -108,41 +138,41 @@ public class BasicGenerationArmadilloTest extends AbstractSymtabTest {
private void testObjectDetectorInstancingL0(int number) throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector"+number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector" + number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector"+number+"/l0");
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector" + number + "/l0");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/detectionObjectDetector"+number+"/l0/";
String restPath = "armadillo/detectionObjectDetector" + number + "/l0/";
testFilesAreEqual(files, restPath);
}
private void testObjectDetectorInstancingL1(int number) throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector"+number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector" + number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setUseAlgebraicOptimizations(true);
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector"+number+"/l1");
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector" + number + "/l1");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/detectionObjectDetector"+number+"/l1/";
String restPath = "armadillo/detectionObjectDetector" + number + "/l1/";
testFilesAreEqual(files, restPath);
}
private void testObjectDetectorInstancingL2(int number) throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector"+number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector" + number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setUseThreadingOptimization(true);
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector"+number+"/l2");
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector" + number + "/l2");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/detectionObjectDetector"+number+"/l2/";
String restPath = "armadillo/detectionObjectDetector" + number + "/l2/";
testFilesAreEqual(files, restPath);
}
......@@ -150,14 +180,14 @@ public class BasicGenerationArmadilloTest extends AbstractSymtabTest {
private void testObjectDetectorInstancingL3(int number) throws IOException {
TaggingResolver symtab = createSymTabAndTaggingResolver("src/test/resources");
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector"+number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
ExpandedComponentInstanceSymbol componentSymbol = symtab.<ExpandedComponentInstanceSymbol>resolve("detection.objectDetector" + number, ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentSymbol);
GeneratorCPP generatorCPP = new GeneratorCPP();
generatorCPP.setUseThreadingOptimization(true);
generatorCPP.useArmadilloBackend();
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector"+number+"/l3");
generatorCPP.setGenerationTargetPath("./target/generated-sources-cpp/armadillo/detectionObjectDetector" + number + "/l3");
List<File> files = generatorCPP.generateFiles(symtab, componentSymbol, symtab);
String restPath = "armadillo/detectionObjectDetector"+number+"/l3/";
String restPath = "armadillo/detectionObjectDetector" + number + "/l3/";
testFilesAreEqual(files, restPath);
}
}
#ifndef HELPERA_H
#define HELPERA_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include "armadillo.h"
#include <stdarg.h>
#include <initializer_list>
using namespace arma;
class HelperA{
public:
static mat getEigenVectors(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenVectors;
}
static vec getEigenValues(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenValues;
}
static mat getKMeansClusters(mat A, int k){
mat clusters;
kmeans(clusters,A.t(),k,random_subset,20,true);
printf("cluster centroid calculation done\n");
std::ofstream myfile;
myfile.open("data after cluster.txt");
myfile << A;
myfile.close();
std::ofstream myfile2;
myfile2.open("cluster centroids.txt");
myfile2 << clusters;
myfile2.close();
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);
std::ofstream myfile3;
myfile3.open("data after index.txt");
myfile3 << indexedData;
myfile3.close();
return indexedData;
}
static mat getKMeansClustersIndexData(mat A, mat centroids){
mat result=mat(A.n_cols, 1);
for(int i=0;i<A.n_cols;++i){
result(i, 0) = getIndexForClusterCentroids(A, i, centroids);
}
return result;
}
static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){
int index=1;
double lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);
for(int i=1;i<centroids.n_cols;++i){
double curDistance=getEuclideanDistance(A, colIndex, centroids, i);
if(curDistance<lowestDistance){
lowestDistance=curDistance;
index=i+1;
}
}
return index;
}
static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){
double distance=0;
for(int i=0;i<A.n_rows;++i){
double elementA=A(i,colIndexA);
double elementB=B(i,colIndexB);
double diff=elementA-elementB;
distance+=diff*diff;
}
return sqrt(distance);
}
static mat getSqrtMat(mat A){
cx_mat result=sqrtmat(A);
return real(result);
}
static mat invertDiagMatrix(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = 1/curVal;
}
return A;
}
};
#endif
#ifndef TEST_BASICCONSTANTASSIGNMENT
#define TEST_BASICCONSTANTASSIGNMENT
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo.h"
using namespace arma;
class test_basicConstantAssignment{
public:
mat CONSTANTCONSTANTVECTOR0;
void init()
{
CONSTANTCONSTANTVECTOR0 = mat(4,4);
CONSTANTCONSTANTVECTOR0(0,0) = 1;
CONSTANTCONSTANTVECTOR0(0,1) = 2;
CONSTANTCONSTANTVECTOR0(0,2) = 3;
CONSTANTCONSTANTVECTOR0(0,3) = 4;
CONSTANTCONSTANTVECTOR0(1,0) = 2;
CONSTANTCONSTANTVECTOR0(1,1) = 3;
CONSTANTCONSTANTVECTOR0(1,2) = 4;
CONSTANTCONSTANTVECTOR0(1,3) = 1;
CONSTANTCONSTANTVECTOR0(2,0) = 3;
CONSTANTCONSTANTVECTOR0(2,1) = 4;
CONSTANTCONSTANTVECTOR0(2,2) = 1;
CONSTANTCONSTANTVECTOR0(2,3) = 2;
CONSTANTCONSTANTVECTOR0(3,0) = 4;
CONSTANTCONSTANTVECTOR0(3,1) = 1;
CONSTANTCONSTANTVECTOR0(3,2) = 2;
CONSTANTCONSTANTVECTOR0(3,3) = 3;
}
void execute()
{
mat m = CONSTANTCONSTANTVECTOR0;
}
};
#endif
#ifndef HELPERA_H
#define HELPERA_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include "armadillo.h"
#include <stdarg.h>
#include <initializer_list>
using namespace arma;
class HelperA{
public:
static mat getEigenVectors(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenVectors;
}
static vec getEigenValues(mat A){
vec eigenValues;
mat eigenVectors;
eig_sym(eigenValues,eigenVectors,A);
return eigenValues;
}
static mat getKMeansClusters(mat A, int k){
mat clusters;
kmeans(clusters,A.t(),k,random_subset,20,true);
printf("cluster centroid calculation done\n");
std::ofstream myfile;
myfile.open("data after cluster.txt");
myfile << A;
myfile.close();
std::ofstream myfile2;
myfile2.open("cluster centroids.txt");
myfile2 << clusters;
myfile2.close();
mat indexedData=getKMeansClustersIndexData(A.t(), clusters);
std::ofstream myfile3;
myfile3.open("data after index.txt");
myfile3 << indexedData;
myfile3.close();
return indexedData;
}
static mat getKMeansClustersIndexData(mat A, mat centroids){
mat result=mat(A.n_cols, 1);
for(int i=0;i<A.n_cols;++i){
result(i, 0) = getIndexForClusterCentroids(A, i, centroids);
}
return result;
}
static int getIndexForClusterCentroids(mat A, int colIndex, mat centroids){
int index=1;
double lowestDistance=getEuclideanDistance(A, colIndex, centroids, 0);
for(int i=1;i<centroids.n_cols;++i){
double curDistance=getEuclideanDistance(A, colIndex, centroids, i);
if(curDistance<lowestDistance){
lowestDistance=curDistance;
index=i+1;
}
}
return index;
}
static double getEuclideanDistance(mat A, int colIndexA, mat B, int colIndexB){
double distance=0;
for(int i=0;i<A.n_rows;++i){
double elementA=A(i,colIndexA);
double elementB=B(i,colIndexB);
double diff=elementA-elementB;
distance+=diff*diff;
}
return sqrt(distance);
}
static mat getSqrtMat(mat A){
cx_mat result=sqrtmat(A);
return real(result);
}
static mat invertDiagMatrix(mat A){
for(int i=0;i<A.n_rows;++i){
double curVal = A(i,i);
A(i,i) = 1/curVal;
}
return A;
}
};
#endif
#ifndef TEST_BASICCONSTANTASSIGNMENT2
#define TEST_BASICCONSTANTASSIGNMENT2
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#include "armadillo.h"
using namespace arma;
class test_basicConstantAssignment2{
public:
mat CONSTANTCONSTANTVECTOR0;
void init()
{
CONSTANTCONSTANTVECTOR0 = mat(5,4);
CONSTANTCONSTANTVECTOR0(0,0) = 1;
CONSTANTCONSTANTVECTOR0(0,1) = 2;
CONSTANTCONSTANTVECTOR0(0,2) = 3;
CONSTANTCONSTANTVECTOR0(0,3) = 4;
CONSTANTCONSTANTVECTOR0(1,0) = 2;
CONSTANTCONSTANTVECTOR0(1,1) = 3;
CONSTANTCONSTANTVECTOR0(1,2) = 4;
CONSTANTCONSTANTVECTOR0(1,3) = 1;
CONSTANTCONSTANTVECTOR0(2,0) = 3;
CONSTANTCONSTANTVECTOR0(2,1) = 4;
CONSTANTCONSTANTVECTOR0(2,2) = 1;
CONSTANTCONSTANTVECTOR0(2,3) = 2;
CONSTANTCONSTANTVECTOR0(3,0) = 4;
CONSTANTCONSTANTVECTOR0(3,1) = 1;
CONSTANTCONSTANTVECTOR0(3,2) = 2;
CONSTANTCONSTANTVECTOR0(3,3) = 3;
CONSTANTCONSTANTVECTOR0(4,0) = 4;
CONSTANTCONSTANTVECTOR0(4,1) = 4;
CONSTANTCONSTANTVECTOR0(4,2) = 4;
CONSTANTCONSTANTVECTOR0(4,3) = 4;
}
void execute()
{
mat m = CONSTANTCONSTANTVECTOR0;
}
};
#endif
#ifndef HELPER_H
#define HELPER_H
#define _GLIBCXX_USE_CXX11_ABI 0
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
#include <stdarg.h>
#include <initializer_list>
class Helper
{
public:
static void init()
{
string_vector argv(2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main(2, argv.c_str_vec(), 1);
//octave_debug=1;
//feval ("pkg", ovl ("load", "all"), 0);
}
static octave_value_list convertToOctaveValueList(double a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a)
{
octave_value_list in;
in(0) = a;
return in;
}
static octave_value_list convertToOctaveValueList(double a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(std::initializer_list<double> args)
{
octave_value_list in;
int counter = 0;
for(double element : args) {
in(counter) = octave_value(element);
++counter;
}
return in;
}
static octave_value_list convertToOctaveValueList(Matrix a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(RowVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list convertToOctaveValueList(ColumnVector a, double b)
{
octave_value_list in;
in(0) = a;
in(1) = b;
return in;
}
static octave_value_list callOctaveFunction(octave_value_list in, std::string functionName,int argsOut)
{
/*octave_idx_type n = 2;
octave_value_list in;
for(octave_idx_type i = 0; i < n; i++)
in(i) = octave_value(5 * (i + 2));
octave_value_list out = feval("gcd", in, 1);