Changes between Initial Version and Version 1 of IgorThreading

May 14, 2008 1:03:01 PM (14 years ago)



  • IgorThreading

    v1 v1  
     1'''Calculations can be speeded up in two ways:'''[[BR]] 
     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]] 
     5(2) Multithreading of the calculation to make use of multiple processors. 
     8Below is a snippet of code from Cylinder_PolyRadius.ipf showing how the normal function definition: 
     10Function Cyl_PolyRadius(cw,yw,xw) : !FitFunc 
     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: 
     14No threading, Igor Function = '''46.87 s''' 
     16No threading, XOP = '''6.47 s''' 
     18Threading, XOP = '''3.95 s''' 
     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 ! ''' 
     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]] 
     27//  Fit function that is actually a wrapper to dispatch the calculation to N threads 
     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 
     34Function Cyl_PolyRadius(cw,yw,xw) : FitFunc 
     35        Wave cw,yw,xw 
     37#if exists("Cyl_PolyRadiusX") 
     39        Variable npt=numpnts(yw) 
     40        Variable i,nthreads= ThreadProcessorCount 
     41        variable mt= ThreadGroupCreate(nthreads) 
     43//      Variable t1=StopMSTimer(-2) 
     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 
     50        do 
     51                variable tgs= ThreadGroupWait(mt,100) 
     52        while( tgs != 0 ) 
     54        variable dummy= ThreadGroupRelease(mt) 
     56//      Print "elapsed time = ",(StopMSTimer(-2) - t1)/1e6 
     59                yw = fCyl_PolyRadius(cw,xw)             //the Igor, non-XOP, non-threaded calculation 
     61        return(0) 
     64//// experimental threaded version... 
     65// don't try to thread the smeared calculation, it's good enough 
     66// to thread the unsmeared version 
     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 
     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) 
     76        yw[p1,p2] = fCyl_PolyRadius(cw,xw) 
     79        return 0