How to improve the performance of a program by dividing executions into C-linked processes?

Asked

Viewed 41 times

2

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 divide the construction of each vector (image) between processes and after processing send via PIPE to a process.

1 answer

0

It is difficult to divide a task into several processes, because 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 something viable. To improve the process of Pipeline, which is already 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 syntheses it is not possible to divide the process in two, because it has much shared data. To optimize this you can find out how to use the interruptions (routines) of the Graphics Card (Integrated or Dedicated), to perform heavy calculations, or use a graphical library such as Opengl or Directx.

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

  • Interface (not embedded on graphical screen, only for settings).
  • File Loading (Images and Objects): Which is dexterous when all files are loaded and changing the value of a global flag, indicating to the graphical part that it can already run what has been loaded.
  • Physics Simulation and Graphics Processing (using Opengl).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.