Commit 5abad992 authored by Berk Geveci's avatar Berk Geveci
Browse files

First implementation of SMP parallel primitives.

Added primitives for developing multi-threaded parallel
algorithms. Include are vtkSMPTools, which currently
provides a parallel For implementation; support for simple
and initializable functors, which are needed to implement
parallel kernels and thread local storage objects that
provide storage local to each thread.

Change-Id: I4c27b25bc2dd7f0ce3194d4488864fef4444c9d3
parent 75062ae4
......@@ -55,6 +55,7 @@ elseif ("${VTK_SMP_IMPLEMENTATION_TYPE}" STREQUAL "Kaapi")
elseif ("${VTK_SMP_IMPLEMENTATION_TYPE}" STREQUAL "Simple")
set(VTK_SMP_IMPLEMENTATION_LIBRARIES)
set(VTK_SMP_ATOMIC_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/SMP/Sequential")
message(WARNING "The Simple backend for SMP operations is an experimental backend that is mainly used for debugging currently. We recommend that you use either the TBB or the Kaapi backend for production work. Use the Sequential backend if you would like to turn off any SMP parallelism.")
elseif ("${VTK_SMP_IMPLEMENTATION_TYPE}" STREQUAL "Sequential")
set(VTK_SMP_IMPLEMENTATION_LIBRARIES)
set(VTK_SMP_ATOMIC_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/SMP/Sequential")
......@@ -69,6 +70,12 @@ endif()
configure_file(${VTK_SMP_ATOMIC_DIRECTORY}/vtkAtomicInt.h.in
${CMAKE_CURRENT_BINARY_DIR}/vtkAtomicInt.h COPY_ONLY)
configure_file(SMP/${VTK_SMP_IMPLEMENTATION_TYPE}/vtkSMPThreadLocal.h.in
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPThreadLocal.h COPY_ONLY)
configure_file(SMP/${VTK_SMP_IMPLEMENTATION_TYPE}/vtkSMPToolsInternal.h.in
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPToolsInternal.h COPY_ONLY)
#-----------------------------------------------------------------------------
include_directories(${VTK_SOURCE_DIR}/ThirdParty/utf8/source)
......@@ -196,6 +203,11 @@ SET(Module_SRCS
vtkType.h
vtkSystemIncludes.h
${VTK_ATOMIC_CXX_FILE}
vtkSMPThreadLocalObject.h
vtkSMPTools.h
SMP/${VTK_SMP_IMPLEMENTATION_TYPE}/vtkSMPTools.cxx
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPToolsInternal.h
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPThreadLocal.h
${CMAKE_CURRENT_BINARY_DIR}/vtkAtomicInt.h
${CMAKE_CURRENT_BINARY_DIR}/vtkConfigure.h
${CMAKE_CURRENT_BINARY_DIR}/vtkMathConfigure.h
......@@ -528,6 +540,11 @@ set_source_files_properties(
vtkMathConfigure.h
vtkVersionMacros.h
vtkTypeTemplate.h
vtkSMPThreadLocalObject.h
vtkSMPTools.h
SMP/${VTK_SMP_IMPLEMENTATION_TYPE}/vtkSMPTools.cxx
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPToolsInternal.h
${CMAKE_CURRENT_BINARY_DIR}/vtkSMPThreadLocal.h
${CMAKE_CURRENT_BINARY_DIR}/vtkAtomicInt.h
${VTK_ATOMIC_CXX_FILE}
......
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPThreadLocal.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkSMPThreadLocal - A Kaapi based thread local storage implementation.
// .SECTION Description
// A thread local object is one that maintains a copy of an object of the
// template type for each thread that processes data. vtkSMPThreadLocal
// creates storage for all threads but the actual objects are created
// the first time Local() is called. Note that some of the vtkSMPThreadLocal
// API is not thread safe. It can be safely used in a multi-threaded
// environment because Local() returns storage specific to a particular
// thread, which by default will be accessed sequentially. It is also
// thread-safe to iterate over vtkSMPThreadLocal as long as each thread
// creates its own iterator and does not change any of the thread local
// objects.
//
// A common design pattern in using a thread local storage object is to
// write/accumulate data to local object when executing in parallel and
// then having a sequential code block that iterates over the whole storage
// using the iterators to do the final accumulation.
//
// .SECTION Warning
// There is absolutely no guarantee to the order in which the local objects
// will be stored and hence the order in which they will be traversed when
// using iterators. You should not even assume that two vtkSMPThreadLocal
// populated in the same parallel section will be populated in the same
// order. For example, consider the following
// \verbatim
// vtkSMPThreadLocal<int> Foo;
// vtkSMPThreadLocal<int> Bar;
// class AFunctor
// {
// void Initialize() const
// {
// int& foo = Foo.Local();
// int& bar = Bar.Local();
// foo = random();
// bar = foo;
// }
//
// void operator()(vtkIdType, vtkIdType) const
// {}
// };
//
// AFunctor functor;
// vtkParalllelUtilities::For(0, 100000, functor);
//
// vtkSMPThreadLocal<int>::iterator itr1 = Foo.begin();
// vtkSMPThreadLocal<int>::iterator itr2 = Bar.begin();
// while (itr1 != Foo.end())
// {
// assert(*itr1 == *itr2);
// ++itr1; ++itr2;
// }
// \endverbatim
//
// It is possible and likely that the assert() will fail using the TBB
// backend. So if you need to store values related to each other and
// iterate over them together, use a struct or class to group them together
// and use a thread local of that class.
#ifndef __vtkSMPThreadLocal_h
#define __vtkSMPThreadLocal_h
#include "vtkSystemIncludes.h"
#include <vector>
#include <kaapic.h>
VTKCOMMONCORE_EXPORT void vtkSMPToolsInitialize();
template <typename T>
class vtkSMPThreadLocal
{
typedef std::vector<T> TLS;
typedef typename TLS::iterator TLSIter;
public:
// Description:
// Default constructor. Creates a default exemplar.
vtkSMPThreadLocal() : Exemplar()
{
this->Initialize();
}
// Description:
// Constructor that allows the specification of an exemplar object
// which is used when constructing objects when Local() is first called.
// Note that a copy of the exemplar is created using its copy constructor.
vtkSMPThreadLocal(const T& exemplar)
{
this->Exemplar = exemplar;
this->Initialize();
}
// Description:
// Returns an object of type T that is local to the current thread.
// This needs to be called mainly within a threaded execution path.
// It will create a new object (local to the tread so each thread
// get their own when calling Local) which is a copy of exemplar as passed
// to the constructor (or a default object if no exemplar was provided)
// the first time it is called. After the first time, it will return
// the same object.
T& Local()
{
int tid = this->GetThreadID();
if (!this->Initialized[tid])
{
this->Internal[tid] = this->Exemplar;
this->Initialized[tid] = true;
}
return this->Internal[tid];
}
// Description:
// Subset of the standard iterator API.
// The most common design patter is to use iterators in a sequential
// code block and to use only the thread local objects in parallel
// code blocks.
// It is thread safe to iterate over the thread local containers
// as long as each thread uses its own iterator and does not modify
// objects in the container.
class iterator
{
public:
iterator& operator++()
{
this->InitIter++;
this->Iter++;
// Make sure to skip uninitialized
// entries.
while(this->InitIter != this->EndIter)
{
if (*this->InitIter)
{
break;
}
this->InitIter++;
this->Iter++;
}
return *this;
}
bool operator!=(const iterator& other)
{
return this->Iter != other.Iter;
}
T& operator*()
{
return *this->Iter;
}
private:
friend class vtkSMPThreadLocal<T>;
std::vector<unsigned char>::iterator InitIter;
std::vector<unsigned char>::iterator EndIter;
TLSIter Iter;
};
// Description:
// Returns a new iterator pointing to the beginning of
// the local storage container. Thread safe.
iterator begin()
{
TLSIter iter = this->Internal.begin();
std::vector<unsigned char>::iterator iter2 =
this->Initialized.begin();
std::vector<unsigned char>::iterator end =
this->Initialized.end();
// fast forward to first initialized
// value
while(iter2 != end)
{
if (*iter2)
{
break;
}
iter2++;
iter++;
}
iterator retVal;
retVal.InitIter = iter2;
retVal.EndIter = end;
retVal.Iter = iter;
return retVal;
};
// Description:
// Returns a new iterator pointing to past the end of
// the local storage container. Thread safe.
iterator end()
{
iterator retVal;
retVal.InitIter = this->Initialized.end();
retVal.EndIter = this->Initialized.end();
retVal.Iter = this->Internal.end();
return retVal;
}
private:
TLS Internal;
std::vector<unsigned char> Initialized;
T Exemplar;
void Initialize()
{
vtkSMPToolsInitialize();
this->Internal.resize(this->GetNumberOfThreads());
this->Initialized.resize(this->GetNumberOfThreads());
std::fill(this->Initialized.begin(),
this->Initialized.end(),
false);
}
inline int GetNumberOfThreads()
{
return kaapi_getconcurrency();
}
inline int GetThreadID()
{
return kaapi_get_self_kid();
}
};
#endif
// VTK-HeaderTest-Exclude: vtkSMPThreadLocal.h
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPTools.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSMPTools.h"
#include "vtkCriticalSection.h"
#include <kaapic.h>
VTKCOMMONCORE_EXPORT void vtkSMPToolsInitialize()
{
vtkSMPTools::Initialize(0);
}
struct vtkSMPToolsInit
{
vtkSMPToolsInit()
{
kaapic_init(KAAPIC_START_ONLY_MAIN);
}
~vtkSMPToolsInit()
{
kaapic_finalize();
}
};
static bool vtkSMPToolsInitialized = 0;
static vtkSimpleCriticalSection vtkSMPToolsCS;
//--------------------------------------------------------------------------------
void vtkSMPTools::Initialize(int)
{
vtkSMPToolsCS.Lock();
if (!vtkSMPToolsInitialized)
{
static vtkSMPToolsInit aInit;
vtkSMPToolsInitialized = true;
}
vtkSMPToolsCS.Unlock();
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPToolsInternal.h.in
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include <kaapic.h>
VTKCOMMONCORE_EXPORT void vtkSMPToolsInitialize();
namespace vtk
{
namespace detail
{
namespace smp
{
template <typename T>
inline void vtkSMPToolsDoFor(int32_t b, int32_t e, int32_t, T* o )
{
o->Execute(b, e);
}
template <typename FunctorInternal>
static void vtkSMPTools_Impl_For(
vtkIdType first, vtkIdType last, vtkIdType grain,
FunctorInternal& fi)
{
vtkSMPToolsInitialize();
vtkIdType n = last - first;
if (!n)
{
return;
}
vtkIdType g = grain ? grain : sqrt(n);
kaapic_begin_parallel(KAAPIC_FLAG_DEFAULT);
kaapic_foreach_attr_t attr;
kaapic_foreach_attr_init(&attr);
kaapic_foreach_attr_set_grains(&attr, g, g);
kaapic_foreach( first, last, &attr, 1, vtkSMPToolsDoFor<FunctorInternal>, &fi );
kaapic_end_parallel(KAAPIC_FLAG_DEFAULT);
kaapic_foreach_attr_destroy(&attr);
}
}
}
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPThreadLocal.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkSMPThreadLocal - A simple thread local implementation for sequential operations.
// .SECTION Description
// A thread local object is one that maintains a copy of an object of the
// template type for each thread that processes data. vtkSMPThreadLocal
// creates storage for all threads but the actual objects are created
// the first time Local() is called. Note that some of the vtkSMPThreadLocal
// API is not thread safe. It can be safely used in a multi-threaded
// environment because Local() returns storage specific to a particular
// thread, which by default will be accessed sequentially. It is also
// thread-safe to iterate over vtkSMPThreadLocal as long as each thread
// creates its own iterator and does not change any of the thread local
// objects.
//
// A common design pattern in using a thread local storage object is to
// write/accumulate data to local object when executing in parallel and
// then having a sequential code block that iterates over the whole storage
// using the iterators to do the final accumulation.
//
// Note that this particular implementation is designed to work in sequential
// mode and supports only 1 thread.
#ifndef __vtkSMPThreadLocal_h
#define __vtkSMPThreadLocal_h
#include "vtkSystemIncludes.h"
#include <vector>
template <typename T>
class vtkSMPThreadLocal
{
typedef std::vector<T> TLS;
typedef typename TLS::iterator TLSIter;
public:
// Description:
// Default constructor. Creates a default exemplar.
vtkSMPThreadLocal()
{
this->Initialize();
}
// Description:
// Constructor that allows the specification of an exemplar object
// which is used when constructing objects when Local() is first called.
// Note that a copy of the exemplar is created using its copy constructor.
vtkSMPThreadLocal(const T& exemplar) : Exemplar(exemplar)
{
this->Initialize();
}
// Description:
// Returns an object of type T that is local to the current thread.
// This needs to be called mainly within a threaded execution path.
// It will create a new object (local to the tread so each thread
// get their own when calling Local) which is a copy of exemplar as passed
// to the constructor (or a default object if no exemplar was provided)
// the first time it is called. After the first time, it will return
// the same object.
T& Local()
{
int tid = this->GetThreadID();
if (!this->Initialized[tid])
{
this->Internal[tid] = this->Exemplar;
this->Initialized[tid] = true;
}
return this->Internal[tid];
}
// Description:
// Subset of the standard iterator API.
// The most common design patter is to use iterators in a sequential
// code block and to use only the thread local objects in parallel
// code blocks.
// It is thread safe to iterate over the thread local containers
// as long as each thread uses its own iterator and does not modify
// objects in the container.
class iterator
{
public:
iterator& operator++()
{
this->InitIter++;
this->Iter++;
// Make sure to skip uninitialized
// entries.
while(this->InitIter != this->EndIter)
{
if (*this->InitIter)
{
break;
}
this->InitIter++;
this->Iter++;
}
return *this;
}
bool operator!=(const iterator& other)
{
return this->Iter != other.Iter;
}
T& operator*()
{
return *this->Iter;
}
private:
friend class vtkSMPThreadLocal<T>;
std::vector<bool>::iterator InitIter;
std::vector<bool>::iterator EndIter;
TLSIter Iter;
};
// Description:
// Returns a new iterator pointing to the beginning of
// the local storage container. Thread safe.
iterator begin()
{
TLSIter iter = this->Internal.begin();
std::vector<bool>::iterator iter2 =
this->Initialized.begin();
std::vector<bool>::iterator end =
this->Initialized.end();
// fast forward to first initialized
// value
while(iter2 != end)
{
if (*iter2)
{
break;
}
iter2++;
iter++;
}
iterator retVal;
retVal.InitIter = iter2;
retVal.EndIter = end;
retVal.Iter = iter;
return retVal;
};
// Description:
// Returns a new iterator pointing to past the end of
// the local storage container. Thread safe.
iterator end()
{
iterator retVal;
retVal.InitIter = this->Initialized.end();
retVal.EndIter = this->Initialized.end();
retVal.Iter = this->Internal.end();
return retVal;
}
private:
TLS Internal;
std::vector<bool> Initialized;
T Exemplar;
void Initialize()
{
this->Internal.resize(this->GetNumberOfThreads());
this->Initialized.resize(this->GetNumberOfThreads());
std::fill(this->Initialized.begin(),
this->Initialized.end(),
false);
}
inline int GetNumberOfThreads()
{
return 1;
}
inline int GetThreadID()
{
return 0;
}
};
#endif
// VTK-HeaderTest-Exclude: vtkSMPThreadLocal.h
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPTools.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkSMPTools.h"
// Simple implementation that runs everything sequentially.
//--------------------------------------------------------------------------------
void vtkSMPTools::Initialize(int)
{
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSMPToolsInternal.h.in
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR