情况是这样的,我为了试验在device函数中是否可以将输入输出都改为引用,是否会对结果有影响:
首先定义了结构体:
struct cuda_test_structure
{int a_int;
float b_float;
device float quotient(){return b_float/a_int;}
device cuda_test_structure& operator=(const cuda_test_structure& right_dev_input)
{a_int=right_dev_input.a_int;
b_float=right_dev_input.b_float;
return *this;
}
};
然后在device函数中,把两个cuda_test_structure序列相加:浮点+浮点,整数+整数
device cuda_test_structure device_function_add(cuda_test_structure left_input_dev,cuda_test_structure right_input_dev)
{ cuda_test_structure temp_structure;
temp_structure.a_int=left_input_dev.a_int+right_input_dev.a_int;
temp_structure.b_float=left_input_dev.b_float+right_input_dev.b_float;
return temp_structure;
}
在global里面,有cuda_test_structure a[100]和cuda_test_structure b[100]
相加正常的结果是
c[100],每一项都整数为200,浮点为100.0f
然后我把device函数改为引用做输入和输出之后,情况就不正常了,
device cuda_test_structure& device_function_add(cuda_test_structure& left_input_dev,cuda_test_structure & right_input_dev)
另外说明一下,当device和调用它的global在同一个源文件里面的时候,即使使用引用来做输入输出,也可以得出正常的结果;以上的不同比较结果,都是在device和global不在同一个源文件的情况下出现。