Share CGAL geometry between processes in C ++

I am looking for the fastest way to send CGAL geometry between processes (C ++). Suppose we have 2 processes - A and B. Process A is the generation of geometry, and process B displays it. I want to knit them in the fastest way. Geometry is a type of polyhedron of CGALs.

I know that I can use shared memory, but then I have some problems:

  • When I want to copy geometry from process A to shared memory, I can use the stream polyhedron to / from the OFF format, but I'm not interested in this, because the conversion to this format is too slow for my purpose.
  • I can create shared memory and use “hosting new” to create my object in shared memory and overcome the overhead of streaming and converting, but then I have no control over the memory allocation using Polyhedron's internal functions. (For example, when adding a new vertex with Polyhedron_incremental_builder_3 I cannot specify where exactly it should be placed in memory - I can simply call B.add_vertex (Point (0, 0, 0)), and the memory allocation is processed internally inside this method)

Is there a way to create an object in a specific place in shared memory and ensure that it and its dynamic structures "live" in that memory?

Or maybe another quick way to share dynamic data (i.e. Halfedge structures) between two processes?

+4
source share
2 answers

I no longer control the allocation of memory by the internal polyhedron of a function.

You really have control.

The reference guide says:

The Polygon_2 class implements polygons. Polygon_2 is parameterized by the traits class and container class. The last can be any class that meets the requirements of the STL container. By default, the vector class is used.

In addition to using a new location for the polygon itself, you need a container that can be placed in shared memory. You can try using boost::interprocess::vector or flip your own container class.

If you use boost::interprocess::vector , you need to create a wrapper class for it, because unlike the STL container, its constructor requires a distribution object. Polygon_2 will not be able to build it correctly. Thus, you will have to get the distributed memory allocation object from some global variable. For instance:

 using namespace boost::interprocess; typedef allocator<int, managed_shared_memory::segment_manager> ShmemAllocator; ShmemAllocator some_global_shmem_allocator; template <typename T> class my_shared_memory_vector : vector<T, ShmemAllocator> { public: my_shared_memory_vector() : vector(some_global_shmem_allocator) {} }; 

Disclaimer: I actually did nothing myself. If your computer catches a flame as a result of this, and your house burns, do not hold me responsible. It would be wise to double-check (by looking at the GCAL source) that any Polygon_2 memory Polygon_2 is actually controlled by the container.

Edit : I misunderstood the question, he asks about polyhedrons, not about polygons. See Comment below.

+1
source

Of course, the most obvious task would be to use threads instead of processes. This would solve the whole problem without any effort.

In addition to this, with the exception of hacking your compiler's runtime library to replace its memory management, you can really override the “new operator” for the class and / or you can provide a global one. This will allow you to replace the “new” calls with your own memory allocation code. You can use the global flag that you set before calling CGAL and reset after that to tell the memory allocator which heap of memory you want to use (you will obviously have to use some form of heap management for shared memory).

Overriding a new operator will work, of course, only for “new” calls. Everything that is distributed, for example. via malloc () or any system call will not go through your code. You can try providing your own malloc () and free () calls (functions contained in object files, preferably functions from libraries) to see if this can work, but they probably have to deal with the operating system for memory management since you will lose library functions. It will definitely be dirty.

0
source

Source: https://habr.com/ru/post/1436988/


All Articles