Open main menu

CDOT Wiki β

Top Solo

Revision as of 12:23, 7 February 2013 by Szahmad (talk | contribs) (Initial Profile:)

Top Solo

Overview

team name - classical DoTA reference


CImage is a free and open source cross platform Image Processing solution written in C++:


CImage

GitHub

Shayan's Blog



Assignment 1

I am going to try and profile and optimize the following function from the CImg Library. This function creates triangles on the screen and allows the user to make them clump up by clicking the mouse buttons.

Compilation with profiling on Mac OSX:


g++ -o CImg_demo CImg_demo.cpp -O2 -g -pg -I.. -Wall -W -ansi -pedantic -Dcimg_use_vt100 -I/usr/X11R6/include -lm -L/usr/X11R6/lib -lpthread -lX11



// Item : Filled Triangles

//-------------------------

void* item_filled_triangles() {

	

   // Size

   const int SIZE = 100;



  // Create a colored 640x480 background image which consists of different color shades.

  CImg<float> background(640,480,1,3);

  cimg_forXY(background,x,y) background.fillC(x,y,0,

                                              x*std::cos(6.0*y/background.height()) + y*std::sin(9.0*x/background.width()),

                                              x*std::sin(8.0*y/background.height()) - y*std::cos(11.0*x/background.width()),

                                              x*std::cos(13.0*y/background.height()) - y*std::sin(8.0*x/background.width()));

  background.normalize(0,180);



  // Init images and create display window.

  CImg<unsigned char> img0(background), img;

  unsigned char white[] = { 255, 255, 255 }, color[100][3];

  CImgDisplay disp(img0,"[#6] - Filled Triangles (Click to shrink)");



  // Define random properties (pos, size, colors, ..) for all triangles that will be displayed.

  float posx[SIZE], posy[SIZE], rayon[SIZE], angle[SIZE], veloc[SIZE], opacity[SIZE];

  int num = 1;

  std::srand((unsigned int)time(0));

 

  // I'm thinking of offloading these operations to the GPU

  for (int k = 0; k<SIZE; ++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());

  }



  // Start animation loop.

  while (!disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC()) {

    img = img0;



	/* Maybe offload this for loop to GPU? */

    // 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());

      }

    }



    // Display current animation framerate, and refresh display window.

    img.draw_text(5,5,"%u frames/s",white,0,0.5f,13,(unsigned int)disp.frames_per_second());

    img0.resize(disp.display(img).resize(false).wait(20));

    if (++num>SIZE) num = SIZE;



    // Allow the user to toggle fullscreen mode, by pressing CTRL+F.

    if (disp.is_keyCTRLLEFT() && disp.is_keyF()) disp.resize(640,480,false).toggle_fullscreen(false);

  }

  return 0;

}


Initial Profile (on OpenSUSE Linux at Seneca):


Flat profile:




