cl :: vector vs std :: vector:不同的迭代器行为 [英] cl::vector vs std::vector: different iterator behaviour

查看:113
本文介绍了cl :: vector vs std :: vector:不同的迭代器行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT:按PlasmaHH建议的内存位置添加了调试输出。

Added debugging output with memory locations as suggested by PlasmaHH.

我不明白cl的不同行为: :在OpenCL的C ++绑定中的向量<>。考虑以下代码:

I don't understand the different behaviour of the cl::vector<> in the C++ bindings for OpenCL. Consider the following code:

标题 Top.hpp

class Top {
public:
    void setBool(bool b);
    bool getBool();
private:
    bool status;
};

Top.cpp

#include "Top.hpp"   

void Top::setBool(bool b) {
    std::cout << (void*)this << " setBool("<< b<< ")\n";
    status = b;
}

bool Top::getBool() {
    std::cout << (void*)this << " getBool() returns " << status << std::endl;
    return status;
}

使用上述:

#define __NO_STD_VECTOR

#include <iostream>
#include "CL/cl.hpp"
#include "Top.hpp"

using namespace cl;
using namespace std;

cl::vector<Top> js;

int main() {
    js.push_back(Top());
    js[0].setBool(true);
    cout << js[0].getBool() << endl;
    for(cl::vector<Top>::iterator i = js.begin(); i != js.end(); ++i) {
        (*i).setBool(false);
    }
    cout << js[0].getBool() << endl;
}

使用 __ NO_STD_VECTOR std :: vector被覆盖。输出为

With __NO_STD_VECTOR the std::vector is overridden. The output is

0x6021c0 setBool(1)
0x6021c0 getBool() returns 1
0x7fffae671d60 setBool(0)
0x6021c0 getBool() returns 1



因此迭代器返回的位置肯定是错误的。

So the location returned by the iterator is definitely wrong.

使用上面的 std :: vector (并将命名空间更改为 std 当然)但是给出了预期的输出:

Using the above with the std::vector (and changing the namespaces to std of course) however gives the expected output:

0x1be0010 setBool(1)
0x1be0010 getBool() returns 1
0x1be0010 setBool(0)
0x1be0010 getBool() returns 0

此迭代器的行为不同,但它应该替换std :: vector以避免兼容性问题。我缺少一些东西?

This iterator is acting differently, but it's supposed to replace the std::vector to avoid compatibility issues. Am I missing something?

推荐答案

不是一个专家在OpenCL的任何想象力,但我很感兴趣,转到 CUDA / OpenCL计算。我看来他们的*运算符返回一个副本而不是引用:

Not an expert at OpenCL by any stretch of the imagination, but I'm interested so I went over to CUDA/OpenCL Computing. I appears that their * operator returns a copy rather than a reference:

00706         T operator *()
00707         {
00708             return vec_[index_];
00709         }

而第一个非常量vector [参考:

Whereas the (first, non-const) vector [] operator returns a reference:

00621     T& operator[](int index)
00622     {
00623         return data_[index];
00624     }
00625   
00626     T operator[](int index) const
00627     {
00628         return data_[index];
00629     }

尝试直接迭代向量(使用旧的int i = 0 ,...),看看是否给出不同的结果。如果是这样,您可能想要输入一个错误报告(先检查),因为这是对于*运算符的意外行为。

Try iterating through the vector directly (using the old "int i = 0, ...") and see if that gives different results. If so, you might want to put in a bug report (check first) since this is unexpected behavior for the * operator.

这篇关于cl :: vector vs std :: vector:不同的迭代器行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