In MATLAB, How can I set code or a specific task to be executed by specific one or more core of a cpu?
    3 views (last 30 days)
  
       Show older comments
    
    la
 on 30 Dec 2014
  
    
    
    
    
    Commented: Reinhold Erwin Suchowitzky Toba
 on 27 Jul 2023
            I am using Matlab to simulate my project. In design of this project, several software agents work together in parallel (each agent has its own tasks), these agents cooperate together and sometimes they exchange data and messages with each other. Now the question is:in MATLAB,how these agents can carry out their tasks in parallel? In MATLAB, How can I set code or a specific task to be executed by specific one or more core of a CPU? Thanks
0 Comments
Accepted Answer
  Sean de Wolski
      
      
 on 30 Dec 2014
        If you have the Parallel Computing Toolbox, spmd is the construct you will want. You can use labindex to determine the worker and labSend,labRecieve,labBroadcast to transmit data between workers.
spmd
  if labindex == 1
      % First worker, do whatever
  elseif labindex == 2
      % Second worker do whatever
  end
end
3 Comments
  Marco Feder
 on 11 May 2020
				@Sean de Wolski
I'm sorry for necroposting, but since the OP asked how "a specific task to be executed by specific one or more core of a CPU?", it doesn't seem to me the question has been fully answered in the case one wants to use more cores. (I've never used PCT, so probably I'm missing something). 
I mean: if one has 4 physical cores, and wants to use 2 cores to perform task1 and the other 2 to perform task2, how can this be done with the spmd construct above? It's not clear to me how this can be done just using "labindex".
More Answers (0)
See Also
Categories
				Find more on MATLAB Parallel Server in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!