Tag: cython

如何通过cython将MPI通信器从python传递给C?

我试图通过cython将一个MPI_Comm通信器句柄作为参数包装一个C函数。 因此,我希望能够从python调用该函数,并将其传递给mpi4py.MPI.Comm对象。 我想知道的是,如何从mpi4py.MPI.Comm转换为MPI_Comm 。 为了演示,我使用了一个简单的“Hello World!” – 类型函数: helloworld.h : #ifndef HELLOWORLD #define HELLOWORLD #include void sayhello(MPI_Comm comm); #endif helloworld.c : #include #include “helloworld.h” void sayhello(MPI_Comm comm){ int size, rank; MPI_Comm_size(comm, &size); MPI_Comm_rank(comm, &rank); printf(“Hello, World! ” “I am process %d of %d.\n”, rank, size); } 我现在想从python中调用这个函数,如下所示: from_python.py : import mpi4py import helloworld_wrap helloworld_wrap.py_sayhello(mpi4py.MPI.COMM_WORLD) 意思是mpirun […]

Py_InitModule4()在嵌入式Python解释器上返回NULL(使用Cython)

我正在使用Cython编写C-Plugin。 最后,这应该将Python-Interpreter嵌入到Windows上的iTunes中。 为了使其工作,需要一个引导程序 。 它实现iTunes插件入口点,初始化或最终确定,或者任何需要,然后调用从Cython生成的代码。 我在Windows 7 64Bit和CPython 2.7上使用MinGW gcc 4.6.2 。 前言 Windows上的iTunes插件入口点称为iTunesPluginMain 。 据我所知,实现插件的共享库不会一直保持iTunes运行,因此一旦调用入口点就无法存储任何全局变量。 这就是为什么iTunes要求开发人员将void*指针存储到每次调用iTunesPluginMain传递的句柄。 iTunesPluginMain进行多次通知,例如初始化和清理。 引导程序看起来像这样: /** coding: utf-8 file: bootstrap.c Copyright (c) 2012 by Niklas Rosenstein This file implements the bootstrapper for loading the PyTunes plugin. **/ #include #include #include #include #include “pytunes.c” #define EXPORT(type) __declspec(dllexport) type extern void initpytunes(void); extern […]

在C代码中调用python函数

我在python中写了两个数字的小代码。 addition.py def add(a,b): c=a+b print “result is…”,c mainpgm.c ??? 如何调用C(mainpgm)程序中的addition.py/add函数。 我已经看到了一些链接,但我不清楚。 有关在c中调用该函数的任何建议。

Python运行时:重新编译和重用C库

我正在开发一种用于对用户定义函数进行数值分析的工具。 我们的想法是在Python中创建一个方便的UI,用户可以在其中输入C函数,然后按下按钮 – 并接收一些输出数据。 计算可能需要几分钟或几小时,因此不能接受仅限Numpy的性能。 我尝试了以下方法:基于Python的UI调用gcc,从用户函数编译dll,这是我在Cython包装中使用的基于C的核心算法所使用的。 它工作,但由于没有办法完全卸载python模块,我不能重新编译用户定义的函数,直到整个UI程序关闭并再次运行。 我现在看到的唯一方法是分离计算核心和UI进程,然后通过共享内存/消息传递使它们进行交互。 当用户想要更新他的function时,程序终止核心,重新编译DLL并再次启动核心。 在这种情况下你能建议任何常见做法吗? 谢谢!

Cython致命错误:Python.h没有这样的文件或目录

我一直在使用Cython将我的Python文件编译成C文件,然后使用MinGW从C文件创建可执行文件。 Cython工作正常,我可以在命令行输入cython test.pyx并获取一个C文件。 问题是当我尝试从C文件编译可执行文件时。 如果我输入gcc test.c我会收到以下错误: test.c:4:20: fatal error: Python.h: No such file or directory #include “Python.h” ^ compilation terminated. 我真的很感激一些帮助。 我正在运行Windows 7和python 3.5。

用C头文件编译Cython错误

所以我试图用Cython包装一些C代码。 我阅读了阅读应用Cython的教程来做这个( 1,2 ),但这些教程没有说明如何编译代码一旦你用Cython包装它,所以我有一个错误说它找不到我的C码。 首先,我的cython脚本(“calcRMSD.pyx”): import numpy as np cimport numpy as np cdef extern from “rsmd.h”: double rmsd(int n, double* x, double* y) #rest of the code ommited 我试图包装的C代码(“rmsd.h”): #include #include #include #include extern “C” { // svd from lapack void dgesvd_(char*,char*,int*,int*,double*,int*,double*,double*,int*,double*, int*,double*,int*,int*); } double rmsd(int n, double* x, double* y) { //code omitted […]

