MATLAB: How to take advantage of AWS Ec2 if the code cannot be parallelized

awsec2MATLABparallel computing

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?

Best Answer

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