我memoryview在一个numpy数组上有一个,并希望使用以下命令将另一个numpy数组的内容复制到其中memoryview:
import numpy as np
cimport numpy as np
cdef double[:,::1] test = np.array([[0,1],[2,3]], dtype=np.double)
test[...] = np.array([[4,5],[6,7]], dtype=np.double)
Run Code Online (Sandbox Code Playgroud)
但为什么这是不可能的呢?它让我一直在说
类型错误:只有长度为 1 的数组可以转换为 Python 标量 Blockquote
如果我从 a 复制memoryview到 a memoryview,或者从numpy数组numpy复制到数组,它工作正常,但是如何从numpy数组复制到 a memoryview?
有没有一种方便的方法来使用新的 rcParams 更新已经存在的 matplotlib 图?背景是我想导出具有不同属性的图形(例如线宽、字体..)。有类似“redraw()”选项吗?谢谢你!
我正在使用 prange 迭代这样的列表:
from cython.parallel import prange, threadid
cdef int tid
cdef CythonElement tEl
cdef int a, b, c
# elList: python list of CythonElement instances is passed via function call
for n in prange(nElements, schedule='dynamic', nogil=True):
with gil:
tEl = elList[n]
tid = threadid()
a = tEl.a
b = tEl.b
c = tEl.c
print("thread {:} elnumber {:}".format(tid, tEl.elNumber))
#nothing is done here
with gil:
print("thread {:} elnumber {:}".format(tid, tEl.elNumber))
# some other computations based on a, b and c …Run Code Online (Sandbox Code Playgroud) 我想并行化迭代,其中评估了许多cython实例实例,结果存储在全局numpy数组中:
for cythonInstance in myCythonInstances:
success = cythonInstance.evaluate(someConstantGlobalVariables,) # very CPU intense
if success == False:
break
globalNumpyArray[instanceSpecificLocation] = cythonInstance.resultVector[:]
Run Code Online (Sandbox Code Playgroud)
实例评估的结果彼此独立.实例之间没有任何类型的交互,除了结果写入相同的全局数组,但是在固定的,预定的和独立的位置.如果一个评估失败,则必须停止迭代.
据我所知,2种可能性是可能的:1)使用多处理包2)制作cython函数并使用prange/openmp.
我根本没有并行化的经验.哪种解决方案更可取,还是有更好的替代方案?谢谢!
我有一个模板
template <int a, int b>
class MyTemplateClass
{
// ....
void computeSomething();
};
Run Code Online (Sandbox Code Playgroud)
我想部分专注于 b 的两个特殊情况:
template<int a>
void MyTemplateClass<a, 2> :: computeSomething()
{
// Special case for b=2 here
}
template<int a>
void MyTemplateClass<a, 3> :: computeSomething()
{
// Special case for b=3 here
}
Run Code Online (Sandbox Code Playgroud)
但是,据我所知,部分特化对于成员函数是无效的。我怎样才能实现我想要的?还有其他解决方案吗?谢谢!
我目前正在寻找一个 C++ 库(最好只有头文件),用于使用像爱因斯坦求和这样的符号的高阶张量收缩。
Fastor ( https://github.com/romeric/Fastor ) 似乎非常适合,并且由于 Eigen(我经常使用它)有一个张量模块,我做了一个小的比较,包括一个简单循环实现的基准:
#include <Fastor.h>
#include "unsupported/Eigen/CXX11/Tensor"
#include <ctime>
int main() {
clock_t tstart, tend;
{
using namespace Fastor;
Tensor<double,20,50,10> A;
Tensor<double,50,10,20,4> B;
Tensor<double, 4> C;
Tensor<double, 10, 10, 4> D;
A.ones();
B.ones();
C.zeros();
D.zeros();
enum {I,J,K,L,M,N};
tstart = clock();
C += einsum<Index<I,J,K>,Index<J,K,I,M>>(A,B);
tend = clock();
std::cout << "Fastor, C_M = A_IJK * B_JKIM:\t"<< tend - tstart << std::endl;
tstart = clock();
D += einsum<Index<I,J,K>,Index<J,M,I,N>>(A,B);
tend = clock();
std::cout << "Fastor, D_KMN = A_IJ …Run Code Online (Sandbox Code Playgroud) 这是转换类似内容的最高效方式
problem = [ [np.array([1,2,3]), np.array([4,5])],
[np.array([6,7,8]), np.array([9,10])]]
Run Code Online (Sandbox Code Playgroud)
成
desired = np.array([[1,2,3,4,5],
[6,7,8,9,10]])
Run Code Online (Sandbox Code Playgroud)
不幸的是,最终的列数和行数(以及子数组的长度)事先是未知的,因为从二进制文件中读取子数据,逐个记录.
考虑一个像这样的类
class Element{
public:
// approx. size of ~ 2000 Byte
BigStruct aLargeMember;
// some method, which does not depend on aLargeMember
void someMethod();
}
Run Code Online (Sandbox Code Playgroud)
现在假设 Element 的许多实例(例如,运行时期间有 100,000,000 个实例,同时存在大约 50,000 个实例)在运行时创建,并且通常仅someMethod()被调用,而不需要为 分配内存aLargeMember。(这个说明性示例源自非线性有限元代码,类Element实际上代表有限元。)
现在我的问题:由于aLargeMember并不经常需要,并且考虑到 的大量实例, 动态Element创建是否有利?aLargeMember例如
class Element{
public:
// approx. size of ~ 2000 Byte
std::unique_ptr<BigStruct> aLargeMember;
// only called when aLargeMember is needed
void initializeALargeMember{
aLargeMember = std::unique_ptr<BigStruct>( new BigStruct() );}
// some method, which does …Run Code Online (Sandbox Code Playgroud) c++ ×3
cython ×3
numpy ×3
python ×3
eigen ×1
heap-memory ×1
matplotlib ×1
memoryview ×1
templates ×1