记录一下,cuda初学者,遇到了将一个float类型的指针强制转换成float2类型指针的情况,不太明白是怎么进行转换的,遂做了一个小实验
#include<cuda.h>
#include<cuda_runtime.h>
#include<iostream>
using std::cout;
using std::endl;
int main()
{
float* arr1 = new float[6]{ 0 };
arr1[0] = 0.1;
arr1[1] = 0.2;
arr1[2] = 0.3;
arr1[3] = 0.4;
arr1[4] = 0.5;
arr1[5] = 0.6;
float2* arr2 = (float2 *)arr1;
cout << "arr2[0].x = " << arr2[0].x << endl
<< "arr2[0].y = " << arr2[0].y << endl
<< "arr2[1].x = " << arr2[1].x << endl
<< "arr2[1].y = " << arr2[1].y << endl
<< "arr2[2].x = " << arr2[2].x << endl
<< "arr2[2].y = " << arr2[2].y << endl
<< "arr2[3].x = " << arr2[3].x << endl
<< "arr2[3].y = " << arr2[3].y << endl;
cout << endl << endl;
cout << "arr2->x = " << arr2->x << endl << "arr2->y = " << arr2->y << endl;
return 0;
}
不难看出,这种强制类型转换就是将float类型的数组从第零个元素开始,每两个相邻元素捏成一个float2类的对象,并且是先x后y,生成的float2类型的数组是原长度的一般