我从python调用intel的数学内核库。根据linux的top命令,到目前为止,它只使用一个cpu,而不是全部12个cpu。如何使它使用全部12个cpu?在

我尝试过将三个环境变量(OMP_NUM_THREADS、MKL_NUM_THREADS、MKL_DOMAIN_NUM_THREADS)设置为12。在

我也试过mkl_set_num_线程。在

使用mkl的numpy可以在密集矩阵乘法中使用所有12个cpu。在

mkl是2016版。在

欢迎提出任何建议。在

谢谢。在

测试代码如下:from ctypes import *

import scipy.sparse as spsp

import numpy as np

import multiprocessing as mp

# Load the share library

mkl = cdll.LoadLibrary("libmkl_rt.so")

def get_csr_handle2(data, indices, indptr, shape):

a_pointer = data.ctypes.data_as(POINTER(c_float))

ja_pointer = indices.ctypes.data_as(POINTER(c_int))

ia_pointer = indptr.ctypes.data_as(POINTER(c_int))

return (a_pointer, ja_pointer, ia_pointer, shape)

def get_csr_handle(A,clear=False):

if clear == True:

A.indptr[:] = 0

A.indices[:] = 0

A.data[:] = 0

return get_csr_handle2(A.data, A.indices, A.indptr, A.shape)

def csr_t_dot_csr(A_handle, C_handle, nz=None):

# Calculate (A.T).dot(A) and put result into C

#

# This uses one-based indexing

#

# Both C.data and A.data must be in np.float32 type.

#

# Number of nonzero elements in C must be greater than

# or equal to the size of C.data

#

# size of C.indptr must be greater than or equal to

# 1 + (num rows of A).

#

# C_data = np.zeros((nz), dtype=np.single)

# C_indices = np.zeros((nz), dtype=np.int32)

# C_indptr = np.zeros((m+1),dtype=np.int32)

(a_pointer, ja_pointer, ia_pointer, A_shape) = A_handle

(c_pointer, jc_pointer, ic_pointer, C_shape) = C_handle

trans_pointer = byref(c_char('T'))

sort_pointer = byref(c_int(0))

(m, n) = A_shape

sort_pointer = byref(c_int(0))

m_pointer = byref(c_int(m)) # Number of rows of matrix A

n_pointer = byref(c_int(n)) # Number of columns of matrix A

k_pointer = byref(c_int(n)) # Number of columns of matrix B

# should be n when trans='T'

# Otherwise, I guess should be m

###

b_pointer = a_pointer

jb_pointer = ja_pointer

ib_pointer = ia_pointer

###

if nz == None:

nz = n*n #*n # m*m # Number of nonzero elements expected

# probably can use lower value for sparse

# matrices.

nzmax_pointer = byref(c_int(nz))

# length of arrays c and jc. (which are data and

# indices of csr_matrix). So this is the number of

# nonzero elements of matrix C

#

# This parameter is used only if request=0.

# The routine stops calculation if the number of

# elements in the result matrix C exceeds the

# specified value of nzmax.

info = c_int(-3)

info_pointer = byref(info)

request_pointer_list = [byref(c_int(0)), byref(c_int(1)), byref(c_int(2))]

return_list = []

for ii in [0]:

request_pointer = request_pointer_list[ii]

ret = mkl.mkl_scsrmultcsr(trans_pointer, request_pointer, sort_pointer,

m_pointer, n_pointer, k_pointer,

a_pointer, ja_pointer, ia_pointer,

b_pointer, jb_pointer, ib_pointer,

c_pointer, jc_pointer, ic_pointer,

nzmax_pointer, info_pointer)

info_val = info.value

return_list += [ (ret,info_val) ]

return return_list

def test():

num_cpu = 12

mkl.mkl_set_num_threads(byref(c_int(num_cpu))) # try to set number of mkl threads

print "mkl get max thread:", mkl.mkl_get_max_threads()

test_csr_t_dot_csr()

def test_csr_t_dot_csr():

AA = np.random.choice([0,1], size=(12,750000), replace=True, p=[0.99,0.01])

A_original = spsp.csr_matrix(AA)

A = A_original.astype(np.float32).tocsc()

A = spsp.csr_matrix( (A.data, A.indices, A.indptr) )

A.indptr += 1 # convert to 1-based indexing

A.indices += 1 # convert to 1-based indexing

A_ptrs = get_csr_handle(A)

C = spsp.csr_matrix( np.ones((12,12)), dtype=np.float32)

C_ptrs = get_csr_handle(C, clear=True)

print "=call mkl function="

while (True):

return_list = csr_t_dot_csr(A_ptrs, C_ptrs)

if __name__ == "__main__":

test()

python mkl使用_直接从python调用intel的mkl时,请使用多个线程相关推荐

  1. java php python 高并发_关于php如何调用Python快速发送高并发邮件的示例代码

    1 简介 在PHP中发送邮件,通常都是封装一个php的smtp邮件类来发送邮件.但是PHP底层的socket编程相对于python来说效率是非常低的.CleverCode同时写过用python写的爬虫 ...

  2. python递归函数例题_递归案例python

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 而对应的中文翻译 "递归" 却表达了两个意思:"递 ...

  3. python是什么编程教程-编程python是什么_谁的Python教程最好?

    谁的Python教程最好? 建议你可以看看这里的<Python基础教程>和<Python学习手册>应该适合你的. 希望对你有用. 记得采纳呀~ Python中的9个代码小实例! ...

  4. python从零开始进阶_从零开始学Python - 第020课:函数使用进阶

    在之前的课程中,我们讲到过关于函数的知识,我们还讲到过Python中常用的数据类型,这些类型的变量都可以作为函数的参数或返回值:通过前几节课的学习,我们又知道了写在类中的函数通常称之为方法,它代表了类 ...

  5. print python 如何加锁_深度解密Python单例模式

    相关代码已经上传至Github:Python_Development_Interview,大家可以收藏专题-Python的设计模式:解密+实战,之后会持续更新相关的设计模式. 1. 认识单例模式 认识 ...

  6. python len函数_知识清单Python必备的69个函数,你掌握了吗?

    本文纲要 Python 作为一门高级编程语言,为我们提供了许多方便易用的内置函数,节省了不少开发应用的时间.目前,Python 3.7 共有 69 个内置函数,一些是我们耳熟能详的函数,另一些却不是很 ...

  7. python qq模块_常用的Python模块

    目录 1.使用copy模块来复制 >>> class Animal: def _init_(self, species, number_of_legs, color): self.s ...

  8. python版本切换_怎么切换python版本

    展开全部 (1)分别安2113装 python-2.7.12.amd64.msi python-3.5.2-amd64.exe (python官网下载的) 顺序无所谓(为5261了看着4102方便,我 ...

  9. python求加速度_如何利用Python 为自然语言处理加速度

    自去年发布 Python 的指代消解包(coreference resolution package)之后,很多用户开始用它来构建许多应用程序,而这些应用与我们最初的对话应用完全不同. 利用 spaC ...

最新文章

  1. Oracle 系统表大全
  2. 理解spark中的job、stage、task
  3. 2018:WebRTC开发五大趋势
  4. 可以无限量服用的药材
  5. 操作系统 —— 内存管理
  6. Python裸奔也疯狂:批量爬取中国工程院院士信息
  7. python变量类型声明_python基础知识:变量的定义以及类型
  8. linux中设备配额 磁盘加密
  9. day15-CSS内容补充之overflow
  10. (戴尔灵越7572)笔记本外扩显示器以后,笔记本没有声音了的解决办法
  11. 一步一步跟着杨中科.net视频学c#基础(1)
  12. matlab实现A律13折线的编码和译码以及量化误差的计算
  13. 激光雷达还是摄影测量?两者数据融合如何提高点云质量
  14. Python必会的单元测试框架 —— unittest
  15. phpStrom连接MySQL数据库
  16. 无线蓝牙耳机手机端app开发_汪峰耗时1500天造了一款耳机,秒杀苹果AirPods!
  17. 在 iphone 手机浏览器无法下载(主要指 safari 和 chrome ) excel ,但是可以直接预览 excel
  18. 生物质的特性对其与煤共气化过程的影响
  19. 全国主要地级市按拼音排序json数据
  20. 有大红本之后如何落户太原

热门文章

  1. 解决在安装Autokroma Influx插件的 Premiere Pro for Mac m1电脑上,导入Flac/MKV/FLV文件后,拖入音轨无声音、导入失败等一系列问题?
  2. 最大熵模型原理和定义
  3. 动漫人物的耳朵怎么画?耳朵绘画技巧!
  4. 安装Ubuntu20.04双系统
  5. 赛元芯片读取24c02成败回忆
  6. 【愚公系列】2021年12月 Java教学课程 38-Lambda表达式
  7. Ubuntu 20.04 teamviewer 的安装及卸载
  8. ❤️爆肝万字!一文最全总结之Spring从入门到入土❤️(建议收藏)
  9. 网络通信协议,TCP和UDP 的区别
  10. ubuntu apt-get报E: 无法打开锁文件 /var/lib/dpkg/lock-frontend - open (2: 没有那个文件或目录)