3 __global__ void genere_snake_rectangle_4nodes_gpu(snake_node_gpu * d_snake, int dist_bords, int i_dim, int j_dim){
7 d_snake[n].posi = dist_bords ;
8 d_snake[n].posj = dist_bords ;
11 d_snake[n].posi = i_dim - dist_bords ;
12 d_snake[n].posj = dist_bords ;
15 d_snake[n].posi = i_dim - dist_bords ;
16 d_snake[n].posj = j_dim - dist_bords ;
19 d_snake[n].posi = dist_bords ;
20 d_snake[n].posj = j_dim - dist_bords ;
22 for (int i=0; i<4; i++)
24 d_snake[i].freeman_in = 0;
25 d_snake[i].freeman_out = 0;
26 d_snake[i].centre_i = 0;
27 d_snake[i].centre_j = 0;
28 d_snake[i].last_move = 0;
29 d_snake[i].nb_pixels = 0;
30 d_snake[i].code_segment = 0;
37 __global__ void genere_snake_rectangle_Nnodes_gpu(snake_node_gpu * d_snake, int dist_bords, int i_dim, int j_dim){
41 int i , h= i_dim-2*dist_bords, l= j_dim-2*dist_bords ;
42 int inch = h/(nb_node_seg+1), incl= l/(nb_node_seg+1) ;
43 if (threadIdx.x == 0){
46 d_snake[n].posi = dist_bords ;
47 d_snake[n].posj = dist_bords ;
49 /*entre sommet 0 et 1*/
51 while (i < nb_node_seg)
53 if ( (d_snake[n-1].posi + inch)-(i_dim - dist_bords) > limite )
54 d_snake[n].posi = d_snake[n-1].posi + inch ;
56 d_snake[n].posi = d_snake[n-1].posi + inch/2 ;
57 d_snake[n].posj = dist_bords ;
58 d_snake[n-1].nb_pixels = d_snake[n].posi - d_snake[n-1].posi ;
62 d_snake[n].posi = i_dim - dist_bords ;
63 d_snake[n].posj = dist_bords ;
64 d_snake[n-1].nb_pixels = d_snake[n].posi - d_snake[n-1].posi ;
68 while (i< nb_node_seg)
70 if ( (j_dim - dist_bords) - (d_snake[n-1].posj + incl) > limite )
71 d_snake[n].posj = d_snake[n-1].posj + incl ;
73 d_snake[n].posj = d_snake[n-1].posj + incl/2 ;
74 d_snake[n].posi = i_dim - dist_bords ;
75 d_snake[n-1].nb_pixels = d_snake[n].posj - d_snake[n-1].posj ;
79 d_snake[n].posi = i_dim - dist_bords ;
80 d_snake[n].posj = j_dim - dist_bords ;
81 d_snake[n-1].nb_pixels = d_snake[n].posj - d_snake[n-1].posj ;
85 while (i< nb_node_seg)
87 if ( (d_snake[n-1].posi - inch) - dist_bords > limite )
88 d_snake[n].posi = d_snake[n-1].posi - inch ;
90 d_snake[n].posi = d_snake[n-1].posi - inch/2 ;
91 d_snake[n].posj = j_dim - dist_bords ;
92 d_snake[n-1].nb_pixels = d_snake[n-1].posi - d_snake[n].posi ;
96 d_snake[n].posi = dist_bords ;
97 d_snake[n].posj = j_dim - dist_bords ;
98 d_snake[n-1].nb_pixels = d_snake[n-1].posi - d_snake[n].posi ;
102 while (i< nb_node_seg)
104 if ( (d_snake[n-1].posj - incl) - dist_bords > limite)
105 d_snake[n].posj = d_snake[n-1].posj - incl ;
107 d_snake[n].posj = d_snake[n-1].posj - incl/2 ;
108 d_snake[n].posi = dist_bords ;
109 d_snake[n-1].nb_pixels = d_snake[n-1].posj - d_snake[n].posj ;
112 d_snake[n-1].nb_pixels = d_snake[n-1].posj - d_snake[0].posj ;
115 d_snake[i].freeman_in = 0;
116 d_snake[i].freeman_out = 0;
117 d_snake[i].centre_i = 0;
118 d_snake[i].centre_j = 0;
119 d_snake[i].last_move = 1;
120 d_snake[i].code_segment = 0;
126 __global__ void calcul_contribs_segments_snake(snake_node_gpu * d_snake, int nb_nodes,
127 t_cumul_x * cumul_x, t_cumul_x2 * cumul_x2,
128 int l, uint2 * liste_pix, t_sum_x2 * gsombloc, int * d_table_freeman)
130 // indices des elements
131 int blockSize = blockDim.x ;
132 int tib = threadIdx.x ;
133 int nblocs_seg = gridDim.x / nb_nodes ;
134 int idx = blockDim.x*blockIdx.x + threadIdx.x ;
135 int segment = blockIdx.x / nblocs_seg ;
136 int tis = idx - segment*nblocs_seg*blockDim.x ;
138 //tab pour coordonnées pixels & contribs pixels de taille = (blockDim.x+offset(dec,dec2) )*(sizeof(t_sum_1+t_sum_x+t_sum_x2))
139 extern __shared__ t_sum_1 scumuls_1[] ; // blockDim varie selon la longueur des segments => taille smem dynamique
140 t_sum_x* scumuls_x = (t_sum_x*) &scumuls_1[CFI(blockDim.x)] ;
141 t_sum_x2* scumuls_x2 = (t_sum_x2*) &scumuls_x[CFI(blockDim.x)] ;
144 uint x1, y1, x2, y2 ;
149 //gestion du bouclage du snake
150 if (n2 >= nb_nodes) n2 = 0 ;
152 //affectation des differentes positions aux différents segments 'blocs de threads'
153 x1 = d_snake[n1].posj ;
154 y1 = d_snake[n1].posi ;
155 x2 = d_snake[n2].posj ;
156 y2 = d_snake[n2].posi ;
158 //params des deplacements
161 uint abs_dx = ABS(dx);
162 uint abs_dy = ABS(dy);
163 uint nb_pix = abs_dy>abs_dx?(abs_dy+1):(abs_dx+1); // alternative -> lecture ds liste_points[]
168 //calcul liste des pixels du segment (x1,y1)-(x2,y2)
169 if (dy > 0) incy=1; else incy=-1 ;
170 if (dx > 0) incx=1; else incx=-1 ;
173 if (abs_dy > abs_dx){
175 double k = (double)dx/dy ;
176 p.x = y1 + incy*tis ;
177 p.y = x1 + floor((double)incy*k*tis+0.5) ;
178 //enreg. coords. pixels en global mem pour freemans
180 if ((tis < 2)||(tis > nb_pix - 3)||(tis == nb_pix/2))
182 liste_pix[idx].x = p.x ;
183 liste_pix[idx].y = p.y ;
187 //1 thread par colonne
188 double k=(double)dy/dx ;
189 p.x = y1 + floor((double)(incx*k*tis)+0.5) ;
190 p.y = x1 + incx*tis ;
192 xsuiv = y1 + floor((double)(incx*k*(tis+1))+0.5) ;
193 xprec = y1 + floor((double)(incx*k*(tis-1))+0.5) ;
195 //enreg. coords. pixels en global mem pour freeman
197 //on peut calculer les freemans des segments
198 //sans stocker l'ensemble des valeurs des pixels
199 //juste avec les derivees aux extremites a calculer ici
201 if ((tis < 2)||(tis > nb_pix - 3)||(tis == nb_pix/2))
203 liste_pix[idx].x = p.x ;
204 liste_pix[idx].y = p.y ;
211 //calcul contribs individuelles des pixels
213 if ( (tis >0) && (tis < nb_pix-1)
214 && ( (abs_dy <= abs_dx)
215 && ( (xprec > p.x) || (xsuiv > p.x))
216 || (abs_dy > abs_dx) ) )
218 int pos = p.x * l + p.y ;
219 scumuls_1[ CFI(tib)] = 1+p.y;
220 scumuls_x[ CFI(tib)] = cumul_x[ pos ] ;
221 scumuls_x2[CFI(tib)] = cumul_x2[ pos ];
223 scumuls_1[ CFI(tib)] = 0;
224 scumuls_x[ CFI(tib)] = 0;
225 scumuls_x2[CFI(tib)] = 0;
229 //somme des contribs individuelles
230 // unroll des sommes partielles en smem
232 if (blockSize >= 512) {
234 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 256) ];
235 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 256) ];
236 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 256) ];
241 if (blockSize >= 256) {
243 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 128) ];
244 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 128) ];
245 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 128) ];
249 if (blockSize >= 128) {
251 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 64) ];
252 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 64) ];
253 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 64) ];
258 //32 threads <==> 1 warp
262 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 32) ];
263 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 32) ];
264 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 32) ];
267 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 16) ];
268 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 16) ];
269 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 16) ];
272 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 8) ];
273 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 8) ];
274 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 8) ];
276 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 4) ];
277 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 4) ];
278 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 4) ];
280 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 2) ];
281 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 2) ];
282 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 2) ];
284 scumuls_1[ CFI(tib)] += scumuls_1[ CFI(tib + 1) ];
285 scumuls_x[ CFI(tib)] += scumuls_x[ CFI(tib + 1) ];
286 scumuls_x2[CFI(tib)] += scumuls_x2[CFI(tib + 1) ];
289 // resultat sommes partielles en gmem
291 gsombloc[ blockIdx.x ] = (t_sum_x2) scumuls_1[0];
292 gsombloc[ blockIdx.x + gridDim.x ] = (t_sum_x2) scumuls_x[0];
293 gsombloc[ blockIdx.x + 2*gridDim.x ] = (t_sum_x2) scumuls_x2[0];
296 //calculs freemans, centre et code segment
297 //1 uint4 par segment
301 Di = 1 + liste_pix[idx+1].x - liste_pix[idx].x ;
302 Dj = 1 + liste_pix[idx+1].y - liste_pix[idx].y ;
303 d_snake[segment].freeman_out = d_table_freeman[3*Di + Dj] ;
305 if (dy > 0 ) d_snake[segment].code_segment = -1 ;
306 if (dy < 0 ) d_snake[segment].code_segment = 1 ;
307 if (dy == 0) d_snake[segment].code_segment = 0 ;
310 if (tis == nb_pix-1){
311 Di = 1 + liste_pix[idx].x - liste_pix[idx-1].x ;
312 Dj = 1 + liste_pix[idx].y - liste_pix[idx-1].y;
313 d_snake[segment].freeman_in = d_table_freeman[3*Di + Dj] ;
316 if (tis == (nb_pix/2)){
317 d_snake[segment].centre_i = liste_pix[idx].x ;
318 d_snake[segment].centre_j = liste_pix[idx].y ;
323 sommme des contribs par bloc -> contribs segment, pour le snake
325 execution sur : 1bloc / 1 thread par segment
328 __global__ void somsom_snake(t_sum_x2 * somblocs, int nb_nodes, unsigned int nb_bl_seg, snake_node_gpu * d_snake){
331 unsigned int seg = blockIdx.x ;
333 //un thread par segment
340 for (int b=0; b < nb_bl_seg ; b++){
341 sdata[0] += somblocs[seg*nb_bl_seg + b];
342 sdata[1] += somblocs[(seg + nb_nodes)*nb_bl_seg + b];
343 sdata[2] += somblocs[(seg + 2*nb_nodes)*nb_bl_seg + b];
348 d_snake[seg].sum_1 = sdata[0];
349 d_snake[seg].sum_x = sdata[1];
350 d_snake[seg].sum_x2 = sdata[2];
354 __device__ double codage_gl_gauss(uint64 stat_sum_1, uint64 stat_sum_x, uint64 stat_sum_x2,
355 uint64 n_dim, uint64 SUM_X, uint64 SUM_X2){
356 uint64 stat_sum_xe ; /* somme des xn region exterieure */
357 uint32 ne ; /* nombre de pixel region exterieure */
358 double sigi2, sige2; /* variance region interieure et exterieure */
360 /* variance des valeurs des niveaux de gris a l'interieur du snake */
362 ((double)stat_sum_x2/(double)stat_sum_1) -
363 ((double)stat_sum_x/(uint64)stat_sum_1)*((double)stat_sum_x/(uint64)stat_sum_1) ;
365 /* variance des valeurs des niveaux de gris a l'exterieur du snake */
366 ne = n_dim-stat_sum_1 ;
367 stat_sum_xe = SUM_X - stat_sum_x ;
369 ((double)SUM_X2-stat_sum_x2)/(double)ne -
370 ((double)stat_sum_xe/(uint64)ne)*((double)stat_sum_xe/(uint64)ne) ;
372 if ((sigi2 > 0)|(sige2 > 0))
373 return 0.5*((double)stat_sum_1*log(sigi2) + (double)ne*log(sige2)) ;
378 __global__ void calcul_stats_snake(snake_node_gpu * d_snake, int nnodes, int64 * d_stats_snake, double * vrais_min,
379 t_cumul_x * cumul_x, t_cumul_x2 * cumul_x2, int * TABLE_CODAGE, uint32 l
383 int id_nx, id_nprec, id_nprecprec ;
384 int code_noeud, code_segment, pos ;
385 __shared__ int64 s_stats_snake[3] ;
387 //init stats en shared mem
388 s_stats_snake[0] = 0 ;
389 s_stats_snake[1] = 0 ;
390 s_stats_snake[2] = 0 ;
393 for (id_nx = 0; id_nx < nnodes; id_nx++)
395 if (id_nx == 0) id_nprec = nnodes - 1;
396 else id_nprec = id_nx - 1;
397 if (id_nprec == 0) id_nprecprec = nnodes -1 ;
398 else id_nprecprec = id_nprec - 1 ;
399 /* gestion des segments partant du noeud */
400 /* vers le noeud suivant dans l'ordre trigo */
401 code_segment = d_snake[id_nprec].code_segment ;
402 if (code_segment > 0)
404 /* on somme les contributions */
405 s_stats_snake[0] += d_snake[id_nprec].sum_1 ;
406 s_stats_snake[1] += d_snake[id_nprec].sum_x ;
407 s_stats_snake[2] += d_snake[id_nprec].sum_x2 ;
409 else if (code_segment < 0)
411 /* on soustrait les contributions */
412 s_stats_snake[0] -= d_snake[id_nprec].sum_1 ;
413 s_stats_snake[1] -= d_snake[id_nprec].sum_x ;
414 s_stats_snake[2] -= d_snake[id_nprec].sum_x2 ;
416 // else (code_segment == 0), on ne fait rien
417 /* gestion des pixels connectant les segments */
418 /* pixel de depart du segment actuel np --> np->noeud_suiv */
419 /* freeman_out = np->freeman_out ; */
420 /* freeman_in = np->noeud_prec->freeman_in ; */
421 pos = d_snake[id_nprecprec].freeman_in*8 + d_snake[id_nprec].freeman_out ;
422 code_noeud = TABLE_CODAGE[pos] ;
423 pos = d_snake[id_nprec].posi*l + d_snake[id_nprec].posj ;
427 /* on somme les contributions */
428 s_stats_snake[0] += 1 + d_snake[id_nprec].posj ;
429 s_stats_snake[1] += cumul_x[pos] ;
430 s_stats_snake[2] += cumul_x2[pos] ;
432 else if (code_noeud < 0)
434 /* on soustrait les contributions */
435 s_stats_snake[0] -= 1 + d_snake[id_nprec].posj ;
436 s_stats_snake[1] -= cumul_x[pos] ;
437 s_stats_snake[2] -= cumul_x2[pos] ;
439 // else (code_pixel == 0), on ne fait rien
441 d_stats_snake[0] = s_stats_snake[0] ;
442 d_stats_snake[1] = s_stats_snake[1] ;
443 d_stats_snake[2] = s_stats_snake[2] ;
445 *vrais_min = codage_gl_gauss(s_stats_snake[0], s_stats_snake[1], s_stats_snake[2],
446 d_stats_snake[3], d_stats_snake[4], d_stats_snake[5]);