由于未知类型名称’uint64_t’,Cythonizing失败

这可能是一个新手问题。 我无法cython化一个简单的helloworld.pyx教程脚本,而完全相同的代码适用于linux: print(“hello world”) 这是setup.py脚本: from distutils.core import setup from Cython.Build import cythonize setup(ext_modules = cythonize(‘helloworld.pyx’)) 但是我在运行python setup.py build_ext –inplace后得到了这个: running build_ext building ‘helloworld’ extension creating build creating build/temp.macosx-10.7-x86_64-3.6 gcc -Wno-unused-result -Wsign-compare -Wunreachable-code -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/Users/haotian/anaconda3/include -arch x86_64 -I/Users/haotian/anaconda3/include -arch x86_64 -I/Users/haotian/anaconda3/include/python3.6m -c helloworld.c -o build/temp.macosx-10.7-x86_64-3.6/helloworld.o In file included from helloworld.c:16: […]

使用cython将double转换为char *

在Cython中,如何在不使用Python对象(例如bytes或str )作为中间体的情况下转换生成C double的C字符串( char * )表示? 事实上,我已经在C扩展名文件(.pyx)中定义了我的函数,如下所示: cdef void function(self, char* var1) nogil: cdef char* chaine =”” cdef double inter = 0.0 #Here there is some treatment which modifies the value of the local variable ‘inter’ so that it contains a double value different from 0 strcat(chaine , “(“) strcat(chaine , inter) strcat(chaine , “)**beta”) […]

如何将自定义C(它本身需要特殊的链接选项进行编译)与Cython链接?

所以我正在尝试创建一个使用我编写的.c文件的Cython模块。 该.c文件需要一个特殊的链接选项(要编译它,我需​​要gcc -o mycode mycode.c -lfftw3f )。 我可能只是在Cython中重写我的.c文件,但我想知道如何做到这一点。 我正在使用fftw3,并且在编译时,如果你想使用浮点版本,你需要在.c文件中使用-lfftw3f选项#include 。 我的setup.py如下所示: from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext sourcefiles = [‘mycode_caller.pyx’, ‘mycode.c’] ext_modules = [Extension(“myext”, sourcefiles, libraries=[‘fttw3f’])] setup( name = ‘My Extension’, cmdclass = {‘build_ext’: build_ext}, ext_modules = ext_modules ) 我创建了一个名为mycode.h的头文件,如下所示,并包含transform()函数的原型,该函数在mycode.c定义: #include #include #ifndef FOURIER_H_INCLUDED #define FOURIER_H_INCLUDED fftwf_complex** transform(float** in, […]

如何使用Cython强制使用长双打?

我提前为我对C的不良知识道歉:我使用Python编写代码,并使用标准C函数编写了一些Cython模块,以大大提高速度。 但是,我需要一个高于1e308的范围(是的,你读得对),这是我目前通过使用类型double complex和函数cexp和cabs 。 我试图使用函数cexpl和cabsl ,并声明我的变量是long double complex类型,但是在1e308之后我仍然遇到溢出。 这可能意味着我的编译器将长双精度转换为双精度数,是吗? 但根据维基百科, 使用GNU C编译器,x86处理器的长双精度是80位扩展精度,无论该类型使用的物理存储器是什么(可以是96位或128位)。[4] 我正在使用带有Linux Linux的64位系统(如果重要的话,还有Python 2.7.8)。 如何编写强制使用长双打的Cython模块? 对于我正在做的一些科学计算,我需要这么大的数字。 编辑:使用标志-m128bit-long-double进行编译会得到相同的结果。 如此处所示, 在x86-64编译器中,-m128bit-long-double是默认选择,因为它的ABI指定long double要在16字节边界上对齐。 所以这似乎没什么区别。 我还运行了以下程序来检查系统上的范围: #include #include int main() { printf(“Storage size for float : %d \n”, sizeof(float)); printf(“Minimum float positive value: %E\n”, FLT_MIN ); printf(“Maximum float positive value: %E\n”, FLT_MAX ); printf(“Precision value: %d\n”, FLT_DIG ); […]