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.