Skip to content

Commit be6622e

Browse files
committed
Merge remote-tracking branch 'origin/parallel'
Conflicts: matlab2cpp/m2cpp.py matlab2cpp/rules/_expression.py matlab2cpp/rules/_reserved.py
2 parents 38e14a9 + d8ce9e7 commit be6622e

File tree

11 files changed

+138
-35
lines changed

11 files changed

+138
-35
lines changed

m2cpp.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@
3232
Automatically populate the `<filename>.py` file with datatype with suggestions
3333
if possible.""")
3434
parser.add_argument("-S", '--matlab-suggest', action="store_true",
35-
help="""Creates a folder m2cpp_temp. In the folder the matlab file(s) to be translated are also put. These matlab file(s) are slightly modified so that they output data-type information of the variables to file(s). This output can then be used to set the datatypes for the translation.""")
35+
help="""Creates a folder m2cpp_temp. In the folder the matlab file(s) to be translated are also put. \
36+
These matlab file(s) are slightly modified so that they output data-type information of the variables \
37+
to file(s). This output can then be used to set the datatypes for the translation.""")
3638

3739
parser.add_argument("-r", '--reset', action="store_true",
3840
help="""\
@@ -58,16 +60,25 @@
5860

5961
parser.add_argument("-p", "--paths_file", type=str, dest="paths_file",
6062
help="""\
61-
Flag and paths_file (-p path_to_pathsfile). m2cpp will look for matlab files in the location specified in the paths_file""")
63+
Flag and paths_file (-p path_to_pathsfile). m2cpp will look for matlab files in the location specified \
64+
in the paths_file""")
6265

6366
parser.add_argument("-omp", '--enable-omp', action="store_true",
6467
help="""\
65-
OpenMP code is inserted for Parfor and loops marked with the pragma %%#PARFOR (in Matlab code) when this flag is set.""")
68+
OpenMP code is inserted for Parfor and loops marked with the pragma %%#PARFOR (in Matlab code) when this \
69+
flag is set.""")
6670

6771
parser.add_argument("-tbb", '--enable-tbb', action="store_true",
6872
help="""\
6973
TBB code is inserted for Parfor and loops marked with the pragma %%#PARFOR (in Matlab code) when this flag is set.""")
7074

75+
parser.add_argument("-ref", '--reference', action="store_true",
76+
help="""\
77+
For the generated C++ code, function input parameters are "copied by value" as default. With this flag some \
78+
input parameters in the generated code can be const references. There can be some performance advantage of using
79+
const references instead of "copied by value". Note that Matlab "copies by value". \
80+
The Matlab code you try to translate to C++ code could try read as well as write to this input variable. \
81+
The code generator doesn't perform an analysis to detect this and then "copy by value" for this variable.""")
7182
parser.add_argument("-l", '--line', type=int, dest="line",
7283
help="Only display code related to code line number `<line>`.")
7384