Each sample counts as 0.01 seconds.

  %   cumulative   self              self     total           

 time   seconds   seconds    calls  us/call  us/call  name    

 82.26      2.55     2.55  4820368     0.53     0.53  frame_dummy

 12.26      2.93     0.38                             cimg_library::CImg<unsigned char>& cimg_library::CImg<unsigned char>::draw_line<unsigned char>(int, int, int, int, unsigned char const*, float, unsigned int, bool)

  2.58      3.01     0.08    10965     7.30     7.33  cimg_library::CImg<unsigned char>& cimg_library::CImg<unsigned char>::draw_image<float, float>(int, int, int, int, cimg_library::CImg<float> const&, cimg_library::CImg<float> const&, float, float)

  1.94      3.07     0.06                             cimg_library::CImg<unsigned char>& cimg_library::CImg<unsigned char>::_draw_triangle<unsigned char>(int, int, int, int, int, int, unsigned char const*, float, float)

  0.32      3.08     0.01   298115     0.03     0.03  cimg_library::CImg<unsigned char>::is_empty() const

  0.32      3.09     0.01                             item_3d_reflection()

  0.32      3.10     0.01                             cimg_library::CImg<float>::fillC(unsigned int, unsigned int, unsigned int, double, ...)

  0.00      3.10     0.00    14040     0.00     0.00  cimg_library::CImg<float>::assign(unsigned int, unsigned int, unsigned int, unsigned int)

  0.00      3.10     0.00    13270     0.00     0.00  cimg_library::CImg<float>::assign(float const*, unsigned int, unsigned int, unsigned int, unsigned int)

  0.00      3.10     0.00     9053     0.00     0.00  cimg_library::cimg::X11_attr()

  0.00      3.10     0.00     7130     0.00     0.00  cimg_library::CImg<float>::~CImg()

  0.00      3.10     0.00     2305     0.00     0.00  cimg_library::CImg<float>::move_to(cimg_library::CImg<float>&)

  0.00      3.10     0.00     1793     0.00     0.00  cimg_library::CImg<float>& cimg_library::CImg<float>::assign<float>(cimg_library::CImg<float> const&, bool)

  0.00      3.10     0.00     1024     0.00     0.00  cimg_library::CImg<unsigned long>::CImg(unsigned int, unsigned int, unsigned int, unsigned int)

  0.00      3.10     0.00     1000     0.00     0.53  cimg_library::CImgDisplay& cimg_library::CImgDisplay::render<unsigned char>(cimg_library::CImg<unsigned char> const&, bool)

  0.00      3.10     0.00     1000     0.00    80.40  cimg_library::CImg<unsigned char>& cimg_library::CImg<unsigned char>::_draw_text<unsigned char, unsigned char, float>(int, int, char const*, unsigned char const*, unsigned char const*, float, cimg_library::CImgList<float> const&)

  0.00      3.10     0.00     1000     0.00     0.00  cimg_library::CImg<unsigned char>::assign(unsigned int, unsigned int, unsigned int, unsigned int)

  0.00      3.10     0.00     1000     0.00    81.11  cimg_library::CImg<unsigned char>& cimg_library::CImg<unsigned char>::draw_text<unsigned char, unsigned char>(int, int, char const*, unsigned char const*, unsigned char const*, float, unsigned int, ...)

  0.00      3.10     0.00      769     0.00     0.70  cimg_library::CImg<float>::resize(int, int, int, int, int, unsigned int, float, float, float, float)

  0.00      3.10     0.00      769     0.00     0.00  cimg_library::CImg<float>::CImg(cimg_library::CImg<float> const&)

  0.00      3.10     0.00      769     0.00     0.70  cimg_library::CImg<float>::get_resize(int, int, int, int, int, unsigned int, float, float, float, float) const

  0.00      3.10     0.00      768     0.00     0.00  cimg_library::CImg<unsigned long>::fill(unsigned long)

  0.00      3.10     0.00      702     0.00     0.00  cimg_library::CImg<float>::draw_image(int, int, int, int, cimg_library::CImg<float> const&, float)

  0.00      3.10     0.00      513     0.00     0.00  cimg_library::CImg<float>::assign(unsigned int, unsigned int, unsigned int, unsigned int, float)

  0.00      3.10     0.00      512     0.00     0.00  cimg_library::CImgList<float>::insert(cimg_library::CImg<float> const&, unsigned int, bool)

  0.00      3.10     0.00      189     0.00     0.00  cimg_library::CImg<float>::CImg(unsigned int, unsigned int, unsigned int, unsigned int)

  0.00      3.10     0.00      189     0.00     0.00  cimg_library::CImg<float>::get_crop(int, int, int, int, int, int, int, int, bool) const

  0.00      3.10     0.00       67     0.00     0.00  cimg_library::CImg<float>::CImg(unsigned int, unsigned int, unsigned int, unsigned int, float)

  0.00      3.10     0.00        6     0.00     0.00  cimg_library::CImgList<float>::assign(unsigned int)

  0.00      3.10     0.00        3     0.00     0.35  cimg_library::CImgDisplay::_handle_events(_XEvent const*)

  0.00      3.10     0.00        2     0.00     0.00  cimg_library::CImgList<float>::assign(cimg_library::CImgList<float> const&, bool)

  0.00      3.10     0.00        2     0.00     0.00  cimg_library::CImgList<float>::~CImgList()

  0.00      3.10     0.00        1     0.00     0.00  _GLOBAL__sub_I__Z22item_blurring_gradientv

  0.00      3.10     0.00        1     0.00     0.00  cimg_library::CImgDisplay::_map_window()

  0.00      3.10     0.00        1     0.00     0.00  cimg_library::CImgDisplay::_assign(unsigned int, unsigned int, char const*, unsigned int, bool, bool)

  0.00      3.10     0.00        1     0.00     0.00  cimg_library::CImg<unsigned char>::~CImg()

  0.00      3.10     0.00        1     0.00   136.13  cimg_library::CImgList<float>::font(unsigned int, bool)

  0.00      3.10     0.00        1     0.00   136.13  cimg_library::CImgList<float>::_font(unsigned int const*, unsigned int, unsigned int, bool)

  0.00      3.10     0.00        1     0.00     0.00  cimg_library::CImgList<float>& cimg_library::CImgList<float>::insert<float>(cimg_library::CImgList<float> const&, unsigned int, bool)

  0.00      3.10     0.00        1     0.00     0.00  cimg_library::CImgList<float>::CImgList(cimg_library::CImgList<float> const&)

  0.00      3.10     0.00        1     0.00   136.13  cimg_library::CImgList<float>::get_crop_font() const

</big>

Assignment 2

=== Assignment 3 ===