Created
June 7, 2012 19:42
-
-
Save dangets/2891118 to your computer and use it in GitHub Desktop.
CUDA Thrust Structure of Arrays reference as normal structure (non-working)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdlib> | |
#include <thrust/iterator/zip_iterator.h> | |
using std::size_t; | |
template<typename Vector> | |
struct Particles { | |
typedef typename Vector::value_type T; | |
Particles(size_t length_) : | |
length(length_), | |
pos_x(length), pos_y(length), pos_z(length) { } | |
size_t length; | |
Vector pos_x; | |
Vector pos_y; | |
Vector pos_z; | |
template <typename TOther> | |
Particles<Vector>& operator=(const TOther &other) { | |
pos_x = other.pos_x; | |
pos_y = other.pos_y; | |
pos_z = other.pos_z; | |
return *this; | |
} | |
template <typename ZipIter> | |
ZipIter begin() { | |
return thrust::make_zip_iterator(thrust::make_tuple( | |
pos_x.begin(), | |
pos_y.begin(), | |
pos_z.begin() | |
)); | |
} | |
template <typename ZipIter> | |
ZipIter end() { | |
return thrust::make_zip_iterator(thrust::make_tuple( | |
pos_x.end(), | |
pos_y.end(), | |
pos_z.end() | |
)); | |
} | |
template <typename ZipIter> | |
struct Ref { | |
T &pos_x; | |
T &pos_y; | |
T &pos_z; | |
Ref(ZipIter z) : | |
pos_x(thrust::get<0>(z)), | |
pos_y(thrust::get<1>(z)), | |
pos_z(thrust::get<2>(z)) | |
{ } | |
}; | |
}; | |
// debugging function to print elements | |
template <typename Particles> | |
void ParticlesPrint(Particles &p, std::ostream &out) { | |
for (size_t i=0; i<p.length; i++) { | |
out << p.pos_x[i] << " " | |
<< p.pos_y[i] << " " | |
<< p.pos_z[i] << std::endl; | |
} | |
} | |
struct ParticlesRandomizePositionFunctor { | |
ParticlesRandomizePositionFunctor(float xmin_, float xmax_, float ymin_, float ymax_, float zmin_, float zmax_) : | |
xmin(xmin_), xmax(xmax_), | |
ymin(ymin_), ymax(ymax_), | |
zmin(zmin_), zmax(zmax_) { } | |
const float xmin, xmax; | |
const float ymin, ymax; | |
const float zmin, zmax; | |
template <typename Tuple> | |
__host__ __device__ | |
void operator()(Tuple tup) const { | |
//Particles::ParticleRef p(tup); | |
//p.pos_x = xmax * (float)drand48(); | |
//p.pos_y = ymax * (float)drand48(); | |
//p.pos_z = zmax * (float)drand48(); | |
//thrust::get<0>(tup) = xmax * (float)drand48(); | |
//thrust::get<1>(tup) = ymax * (float)drand48(); | |
//thrust::get<2>(tup) = zmax * (float)drand48(); | |
} | |
}; | |
template<typename Particles> | |
void ParticlesRandomizePosition(Particles &p, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) | |
{ | |
thrust::for_each( | |
p.begin(), | |
p.end(), | |
ParticlesRandomizePositionFunctor(xmin, xmax, ymin, ymax, zmin, zmax) | |
); | |
} | |
int main(int argc, const char *argv[]) | |
{ | |
Particles< thrust::host_vector<float> > hp(16); | |
Particles< thrust::device_vector<float> > dp(16); | |
ParticlesRandomizePosition(hp, 0, 64, 0, 64, 0, 16); | |
ParticlesPrint(hp, std::cout); | |
std::cout << "-----------------" << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment