首页 > 解决方案 > 在pyopencl中编写可以转换类型的函数

问题描述

我正在尝试为 pyopencl 中的 COMPLEX (cfloat_t) 类型编写一些附加函数。我几乎没有使用 OpenCL 的经验,并且有点被语法所困扰。我想编写一些可以接受 REAL 输入(在本例中为浮点数)并将它们转换为 COMPLEX 类型的基本函数。例如,欧拉公式 e^ix = cos(x) + isin(x) 将 x 作为 REAL 输入

我以为这会像

#include <pyopencl-complex.h>    
#define REAL      float
#define COMPLEX   cfloat_t
COMPLEX c_exp_i(REAL x)        {return (COMPLEX)(cos(x), sin(x));}

但是当我尝试运行它时,我看到一条错误消息

error: cast to union type from type 'float' not present in union

标签: pythonopenclpyopencl

解决方案


阅读 pyopencl-complex.h 后,我意识到有一个 cfloat_new() 函数可以解决所有问题

COMPLEX c_exp_i(REAL x)     {return cfloat_new(cos(x), sin(x));}

推荐阅读