Hi guys,
I have a file that takes 340 seconds to run, I've learned just now that I can use my GPU instead of the CPU to run this code faster.
How is that done? is there any good recommendations for where and how to learn to use the Parallel computing toolbox?
Here's part of my code, if anyone can give me an example on it that'd be amazing.
%% Scan for news, filter them and assign tickers
for symboli = 1:NofStocks
newsone = news(cMoneyNet,'Number',NewsNum,'Symbol',symbol(symboli),'SearchTerm',term); % get news type 1
newstwo = news(cMoneyNet,'Number',NewsNum,'Symbol',symbol(symboli),'Category',category); % get news type 2
% Filter the news (type 1)
filteri = startsWith(newsone.ArticleTitle,"Analyst Actions") ...
| contains(newsone.ArticleTitle,"PT") ...
& not(contains(newsone.ArticleTitle,"OPTIONS"));
newsone = newsone(filteri,:);
% Assign a ticker to each piece of news
newsone.Ticker(:,1) = symbol(symboli);
newstwo.Ticker(:,1) = symbol(symboli);
% Join the news - type 1 and type 2
newsonetwo = vertcat(newsone,newstwo);
% Store the news from this ticker before moving to the next one
newsdata{symboli,1} = newsonetwo;
end

 採用された回答

Edric Ellis
Edric Ellis 2020 年 3 月 24 日

1 投票

  • It's hard to tell exactly what computations you're performing there. It would be better if you could give us a reproducible example of something that is particularly time-consuming in your workflow. Use the MATLAB Profiler to work out where this might be, and slim this down to a standlone piece of code that we can run.
  • The Profiler might already give you some idea as to where you can save time. It's always worthwhile improving your "serial" code as much as you can before you attempt to go parallel
  • Whether the GPU is appropriate depends very much on your problem type. GPUs are great when you have big chunks of numeric data on which you wish to apply vectorised operations. It looks like perhaps you have table data containing text, the GPU would not work for that, but perhaps process-based parallelism might help (i.e. using parfor).
  • It might help to look through this page telling you which Parallel Computing option might work best for you.

4 件のコメント

Or Shem Tov
Or Shem Tov 2020 年 3 月 25 日
Hi,
Thank your for your reply.
I have tried the profiler as you suggested, it seems like most of the time my code spends time computing the function "news", which is a function that comes with the Money.Net subscription, here's the documentation:
This program basically has a set of strings (symbol) and runs a loop to retrieve 2 types of news from Money.Net, by category and by search term for each symbol.
The loop runs once for every symbol inputed in the list of strings.
After it imports the news rows, it filters through the news, assigns the symbol to a column called "Ticker" and collects all the rows to the newsdata variable, later on I use this command to combine all the results.
data = vertcat(newsdata{1:NofStocks,1});
I have tried using parfor but for some reason it doesn't work on this loop, maybe it's because I'm using an external function? (the news function, which also requires me to connect to their API earlier in the code)
I have read a bit about the parfor, parfeval and spmd, do you think one of them can fit this issue?
Much thanks!
Edric Ellis
Edric Ellis 2020 年 3 月 25 日
I'm not familiar with the news function or the moneynet class, but there's a chance that parfor could speed things up for you, providing Money.Net allows you multiple simultaneous connections, and that that actually speeds things up.
My guess is that you need to construct a new moneynet instance on each worker. You could use a parallel.pool.Constant object to help here. So, you'd do something like this:
username = '...';
password = '...';
% Build a parallel.pool.Constant that creates a moneynet instance
% and then calls "close" on that when workerMoneyNet goes out of scope.
workerMoneyNet = parallel.pool.Constant(@() moneynet(username, password), @close);
parfor idx = 1:N
% Get the actual moneynet instance from within the "Constant"
cMoneyNet = workerMoneyNet.Value;
% Use cMoneyNet as before...
end
Edric Ellis
Edric Ellis 2020 年 3 月 25 日
Ah, I've just found out that Money.Net doesn't allow multiple simultaneous connections in that form, so I don't think the code in my previous comment will work.
So, unfortunately it seems like if the news() call is the main bottleneck, there isn't currently any way to parallelise that piece.
Or Shem Tov
Or Shem Tov 2020 年 3 月 26 日
I understand, thank you for your help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeParallel Computing についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by