data transferred in calling a user defined function - matlab

I need to analyze a big source code.Code contains several function calls.
Depending upon the computation and communication between function calls, i will need to figure out the best configuration scheme for the overall execution of the source code.
According to me ,
Data communicated in calling a function(if it is on different machine,server etc)=Input Data Size+Output Data Size
for getting the input data size and output data size ,i think i should rewrite all functions to have variable number of inputs and variables outputs.
[varargout] samplefunction(varargin) {
FOR i=0:nargin
inputdata=inputdata+sizeof(varargin(i));
% Do stuff here
}
isn't there a way to calculate size of cell array(varargin/varargout) directly in Matlab ?
or if u can suggest another approach to measure communicated data between function call ?

A call to cellfun() with string inputs would be very fast:
sizes = [cellfun('size',varargin,1); cellfun('size',varargin,2)];
or
lenghts = cellfun('length',varargin);
or
numels = cellfun('prodofsize',varargin);

Related

MATLAB - Use a user-defined class method in parallel on GPU

Let's say I have a class named Stack and it has a method that takes some other properties of Stack objects as input arguments which consist of vectors and scalars. The input vectors and output matrix change sizes depending on the object.
classdef Stack
...
...
function out = some_method(scalar1,scalar2,...,vec1,vec2...) % out has different number of
% columns depending on the size of vec1,vec2 etc.
...
out = ... % size of (4,m) where m changes for each object
end
This specific method of Stack should be called thousands of times to be used in another script for some other purpose. Since it takes time to use this method serially, a parallel solution will save a lot of time for bigger calculations.
I attempted to use cellfun by passing the indices of Stack objects inside a function hoping that I could run it on my GPU but cellfun doesn't support GPU computing. I also tried to use arrayfun but it doesn't support UniformOutput parameter on GPUs. I can't store different sizes of outputs inside a cell if I want to use my GPU.
My question is, is parallel computing on GPU possible for a class method/function that returns different sizes of outputs each time? If not, what could be a possible workaround for this problem?
Update: A short summary of my problem is; I would like to run the same function in parallel for each Stack object with their own inputs individually, knowing that their outputs don't match in sizes and collect them in a cell array at the end.

How can I optimize machine learning hyperparameters to be reused in multiple models?

