Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread safety of embree device? #207

Closed
saedrna opened this issue Aug 31, 2018 · 7 comments
Closed

Thread safety of embree device? #207

saedrna opened this issue Aug 31, 2018 · 7 comments

Comments

@saedrna
Copy link

saedrna commented Aug 31, 2018

I have a embree device and a scene created and commited in the main thread, and I want to do rtcIntersect using the same device from different threads (using openmp).

Sometimes the program just hang forever or crashes randomly.

I would like to ask that if Embree device is thread safe or not? This will help me to isolate the problem.

Thanks,

Han

@svenwoop
Copy link
Contributor

svenwoop commented Sep 4, 2018

API calls to the same object are not thread safe in general, thus you cannot have two threads modify some geometry. However, the rtcIntersect/rtcOccluded calls can be invoked from multiple threads, thus what you do is correct.

Where is the crash appearing?

@saedrna
Copy link
Author

saedrna commented Sep 5, 2018

Thanks, I think I can do some unit test first to check my code

@saedrna
Copy link
Author

saedrna commented Sep 5, 2018

Here is my code.
On the program start, this is set:

    _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
    _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);

The function below is used parallely outside.

std::vector<bool> MeshEmbreeBVH::ray_intersected(const Vector3d &origin, const std::vector<Vector3d> &direction,
                                                 float dnear /*= 0.0f*/, float dfar /*= FLT_MAX*/,
                                                 int mask /*= 0xFFFFFFFF*/) {

    std::vector<bool> hits(direction.size());
    std::vector<RTCRay> rays(direction.size());
    for (int i = 0; i < direction.size(); ++i) {
        RTCRay ray;
        ray.org_x = origin(0);
        ray.org_y = origin(1);
        ray.org_z = origin(2);

        ray.dir_x = direction[i](0);
        ray.dir_y = direction[i](1);
        ray.dir_z = direction[i](2);

        ray.tnear = dnear;
        ray.tfar = dfar;
        ray.mask = -1;
        ray.time = 0.0f;
        ray.flags = 0;
        ray.id = i;

        rays[i] = ray;
    }

    RTCIntersectContext context;
    rtcInitIntersectContext(&context);
    context.flags = RTC_INTERSECT_CONTEXT_FLAG_NONE;
    rtcOccluded1M(scene_, &context, rays.data(), rays.size(), sizeof(RTCRay));
    CHECK_EMBREE_ERROR(device_);

    std::vector<bool> results(rays.size(), false);
    for (int i = 0; i < rays.size(); ++i) {
        if (rays[i].tfar == -std::numeric_limits<float>::infinity()) {
            results[i] = true;
        } else {
            results[i] = false;
        }
    }

    return results;
}

And the macro is

#define CHECK_EMBREE_ERROR(device)                                                                                     \
    {                                                                                                                  \
        auto error = rtcGetDeviceError(device);                                                                        \
        CHECK(error == RTC_ERROR_NONE) << "Error code " << error;                                                      \
    }

So the only thing that I am uncertain is rtcOccluded1M and rtcGetDeviceError, if all of them are thread safe, then probably it's other problems in my code.

@svenwoop
Copy link
Contributor

svenwoop commented Sep 5, 2018

Does the code work if you do not parallelize this function?

@saedrna
Copy link
Author

saedrna commented Sep 5, 2018

@svenwoop yes, it works if I comment out openmp

@svenwoop
Copy link
Contributor

svenwoop commented Sep 5, 2018

Could you please give a stack trace where it crashes exactly?

@saedrna
Copy link
Author

saedrna commented Sep 6, 2018

I think I have solved the problem. It is not related to embree. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants