Ctree Specializer使用for循环索引进行计算,而不是实际的数组值

2024-09-28 03:17:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在实现一个简单的异或减缩器,但它无法返回适当的值。你知道吗

Python代码(输入):

class LazySpecializedFunctionSubclass(LazySpecializedFunction):

    subconfig_type = namedtuple('subconfig',['dtype','ndim','shape','size','flags'])

    def __init__(self, py_ast = None):
        py_ast = py_ast or get_ast(self.kernel)
        super(LazySlimmy, self).__init__(py_ast)

    # [... other code ...]

    def points(self, inpt):
        iter = np.nditer(input, flags=['c_index'])
        while not iter.finished:
            yield iter.index
            iter.iternext()


class XorReduction(LazySpecializedFunctionSubclass):
    def kernel(self, inpt):
        '''
            Calculates the cumulative XOR of elements in inpt, equivalent to
            Reduce with XOR
        '''
        result = 0
        for point in self.points(inpt):  # self.points is defined in LazySpecializedFunctionSubclass
            result = point ^ result      # notice how 'point' here is the actual element in self.points(inpt), not the index
        return result

C代码(输出):

// <file: module.c>
void kernel(long* inpt, long* output) {
    long result = 0;
    for (int point = 0; point < 2; point ++) {
         result = point ^ result;   // Notice how it's using the index, point, not inpt[point]. 
    };
    * output = result;
};

有没有办法解决这个问题?你知道吗


Tags: theinpyselfindexdefnotresult
1条回答
网友
1楼 · 发布于 2024-09-28 03:17:01

问题是,您以不同的方式使用point,在XorReduction内核方法中,您迭代数组中的值,但在生成的C代码中,您迭代数组的索引。这样就可以对索引进行C代码异或归约。 生成的C函数应该更像

// <file: module.c>
void kernel(long* inpt, long* output) {
    long result = 0;
    for (int point = 0; point < 2; point ++) {
         result = inpt[point] ^ result;  // you did not reference your input in the question
    };
    * output = result;
};

相关问题 更多 >

    热门问题