# How to create vtkPartitionedDataSet in parallel using SMP?

**URL:** <https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085>\
**Category:** Development\
**Created:** [August 4, 2022, 9:53pm UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085 "2022-08-04T21:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cameron](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cameron/32/4063_2.png) [@Cameron](https://discourse.vtk.org/u/Cameron)\
**Post date:** [August 4, 2022, 9:53pm UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085/1 "2022-08-04T21:53:45Z")

</div>

I need to create a number of vtkPartitionedDataSet object which is completely independent of each other.  
Then, they will be added to vtkPartitionedDataSetCollection. How do I parallelize this process? Are there any out of box class I can use?  
I was looking into `vtkThreadedCompositeDataPipeline` but it seems I still need to implement ThreadedExecute() method?

Thanks,  
Cam

---

<div class="post-metadata">

**Author:** ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)\
**Post date:** [August 5, 2022, 1:19pm UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085/2 "2022-08-05T13:19:16Z")

</div>

Use SMPTools:  
[https://www.kitware.com/simple-parallel-computing-with-vtksmptools/](https://www.kitware.com/simple-parallel-computing-with-vtksmptools/)

---

<div class="post-metadata">

**Author:** ![Cameron](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cameron/32/4063_2.png) [@Cameron](https://discourse.vtk.org/u/Cameron)\
**Post date:** [August 14, 2022, 6:28am UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085/3 "2022-08-14T06:28:05Z")

</div>

Hi Dan,

I am able to use vtkSMPTools to parallelize my computation in some scenarios. But I am stuck at using it for pipeline. E.g.

```auto
class Slice
{
public:
    vtkAppendPolyData* AppendFilter;
    vtkDataSet* Input;
    double Center[3];
    double N0[3];
    double Spacing;
    int SortType;

    Slice(vtkAppendPolyData* appendFilter, vtkDataSet* input, const double* center,
          const double* n0, double spacing, int sortType) : 
        AppendFilter(appendFilter),
        Input(input),
        Spacing(spacing),
        SortType(sortType)
    {
        Center[0] = center[0];
        Center[1] = center[1];
        Center[2] = center[2];
        N0[0] = n0[0];
        N0[1] = n0[1];
        N0[2] = n0[2];
    } 

    void operator()(vtkIdType id, vtkIdType endId)
    {
        for (; id < endId; id++)
        {
            vtkNew<vtkPlane> plane;
            double origin[3] {this->Center[0] + id * this->N0[0] * this->Spacing,
                            this->Center[1] + id * this->N0[1] * this->Spacing,
                            this->Center[2] + id * this->N0[2] * this->Spacing};

            plane->SetOrigin(origin);
            plane->SetNormal(N0);
            // vtkNew<vtkPolyData> contour;
            vtkNew<vtkCutter> cutter;
            cutter->SetCutFunction(plane);
            cutter->SetInputData(this->Input);
            cutter->SetSortBy(this->SortType);
            this->AppendFilter->AddInputConnection(cutter->GetOutputPort());
        }
    }
};

```

There is `Critical error detected c0000374`. Is using vtkObject inside operator() method not thread safe? What is causing this to fail?

Thanks,  
Cam

---

<div class="post-metadata">

**Author:** ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)\
**Post date:** [August 15, 2022, 3:42pm UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085/4 "2022-08-15T15:42:30Z")

</div>

Shared data between threads has to be considered very carefully. Seems like AppendFilter is shared between threads - that is certain to cause problems. Advanced Capabilities and Future Work section in the linked articles gives you suggestions on how to use shared data and how to use VTK objects inside your threads. I would also browse the VTK source to see examples on how SMPTools is used.

---

<div class="post-metadata">

**Author:** ![danlipsa](https://discourse.vtk.org/user_avatar/discourse.vtk.org/danlipsa/32/390_2.png) [@danlipsa](https://discourse.vtk.org/u/danlipsa)\
**Post date:** [August 15, 2022, 5:22pm UTC](https://discourse.vtk.org/t/how-to-create-vtkpartitioneddataset-in-parallel-using-smp/9085/5 "2022-08-15T17:22:33Z")

</div>

Also, see the following presentation with several examples of how to use SMPTools

[https://data.kitware.com/#item/610a923c2fa25629b9c9616a](https://data.kitware.com/#item/610a923c2fa25629b9c9616a)
