Imposing parallel maximum running time for MatLab function in loops - multithreading

Fresh to the Matlab, not so familiar with coding and programing in MatLab.
function fucx()
for i = 1:3
for j = 1:3
for k = 1:3
try
%MainFuc()
%TimerFuc()
time = cputime;
time = cputime-time;
a = timer;
set (a, 'executionMode', 'fixedRate')
set (a,'timerfcn','disp(time)')
start(a)
timeStop = time;
if (timeStop>60) % in seconds
disp('RunOutOfTime: Program terminated');
stop(a)
break;
end;
catch
%%Err()
end
end
end
end
end
Thanks a lot for any help.
%%Update%%
I see the recommended answer as below. I've tried this one.
start = tic;
for i=1:1000000
if (mod(i,1000)==0)
if (toc(start) > 2) % here, 2 seconds
error('Took toooo loooong');
end
end
end
The thing I want to assure is that the timer or tic function running in parallel to the MainFuc() and know when to stop the loop.

You should use the "tic" and "toc" functions from matlab, see the documentation here Matlab tic function.
When you call "tic", the timer restarts, and every time you call "toc" it gives you the time in seconds since the last "tic". Then if you call "tic" another time, the timer will restart.
Here is the code you need.
a = 0;
for i = 1:3
for j = 1:3
tic;
for k = 1:10^7
try
%MainFuc()
a = a + 1;
catch
%%Err()
end
%TimerFuc()
time = toc;
if (time>2) % in seconds
disp('RunOutOfTime: Program terminated');
break;
end;
end
end
end

Related

Matrix multiplication is slower when multithreading in Julia

I am working with big matrices (size of 30k rows and ~100 columns). I am doing some matrix multiplication and the process would take around 20 seconds. This is my code:
#time begin
result = -1
data = -1
for i=1:size
first_matrix = #view data[i * split,:]
for j=1:size
second_matrix = #view Qg[j * split,:]
matrix_multiplication = first_matrix * second_matrix'
current_sum = sum(matrix_multiplication)
global result
if current_sum > result
result = current_sum
data = matrix_multiplication[1,1]
end
end
end
end
Trying to optimize this a little more, I tried to use multi-threading (julia --thread 4) to get better performance.
#time begin
global result = -1
global data = -1
lock = ReentrantLock()
for i=1:size
first_matrix = #view data[i * split,:]
Threads.#threads for j=1:size
second_matrix = #view Qg[j * split,:]
matrix_multiplication = first_matrix * second_matrix'
current_sum = sum(matrix_multiplication)
global result
if current_sum > result
lock(lock)
result = current_sum
data = matrix_multiplication[1,1]
unlock(lock)
end
end
end
end
By adding multi-threading I thought I would get an increase in performance, but the performance got worse (~40 seconds). I removed the lock to see if that was the issue, but still got the same performance. I am running this on a Dual-Core Intel Core i5 (MacBook pro). Does anyone know why my multi-threading code doesn't work?

Does xlswrite have limitations?

I'm running MATLAB R2017a. I am trying to execute a simple program that writes 3 characters to an Excel file. When I run the program with a small number of values it is fine but when I increase it to the millions, the program pauses.
Does anyone know why the programming is pausing like this?
X = []
filename = 'PopltnFL.xlsx';
NumTrump = 4617886;
NumClinton = 4504975;
NumOther = 297025;
*% Values for which program runs without puasing*
% NumTrump = 4;
% NumClinton = 4;
% NumOther = 2;
%
for ii = 1:NumTrump
X = [X,'T'];
end
for jj = 1:NumClinton
X = [X,'C'];
end
for kk = 1:NumOther
X = [X,'O'];
end
X = X';
xlswrite(filename,X)

How to continuously read from the serial port in Matlab?

