How to improve the perfomance of a program by dividing the executions into processes in language C?

1
int main(int argc, char ** argv)
{
    int i,j;
    uchar *image;
    camera c;
    point eye;
    point lookat;
    int samples; 
    int s;    
    float rcp_samples;// = 1.0 / (float)samples;
    //char fname[20];
    //ray * rays;
    //color cor;

    //srand ( time(NULL) );

    //---init virtual camera---
    //point eye = {10.0f,400.0f,1000.0f};
    //point eye = {0.0f,2.0f,-20.0f};
    eye.x = 0.0f;
    eye.y = 2.0f;
    eye.z = -20.0f;

    //point lookat = {0.5f,0.0f,0.0f};
    lookat.x = 0.5f;
    lookat.y = 0.0f;
    lookat.z = 0.0f;

    initCamera(&c,eye,lookat,WID,HEI);
    setupCamera(&c);

    //---malloc the image frame---
    image = (uchar *) malloc(c.view.width * c.view.height * 3 * sizeof(uchar));
    if(image == NULL)
    {
        fprintf(stderr,"Error. Cannot malloc image frame.\n");
        return 0;
    }

    //---just init the image frame with some data---
    initImage(&c,image);

    //---insert random N_SPHERES into the 'data' array
    //generateRandomSpheres();
    generateScene();

    //---insert random N_LIGHTS into the 'lights' array
    generateRandomLightSources();

    //---create a 1D array with primary rays coordinates
    //rays = generatePrimaryRays(&c);

    for(i=0; i<NRAN; i++) urand[i].x = (double)rand() / RAND_MAX - 0.5;
    for(i=0; i<NRAN; i++) urand[i].y = (double)rand() / RAND_MAX - 0.5;
    for(i=0; i<NRAN; i++) irand[i] = (int)(NRAN * ((double)rand() / RAND_MAX));

    //---ray tracing loop---

    samples = 8;
    s = 0;    
    rcp_samples = 1.0 / (float)samples;

    for(i = 0 ; i < c.view.width ; i++)
    {
        for(j = 0 ; j < c.view.height ; j++)
        {
            float r, g, b;
            r = g = b = 0.0;

            for(s=0; s<samples; s++) {
                ray rr = get_primary_ray(&c,i,j,s);    
                color col = trace(c,&rr,0);
                r += col.r;
                g += col.g;
                b += col.b;
            }

            r = r * rcp_samples;
            g = g * rcp_samples;
            b = b * rcp_samples;

            //ray rr = get_primary_ray(&c, i, j, samples); 
            //color clr = trace(c,&rr,0);

            //red green blue color components
            image[ 3* (i * c.view.height + j) + 0] = floatToIntColor(r);
            image[ 3* (i * c.view.height + j) + 1] = floatToIntColor(g);
            image[ 3* (i * c.view.height + j) + 2] = floatToIntColor(b);
        }
    }

    //printPrimaryRays(rays,c.view.width*c.view.height); //for testing only

    if(save_bmp("output_rt.bmp",&c,image) != 0)
    {
        fprintf(stderr,"Cannot write image 'output.bmp'.\n");
        return 0;
    }

I want to split the construct of each vector (image) between processes and after processing send via PIPE to a process.

    
asked by anonymous 22.04.2018 / 00:43

1 answer

0

It is difficult to divide a task into several processes, since it needs the data to be very independent (only for flags and some pointers to avoid competition problems), and that the processes run for a long time (usually until the user cancels ), to be considered viable. To improve the Pipeline process, which is already an automatic processor, a good tip is to use an updated compiler, such as Visual Studio, which is very optimized for pipeline.

For real-time image synthesis it is not possible to divide the process into two because it has a lot of shared data. To optimize this you can look for how to use graphical (Integrated or Dedicated) interrupts (routines), to perform heavy calculations, or to use a graphic library such as OpenGl or DirectX.

In the game I develop for example, I use a separate thread for:

  • Interface (not embedded in graphical screen, only for configurations).
  • Uploading Files (Images and Objects): It is destroyed when all files are loaded and changing the value of a global flag, indicating to the graphic part that it can already run what was loaded.
  • Physics Simulation and Graphic Processing (using OpenGL).
22.04.2018 / 02:17