How to take advantage of AWS Ec2 if my code cannot be parallelized?

1 view (last 30 days)
I launched an EC2 instance to run my Matlab code faster. Despite the fact that I chose a machine much more powerful than my Mac, the code ran slower on AWS.
The problem seems to be that my code is not parallelized. Apparently, while using my Mac Matlab will automatically parallelize certain operations, it does not on AWS (this is what I understood, does it make sense? Why should that be the case?).
I parallelized my code to find out that the parallelized version runs slower on my Mac. After some research I came to the conclusion that there might be two reasons for that:
  1. When I parallelize (e.g., use a parfor) each worker operates with one core and this shuts down the "automatic parallelization" of Matlab
  2. the part that takes the most time in my code is a loop in which each operation takes little time, but the loop is long (many of these iterations). As each operation does not take a lot of time, the overhead time outweighs the "benefits".
My question is: how can I take advantage of EC2 if parallelizing my code does not solve the problem?
Why is multi-threading off on EC2? Can I activate it somehow?

Accepted Answer

Walter Roberson
Walter Roberson on 2 Jun 2020
Edit the cluster profile to increase the number of threads per worker. The parpool size must be reduced by the same proportion.
You might also need to use coarser grained parallelism, move the parfor further back. This might require manually partitioning the problem into sections that can be calculated independently, followed by some "stich-up" work to handle boundary conditions. https://www.mathworks.com/help/parallel-computing/discover-clusters-and-use-cluster-profiles.html
  3 Comments
Walter Roberson
Walter Roberson on 3 Jun 2020
The total number of threads available depends on which EC2 package you hired.
You can benchmark on number of threads to allocate per worker. It depends on what kind of operations you are doing and the size of your matrices. It is common that 4 threads per worker is productive enough to make it worth doing; the benefits of adding more workers tend to fall off after that, and it is not uncommon to see people say that after 6 threads per worker they could no longer justify the extra costs.
You can do some testing on your own system, by adjusting the number of threads in your basic system, in serial mode.
Tecon
Tecon on 5 Jun 2020
Thanks again. I followed your advice and I managed to speed up the code.
For curiosity, do you know why the multi-threading does not work on Ec2? I can't see any reason.
Does this imply that for the non-parallelized parts of my code, my Mac will be faster that any machine in Ec2?

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel Computing Fundamentals in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!