I have a number of datasets, to each of which I want to fit a Gaussian process regression model. The default hyperparameters selected by fitrgp seem subjectively to produce less-than-ideal models. Enabling hyperparameter optimisation tends to result in a meaningful improvement but occasionally produces extreme overfitted values and is a computationally hungry process which prohibits an optimization for every model anyway.
Since fitrgp simply wraps bayesopt for its hyperparameter optimization, is it possible to call bayesopt directly to minimize some aggregate of the loss for multiple models (say, the mean) rather than the loss for one model at a time?
For example, if each dataset is contained in a cell array of tables tbls, I want to find a single value for sigma which can be imposed in calls to fitrgp for each table:
gprMdls = cellfun(#(tbl) {fitrgp(tbl,'ResponseVarName', 'Sigma',sigma)}, tbls);
Where numel(tbls) == 1 the process would be equivalent to:
gprMdl = fitrgp(tbls{1},'ResponseVarName', 'OptimizeHyperparameters','auto');
sigma = gprMdl.Sigma;
but this implementation doesn't naturally extend to a result where a single Sigma value is optimized for multiple models.
I managed this in the end by directly intervening in the built-in optimization routines.
By placing a breakpoint at the start of bayesopt (via edit bayesopt) and calling fitrgp with a single input dataset, I was able to determine from the Function Call Stack that the objective function used by bayesopt is constructed with a call to classreg.learning.paramoptim.createObjFcn. I also captured and stored the remaining input arguments to bayesopt to ensure my function call would be exactly analagous to one constructed by fitrgp.
Placing a breakpoint at the start of classreg.learning.paramoptim.createObjFcn and making a fresh call to fitrgp I was able to capture and store the input arguments to this function, so I could then create objective functions for different tables of predictors.
For my cell array of tables tbls, and all other variables kept as named in the captured createObjFcn scope:
objFcns = cell(size(tbls));
for ii = 1:numel(tbls)
objFcn{ii} = classreg.learning.paramoptim.createObjFcn( ...
BOInfo, FitFunctionArgs, tbls{ii}, Response, ...
ValidationMethod, ValidationVal, Repartition, Verbose);
end
An overall objective function can then be constructed by taking the mean of the objective functions for each dataset:
objFcn = #(varargin) mean(cellfun(#(f) f(varargin{:}),objFcns));
I was then able to call bayesopt with this objFcn along with the remaining arguments captured from the original call. This produced a set of hyperparameters as required and they seem to perform well for all datasets.

Access/Index Array In Simulink

I have a 2D matrix/array in my model, as shown in image. I need to be able to index/access it randomly and pass it as a signal. How do I do this?
I can't use From File block, because the storage is forced to be double and too large for my embedded design.
It doesn't appear I can use From Workspace block...because this array is defined in my model as SoundArray.
This seem like it should be SO SIMPLE, but I just can’t figure it out. The only way I can think of doing it is in custom C code…which I don’t want to do.
Thanks
Array Definition and Model At Bottom
A matlab Function block (formerly EML-block) can pick up model workspace data if it is in "Parameter" scope and you define a Parameter input in the Function block. You could then use other inputs for controlling the random access, then return the desired position as a signal output from the Matlab function block.
function y = fcn(i,j,soundArray)
y = soundArray(i,j);
(Where soundArray is defined as a Parameter, and i and j are Inputs)
Edit:
Or define a Data Store Memory (add definition block). Then put a Data Store Read block for that memory which is routed to a selector block with 2 dimensions and "starting index (port)" for both those dimensions.
I believe you can use Model Workspace data to initialize the Data Store Memory, but I don't think that Model Workspace data is "live" during simulation.

MATLAB execute function for multiple variables in loop

To process data in MATLAB I have to execute a certain function, let's call it function(). Since there is much data to be processed, like large array Time or Voltage (but many more) I execute those one by one like this:
TimeNew = function(Time);
VoltageNew = function(Voltage);
... etc
So this is done around 10 times. Moreover, I have to do such a thing multiple times, resulting in around 30 lines of code which all do the same thing but to a different variable.
Is there a way to optimize this? I am using the most recent version of MATLAB (2015b) and have all toolboxes installed.
A possible solution could be to store the input array into a struct, them use that struct as input of the function.
In the function you can identify the number and content of each field by using fieldnames and getfiled built-in function.
The function could return a structure as output whose names can be made the same as the ones of the input struct.
In the example below, three arrays are generated and the function siply compute their square.
var_1=1:10;
var_2=11:20;
var_3=21:30;
str_in=struct('var_1',var_1,'var_2',var_2,'var_3',var_3)
str_out=my_function(str_in)
The function
function [str_out]=my_function(str_in)
f_names=fieldnames(str_in)
n_fields=length(f_names);
for i=1:n_fields
x=getfield(str_in,f_names{i})
str_out.(f_names{i})=x.^2;
end
Hope this helps.
Qapla'
You could try cellfun
allResultsAsACell = cellfun(#function, {Time,Voltage,...,varN});
This is equivalent to
allResultsAsACell{1} = function(Time);
allResultsAsACell{2} = function(Voltage);
...
allResultsAsACell{N} = function{VarN};
The issue is just matching up the indices with the values. I'm sure you could code those in as well if you needed (e.g. timeInd = 1; voltageInd =2; ...)
To see more on the cellfun method, type
help cellfun
into your MATLAB terminal.

Minimize inputs by loading values in MATLAB functions

In MATLAB is it be better (as far as optimization is concerned):
1)To have a function "foo" with a lot of inputs that come from the outputs from other functions
or
2)Save at the end of the functions the results to a results.mat file and load it in the "foo" function and minimize its inputs this way?
almost always: option 1.
As option 2 depends on file IO, and thus one write and one read to/from a hard disk, SSD or similar, it will likely lose out against keeping variables in RAM. Moreover, if you pass arguments to a function, and that function only reads them, no explicit copies of that variable are made. This is not true for the .mat file solution, as something that you already have in memory will be explicitly copied onto an extremely slow device (HDD, SSD), and then again read back into memory from the extremely slow device, just to save on a few input arguments.
So, unless you're working with big data sets and your variables give you out-of-memory errors, keep everything in RAM as much as possible.
You can minimize argument count by simply collecting data in a container data type. MATLAB has cell and struct for this purpose (or classdef, if you include value classes). You can transform this:
[outarg1, outarg2] = function(arg1, arg2, arg3,...)
into
[outarg1, outarg2] = function(S)
where
S = struct(...
'arg1', function1(X),...
'arg2', function2(X,Y,Z),...
'arg3', function3(X,Z),...
%// etc.
);
or
S = {
function1(X)
function2(X,Y,Z)
function3(X,Z)
%// etc.
}
or similar. Or you can make use of the special cell/functions called varargin/nargin and varargout/nargout:
varargout = function(varargin)
% rename input arguments
arg1 = varargin{1};
arg2 = varargin{2};
%// etc.
% assign all output arguments in one go
[varargout{1:nargout}] = deal(outargs{:}));
end % function
You can read more about all these things by typing help <thing> on the MATLAB command prompt. For example,
help cell
will give you a wealth of information on what a cell is and how to use it.
I think using global variables is a better way to optimize memory and processing requirements.
you can use keyword global.