python mkl使用_直接从python调用intel的mkl时,请使用多个线程
我从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时,请使用多个线程相关推荐
- java php python 高并发_关于php如何调用Python快速发送高并发邮件的示例代码
1 简介 在PHP中发送邮件,通常都是封装一个php的smtp邮件类来发送邮件.但是PHP底层的socket编程相对于python来说效率是非常低的.CleverCode同时写过用python写的爬虫 ...
- python递归函数例题_递归案例python
广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! 而对应的中文翻译 "递归" 却表达了两个意思:"递 ...
- python是什么编程教程-编程python是什么_谁的Python教程最好?
谁的Python教程最好? 建议你可以看看这里的<Python基础教程>和<Python学习手册>应该适合你的. 希望对你有用. 记得采纳呀~ Python中的9个代码小实例! ...
- python从零开始进阶_从零开始学Python - 第020课:函数使用进阶
在之前的课程中,我们讲到过关于函数的知识,我们还讲到过Python中常用的数据类型,这些类型的变量都可以作为函数的参数或返回值:通过前几节课的学习,我们又知道了写在类中的函数通常称之为方法,它代表了类 ...
- print python 如何加锁_深度解密Python单例模式
相关代码已经上传至Github:Python_Development_Interview,大家可以收藏专题-Python的设计模式:解密+实战,之后会持续更新相关的设计模式. 1. 认识单例模式 认识 ...
- python len函数_知识清单Python必备的69个函数,你掌握了吗?
本文纲要 Python 作为一门高级编程语言,为我们提供了许多方便易用的内置函数,节省了不少开发应用的时间.目前,Python 3.7 共有 69 个内置函数,一些是我们耳熟能详的函数,另一些却不是很 ...
- python qq模块_常用的Python模块
目录 1.使用copy模块来复制 >>> class Animal: def _init_(self, species, number_of_legs, color): self.s ...
- python版本切换_怎么切换python版本
展开全部 (1)分别安2113装 python-2.7.12.amd64.msi python-3.5.2-amd64.exe (python官网下载的) 顺序无所谓(为5261了看着4102方便,我 ...
- python求加速度_如何利用Python 为自然语言处理加速度
自去年发布 Python 的指代消解包(coreference resolution package)之后,很多用户开始用它来构建许多应用程序,而这些应用与我们最初的对话应用完全不同. 利用 spaC ...
最新文章
- 2021科大讯飞-车辆贷款违约预测赛事 Top1方案!
- 蜗蜗 Linux内核芬妮下,Linux内核的整体架构
- 华为诺亚加法网络再升级:精度提升,可以逼近任意函数
- (转)I,P,B帧和PTS,DTS的关系
- strncmp函数——比较特定长度的字符串
- Mysql 锁的机制
- 面经(一)——5G和物联网的关系
- JS回调函数(callback)
- Spring中的Advice类型及其应用
- JavaScript高级编程
- input file reader
- go并发编程实战 第二版 pdf_完美!啃透P9大佬这份完整版的《并发编程宝典》,成为Offer收割机...
- vue-项目完成的项目报告
- IDEA的依赖包报红问题
- 马蜂窝火车票系统服务化改造初探
- Windows 10 语言包下载 LCU补丁添加 DISM++及wsusscn3.cab下载(WindowsISO映像定制 )
- 在 Linux 中安装 Firefox
- 腾讯“鹅城”开建,斥资370亿;提前泄露小米新机博主被判赔偿100万;传字节跳动实习生删除GB以下所有机器学习模型 | EA周报...
- TortoiseGit commands(命令)(完整中文翻译)
- 卸载conda安装的环境