Function multithreading with Multithread keyword

In this example, I fill a matrix with the results of a parallel analysis. Each column contains the results from one analysis, with columns computed in parallel, and then assigned to the matrix in parallel. It is generally fairly easy to use this method with existing functions (like 'myFunc') that return waves. They must simply be made threadsafe.

function getResults(input)
  wave input // Some input wave.  
  make /free/wave/n=100 myWaves // Wave reference waves to temporarily hold the results of your analysis
  make /free/t/n=100 args=num2str(x+1) // List of parameters to search, in this case a simple list of numbers from 1 through 100.  
  multithread myWaves=myFunc(input,args[p]) // Fill the wave reference wave with references to the results of the analysis, which are computed in this line.  
  wave oneResult=myWaves[0] // The first result.  
  make /o/n=(numpnts(oneResult),numpnts(myWaves)) myResults // Make the results matrix to have dimensionality equal to the length of a results wave (rows) by the number of result waves (columns).  
  make /free/n=(numpnts(myWaves)) dummy // Only exists to force columns to be assigned to matrix 'myResults' in a multithreaded fashion.  
  multithread dummy=setColumn(myResults,myWaves[p],p) // Now set the columns of 'myResults' with the results computed and stored in the waves referred to be 'myResults'.  
end

// Your function to be executed in parallel, for example performing analysis for all of the different values contained in the original 'args' wave.  
threadsafe function /wave myFunc(w,args)
  wave w
  string args // An argument list specific to one function call, for example one parameter value in a parameter search.  

  // Add Some code that fills a wave 'result' with the results of the analysis.  It might be dependent on the input 'w' and the thread-dependent arguments in 'args'.  In this case I ignore 'w' for simplicity.  
  make /free/n=1000 result=gnoise(str2num(args))
  return result
end

// Reusable function to set the column of a matrix 'M' to the values in wave 'source'.  I use this in all implementations of this method.  
threadsafe function setColumn(M,source,col)
  wave M,source
  variable col
   
  imagetransform /D=source /G=(col) putCol M
end

Forum

Support

Gallery

Igor Pro 9

Learn More

Igor XOP Toolkit

Learn More

Igor NIDAQ Tools MX

Learn More