sábado, 26 de septiembre de 2009

Empuje

Thrust es el nuevo descubrimiento. Para los que desarrollamos en C++, CUDA se hace un poco cuesta arriba, demasiado parecido a C...

Esta biblioteca implementa algoritmos similares a la STL, con una sintaxis casi igual, pero con la ventaja de que se ejecutan en paralelo en nuestra GPU. Se tiene total control sobre la memoria del ordenador y la GPU, y el código es lo más legible que he visto para computación en gráficas.

Un ejemplo sencillo:
#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/generate.h>
#include <thrust/sort.h>
#include <thrust/copy.h>
#include <cstdlib>

int main(void)
{
// generate 16M random numbers on the host
thrust::host_vector<int> h_vec(1 << 24);
thrust::generate(h_vec.begin(), h_vec.end(), rand);

// transfer data to the device
thrust::device_vector<int> d_vec = h_vec;

// sort data on the device (145M keys per second on a GTX 280)
thrust::sort(d_vec.begin(), d_vec.end());

// transfer data back to host
thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());

return 0;
}

No hay comentarios: