diff options
Diffstat (limited to 'src')
-rwxr-xr-x | src/Makefile | 20 | ||||
-rw-r--r-- | src/fun.cpp | 180 | ||||
-rw-r--r-- | src/fun.h | 38 | ||||
-rw-r--r-- | src/fun.o | bin | 0 -> 4617248 bytes | |||
-rwxr-xr-x | src/libfun.so | bin | 0 -> 2887768 bytes | |||
-rw-r--r-- | src/main.cpp | 112 | ||||
-rwxr-xr-x | src/testfun | bin | 0 -> 41008 bytes |
7 files changed, 350 insertions, 0 deletions
diff --git a/src/Makefile b/src/Makefile new file mode 100755 index 0000000..ad535f0 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,20 @@ +LIBNAME = fun + +all: lib$(LIBNAME).so test$(LIBNAME) + +$(LIBNAME).o: $(LIBNAME).h $(LIBNAME).cpp + mkoctfile -c -fPIC $(LIBNAME).cpp + +lib$(LIBNAME).so: $(LIBNAME).o + g++ -loctave -loctinterp -shared -o lib$(LIBNAME).so $(LIBNAME).o + +test$(LIBNAME): lib$(LIBNAME).so main.cpp fun.h + g++ -Wall -L$(PWD) -Wl,-rpath=$(PWD) -o $@ main.cpp -l$(LIBNAME) -g -loctave -loctinterp + +install: + cp fun.h ../thirdparty/linux/include/ + cp libfun.so ../thirdparty/linux/lib/x64/ + +.PHONY: clean +clean: + $(RM) -f *.out *.so *.o *.oct diff --git a/src/fun.cpp b/src/fun.cpp new file mode 100644 index 0000000..31d5dc1 --- /dev/null +++ b/src/fun.cpp @@ -0,0 +1,180 @@ +#include <iostream>
+#include <stdlib.h>
+#include <octave/oct.h>
+#include <octave/octave.h>
+#include <octave/parse.h>
+#include <octave/interpreter.h>
+#include <math.h>
+#include <string>
+#include "fun.h"
+
+extern "C"
+{
+ int fun(FUNCARGS *inp, FUNCCALL *funcall)
+ {
+
+ static octave::interpreter interpreter;
+ bool status = interpreter.initialized();
+
+ if(status==false)
+ {
+ interpreter.initialize ();
+ int status_exec = interpreter.execute ();
+
+ if (status_exec != 0)
+ {
+ std::cerr << "creating embedded Octave interpreter failed!"
+ << std::endl;
+ }
+ }
+
+ try
+ {
+ octave_value_list in;
+ unsigned int k;
+ int l;
+ int str_count = 0;
+ char str_fun[20];
+ char str_pkg[20];
+ int pkg=0;
+ int nouts;
+ for(l=0;l<funcall->n_in_arguments;l++)
+ {
+ if(inp[l].type==TYPE_DOUBLE)
+ {
+ if(inp[l].is_in_cmplx==1)
+ {
+ ComplexMatrix matr = ComplexMatrix (inp[l].n_in_rows,inp[l].n_in_cols);
+ double* id_real = (double *)inp[l].in_data_real;
+ double* id_img = (double *)inp[l].in_data_img;
+ k=0;
+ for (int r=0;r<inp[l].n_in_rows;r++)
+ {
+ for(int c=0;c<inp[l].n_in_cols;c++)
+ {
+ Complex cc(id_real[k],id_img[k]);
+ matr(r,c) = cc;
+ k++;
+ }
+ }
+ in(l-str_count) = octave_value(matr);
+ }
+ else
+ {
+ Matrix inMatrix_x(inp[l].n_in_rows,inp[l].n_in_cols);
+ double* id = (double *)inp[l].in_data_real;
+ k=0;
+ for( unsigned int i = 0; i < inp[l].n_in_rows; i++ )
+ {
+ for( unsigned int j = 0; j < inp[l].n_in_cols; j++ )
+ {
+ inMatrix_x(i, j) = id[k];
+ k++;
+ }
+ }
+ in(l-str_count) = inMatrix_x;
+ }
+ }
+ else if(inp[l].type==TYPE_STRING)
+ {
+ //std::cout << "In fun string. l is : " << l << '\n';
+
+ char* c = (char *)inp[l].in_data_real;
+ //std::cout << "String is: " << c << '\n';
+ if(l==0)
+ strcpy(str_fun,c);
+ else if(l==1)
+ {
+ strcpy(str_pkg,c);
+ pkg=1;
+ }
+ else
+ in(l-str_count) = c;
+
+ str_count++;
+ //std::cout << "String is: " << c << '\n';
+ }
+ }
+
+ if(pkg==1)
+ {
+ //std::cout << "loading package " << str_pkg << '\n';
+ octave::feval ("pkg", ovl ("load", str_pkg), 0);
+ }
+
+ octave_value_list out = octave::feval (str_fun, in, funcall->n_out_user);
+
+
+ int row;
+ int col;
+ nouts = out.length();
+ funcall->n_out_arguments = nouts;
+//std::cout << "funcall->n_out_arguments is: " << funcall->n_out_arguments << '\n';
+
+ for( unsigned int ii = 0; ii < nouts; ii++ )
+ {
+ if(out(ii).iscomplex()==1)
+ {
+ inp[ii].is_out_cmplx=1;
+ //std::cout << "out "<< ii<< " is complex" << '\n';
+ ComplexMatrix cmOut(out(ii).complex_matrix_value());
+ //std::cout << "cmOut "<< cmOut << '\n';
+ //std::cout << "Out(ii) "<< out(ii).complex_matrix_value() << '\n';
+ //std::cout << "out(ii) "<< out(ii) << '\n';
+ row = cmOut.rows();
+ col = cmOut.columns();
+ inp[ii].n_out_rows = row;
+ inp[ii].n_out_cols = col;
+ k=0;
+ inp[ii].out_data_real = malloc(sizeof(double)*(row*col));
+ inp[ii].out_data_img = malloc(sizeof(double)*(row*col));
+ double* rd = (double *)inp[ii].out_data_real;
+ double* cd = (double *)inp[ii].out_data_img;
+ for(unsigned int i=0;i<row;i++)
+ {
+ for(unsigned int j=0;j<col;j++)
+ {
+ rd[k]=real(cmOut(k));
+ cd[k]=imag(cmOut(k));
+ //std::cout << "out img "<< k << " is :" << (double)imag(cmOut(k)) << '\n';
+ k++;
+ }
+ }
+ }
+ else
+ {
+ //std::cout << "out "<< ii<< " is NOT complex" << '\n';
+ inp[ii].is_out_cmplx=0;
+ Matrix mOut(out(ii).matrix_value());
+ row = mOut.rows();
+ col = mOut.columns();
+ inp[ii].n_out_rows = row;
+ inp[ii].n_out_cols = col;
+ k=0;
+ inp[ii].out_data_real = malloc(sizeof(double)*(row*col));
+ double* dd = (double *)inp[ii].out_data_real;
+ for(unsigned int i=0;i<row;i++)
+ {
+ for(unsigned int j=0;j<col;j++)
+ {
+ dd[k]=mOut(k);
+ k++;
+ }
+ }
+ }
+ }
+ }
+ catch (const octave::exit_exception& ex)
+ {
+ std::cerr << "Octave interpreter exited with status = "
+ << ex.exit_status () << std::endl;
+ return 1;
+ }
+ catch (const octave::execution_exception&)
+ {
+ std::cerr << "error encountered in Octave evaluator!" << std::endl;
+ return 1;
+ }
+ return 0;
+ }
+}
diff --git a/src/fun.h b/src/fun.h new file mode 100644 index 0000000..ac0c166 --- /dev/null +++ b/src/fun.h @@ -0,0 +1,38 @@ +#define fun_h__ +//extern "C" int fun (double* answ, double* in1, int in1_row, std::string name, std::string opt); + +extern "C" +{ + + typedef enum + { + TYPE_DOUBLE, + TYPE_STRING, + }FUNCTYPE; + + typedef struct + { + FUNCTYPE type; + int n_in_rows; + int n_in_cols; + int n_out_rows; + int n_out_cols; + int is_in_cmplx; + int is_out_cmplx; + void* in_data_real; + void* in_data_img; + void* out_data_real; + void* out_data_img; + }FUNCARGS; + + typedef struct { + int n_in_arguments; // number of input arguments + int n_out_arguments; // number of output arguments + int n_out_user; // number of output arguments + char *err; // Name + //char *package; //Name of octave package to be loaded + FUNCARGS *argument; +} FUNCCALL; + + int fun(FUNCARGS *arr, FUNCCALL *call); +} diff --git a/src/fun.o b/src/fun.o Binary files differnew file mode 100644 index 0000000..b939504 --- /dev/null +++ b/src/fun.o diff --git a/src/libfun.so b/src/libfun.so Binary files differnew file mode 100755 index 0000000..81fa71f --- /dev/null +++ b/src/libfun.so diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..36be819 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,112 @@ +#include<iostream> +#include <stdlib.h> +#include <string.h> +#include"fun.h" +//octave_fun("arburg","signal", [1,2,3,4,5],2) +int main(void) +{ + FUNCCALL funcall; + FUNCCALL *funptr = &funcall; + funcall.n_in_arguments = 2; + funcall.n_out_user = 1; + + FUNCARGS ins[funcall.n_in_arguments*funcall.n_out_user]; + FUNCARGS *argptr = ins; + + int a; + int in_type[4] = {10,1}; + char str[3][20] = {"hamming","signal"}; + int si; + double* d; + int size_double; + for(a = 0; a<funcall.n_in_arguments;a++) + { + if(in_type[a]==1) + { + ins[a].is_in_cmplx = 0; + ins[a].n_in_rows = 1; + ins[a].n_in_cols = 1; + //if(a>2) + //ins[a].n_in_cols = 1; + + size_double = ins[a].n_in_rows*ins[a].n_in_cols; + ins[a].type = TYPE_DOUBLE; + ins[a].in_data_real = malloc(sizeof(double)*size_double); + d = (double *)ins[a].in_data_real; + for(int i=0;i<size_double;i++) + d[i] = (i+4); + } + else if(in_type[a]==10) + { + if(a==0) + si=0;// "hamming"; + else if(a==1) + si=1;// "pkg"; + else + si=2;// "symmetric"; + + ins[a].type = TYPE_STRING; + int len = strlen(str[si]); + ins[a].n_in_rows = 1; + ins[a].n_in_cols = len; + ins[a].in_data_real = malloc(sizeof(char)*len+1); + char* c = (char *)ins[a].in_data_real; + strcpy(c,str[si]); + + std::cout << "func string in main is: " << c << '\n'; + } + + } + + for(int j=0;j<funcall.n_in_arguments;j++) + { + if(in_type[j]==1) + { + d = (double *)ins[j].in_data_real; + size_double = ins[j].n_in_rows*ins[j].n_in_cols; + for(int i=0;i<size_double;i++) + std::cout << "input data" << j<< " is: " << d[i] << '\n'; + } + } + + fun(argptr, funptr); + + + + for(int j=0;j<funcall.n_out_user;j++) + { + double* rd = (double *)ins[j].out_data_real; + for(int i=0;i<(ins[j].n_out_rows*ins[j].n_out_cols);i++) + std::cout << "output data real: " << j<< " is: " << rd[i] << '\n'; + + if(ins[j].is_out_cmplx==1) + { + double* cd = (double *)ins[j].out_data_img; + for(int i=0;i<(ins[j].n_out_rows*ins[j].n_out_cols);i++) + std::cout << "output data img: " << j<< " is: " << cd[i] << '\n'; + } + } + + std::cout << "ins[0].n_in_rows is: " << ins[0].n_in_rows << '\n'; + std::cout << "ins[0].n_in_cols is: " << ins[0].n_in_cols << '\n'; + + for(int i=0;i<funcall.n_in_arguments;i++) + { + free(ins[i].in_data_real); + + if(ins[i].is_in_cmplx==1) + free(ins[i].in_data_img); + } + + for(int i=0;i<funcall.n_out_user;i++) + { + free(ins[i].out_data_real); + + if(ins[i].is_out_cmplx==1) + free(ins[i].out_data_img); + } + //free(ins[1].in_data); + //free(ins[2].in_data); + //free(ins[0].out_data); +return 0; +} diff --git a/src/testfun b/src/testfun Binary files differnew file mode 100755 index 0000000..631bb20 --- /dev/null +++ b/src/testfun |