matlab2cpp/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def main(args):
7878
"""
7979

8080
builder = tree.builder.Builder(disp=args.disp, comments=args.comments,
81-
original=args.original, enable_omp=args.enable_omp, enable_tbb=args.enable_tbb)
81+
original=args.original, enable_omp=args.enable_omp, enable_tbb=args.enable_tbb,
82+
reference=args.reference)
8283

8384
paths_from_file = []
8485
#read setpath.m file and return string list of paths

matlab2cpp/configure/reserved.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,14 @@ def Assigns_max(node):
264264
node[0].suggest = (0, var.mem)
265265
node[1].suggest = "int"
266266

267+
def Get_fliplr(node):
268+
if len(node) > 0:
269+
node.type = node[0].type
270+
271+
def Get_flipud(node):
272+
if len(node) > 0:
273+
node.type = node[0].type
274+
267275
def Get_eye(node):
268276
#set eye type to cx_mat if LHS is complex type
269277
if node.group.cls == "Assign" and node.group[0].mem == 4:
@@ -288,6 +296,9 @@ def Get_triu(node):
288296

289297
Var_eye = Get_eye
290298

299+
def Get_trace(node):
300+
node.type = node[0].type
301+
291302
def Get_transpose(node):
292303
"""Simple transpose
293304
"""

matlab2cpp/m2cpp.py

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -111,19 +111,15 @@
111111
return X;
112112
}
113113
114-
/*
115-
template<typename eT>
116-
inline typename arma::enable_if2<arma::is_real<eT>::value, arma::Col<eT> >::result fspan(eT a, eT step, eT b) {
117-
arma::Col<eT> s;
118-
int n = int((b - a) / step);
119-
if (n < 0) return s;
120-
121-
s.set_size(n + 1);
122-
for (int ii = 0; ii <= n; ii++)
123-
s(ii) = step * ii + a;
124-
return s;
125-
}
126-
*/
114+
inline arma::cx_mat ifft(arma::cx_mat X, int n, int dim)
115+
{
116+
if (dim == 1)
117+
X = arma::ifft(X, n);
118+
else
119+
X = arma::strans(arma::ifft(arma::strans(X), n));
120+
return X;
121+
}
122+
127123
128124
//template<typename eT>
129125
inline rowvec fspan(double a, double step, double b) {
@@ -194,6 +190,7 @@
194190
return A.n_elem;
195191
}
196192
193+
/*
197194
template<typename T>
198195
inline arma::Mat <typename T::elem_type> convmtx(const T& v, int m) {
199196
@@ -208,6 +205,31 @@
208205
out = out.t();
209206
return out;
210207
}
208+
*/
209+
210+
template<typename T>
211+
inline arma::Mat <typename T::elem_type> convmtx(const T& v, int m) {
212+
213+
arma::Mat<typename T::elem_type> out = zeros<arma::Mat<typename T::elem_type> >(v.n_elem + m - 1, m);
214+
arma::Col<typename T::elem_type> aux((typename T::elem_type*)v.memptr(), v.n_elem);
215+
216+
if (v.n_rows == 1)
217+
{
218+
for (int ii = 0; ii < m; ii++) {
219+
out.submat(ii, ii, ii + v.n_elem - 1, ii) = aux;
220+
}
221+
}
222+
else
223+
{
224+
for (int ii = 0; ii < m; ii++) {
225+
out.submat(ii, ii, ii + v.n_elem - 1, ii) = v;
226+
}
227+
}
228+
229+
if (v.n_rows == 1)
230+
out = out.t();
231+
return out;
232+
}
211233
212234
template <typename eT>
213235
inline typename arma::enable_if2< arma::is_real<typename eT::elem_type>::value, typename arma::Mat<typename eT::elem_type> >::result

matlab2cpp/rules/_expression.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def Mul(node):
120120
#mem = max(node[0].mem, 2)
121121

122122
if node.mem == 4 and node[0].dim == 0 and node[0].mem != 4:
123-
out = "(cx_double) %(0)s"
123+
out = "cx_double(%(0)s)"
124124
else:
125125
out = "%(0)s"
126126
index = 1
@@ -388,6 +388,9 @@ def Matrixdivision(node):
388388
elif child.mem < 2 and mem < 2:
389389
out = out + "*1.0/" + str(child)
390390

391+
elif child.type == "int" and mem == 4:
392+
out = out + "/" + "double(" + str(child) + ")"
393+
391394
else:
392395
out = out + "/" + str(child)
393396

@@ -625,10 +628,10 @@ def Transpose(node):
625628
"""
626629

627630
# not complex type
628-
if node.mem < 4:
629-
return "arma::strans(", "", ")"
631+
#if node.mem < 4:
632+
# return "arma::strans(", "", ")"
630633

631-
return "arma::trans(", "", ")"
634+
return "arma::strans(", "", ")"
632635

633636
def Ctranspose(node):
634637
"""Complex transpose

matlab2cpp/rules/_func_return.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,16 @@ def Params(node):
8080
"""
8181

8282
out = ""
83-
for child in node:
84-
out += ", " + type_string(child) + " " + str(child)
85-
return out[2:]
83+
84+
# if -ref, -reference flag option
85+
if node.project.builder.reference:
86+
out += ", ".join(["const " + child.type + "& " + child.name if child.dim > 0 else
87+
child.type + " " + child.name for child in node])
88+
89+
else:
90+
out = ", ".join([child.type + " " + child.name for child in node])
91+
92+
return out
8693

8794

8895
def Declares(node):

matlab2cpp/rules/_func_returns.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,19 @@ def Params(node):
154154

155155
# Create list of parameters
156156
out = ""
157-
for child in node[:]:
158-
out += ", " + type_string(child) + " " + str(child)
159-
return out[2:]
157+
158+
# if -ref, -reference flag option
159+
if node.project.builder.reference:
160+
161+
out += ", ".join(["const " + child.type + "& " + child.name if child.dim > 0 else
162+
child.type + " " + child.name for child in node])
163+
164+
return out
165+
166+
else:
167+
out = ", ".join([child.type + " " + child.name for child in node])
168+
169+
return out
160170

