Skip to content

numpy calculation takes up much cpu sys time #14547

Closed

Description

@Micy1919

I used numpy to write a program to calcuate the cosine of matrix, and built is as a flask service. I found the cpu sys time cos too much when requesting services continuously. who can tell me what is the problem?
np.show_config()
blas_mkl_info:
NOT AVAILABLE
blis_info:
NOT AVAILABLE
openblas_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
blas_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]
lapack_opt_info:
libraries = ['openblas', 'openblas']
library_dirs = ['/usr/local/lib']
language = c
define_macros = [('HAVE_CBLAS', None)]

Reproducing code example:

class ClusterStreaming:

    ...
    def cosine_distance(self, matrix1, matrix2):
        matrix1_matrix2 = np.dot(matrix1, matrix2.transpose())
        matrix1_norm = np.sqrt(np.multiply(matrix1, matrix1).sum(axis=1))
        matrix1_norm = matrix1_norm[:, np.newaxis]
        matrix2_norm = np.sqrt(np.multiply(matrix2, matrix2).sum(axis=1))
        matrix2_norm = matrix2_norm[:, np.newaxis]
        cosine_distance = np.divide(matrix1_matrix2, np.dot(matrix1_norm, matrix2_norm.transpose()))
        return cosine_distance

    def find_most_sim_cluster(self, news_vec, matrix, topK):
        cos_distances = list(self.cosine_distance(np.array([news_vec]), matrix)[0])
        max_topk_index = map(cos_distances.index, heapq.nlargest(topK, cos_distances))
        max_topk_similar = []
        for index in max_topk_index:
            max_topk_similar.append(1 - cos_distances[index])
        return max_topk_index, max_topk_similar


    def flask_api(self, storage):
        ...
        self.cosine_distance()
        self.find_most_sim_cluster()

        return resp



cluster_handler = ClusterStreaming()
cluster_handler.flask_api(..)

Error message:

image

Activity

  1. mattip commented on Sep 19, 2019

    @mattip
    Member

    This kind of question is not really an issue with NumPy, you may find a forum like StackOverflow more amenable to information about profiling or running code.

    With that, you should have answers to questions like: how many cores are available, are you using a NumPy binary with any BLAS backend, what else is running on the target, what does profiling show?

  2. seberg commented on Sep 21, 2019

    @seberg
    Member

    You may want to google for old issues, and things such as threadpoolctl and OMP_NUM_THREADS environment variable. There are open issues about this, so closing this one.

  3. Micy1919 commented on Sep 23, 2019

    @Micy1919
    Author

    You may want to google for old issues, and things such as threadpoolctl and OMP_NUM_THREADS environment variable. There are open issues about this, so closing this one.

    got it, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions