matlab filter execution time - matlab

I need to filter 6 signals with 60000000 samples in each. So data are saved in matrix data(60000000,6). There are several aproaches how to do that:
data=randn(60000000,6);
b=ones(1,1000)/1000;
tic
R=filter(b,1,data);
toc
tic
for i=1:6
R2(:,i)=filter(b,1,data(:,i));
end
toc
tic
parfor i=1:6
R2(:,i)=filter(b,1,data(:,i));
end
toc
By documentation it is recommanded to use 1st form as the fastest one, but in my case it is the slowest.
Elapsed time is 172.235919 seconds.
Elapsed time is 45.354810 seconds.
Elapsed time is 59.250638 seconds.
In process explorer 1st form use only 1 thread of CPU. By documentation it should run on multiple threads in default. Have you experienced same problem?

Related

In the following code, why does Matlab alternate between fast and slow reads?

I am experiencing some strange behaviour in an code that reads a matrix from the hard disk.
I first write a binary file to the hard disk containing a matrix a:
N=17000;
a=rand(N);
fid=fopen('a','Wb');
fwrite(fid, a, 'double');
fclose(fid);
Now, I try reading the opening, reading, and closing the file to see how long the whole procedure takes. Each iteration should take the same amount of time.
for ii=1:10
tic
fid = fopen('a', 'r');
matrix=fread(fid, [N, N], 'double');
fclose(fid);
toc
end
Interestingly, the time it takes to read the file alternates between fast and slow! Is there any explanation for this? Here are the timings for the above loop:
Elapsed time is 1.259988 seconds.
Elapsed time is 2.454427 seconds.
Elapsed time is 1.534250 seconds.
Elapsed time is 2.453246 seconds.
Elapsed time is 1.535322 seconds.
Elapsed time is 2.454762 seconds.
Elapsed time is 1.534847 seconds.
Elapsed time is 2.449777 seconds.
Elapsed time is 1.534265 seconds.
Elapsed time is 2.449074 seconds.
That's very interesting, I wonder if it has to do with the preceding of opening it closing a file. A way to test that possibility may be to add delay(2) in the loop and then see if the times match.

How to end loop if value does not change for X consecutive seconds in Matlab

I am collecting data from a potentiometer connected to an Arduino. In the script, I tell matlab to keep collecting data for 2 minutes. But I need to tell it that if the user does not move the potentiometer for 10 consecutive seconds, then it should stop the loop and move to the next session (write the data to an excel file). Does anybody have ideas on how to achieve this?
Probably tic and toc can help you.
tic starts a stopwatch timer. The function records the internal time at execution of the tic command.
toc reads the elapsed time from the stopwatch timer started by the tic function.
tic;
while toc < 10
% Do your loopy things
if variable_changed
tic; % Restart stopwatch
end
end
Furthermore to be sure tic won't interact with other processes you should store it's value like this:
% First start stopwatch
time_since_last_movement = tic;
while toc(time_since_last_movement) < 10
% Do your loopy things
if variable_changed
time_since_last_movement = tic; % Restart stopwatch
end
end

Multiple Tesla K80 GPU's and parfor loops

I received a computer with 4xGPU's Tesla K80 and I am trying the parfor loops from Matlab PCT to speed up FFT's calculation and it is yet slower.
Here is what I am trying:
% Pupil is based on a 512x512 array
parfor zz = 1:4
gd = gpuDevice;
d{zz} = gd.Index;
probe{zz} = gpuArray(pupil);
Essai{zz} = gpuArray(pupil);
end
tic;
parfor ii = 1:4
gd2 = gpuDevice;
d2{ii} = gd2.Index;
for i = 1:100
[Essai{ii}] = fftn(probe{ii});
end
end
toc
%%
Starting parallel pool (parpool) using the 'local' profile ... connected to 4 workers.
Elapsed time is 1.805763 seconds.
Elapsed time is 1.412928 seconds.
Elapsed time is 1.409559 seconds.
Starting parallel pool (parpool) using the 'local' profile ... connected to 8 workers.
Elapsed time is 0.606602 seconds.
Elapsed time is 0.297850 seconds.
Elapsed time is 0.294365 seconds.
%%
tic; for i = 1:400; Essai{1} = fftn( probe{1} ); end; toc
Elapsed time is 0.193579 seconds !!!
Why is opening 8 workers faster as in principle I stored my variables into 4gpu's only (out of 8)?
Also, how to use a Tesla K80 as a single GPU?
Merci, Nicolas
I doubt that parfor works for multi-GPU systems. If speed is critical and you want to take full advantage of your GPUs, I suggest to write your own little CUDA script using the cuFFT library:
http://docs.nvidia.com/cuda/cufft/#multiple-GPU-cufft-transforms
Here is how to write your mex file containing CUDA code:
http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html
many thanks for your quick reply and for the links ! It is true that I was trying to avoid CUDA but it seems like the best option to spread FFTs.
Although I thought that parfor and spmd were great tools for multiple GPUs..

