Avoid Copies of Arrays in MEX Functions
3 次查看(过去 30 天)
显示 更早的评论
My question is about following example which can be found in documentation:
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function {
ArrayFactory factory;
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
double sm = 0;
const TypedArray<double> inArray = inputs[0];
for (auto& elem : inArray) {
sm += elem;
}
outputs[0] = factory.createScalar(sm);
}
};
How important is to use the keyword "const" in declaration of inArray ? Will there be performance difference if I omit it ? I use following function in MEX module which is called from MexFunction::operator():
void Initialize(TypedArray<double> x, TypedArray<double> y)
{
...
}
void operator()(ArgumentList outputs, ArgumentList inputs)
{
...
Initialize(inputs[1], inputs[2])
...
}
Should I declare the input arguments of Initialize function as const to improve performance ? Problem is that code can be compiled and run without problems but I don't know what happens under the hood. If possible I want to avoid uneccessary allocations and deallocations. Especially if it is that simple like adding one keyword. In other projects unrelated to MATLAB i use references or pointers where possible if the underlying data type is complex. What is preferred way to pass data in MEX modules ?
0 个评论
回答(1 个)
Sameer
2024-8-21
Hi Jan,
Using const in C++ MEX files can make your code safer and sometimes more efficient.
Why Use const?
It signals that a variable won't change, which improves readability and helps prevent bugs, while allowing the compiler to optimize your code, though the impact on performance may be minor.
For the “Initialize” function, pass “TypedArray” objects by reference to avoid unnecessary copying. Use “const TypedArray<double>&” if you don't plan to modify them. This is crucial in MEX functions since we may deal with large datasets and reducing memory operations can boost performance.
Here's an example of how you can define the “Initialize” function to efficiently handle “TypedArray” objects by using “const” references.
#include "mex.hpp"
#include "mexAdapter.hpp"
using namespace matlab::data;
using matlab::mex::ArgumentList;
class MexFunction : public matlab::mex::Function {
ArrayFactory factory;
public:
void operator()(ArgumentList outputs, ArgumentList inputs) {
// Assuming inputs[0] is a TypedArray<double> that we want to sum
double sm = 0;
const TypedArray<double> inArray = inputs[0];
for (auto& elem : inArray) {
sm += elem;
}
outputs[0] = factory.createScalar(sm);
// Call Initialize with inputs[1] and inputs[2]
Initialize(inputs[1], inputs[2]);
}
private:
void Initialize(const TypedArray<double>& x, const TypedArray<double>& y) {
// Example logic using x and y
// Since x and y are passed as const references, they cannot be modified
for (auto& elem : x) {
// Do something with elem, but don't modify x
}
for (auto& elem : y) {
// Do something with elem, but don't modify y
}
}
};
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Data API for C++ 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!