Changes

Jump to: navigation, search

Top Solo

2,723 bytes added, 23:27, 7 February 2013
Potential Parallelization Candidates
=== Potential Parallelization Candidates ===
Upon analyzing this function I discovered two possible areas where I could optimize the code using threads sent to the GPU. The first is a for loop which sets the attributes for 100 triangles in serial. This task can be done in parallel using 100 threads on the GPU.
<pre>
for (int k = 0; k<100; ++k) {
posx[k] = (float)(cimg::rand()*img0.width());
posy[k] = (float)(cimg::rand()*img0.height());
rayon[k] = (float)(10 + cimg::rand()*50);
 
angle[k] = (float)(cimg::rand()*360);
 
veloc[k] = (float)(cimg::rand()*20 - 10);
 
color[k][0] = (unsigned char)(cimg::rand()*255);
 
color[k][1] = (unsigned char)(cimg::rand()*255);
 
color[k][2] = (unsigned char)(cimg::rand()*255);
 
opacity[k] = (float)(0.3 + 1.5*cimg::rand());
 
}
 
</pre>
 
 
 
The second instance where this is possible is a bit tricky. It involves another serial for loop. The purpose of this loop is to draw each of the triangles on the screen and manipulate them later on. I am not 100 percent sure this can be done in parallel in practice but in theory it should be possible because the application is drawing out each triangle one by one.
 
<pre>
// Draw each triangle on the background image.
 
for (int k = 0; k<num; ++k) {
 
const int
 
x0 = (int)(posx[k] + rayon[k]*std::cos(angle[k]*cimg::PI/180)),
 
y0 = (int)(posy[k] + rayon[k]*std::sin(angle[k]*cimg::PI/180)),
 
x1 = (int)(posx[k] + rayon[k]*std::cos((angle[k] + 120)*cimg::PI/180)),
 
y1 = (int)(posy[k] + rayon[k]*std::sin((angle[k] + 120)*cimg::PI/180)),
 
x2 = (int)(posx[k] + rayon[k]*std::cos((angle[k] + 240)*cimg::PI/180)),
 
y2 = (int)(posy[k] + rayon[k]*std::sin((angle[k] + 240)*cimg::PI/180));
 
if (k%10) img.draw_triangle(x0,y0,x1,y1,x2,y2,color[k],opacity[k]);
 
else img.draw_triangle(x0,y0,x1,y1,x2,y2,img0,0,0,img0.width()-1,0,0,img.height()-1,opacity[k]);
 
img.draw_triangle(x0,y0,x1,y1,x2,y2,white,opacity[k],~0U);
 
 
// Make the triangles rotate, and check for mouse click event.
 
// (to make triangles collapse or join).
 
angle[k]+=veloc[k];
 
if (disp.mouse_x()>0 && disp.mouse_y()>0) {
 
float u = disp.mouse_x() - posx[k], v = disp.mouse_y() - posy[k];
 
if (disp.button()) { u = -u; v = -v; }
 
posx[k]-=0.03f*u, posy[k]-=0.03f*v;
 
if (posx[k]<0 || posx[k]>=img.width()) posx[k] = (float)(cimg::rand()*img.width());
 
if (posy[k]<0 || posy[k]>=img.height()) posy[k] = (float)(cimg::rand()*img.height());
 
}
 
}
 
 
</pre>
=== Amdahls Law Calculations ===
1
edit

Navigation menu