1

I have been working on a ray tracer and I was trying to use multiple threads to maximize the performance. I tried couple of approaches but there's no difference in performance. Here's my shot...

void ray_tracer::render()
{
    #define USE_THREADS
    #ifdef USE_THREADS
        for (int threadIndex = 0; threadIndex < mMaxThreads; ++threadIndex) {
            mThreads[threadIndex] = std::thread(&ray_tracer::put_pixel_with_thread, this, threadIndex);
        }
        for (int threadIndex = 0; threadIndex < mMaxThreads; ++threadIndex) {
            if (mThreads[threadIndex].joinable())
                mThreads[threadIndex].join();
        }
    #else
        put_pixel(); //uses single thread
    #endif

    stbi_write_bmp("result.bmp",mImageWidth,mImageHeight,3,mFrameBuffer);
}

and here's the put_pixel_with_thread(int)...

void ray_tracer::put_pixel_with_thread(int threadIndex)
{
    for (int row = 0; row <mImageHeight; ++row)
    {
        for (int col = int((threadIndex / mMaxThreads)*mImageWidth);
            col < int(((threadIndex + 1) / mMaxThreads)*mImageWidth);
            ++col)
        {
            int index = ((row * mImageWidth) + col) * 3;

            mFrameBuffer[index] = 0;
            mFrameBuffer[index + 1] = 0;
            mFrameBuffer[index + 2] = 244;
        }
    }
}

As you can see in put_pixel_with_threads(int) that I tried to split the row for each thread. I don't know what am i doing wrong. I am working on intel i5 6th gen which has 2 hyper-threaded cores. Please help.

Thanks.

Ankit singh kushwah
  • 791
  • 1
  • 5
  • 19
  • 2
    In 2015 Pixar gave a great talk at SIGGRAPH about [Multi-Threading for Visual Effects](http://s2015.siggraph.org/attendees/courses/sessions/multi-threading-visual-effects.html). I'm not sure if the video is available, but if you can find it, it's worth watching. They break down their tests to do multi-threading several different ways: Per frame, per tile, per effect pass, etc. Really interesting and useful info! – user1118321 May 17 '17 at 22:24

1 Answers1

7

This is not really a CG issue but you appear to have an integer division problem. Assuming mMaxThreads is an integer, (threadIndex / mMaxThreads) is always 0 and ((threadIndex + 1) / mMaxThreads) is 0 for all threads but the last one, where it is 1. So one thread is doing all the work.

Olivier
  • 1,575
  • 6
  • 14