# How can I utilize all cores of CPU In my ray tracer with std::thread?

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.

• In 2015 Pixar gave a great talk at SIGGRAPH about Multi-Threading for Visual Effects. 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 Answer

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.