Will there be difference in performance between anonymous functions and normal functions?

Will there be difference in performance between anonymous functionS and normal functions? For example, any change in overhead of function calling?
Thanks and regards!
Unfortunately, I could not find anything specific on the subject. However, anonymous functions should have additional overhead in comparison to normal functions.
You can try it out for yourself. First create the file nonanon.m
function x=nonanon(y)
x=y^2;
end
Then create a cell file with:
%% non anon
tic
for i=1:1000000
z=nonanon(i);
end
toc
%% anon
f=#(x) x^2;
tic
for i=1:1000000
z=f(i);
end
toc
enjoy, the output:
Elapsed time is 0.513759 seconds.
Elapsed time is 14.434895 seconds.
Which concludes that anonymous functions are slower.
I repeated user677656's little test code, but a small variant using y=x*x instead of squaring (in both the nonanon and the anon case):
Elapsed time is 0.517514 seconds.
Elapsed time is 0.223450 seconds.
If I instead use the y=x^2 variant, I get similar results as user677656:
Elapsed time is 0.402366 seconds.
Elapsed time is 7.440174 seconds.
This is with Matlab 2012b. I have no idea why on earth these give different results.
I also tested y=sin(x) which gives similar results as the x*x case, and y=sqrt(x), which resulted in a slight (2.8 vs 3.9 seconds) advantage for the nonanon case.

Several time counters in MATLAB

I have a program running a loop I want to have two time counters, one for the loop, that will tell me how log did one iteration of the loop took, and one for the entire program. To the best of my knowledge tic and toc will work only once.
You're only familiar with this tic toc syntax:
tic; someCode; elapsed = toc;
But there is another syntax:
start = tic; someCode; elapsed = toc(start);
The second syntax makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations (matlab documentation of tic toc).
Here's how to use it in your case. Let's say that this is your code:
for i = 1:M
someCode;
end
Insert the tic and toc like this:
startLoop = tic;
for i = 1:N
startIteration = tic;
someCode;
endIteration = toc(startIteration);
end
endLoop = toc(startLoop);
You can also use the above syntax to create a vector for which the ith element is the time measurement for the ith iteration. Like this:
startLoop = tic;
for i = 1:N
startIteration(i) = tic;
someCode;
endIteration(i) = toc(startIteration(i));
end
endLoop = toc(startLoop);
You can use tic and toc to time nested operations, from the Matlab help for tic:
tStart=tic; any_statements; toc(tStart); makes the same time measurement, but allows you the option of running more than one stopwatch timer concurrently. You assign the output of tic to a variable tStart and then use that same variable when calling toc. MATLAB measures the time elapsed between the tic and its related toc command and displays the time elapsed in seconds. This syntax enables you to time multiple concurrent operations, including the timing of nested operations
I'm not able to try this right now, but you should be able to use multiple tic and toc statements if you store the tic values into variables.
Read Matlab's documentation on this, there is even a section on nesting them. Here is a rough example:
tStartOverall = tic;
...
tStartLoop = tic;
<your loop code here>
tEndLoop = toc(tStartLoop);
...
tEndOverall = toc(tStartOverall);