Hello,
I am trying to use thrust::transform to select a struct variable from a thrust::vectror and copy it to another thurst vector. But it is not copying.
Thanks,
Rafael Scatena
Those are mine results:
First 5 elements of h_triangles:
h_p1x[0] = 16.6072
h_p1x[1] = 16.6072
h_p1x[2] = 16.6072
h_p1x[3] = 16.6951
h_p1x[4] = 16.6951
h_p1x[5] = 16.783
h_p1x[6] = 16.6072
h_p1x[7] = 16.5633
h_p1x[8] = 16.6951
h_p1x[9] = 16.6951
h_p1x[10] = 16.783
h_p1x[11] = 16.783
h_p1x[12] = 16.8269
h_p1x[13] = 16.783
h_p1x[14] = 16.6072
h_p1x[15] = 16.5633
h_p1x[16] = 16.6951
h_p1x[17] = 16.6951
h_p1x[18] = 16.783
h_p1x[19] = 16.783
First 5 elements of d_p1x:
h_p1x[0] = 0
h_p1x[1] = 0
h_p1x[2] = 0
h_p1x[3] = 0
h_p1x[4] = 0
h_p1x[5] = 0
h_p1x[6] = 0
h_p1x[7] = 0
h_p1x[8] = 0
h_p1x[9] = 0
h_p1x[10] = 0
h_p1x[11] = 0
h_p1x[12] = 0
h_p1x[13] = 0
h_p1x[14] = 0
h_p1x[15] = 0
h_p1x[16] = 0
h_p1x[17] = 0
h_p1x[18] = 0
h_p1x[19] = 0
The struct:
struct Triangle{ double p1x; double p1y;
double p1z;double p2x; double p2y; double p2z;
double p3x; double p3y; double p3z;
int Material; int Body;
int64_t triangle_id;
__host__ __device__
Triangle() {}
__host__ __device__
Triangle(double p1x, double p1y, double p1z,double p2x, double p2y, double p2z,
double p3x, double p3y, double p3z,int material, int body, int64_t id)
: p1x(p1x), p1y(p1y), p1z(p1z),p2x(p2x), p2y(p2y), p2z(p2z),
p3x(p3x), p3y(p3y), p3z(p3z),Material(material), Body(body), triangle_id(id) {}
};
The lambda
struct ExtractP1x {__host__ __device__ double operator()(const Triangle& t) const {return t.p1x;}};
The code:
thrust::device_vector<Triangle> d_triangles(triangles.begin(), triangles.end());
// Create thrust device vectors to hold the extracted data
thrust::device_vector<double> d_p1x(h_numTriangles), d_p1y(h_numTriangles), d_p1z(h_numTriangles);
thrust::device_vector<double> d_p2x(h_numTriangles), d_p2y(h_numTriangles), d_p2z(h_numTriangles);
thrust::device_vector<double> d_p3x(h_numTriangles), d_p3y(h_numTriangles), d_p3z(h_numTriangles);
thrust::device_vector<int> d_Material(h_numTriangles), d_Body(h_numTriangles);
thrust::device_vector<int64_t> d_triangle_id(h_numTriangles);
thrust::host_vector<Triangle> h_triangles = d_triangles; // Copy data from device to host
// Print some elements from the host vector (say the first 5)
std::cout << "First 5 elements of h_triangles:" << std::endl;
for (int i = 0; i < 20 && i < h_numTriangles; i++) {std::cout << "h_p1x[" << i << "] = " << h_triangles[i].p1x << std::endl;}
// Copy data from host to device using thrust::transform
thrust::transform(d_triangles.begin(), d_triangles.end(), d_p1x.begin(), ExtractP1x());
// Print some elements from the host vector (say the first 5)
std::cout << "First 5 elements of d_p1x:" << std::endl;
for (int i = 0; i < 20 && i < h_numTriangles; i++) {
std::cout << "h_p1x[" << i << "] = " << h_p1x[i] << std::endl;
}