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

CC BY-SA 3.0
2017-05-17T17:51:15.463Z
2017-05-17T17:51:15.463Z
Olivier
<p>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...</p>

<pre><code>void ray_tracer::render()
{
    #define USE_THREADS
    #ifdef USE_THREADS
        for (int threadIndex = 0; threadIndex &lt; mMaxThreads; ++threadIndex) {
            mThreads[threadIndex] = std::thread(&amp;ray_tracer::put_pixel_with_thread, this, threadIndex);
        }
        for (int threadIndex = 0; threadIndex &lt; 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);
}
</code></pre>

<p>and here's the put_pixel_with_thread(int)...</p>

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

            mFrameBuffer[index] = 0;
            mFrameBuffer[index + 1] = 0;
            mFrameBuffer[index + 2] = 244;
        }
    }
}
</code></pre>

<p>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. </p>

<p>Thanks.</p>

CC BY-SA 3.0
2017-05-17T22:24:59.630Z
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
<raytracing><c++><multithreading>
How can I utilize all cores of CPU In my ray tracer with std::thread?
videogamechef
# How can I utilize all cores of CPU In my ray tracer with std::thread?

(Asked by: username_0 on 2017-05-17)

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.

**Tags:** raytracing, c++, multithreading

> **Comments:**
> **username_1**: 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!  

---

## 1 answers

### Answer by username_2 on 2017-05-17. Score: 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.


---

