我正在写一个光线追踪器(学校项目)。我通过将渲染图像分成水平块(y 线)并让每个线程渲染自己的块来实现多线程。
运行 2 个线程(与无线程或仅 1 个线程相比),在运行时方面性能提升仅约 20-30%。
我很困惑为什么我没有看到更显着的速度提升。我没有使用任何互斥锁/锁。是否可能存在 FPU 单元的争用?
使用 C,用 gcc 和 pthreads 编译,在 osx 和 ubuntu(在 VM 中)、intel i5 cpu 上测试。此外,我所有的数学都是用双打完成的。我的主机操作系统是 osx,我想知道我是否在“平衡”省电模式下运行 - 与高性能(就像它可以在 Windows 上选择一样)。
不幸的是,我不能发布太多代码——这违反了学校的学术诚信政策。
这就是我启动线程的方式:
//set up threads
int chunk_size = y_lines/NUM_THREADS;
for (int i=0; i < NUM_THREADS; i++) {
// setup thread_config[i]
pthread_create(&threads[i], NULL, &thread_do_render, (void *) &thread_config[i]);
}
/* start threads and wait for them to finish */
for (int i=0; i < NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}