opencl
Exemple de noyau de générateur de nombres pseudo-aléatoires
Recherche…
Paramètres
Paramètre | Détails |
---|---|
__global unsigned int * rnd_buffer | unsigned int est normalisé par le standard OpenCL comme étant 32 bits |
* | __global signifie la mémoire principale du périphérique pour l'accès en lecture / écriture |
* | rnd_buffer est juste un nom dans la portée de "programme opencl" (pas hôte mais périphérique) |
Utilisation de la fonction de hachage d'entiers de Thomas Wang
Fonction auxiliaire qui prend une graine et évalue:
uint wang_hash(uint seed)
{
seed = (seed ^ 61) ^ (seed >> 16);
seed *= 9;
seed = seed ^ (seed >> 4);
seed *= 0x27d4eb2d;
seed = seed ^ (seed >> 15);
return seed;
}
une autre fonction auxiliaire qui l'utilise pour initialiser un emplacement de tampon indiqué par "id":
void wang_rnd_0(__global unsigned int * rnd_buffer,int id)
{
uint maxint=0;
maxint--;
uint rndint=wang_hash(id);
rnd_buffer[id]=rndint;
}
et un autre faisant une sortie de flotteur supplémentaire entre 0 et 1
float wang_rnd(__global unsigned int * rnd_buffer,int id)
{
uint maxint=0;
maxint--; // not ok but works
uint rndint=wang_hash(rnd_buffer[id]);
rnd_buffer[id]=rndint;
return ((float)rndint)/(float)maxint;
}
Noyau d'initialisation:
__kernel void rnd_init(__global unsigned int * rnd_buffer)
{
int id=get_global_id(0);
wang_rnd_0(rnd_buffer,id); // each (id) thread has its own random seed now
}
Noyau d'itération unique:
__kernel void rnd_1(__global unsigned int * rnd_buffer)
{
int id=get_global_id(0);
// can use this to populate a buffer with random numbers
// concurrently on all cores of a gpu
float thread_private_random_number=wang_rnd(rnd_buffer,id);
}
Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow