首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否可以使用多线程来加快脚本的速度?

是否可以使用多线程来加快脚本的速度?
EN

Stack Overflow用户
提问于 2021-12-13 12:21:23
回答 1查看 195关注 0票数 2

我必须用DigitalMicrographs的DM-script语言对数据进行CPU密集分析.我注意到,在处理过程中,只有一个CPU内核会中断。是否有一种方法可以通过DM脚本中的多线程实现更好的性能?请举一些简单的例子。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-13 13:24:19

多线程在DM脚本中当然是可能的,并且在这里的F1帮助中记录了它:

速度的提高是否能够实现取决于各种因素,最重要的是单个线程是否需要访问相同的资源(比如相同的数据,或者只有通过主线程- f.e才能获得的一些GMS资源。(用户界面)

此外,当您在图像表达式上使用命令时,许多数据处理在内部已经是多线程的.您可以通过在脚本语言中不需要循环,而是使用图像表达式的方式重新定义分析处理,从而实现更快的效果。

最后,多线程是引入bug和难以调试的意外行为的一种很好的方式。如果你在学习东西的时候遇到了这些事情,不要灰心。

尽管如此,下面的示例(至少在我的机器上)通过在多个并行后台线程上“分块”一些数据来演示速度的提高。

代码语言:javascript
复制
// Example showing the explicit use of multi-threading in DM scripting 

class CMultiThreadtest
{
    image data, keep
    number sx,sy,sz 
    number nChunks, chunksize, lastChunk, doneChunk 
    
    object SetData(object self, image img, number nChunks_) 
    { 
        if ( img.imagegetNumdimensions() != 3 ) throw( "only 3D data for testing please")
        img.ImageGetDimensionSizes(sx,sy,sz)
        nChunks = nChunks_
        if ( sz % nChunks != 0 ) Throw( "Z-size needs to be integer multiple of nChunks for this test.")
        chunksize = sz / nChunks
        
        data:=img
        keep = data
        
        return self
    }
    
    void CompareResult(object self){
        image dif := keep*2-data
        number ok = 0==sum(dif)
        Result("\n\t Result is " + (ok?"correct":"wrong"))
    }
    
    void RunOnData(object self){
        
        // For extra-caution of thread safety, the two lines below shoud be guarded with critical sections
        // but given the near-atomic nature of the call, this is omitted here.
        number chunkIndex = lastChunk
        lastChunk++
        
        image work := data.slice3(0,0,chunkIndex*chunksize, 0,sx,1, 1,sy,1, 2,chunksize,1)
        number startp = GetHighresTickCount()       
        for( number z=0;z<chunksize;z++)
            for( number y=0;y<sy;y++ )
                for( number x=0;x<sx;x++ ){
                        work[x,y,z] *= 2
        }
        number endp = GetHighresTickCount()     
        Result("\n\t\t Process (chunk "+chunkIndex+") done with " + sx*sy*chunksize + " steps in " + (endp-startp)/GetHighResTicksPerSecond())
        
        // For extra-caution of thread safety, the line below shoud be guarded with critical sections
        // but given the near-atomic nature of the call, this is omitted here.
        doneChunk++
    }
    
    void RunWithSubsets(object self, image src, number nChunks_, number inbackground){
        self.SetData(src, nChunks_)
        lastChunk = 0
        doneChunk = 0
        Result("\n.....\n Running with "+nChunks+" chunks of size "+chunksize+ " on " + (inbackground?" multiple background threads":" single main thread") +":")
        number startp = GetHighresTickCount()   
        for( number i=0; i<nChunks; i++){
            if ( inbackground )
                self.StartThread("runondata")
            else
                self.RunOnData()
        }   
        
        while( doneChunk != nChunks ){
            if ( ShiftDown() ){
                Throw("abort")
            doEvents()
            }
        }
        number endp = GetHighresTickCount()     
        Result("\n Total duration:" + (endp-startp)/GetHighResTicksPerSecond())
        self.CompareResult();
        Result("\n.....")
        
    }
};

void Test(){
    image img := RealImage("test cub",4,50,50,10)
    img = random()
    clearresults()
    object tester = Alloc(CMultiThreadtest)
    tester.RunWithSubsets(img, 1, 0)
    tester.RunWithSubsets(img, 1, 1)
    tester.RunWithSubsets(img, 5, 0)
    tester.RunWithSubsets(img, 5, 1)
}
test()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70334583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档