简体   繁体   English

多线程程序中效率低下的Python Scipy矩阵加法

[英]Inefficient Python Scipy Matrix Addition in Multi-threaded Program

I want to process a huge text corpus, i have written two classes which a main class is calling.I have removed some fields and methods in order to be more readable. 我想处理一个庞大的文本语料库,我编写了两个主类正在调用的类。为了便于阅读,我删除了一些字段和方法。

import queue
import threading
class Vectorizer():

 def __init__(self, numThread=10):
    self.docQueue = Queue.Queue(self.queueMaxSize)
    self.words = {}
    self.dimension = 1024
    self.modelLock = threading.Lock()
    for i in range(numThread):
       t = WorkerThread(self)
       t.setDaemon(True)
       t.start()
 def vectorize(self, text):
    '''
    add the text to docQueue. here text is the content of a 
    document 
    '''
    self.docNum += 1
    docVector = createSparseVectorForThisDoc(self.docNum)
    self.docQueue.put((docVector, text))
 def initContextVector(self):
    #return sp.zeros(self.vectorizer.dimension, dtype=int)
    return csr_matrix((1, self.vectorizer.dimension), dtype=int8 )
class WorkerThread(threading.Thread):
 def __init(self, vectorizer):
       self.vectorizer = vectorizer
 def run(self):
                  #get a document/text from queue
        contextVector, text =  self.vectorizer.docQueue.get()

        #do the work
        tokens, textLength = self.prepareTokens(text)
        #extract tokens and their context in order to vectorize them.
        for token in tokens:
            self.vectorizer.modelLock.acquire()
            self.vectorizer.words[token] =    self.vectorizer.words.get(token, self.initContextVector()) + contextVector
            self.vectorizer.modelLock.release()


        self.vectorizer.docQueue.task_done()

I measured the time spent on each statement and the most time is used on the following code which is adding to sparse matrices which are in fact two sparse (not very sparse) vectors. 我测量了在每个语句上花费的时间,并在以下代码上花费了最多的时间,以下代码将添加到实际上是两个稀疏(不是非常稀疏)向量的稀疏矩阵中。

self.vectorizer.words[token] =    self.vectorizer.words.get(token, self.initContextVector()) + contextVector

when i check the cores of the server using htop i don't see a good cpu utilization the overall process is using 130% of a core but it should use about 1000% when i use 10 threads. 当我使用htop检查服务器的核心时,我看不到良好的cpu利用率,整个过程使用的是核心的130%,但是当我使用10个线程时,它应该使用大约1000%。 It never goes over 130% but the addition is a cpu intensive job isn't it? 它永远不会超过130%,但是增加的是CPU密集型工作,不是吗? Is there anythin that i've done wrong? 我做错了什么吗?

Since you're having each thread use the lock, every thread likely has to wait on the previous thread anyways. 由于您让每个线程都使用该锁,因此每个线程可能无论如何都要等待先前的线程。 You may consider breaking it out into processes rather than threads if you have the memory to handle it. 如果您有足够的内存来处理它,则可以考虑将其分为进程而不是线程。 At the moment I can't figure out what your locks are holding up behind because it's all in that one line that you highlighted separately. 目前,我无法弄清楚您的锁背后隐藏着什么,因为它们全都在您单独突出显示的那一行中。

Multiprocessing vs Threading Python 多处理与线程Python

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM