vtk-m/vtkm/cont/Field.h

295 lines
8.8 KiB
C
Raw Normal View History

2015-02-10 17:36:10 +00:00
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
2015-02-10 17:36:10 +00:00
// 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.
//============================================================================
#ifndef vtk_m_cont_Field_h
#define vtk_m_cont_Field_h
2015-02-10 17:36:10 +00:00
#include <vtkm/cont/vtkm_cont_export.h>
#include <vtkm/Range.h>
#include <vtkm/Types.h>
2015-07-16 19:10:01 +00:00
2015-02-10 17:36:10 +00:00
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/UnknownArrayHandle.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace cont
{
2015-02-10 17:36:10 +00:00
2015-02-10 17:36:10 +00:00
/// A \c Field encapsulates an array on some piece of the mesh, such as
/// the points, a cell set, a point logical dimension, or the whole mesh.
2015-02-10 17:36:10 +00:00
///
class VTKM_CONT_EXPORT Field
2015-02-10 17:36:10 +00:00
{
public:
enum struct Association
{
ANY,
WHOLE_MESH,
POINTS,
CELL_SET
};
VTKM_CONT
Field() = default;
VTKM_CONT
Field(std::string name, Association association, const vtkm::cont::UnknownArrayHandle& data);
2015-07-30 14:22:25 +00:00
2017-05-18 14:29:41 +00:00
template <typename T, typename Storage>
VTKM_CONT Field(std::string name,
Association association,
const vtkm::cont::ArrayHandle<T, Storage>& data)
: Field(name, association, vtkm::cont::UnknownArrayHandle{ data })
2015-05-11 20:15:58 +00:00
{
}
Field(const vtkm::cont::Field& src);
Field(vtkm::cont::Field&& src) noexcept;
VTKM_CONT virtual ~Field();
VTKM_CONT Field& operator=(const vtkm::cont::Field& src);
VTKM_CONT Field& operator=(vtkm::cont::Field&& src) noexcept;
VTKM_CONT const std::string& GetName() const { return this->Name; }
VTKM_CONT Association GetAssociation() const { return this->FieldAssociation; }
const vtkm::cont::UnknownArrayHandle& GetData() const;
vtkm::cont::UnknownArrayHandle& GetData();
2015-07-16 19:10:01 +00:00
VTKM_CONT bool IsFieldCell() const { return this->FieldAssociation == Association::CELL_SET; }
VTKM_CONT bool IsFieldPoint() const { return this->FieldAssociation == Association::POINTS; }
VTKM_CONT bool IsFieldGlobal() const { return this->FieldAssociation == Association::WHOLE_MESH; }
VTKM_CONT vtkm::Id GetNumberOfValues() const { return this->Data.GetNumberOfValues(); }
template <typename TypeList>
VTKM_DEPRECATED(1.6, "TypeList no longer supported in Field::GetRange.")
VTKM_CONT void GetRange(vtkm::Range* range, TypeList) const
2015-07-16 19:10:01 +00:00
{
this->GetRange(range);
2015-07-16 19:10:01 +00:00
}
2017-05-18 14:29:41 +00:00
template <typename TypeList>
VTKM_DEPRECATED(1.6, "TypeList no longer supported in Field::GetRange.")
2017-05-18 14:29:41 +00:00
VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange(TypeList) const
2015-07-16 19:10:01 +00:00
{
return this->GetRange();
2015-07-16 19:10:01 +00:00
}
VTKM_CONT const vtkm::cont::ArrayHandle<vtkm::Range>& GetRange() const;
2015-07-30 14:22:25 +00:00
VTKM_CONT void GetRange(vtkm::Range* range) const;
2015-02-10 17:36:10 +00:00
VTKM_CONT void SetData(const vtkm::cont::UnknownArrayHandle& newdata);
template <typename T, typename StorageTag>
VTKM_CONT void SetData(const vtkm::cont::ArrayHandle<T, StorageTag>& newdata)
{
this->SetData(vtkm::cont::UnknownArrayHandle(newdata));
2015-02-10 17:36:10 +00:00
}
VTKM_CONT
2017-05-18 14:29:41 +00:00
virtual void PrintSummary(std::ostream& out) const;
VTKM_CONT
virtual void ReleaseResourcesExecution()
{
this->Data.ReleaseResourcesExecution();
this->Range.ReleaseResourcesExecution();
}
2015-02-10 17:36:10 +00:00
private:
2017-05-18 14:29:41 +00:00
std::string Name; ///< name of field
2015-05-19 20:21:15 +00:00
Association FieldAssociation = Association::ANY;
vtkm::cont::UnknownArrayHandle Data;
mutable vtkm::cont::ArrayHandle<vtkm::Range> Range;
mutable bool ModifiedFlag = true;
2015-02-10 17:36:10 +00:00
};
template <typename Functor, typename... Args>
void CastAndCall(const vtkm::cont::Field& field, Functor&& f, Args&&... args)
{
vtkm::cont::CastAndCall(field.GetData(), std::forward<Functor>(f), std::forward<Args>(args)...);
}
//@{
/// Convenience functions to build fields from C style arrays and std::vector
template <typename T>
vtkm::cont::Field make_Field(std::string name,
Field::Association association,
const T* data,
vtkm::Id size,
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
vtkm::CopyFlag copy)
{
return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, size, copy));
}
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
template <typename T>
VTKM_DEPRECATED(1.6, "Specify a vtkm::CopyFlag or use a move version of make_Field.")
vtkm::cont::Field
make_Field(std::string name, Field::Association association, const T* data, vtkm::Id size)
{
return make_Field(name, association, data, size, vtkm::CopyFlag::Off);
}
template <typename T>
vtkm::cont::Field make_Field(std::string name,
Field::Association association,
const std::vector<T>& data,
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
vtkm::CopyFlag copy)
{
return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandle(data, copy));
}
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
template <typename T>
VTKM_DEPRECATED(1.6, "Specify a vtkm::CopyFlag or use a move version of make_Field.")
vtkm::cont::Field
make_Field(std::string name, Field::Association association, const std::vector<T>& data)
{
return make_Field(name, association, data, vtkm::CopyFlag::Off);
}
template <typename T>
vtkm::cont::Field make_FieldMove(std::string name,
Field::Association association,
std::vector<T>&& data)
{
return vtkm::cont::Field(name, association, vtkm::cont::make_ArrayHandleMove(data));
}
template <typename T>
vtkm::cont::Field make_Field(std::string name,
Field::Association association,
std::vector<T>&& data,
vtkm::CopyFlag vtkmNotUsed(copy))
Improvements to moving data into ArrayHandle We have made several improvements to adding data into an `ArrayHandle`. ## Moving data from an `std::vector` For numerous reasons, it is convenient to define data in a `std::vector` and then wrap that into an `ArrayHandle`. It is often the case that an `std::vector` is filled and then becomes unused once it is converted to an `ArrayHandle`. In this case, what we really want is to pass the data off to the `ArrayHandle` so that the `ArrayHandle` is now managing the data and not the `std::vector`. C++11 has a mechanism to do this: move semantics. You can now pass variables to functions as an "rvalue" (right-hand value). When something is passed as an rvalue, it can pull state out of that variable and move it somewhere else. `std::vector` implements this movement so that an rvalue can be moved to another `std::vector` without actually copying the data. `make_ArrayHandle` now also takes advantage of this feature to move rvalue `std::vector`s. There is a special form of `make_ArrayHandle` named `make_ArrayHandleMove` that takes an rvalue. There is also a special overload of `make_ArrayHandle` itself that handles an rvalue `vector`. (However, using the explicit move version is better if you want to make sure the data is actually moved.) ## Make `ArrayHandle` from initalizer list A common use case for using `std::vector` (particularly in our unit tests) is to quickly add an initalizer list into an `ArrayHandle`. Now you can by simply passing an initializer list to `make_ArrayHandle`. ## Deprecated `make_ArrayHandle` with default shallow copy For historical reasons, passing an `std::vector` or a pointer to `make_ArrayHandle` does a shallow copy (i.e. `CopyFlag` defaults to `Off`). Although more efficient, this mode is inherintly unsafe, and making it the default is asking for trouble. To combat this, calling `make_ArrayHandle` without a copy flag is deprecated. In this way, if you wish to do the faster but more unsafe creation of an `ArrayHandle` you should explicitly express that. This requried quite a few changes through the VTK-m source (particularly in the tests). ## Similar changes to `Field` `vtkm::cont::Field` has a `make_Field` helper function that is similar to `make_ArrayHandle`. It also features the ability to create fields from `std::vector`s and C arrays. It also likewise had the same unsafe behavior by default of not copying from the source of the arrays. That behavior has similarly been depreciated. You now have to specify a copy flag. The ability to construct a `Field` from an initializer list of values has also been added.
2020-07-16 16:32:32 +00:00
{
return make_FieldMove(name, association, std::move(data));
}
template <typename T>
vtkm::cont::Field make_Field(std::string name,
Field::Association association,
std::initializer_list<T>&& data)
{
return make_FieldMove(name, association, vtkm::cont::make_ArrayHandle(std::move(data)));
}
//@}
/// Convenience function to build point fields from vtkm::cont::ArrayHandle
template <typename T, typename S>
vtkm::cont::Field make_FieldPoint(std::string name, const vtkm::cont::ArrayHandle<T, S>& data)
{
return vtkm::cont::Field(name, vtkm::cont::Field::Association::POINTS, data);
}
/// Convenience function to build point fields from vtkm::cont::UnknownArrayHandle
inline vtkm::cont::Field make_FieldPoint(std::string name,
const vtkm::cont::UnknownArrayHandle& data)
{
return vtkm::cont::Field(name, vtkm::cont::Field::Association::POINTS, data);
}
/// Convenience function to build cell fields from vtkm::cont::ArrayHandle
template <typename T, typename S>
vtkm::cont::Field make_FieldCell(std::string name, const vtkm::cont::ArrayHandle<T, S>& data)
{
return vtkm::cont::Field(name, vtkm::cont::Field::Association::CELL_SET, data);
}
/// Convenience function to build cell fields from vtkm::cont::UnknownArrayHandle
inline vtkm::cont::Field make_FieldCell(std::string name,
const vtkm::cont::UnknownArrayHandle& data)
{
return vtkm::cont::Field(name, vtkm::cont::Field::Association::CELL_SET, data);
}
} // namespace cont
} // namespace vtkm
namespace vtkm
{
namespace cont
{
2017-05-18 14:29:41 +00:00
namespace internal
{
template <>
struct DynamicTransformTraits<vtkm::cont::Field>
{
using DynamicTag = vtkm::cont::internal::DynamicTransformTagCastAndCall;
};
} // namespace internal
2015-02-10 17:36:10 +00:00
} // namespace cont
} // namespace vtkm
2018-06-18 17:56:38 +00:00
//=============================================================================
// Specializations of serialization related classes
/// @cond SERIALIZATION
2018-06-18 17:56:38 +00:00
namespace vtkm
{
namespace cont
{
template <typename TypeList = VTKM_DEFAULT_TYPE_LIST>
struct VTKM_DEPRECATED(1.6, "You can now directly serialize Field.") SerializableField
2018-06-18 17:56:38 +00:00
{
SerializableField() = default;
explicit SerializableField(const vtkm::cont::Field& field)
: Field(field)
{
}
vtkm::cont::Field Field;
};
} // namespace cont
} // namespace vtkm
2018-06-18 17:56:38 +00:00
namespace mangled_diy_namespace
2018-06-18 17:56:38 +00:00
{
template <>
struct VTKM_CONT_EXPORT Serialization<vtkm::cont::Field>
{
static VTKM_CONT void save(BinaryBuffer& bb, const vtkm::cont::Field& field);
static VTKM_CONT void load(BinaryBuffer& bb, vtkm::cont::Field& field);
};
// Implement deprecated code
VTKM_DEPRECATED_SUPPRESS_BEGIN
template <typename TypeList>
struct Serialization<vtkm::cont::SerializableField<TypeList>>
2018-06-18 17:56:38 +00:00
{
private:
using Type = vtkm::cont::SerializableField<TypeList>;
2018-06-18 17:56:38 +00:00
public:
static VTKM_CONT void save(BinaryBuffer& bb, const Type& serializable)
{
Serialization<vtkm::cont::Field>::save(bb, serializable.Field);
2018-06-18 17:56:38 +00:00
}
static VTKM_CONT void load(BinaryBuffer& bb, Type& serializable)
{
Serialization<vtkm::cont::Field>::load(bb, serializable.Field);
2018-06-18 17:56:38 +00:00
}
};
VTKM_DEPRECATED_SUPPRESS_END
2018-06-18 17:56:38 +00:00
} // diy
/// @endcond SERIALIZATION
2018-06-18 17:56:38 +00:00
#endif //vtk_m_cont_Field_h