带有OpenMP的OpenGL始终是段错误 [英] OpenGL with OpenMP always segfault

查看:269
本文介绍了带有OpenMP的OpenGL始终是段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个循环,它将填充像素(GL_POINTS)的3D立方体,所以为了加快速度,我想我可以使用OpenMP,并在多核处理器中将其分开。 >

问题是,在循环中使用OpenMP的任何时候程序段错误,这里是循环的代码:

 在glBegin(GL_POINTS); 
#pragma omp parallel for
for(int a = 0; a< m_width * m_height; a ++)
{
uint8_t r,g,b;
r = m_data [a * m_channels];
g = m_data [a * m_channels + 1];
b = m_data [a * m_channels + 2];

glColor3ub(r,g,b);
glVertex3f(r / 255.0 - 0.5,g / 255.0 - 0.5,b / 255.0 - 0.5);
}
glEnd();你可以看到,代码只是从m_data数组中获得一些信息,然后调用glColor3ub和glVertex3f它,如果我运行这个代码没有#pragma的代码运行良好。

gdb显示程序segfaults到达glColor3ub,明确问题是与OpenGL的东西,也许该函数是不是线程安全的?我可以做些什么来纠正代码吗?解决方案

一个关键的部分OpenGL的(这不会给你什么性能明智的)。你可能会做的是使用顶点数组/缓冲区(反正它会更快),并在使用多个线程填充它们的数据,然后再在一个线程中绘制它。



如果一个线程设置当前的颜色并在绘制顶点之前取消调度,会发生什么?但是肯定会发生的是,在某些操作过程中,驱动程序被中断,内部数据被彻底搞砸了。

OpenGL绝对不是线程安全的。

p>

I have in my program a loop that will fill an 3D cube with pixels (GL_POINTS), so to speed up things a little I thought i could use OpenMP and separate this for loop in my multi-core processor.

The problem is that any time I use OpenMP in the loop the program segfaults, here is the code of the loop:

glBegin(GL_POINTS);
#pragma omp parallel for
for (int a = 0; a < m_width * m_height; a++)
{
    uint8_t r, g, b;
    r = m_data[a * m_channels];
    g = m_data[a * m_channels + 1];
    b = m_data[a * m_channels + 2];

    glColor3ub(r, g, b);
    glVertex3f(r / 255.0 - 0.5, g / 255.0 - 0.5, b / 255.0 - 0.5);
}
glEnd();

As you can see, the code just get some information from m_data array and then call glColor3ub and glVertex3f with it, if I run this code without the #pragma the code runs great.

The gdb shows me that the program segfaults when it reach the glColor3ub, making clear that the problem is something with openGL, maybe the function is not thread-safe? Can I make something to correct the code?

解决方案

Don't mess with a single OpenGL context and multithreading, or guard every use of OpenGL by a critical section (which won't buy you anything performance-wise). What you can probably do is use vertex arrays/buffers (which will be faster anyway) and fill their data using multiple threads before drawing it in a single thread.

What should happen if one thread sets the current color and gets unscheduled before it draws the vertex? But what definitely will happen is that the driver gets interrupted in the middle of some operation and its internal data gets totally messed up.

OpenGL is definitely not thread safe.

这篇关于带有OpenMP的OpenGL始终是段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