Changes between Initial Version and Version 1 of IgorThreading


Ignore:
Timestamp:
May 14, 2008 1:03:01 PM (14 years ago)
Author:
srkline
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • IgorThreading

    v1 v1  
     1'''Calculations can be speeded up in two ways:'''[[BR]] 
     2 
     3(1) Using compiled XOP code as supplied in SANSAnalysis.xop. True compiled c-code is much faster than Igor's language which is "semi-compiled".[[BR]] 
     4 
     5(2) Multithreading of the calculation to make use of multiple processors. 
     6 
     7 
     8Below is a snippet of code from Cylinder_PolyRadius.ipf showing how the normal function definition: 
     9 
     10Function Cyl_PolyRadius(cw,yw,xw) : !FitFunc 
     11 
     12can be augmented to dispatch the calculation to N processors. The normal function definition dispatches to a "helper" function (with a "_T" suffix) that works on each separate segment of the calculation. Compiler directives (#if) select between Igor code and XOP code, using the XOP code preferentially if it exists. Currently, not all functions are threaded, since only calculations that are "slow enough" will benefit from threading. There is timing overhead in the creating and dispatching of threads. For this example, the !SmearedCylinder_PolyRadius function was calculated using N=2 processors and 265 data points x 5 trials (summed times are reported), the timings are: 
     13 
     14No threading, Igor Function = '''46.87 s''' 
     15 
     16No threading, XOP = '''6.47 s''' 
     17 
     18Threading, XOP = '''3.95 s''' 
     19 
     20--'''So using an XOP gives a speedup of 7.2x, and using threading is another factor of 1.64x (N=2), for a total speedup of 11.9x ! ''' 
     21 
     22This example is a triple numerical integral, and benefits greatly. Pay no attention to the actual number of seconds the test takes on my old computer. Your mileage may vary.[[BR]] 
     23 
     24 
     25{{{ 
     26// 
     27//  Fit function that is actually a wrapper to dispatch the calculation to N threads 
     28// 
     29// nthreads is 1 or an even number, typically 2 
     30// it doesn't matter if npt is odd. In this case, fractional point numbers are passed 
     31// and the wave indexing works just fine - I tested this with test waves of 7 and 8 points 
     32// and the points "2.5" and "3.5" evaluate correctly as 2 and 3 
     33// 
     34Function Cyl_PolyRadius(cw,yw,xw) : FitFunc 
     35        Wave cw,yw,xw 
     36         
     37#if exists("Cyl_PolyRadiusX") 
     38 
     39        Variable npt=numpnts(yw) 
     40        Variable i,nthreads= ThreadProcessorCount 
     41        variable mt= ThreadGroupCreate(nthreads) 
     42 
     43//      Variable t1=StopMSTimer(-2) 
     44         
     45        for(i=0;i<nthreads;i+=1) 
     46        //      Print (i*npt/nthreads),((i+1)*npt/nthreads-1) 
     47                ThreadStart mt,i,Cyl_PolyRadius_T(cw,yw,xw,(i*npt/nthreads),((i+1)*npt/nthreads-1)) 
     48        endfor 
     49 
     50        do 
     51                variable tgs= ThreadGroupWait(mt,100) 
     52        while( tgs != 0 ) 
     53 
     54        variable dummy= ThreadGroupRelease(mt) 
     55         
     56//      Print "elapsed time = ",(StopMSTimer(-2) - t1)/1e6 
     57         
     58#else 
     59                yw = fCyl_PolyRadius(cw,xw)             //the Igor, non-XOP, non-threaded calculation 
     60#endif 
     61        return(0) 
     62End 
     63 
     64//// experimental threaded version... 
     65// don't try to thread the smeared calculation, it's good enough 
     66// to thread the unsmeared version 
     67 
     68//threaded version of the function 
     69ThreadSafe Function Cyl_PolyRadius_T(cw,yw,xw,p1,p2) 
     70        WAVE cw,yw,xw 
     71        Variable p1,p2 
     72         
     73#if exists("Cyl_PolyRadiusX")                   //this check is done in the calling function, simply hide from compiler 
     74        yw[p1,p2] = Cyl_PolyRadiusX(cw,xw) 
     75#else 
     76        yw[p1,p2] = fCyl_PolyRadius(cw,xw) 
     77#endif 
     78 
     79        return 0 
     80End 
     81}}}