161171

162172
def Declares(node):

matlab2cpp/rules/_program.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,47 @@ def Headers(node):
9191
def Header(node):
9292
func = node.program[1][node.program[1].names.index(node.name)]
9393
if func.backend == "func_return":
94-
code = func[1][0].type + " " + func.name + "(" +\
94+
95+
# if -ref, -reference flag option
96+
if node.project.builder.reference:
97+
98+
code = func[1][0].type + " " + func.name + "(" +\
99+
", ".join(["const " + p.type + "& " + p.name if p.dim > 0 else
100+
p.type + " " + p.name for p in func[2]]) + ") ;"
101+
102+
else:
103+
code = func[1][0].type + " " + func.name + "(" +\
95104
", ".join([p.type + " " + p.name for p in func[2]]) + ") ;"
96105

106+
97107
elif func.backend == "func_returns" and not func[1]:
98-
code = "void " + func.name + "(" +\
108+
109+
# if -ref, -reference flag option
110+
if node.project.builder.reference:
111+
code = "void " + func.name + "(" +\
112+
", ".join(["const " + p.type + "& " + p.name if p.dim > 0 else
113+
p.type + " " + p.name for p in func[2]]) + ") ;"
114+
115+
else:
116+
code = "void " + func.name + "(" +\
99117
", ".join([p.type + " " + p.name for p in func[2]]) + ") ;"
100118

101119
elif func.backend == "func_returns" and func[1]:
102-
code = "void " + func.name + "(" +\
120+
121+
# if -ref, -reference flag option
122+
if node.project.builder.reference:
123+
code = "void " + func.name + "(" +\
124+
", ".join(["const " + p.type + "& " + p.name if p.dim > 0 else
125+
p.type + " " + p.name for p in func[2]]) + ", " +\
126+
", ".join([p.type + "& " + p.name for p in func[1]]) + ") ;"
127+
128+
else:
129+
code = "void " + func.name + "(" +\
103130
", ".join([p.type + " " + p.name for p in func[2]]) + ", " +\
104131
", ".join([p.type + "& " + p.name for p in func[1]]) + ") ;"
132+
133+
134+
105135
return code
106136

107137
Include = "%(name)s"

matlab2cpp/rules/_reserved.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
"eps", "exp", "log", "log2", "log10", "power", "floor", "ceil",
1515
"cos", "acos", "cosh", "acosh",
1616
"sin", "asin", "sinh", "asinh", "mod",
17-
"eye", "flipud", "length", "max", "min", "size", "chol",
18-
"transpose", "ctranspose",
17+
"eye", "fliplr", "flipud", "length", "max", "min", "size", "chol",
18+
"trace", "transpose", "ctranspose",
1919
"abs", "sqrt", "nextpow2", "fft", "ifft", "fft2", "ifft2", "hankel",
2020
"zeros", "ones", "round", "return", "rand",
2121
"qr",
@@ -533,6 +533,9 @@ def Get_eye(node):
533533

534534
raise NotImplementedError
535535

536+
def Get_trace(node):
537+
return "arma::trace(", ", ", ")"
538+
536539
def Get_transpose(node):
537540
"""Simple transpose
538541
"""
@@ -545,6 +548,9 @@ def Get_ctranspose(node):
545548

546549
return "arma::trans(%(0)s)"
547550

551+
def Get_fliplr(node):
552+
return "arma::fliplr(%(0)s)"
553+
548554
def Get_flipud(node):
549555
return "arma::flipud(%(0)s)"
550556

matlab2cpp/rules/assign.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def Assign(node):
5050
if lhs.dim == 0 and rhs.dim == 0:
5151

5252
if lhs.mem >= rhs.mem:
53-
out = "(" + lhs.type + ") " + out
53+
out = "" + lhs.type + "(" + out + ")"
5454
else:
5555
node.warning("Type reduction from %s to %s" %\
5656
(rhs.type, lhs.type))
@@ -69,7 +69,7 @@ def Assign(node):
6969
out = "conv_to<" + lhs.type + ">::from(%(1)s)"
7070
elif lhs.dim == 0 and rhs.dim == 0:
7171
if lhs.mem == 4:
72-
out = "(" + lhs.type + ")" + " %(1)s"
72+
out = "" + lhs.type + "" + "(%(1)s)"
7373

7474
# all the ways things are wrong
7575
elif lhs.dim > 0 and rhs.dim > 0:

0 commit comments

Comments
 (0)