- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现 implicit推荐模型,并且在代码运行时计算 ~11kk 用户超过 ~100k 项目的前 5 个建议时存在问题。
我能够通过 numpy 和一些 cython sparkles(在 jupyter notebook 中)部分解决问题。使用 numpy 排序的行仍然使用单核:
%%cython -f
# cython: language_level=3
# cython: boundscheck=False
# cython: wraparound=False
# cython: linetrace=True
# cython: binding=True
# distutils: define_macros=CYTHON_TRACE_NOGIL=1
from cython.parallel import parallel, prange
import numpy as np
from tqdm import tqdm
def test(users_items=np.random.rand(11402139//1000, 134751//100)
, int N=5, show_progress=True, int num_threads=1):
# Define User count and loops indexes
cdef int users_c = users_items.shape[0], u, i
# Predefine zero 2-D C-ordered array for recommendations
cdef int[:,::1] users_recs = np.zeros((users_c, N), dtype=np.intc)
for u in tqdm(range(users_c), total=users_c, disable=not show_progress):
# numpy .dot multiplication using multiple cores
scores = np.random.rand(134751//1000, 10).dot(np.random.rand(10))
# numpy partial sort
ids_partial = np.argpartition(scores, -N)[-N:]
ids_top = ids_partial[np.argsort(scores[ids_partial])]
# Fill predefined 2-D array
for i in range(N):
users_recs[u, i] = ids_top[i]
return np.asarray(users_recs)
# Working example
tmp = test()
我对其进行了分析 - np.argpartition 消耗了 60% 的函数时间并使用了 onde 核心。我试图让它并行,因为我有一个 80 核的服务器。因此,我对一部分用户(使用多核)执行 .dot 操作,并计划通过 numpy 排序结果(使用单核)并行填充空的预定义数组,但我遇到了问题标题中的错误:
%%cython -f
# cython: language_level=3
# cython: boundscheck=False
# cython: wraparound=False
# cython: linetrace=True
# cython: binding=True
# distutils: define_macros=CYTHON_TRACE_NOGIL=1
from cython.parallel import parallel, prange
import numpy as np
from tqdm import tqdm
from math import ceil
def test(int N=10, show_progress=True, int num_threads=1):
# Define User and Item count and loops indexes
cdef int users_c = 11402139//1000, items_c = 134751//100, u, i, u_b
# Predefine zero 2-D C-ordered array for recommendations
cdef int[:,::1] users_recs = np.zeros((users_c, N), dtype=np.intc)
# Define memoryview var
cdef float[:,::1] users_items_scores_mv
progress = tqdm(total=users_c, disable=not show_progress)
# For a batch of Users
for u_b in range(5):
# Use .dot operation which use multiple cores
users_items_scores = np.random.rand(num_threads, 10).dot(np.random.rand(134751//100, 10).T)
# Create memory view to 2-D array, which I'm trying to sort row wise
users_items_scores_mv = users_items_scores
# Here it starts, try to use numpy sorting in parallel
for u in prange(num_threads, nogil=True, num_threads=num_threads):
ids_partial = np.argpartition(users_items_scores_mv[u], items_c-N)[items_c-N:]
ids_top = ids_partial[np.argsort(users_items_scores_mv[u][ids_partial])]
# Fill predefined 2-D array
for i in range(N):
users_recs[u_b + u, i] = ids_top[i]
progress.update(num_threads)
progress.close()
return np.asarray(users_recs)
得到这个(full error):
Error compiling Cython file:
------------------------------------------------------------
...
# Create memory view to 2-D array,
# which I'm trying to sort row wise
users_items_scores_mv = users_items_scores
# Here it starts, try to use numpy sorting in parallel
for u in prange(num_threads, nogil=True, num_threads=num_threads):
ids_partial = np.argpartition(users_items_scores_mv[u], items_c-N)[items_c-N:]
^
------------------------------------------------------------
/datascc/enn/.cache/ipython/cython/_cython_magic_201b296cd5a34240b4c0c6ed3e58de7c.pyx:31:12: Assignment of Python object not allowed without gil
我阅读了有关内存 View 和分配分配的信息,但没有找到适用于我的情况的示例。
最佳答案
我最终得到了自定义 C++ 函数,它通过 openmp 与 nogil 并行填充 numpy 数组。它需要用 cython 重写 numpy 的 argpartition 部分排序。算法是这样的(3-4可以循环):
解决方案包括:
topnc.h - 自定义函数实现的头文件:
/* "Copyright [2019] <Tych0n>" [legal/copyright] */
#ifndef IMPLICIT_TOPNC_H_
#define IMPLICIT_TOPNC_H_
extern void fargsort_c(float A[], int n_row, int m_row, int m_cols, int ktop, int B[]);
#endif // IMPLICIT_TOPNC_H_
topnc.cpp - 函数体:
#include <vector>
#include <limits>
#include <algorithm>
#include <iostream>
#include "topnc.h"
struct target {int index; float value;};
bool targets_compare(target t_i, target t_j) { return (t_i.value > t_j.value); }
void fargsort_c ( float A[], int n_row, int m_row, int m_cols, int ktop, int B[] ) {
std::vector<target> targets;
for ( int j = 0; j < m_cols; j++ ) {
target c;
c.index = j;
c.value = A[(n_row*m_cols) + j];
targets.push_back(c);
}
std::partial_sort( targets.begin(), targets.begin() + ktop, targets.end(), targets_compare );
std::sort( targets.begin(), targets.begin() + ktop, targets_compare );
for ( int j = 0; j < ktop; j++ ) {
B[(m_row*ktop) + j] = targets[j].index;
}
}
ctools.pyx - 用法示例
# distutils: language = c++
# cython: language_level=3
# cython: boundscheck=False
# cython: wraparound=False
# cython: nonecheck=False
from cython.parallel import parallel, prange
import numpy as np
cimport numpy as np
cdef extern from "topnc.h":
cdef void fargsort_c ( float A[], int n_row, int m_row, int m_cols, int ktop, int B[] ) nogil
A = np.zeros((1000, 100), dtype=np.float32)
A[:] = np.random.rand(1000, 100).astype(np.float32)
cdef:
float[:,::1] A_mv = A
float* A_mv_p = &A_mv[0,0]
int[:,::1] B_mv = np.zeros((1000, 5), dtype=np.intc)
int* B_mv_p = &B_mv[0,0]
int i
for i in prange(1000, nogil=True, num_threads=10, schedule='dynamic'):
fargsort_c(A_mv_p, i, i, 100, 5, B_mv_p)
B = np.asarray(B_mv)
compile.py——编译文件;在终端中通过命令“python compile.py build_ext --inplace -f”运行它(这将生成文件 ctools.cpython-*.so,然后用于导入):
from os import path
import numpy
from setuptools import setup, Extension
from Cython.Distutils import build_ext
from Cython.Build import cythonize
ext_utils = Extension(
'ctools'
, sources=['ctools.pyx', 'topnc.cpp']
, include_dirs=[numpy.get_include()]
, extra_compile_args=['-std=c++0x', '-Os', '-fopenmp']
, extra_link_args=['-fopenmp']
, language='c++'
)
setup(
name='ctools',
setup_requires=[
'setuptools>=18.0'
, 'cython'
, 'numpy'
]
, cmdclass={'build_ext': build_ext}
, ext_modules=cythonize([ext_utils]),
)
关于python - 如何在 cython 的 nogil 循环下将 numpy.ndarray 分配给临时变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54112489/
我有一些想要尽快运行的 Cython 代码。我是否需要释放 GIL 才能执行此操作? 假设我的代码与此类似: import numpy as np # trivial definition just
我有一个函数,我试图返回一个数字和一个整数向量。我拥有的是 cdef func() nogil: cdef vector[int] vect cdef int a_number
我正在尝试使用Numba和Dask来加快慢速计算的速度,这与计算大量点的kernel density estimate相似。我的计划是在jit ed函数中编写计算量大的逻辑,然后使用dask在CPU内
在cython中用malloc和nogil分配内存安全吗?在 nogil 运行的多线程程序中传递指针是否安全? 最佳答案 GIL 到位是因为 CPython 内存管理不是 thread-safe .因
我想在将通过 pxd 导入的文件中的 nogil 函数中创建一个 C++ 字符串。如果我定义 string output = ""或 string output = string("blah"),这将
我有一个包含以下代码的 pyx 文件: cimport cython from libc.math cimport sqrt, abs from libc.stdio cimport printf f
我不太明白引用计数是如何在大/长 nogil 部分的内存 View 中完成的。让我们假设基本上我所有的代码都是 nogil,除了在深处创建一个 numpy-array-to-memoryview。返回
我在编译包含 SymPy 编写的整数的 Cython 代码时遇到问题。一个例子是: 3.814697265625e-7*x1t*(-300540195*xi1**20 + 2294446650*xi1
在 Python 中,我有一个列表列表作为输入: input = [[0,1,2],[0,3,4,5],[0,6]] 在现实中,子列表的数量是数万个。每个子列表的长度可能相差很大,从零或一个值到数百个
我假设如果我使用 nogil 指令在 Cython 中编写我的代码,那确实会绕过 gil,我可以使用 ThreadPoolExecutor 来使用多个核心。或者,更有可能的是,我在实现过程中搞砸了一些
我有一本字典, my_dict = {'a':[1,2,3], 'b':[4,5] , 'c':[7,1,2]) 我想在 Cython nogil 函数中使用这个字典。所以,我试图将其声明为 cdef
我正在尝试使用 Cython。认为我是一个完全的菜鸟。写入一个非常大的文件是我代码中的一个主要瓶颈,所以我想我会研究并行性,但我一直没能找到任何有用的关于并行性写入文件的东西。 是否可以使用 with
我想设计一个 cdef 类,它的方法可以并行运行,因此我需要将它们设置为 nogil .我知道我可以为 cdef 做到这一点方法,但由于某种原因我无法理解我不允许对 cpdef 做同样的事情方法。这尤
我正在尝试实现 implicit推荐模型,并且在代码运行时计算 ~11kk 用户超过 ~100k 项目的前 5 个建议时存在问题。 我能够通过 numpy 和一些 cython sparkles(在
所以我试图帮助另一个 SO 用户,但在这个过程中我无法创建一个 Cython 程序来在 NumPy 之外做一些简单的事情,这迫使我使用 GIL。所以这使得使用 OpenMP(多核)变得不可能。然后我发
我是一名优秀的程序员,十分优秀!