This is my code:
serialPort = 'COM3';
s = serial(serialPort,'BaudRate',9600);
if (s.Status == 'closed')
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 200;
s.BytesAvailableFcn = #Serial_OnDataReceived;
fopen(s);
end
This is the CallBack function
function Serial_OnDataReceived(obj,event)
global s;
global i;
global endCheck;
global yVals;
global xVals;
if (s.Status == 'open')
while s.BytesAvailable > 0 && endCheck ~= '1',
data = str2num(fgetl(s));
dlmwrite ('SensorsReading.csv', data, '-append');
yVals = circshift(yVals,-1);
yVals(end) = data(3);
xVals = circshift(xVals,-1);
i = i + 0.0125;
xVals(end) = i;
end
figure(1);
plot(xVals,yVals);
end
end
Right after the FOPEN function, I get this Warning:
The BytesAvailableFcn is being disabled. To enable the callback property
either connect to the hardware with FOPEN or set the BytesAvailableFcn property.
Does the logic that happens in the callback function Serial_OnDataReceived run on a different thread?
Is there a way to pass parameters to the function? I want to modify an array in the main script from the callback function, which is in a different file. What's the best way to do so?
I am also trying to plot the values when they're updated to show some kind of dynamic animation.
I am not sure about what is generating the warning, but to answer your other question (how to pass parameter to the callback and how to update the plot), a better way to go would be to prepare your plot outside of the callback, then pass the handle to the callback for update only.
nPointsInFigure = 10 ; %// number of "sliding points" in your figure
step = 0.0125 ; %// X points spacing
xVals = linspace(-(nPointsInFigure-1)*step,0,nPointsInFigure) ; %// prepare empty data for the plot
yVals = NaN(nPointsInFigure,1) ;
figure(1) ;
hp = plot( xVals , yVals ) ; %// Generate the plot (with empty data) it will be passed to the callback.
serialPort = 'COM3';
s = serial(serialPort,'BaudRate',9600);
if (s.Status == 'closed')
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 200;
s.BytesAvailableFcn = {#Serial_OnDataReceived,hp,step} ; %// note how the parameters are passed to the callback
fopen(s);
end
Your callback will become:
function Serial_OnDataReceived(obj,event,hp,step)
global endCheck; %// I don't know how you use that so I could not get rid of it.
xVals = get(hp,'XData') ; %// retrieve the X values from the plot
yVals = get(hp,'YData') ; %// retrieve the Y values from the plot
while obj.BytesAvailable > 0 && endCheck ~= '1',
data = str2num(fgetl(s));
dlmwrite ('SensorsReading.csv', data, '-append');
yVals = circshift(yVals,-1);
yVals(end) = data(3);
xVals = circshift(xVals,-1);
xVals(end) = xVals(end-1) + step ;
end
set( hp , 'XData',xVals , 'YData',yVals ) ; %// update the plot with the new values
Also note: (1) You don't need to check for the state open/close of the serial port within the callback function. If the event was triggered it means the port was open. (2) you don't need to declare s as global, the obj parameter is actually the serial object itself.
If the suggestion from Hoki does not work, try using a timer.
% The following line is useful for debugging
mytimerCallback = #(~, ~) disp('Caught error For Tenzo Timer');
if isempty(timerXbee)
timerXbee = timer('ExecutionMode','FixedRate','Period',0.1,...
'TimerFcn',{#storeDataFromSerial},'ErrorFcn', mytimerCallback);
try
start(timerXbee);
catch
disp('Timer Xbee');
disp '******** InstrumentSubscription ERROR *********'
disp (exception.message);
disp '***********************************************'
end
end
% If you are using a polling approach, request data
sendRequest(cmd);
connectionRequested = true;
Then implement the timer function that will be called every period
function storeDataFromSerial(~,~,~)
while (get(xbee, 'BytesAvailable')~=0)
serialProtocol();
end
end
When you want to disconnect the device and close the communication don't forget to stop the timer with
stop(timerXbee)

Accurate time-keeping in node.js

I'm new to node.js, I'm trying to determine the elapsed time between an on and an off event of a switch (using BeagleBone Black), the script works, somewhat with .getTime() however from what I read it's not particularly accurate. So I did some research and attempted to use console.time, however from what I read there is no way to export the time value into a variable in this particular application.
I'm trying to write a script that times valve open and close events for an engine, so accuracy is paramount. The only input is a reed switch that is triggered by a passing magnet attached to a rotating flywheel.
More concisely, is there a way to time on/off events accurately in node.js?
var b = require('bonescript');
b.pinMode('P8_19', b.INPUT);
b.pinMode('P8_17', b.OUTPUT);
b.pinMode('USR3', b.OUTPUT);
b.attachInterrupt('P8_19', true, b.CHANGE, interruptCallback);
var cycle = 0;
//var t = 0;
var start, end;
function interruptCallback() {
if (b.digitalRead('P8_19') == 1)
{
console.log('Magnetic field present!');
cycle=cycle+1;
console.log ('cycle: ' + cycle);
}
else
{
//console.time('t');
start = (new Date()).getTime();
}
//console.timeEnd('t');
//console.log(t);
end = (new Date()).getTime();
console.log('elapsed time: ' + (end - start));
}
This is the full code that I'm currently using. Note: I've also displayed how I attempted to use console.time.
Thank you for your help!
For the most accurate time measure possible, use process.hrtime(). From the documentation:
Returns the current high-resolution real time in a [seconds,
nanoseconds] tuple Array. It is relative to an arbitrary time in the
past. It is not related to the time of day and therefore not subject
to clock drift. The primary use is for measuring performance between
intervals.
The function returns a two-element array that contains the count of seconds and count of nanoseconds. Passing one time object into another will return the difference between the two objects.
For your case:
function interruptCallback() {
if (b.digitalRead('P8_19') == 1) {
cycle = cycle + 1;
} else {
start = process.hrtime();
}
end = process.hrtime(start);
console.log('elapsed time: ' end[0] + ' seconds and ' + end[1] + 'nanoseonds.');
};

Nested Parallel While Loops

I am trying to run two while loops in parallel for the purpose of data acquisition.
This program will run for "n" num_trials with specific trial duration for each trial.
During each trial, the program will collect data while keeping track of the trial duration. For example, the first trial starts collecting data at 1 seconds and stops at 10 seconds. This process will repeat for the rest number of trials
How can I run both while loops in parallel?
Basically, I just want a method that will break the second while loop once the specified trial duration is complete?
Thanks in advance.
count = 0;
num_trials = 5; % Number of Trials = 5 Trials
trial_duration = 10; % Trial Duration = 10 Seconds
global check
check = 0;
% Number of Trials
for i = 1:num_trials
fprintf('Starting Trial %i\n', i);
t_start = tic;
% Start counting and Collecting Data
while toc(t_start) < trial_duration
% Data Collection
while (check ~= 1)
count = count +1;
end
end
fprintf('Ending Trial %i\n', i);
end
Have you tried using a single loop with && ?
while (toc(t_start) < trial_duration) && (check ~= 1)
% Data Collection
count = count +1;
end

Resources