필터 지우기
필터 지우기

Parallel Computing is taking longer than without parallel

조회 수: 4 (최근 30일)
Mechrod
Mechrod 2017년 11월 21일
답변: Walter Roberson 2017년 11월 21일
Hi, I'm testing the time consumed when using parallel computing, using the example (modified) below, from Matlab:
clc; clear;
tic
A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1);
options = optimoptions('ga','MaxGenerations',10,'PopulationSize',200,'UseParallel','Always');
[x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);
toc
clear;
tic
A = [1 1; -1 2; 2 1]; b = [2; 2; 3]; lb = zeros(2,1);
options = optimoptions('ga','MaxGenerations',10,'PopulationSize',200,'UseParallel','Never');
[x,fval,exitflag] = ga(@lincontest6,2,A,b,[],[],lb,[],[],options);
toc
I expected that the first part would run faster than the lower one, but the opposite is happening. Im having result like this:
Optimization terminated: maximum number of generations exceeded.
Elapsed time is 31.912986 seconds.
Optimization terminated: maximum number of generations exceeded.
Elapsed time is 28.565836 seconds.
Before running I start a parallel pool with 2 workers (my pc has 4 cores). I tested with 3 and 4 and the same happens (first part slower than the second part). Anyone can explain why this is happening?? Thanks!!

채택된 답변

Alan Weiss
Alan Weiss 2017년 11월 21일
Check out the first bullet item in this documentation. Your objective function, lincontest6, is very fast to compute, so parallel overhead dominates the calculation.
Alan Weiss
MATLAB mathematical toolbox documentation

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 11월 21일
It is common for working in parallel to take longer than working in serial, for two reasons:
  1. Communications overhead. Even not counting the time it takes to set up the pool (which is not fast), the program and data have to be transferred to the separate processes for each worker, and the results have to be transferred back to the client. If not enough work is being done for each parfor iteration, then the cost of the communications overwhelms any gain from parallel processing;
  2. When working with large enough matrices, for some of the more common computation patterns, MATLAB calls into high performance multi-threaded libraries that use all available cores. However, by default, when you run in parallel, each worker is only allocated one core and so although the same libraries are called all of the work ends up being done in serial... while still having had the overhead of setting up to call the libraries.

카테고리

Help CenterFile Exchange에서 Genetic Algorithm에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by