Skip to content

Commit

Permalink
add vtkm device conversion for uint64 (#1373)
Browse files Browse the repository at this point in the history
vtkm worklet for int64 to float64 conversion

Why? Because we build with VTKM_64BIT_IDS=OFF
  • Loading branch information
nicolemarsaglia authored Sep 17, 2024
1 parent 9d8f347 commit af8498b
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 53 deletions.
167 changes: 114 additions & 53 deletions src/libs/ascent/runtimes/ascent_vtkh_data_adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/Algorithm.h>
#include <vtkm/cont/ArrayCopy.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/ArrayHandleExtractComponent.h>
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/cont/Invoker.h>
#include <vtkh/DataSet.hpp>

// other ascent includes
Expand Down Expand Up @@ -1891,68 +1893,97 @@ VTKHDataAdapter::AddField(const std::string &field_name,
// Array: valueType=x storageType=N4vtkm4cont15StorageTagBasicE 27 values occupying 216 bytes [0 1 2 ... 24 25 26]
// TypeList: N4vtkm4ListIJfdEEE
// ***********************************************************************
// ***********************************************************************
// NOTE: int32 should work as of sept 10 2024
// ***********************************************************************
//
// else if(n_vals.dtype().is_int32())
// {
// // check that the byte stride is a multiple of native stride
// index_t stride = n_vals.dtype().stride();
// index_t element_stride = stride / sizeof(int32);
// //std::cout << "field name: " << field_name << " <int32>"
// // << " byte stride: " << stride
// // << " element_stride: " << element_stride << std::endl;
// // if element_stride is evenly divided by native, we are good to
// // use vtk m array handles
// if( stride % sizeof(int32) == 0 )
// {
// // in this case we can use a strided array handle
// dset->AddField(detail::GetField<int32>(n_vals,
// field_name,
// assoc_str,
// topo_name,
// element_stride,
// zero_copy));
// supported_type = true;
// }
// }
// else if(n_vals.dtype().is_int64())
// {
// // check that the byte stride is a multiple of native stride
// index_t stride = n_vals.dtype().stride();
// index_t element_stride = stride / sizeof(int64);
// //std::cout << "field name: " << field_name << " <int64>"
// // << " byte stride: " << stride
// // << " element_stride: " << element_stride << std::endl;
// // if element_stride is evenly divided by native, we are good to
// // use vtk m array handles
// if( stride % sizeof(int64) == 0 )
// {
// // in this case we can use a strided array handle
// dset->AddField(detail::GetField<int64>(n_vals,
// field_name,
// assoc_str,
// topo_name,
// element_stride,
// zero_copy));
// supported_type = true;
// }
// }
//else if(n_vals.dtype().is_int32())
//{
// // check that the byte stride is a multiple of native stride
// index_t stride = n_vals.dtype().stride();
// index_t element_stride = stride / sizeof(int32);
// //std::cout << "field name: " << field_name << " <int32>"
// // << " byte stride: " << stride
// // << " element_stride: " << element_stride << std::endl;
// // if element_stride is evenly divided by native, we are good to
// // use vtk m array handles
// if( stride % sizeof(int32) == 0 )
// {
// // in this case we can use a strided array handle
// dset->AddField(detail::GetField<int32>(n_vals,
// field_name,
// assoc_str,
// topo_name,
// element_stride,
// zero_copy));
// supported_type = true;
// }
//}
//else if(n_vals.dtype().is_int64())
//{
// // check that the byte stride is a multiple of native stride
// index_t stride = n_vals.dtype().stride();
// index_t element_stride = stride / sizeof(int64);
// //std::cout << "field name: " << field_name << " <int64>"
// // << " byte stride: " << stride
// // << " element_stride: " << element_stride << std::endl;
// // if element_stride is evenly divided by native, we are good to
// // use vtk m array handles
// if( stride % sizeof(int64) == 0 )
// {
// // in this case we can use a strided array handle
// dset->AddField(detail::GetField<int64>(n_vals,
// field_name,
// assoc_str,
// topo_name,
// element_stride,
// zero_copy));
// supported_type = true;
// }
//}
//else if(n_vals.dtype().is_uint64())
//{
// // check that the byte stride is a multiple of native stride
// index_t stride = n_vals.dtype().stride();
// index_t element_stride = stride / sizeof(uint64);
// //std::cout << "field name: " << field_name << " <int64>"
// // << " byte stride: " << stride
// // << " element_stride: " << element_stride << std::endl;
// // if element_stride is evenly divided by native, we are good to
// // use vtk m array handles
// if( stride % sizeof(uint64) == 0 )
// {
// // in this case we can use a strided array handle
// dset->AddField(detail::GetField<uint64>(n_vals,
// field_name,
// assoc_str,
// topo_name,
// element_stride,
// zero_copy));
// supported_type = true;
// }
//}

// vtk-m cant support zero copy for this layout or was not compiled to expose this datatype
// use float64 by default
if(!supported_type)
{
// std::cout << "WE ARE IN UNSUPPORTED DATA TYPE: "
// << n_vals.dtype().name() << std::endl;

// convert to float64, we use this as a comprise to cover the widest range
// std::cout << "WE ARE IN UNSUPPORTED DATA TYPE: "
// << n_vals.dtype().name() << std::endl;
if(n_vals.dtype().is_uint64())
{

vtkm::cont::ArrayHandle<vtkm::Float64> vtkm_arr;
vtkm_arr.Allocate(num_vals);

// TODO -- FUTURE: Do this conversion w/ device if on device
void *ptr = (void*) vtkh::GetVTKMPointer(vtkm_arr);
Node n_tmp;
n_tmp.set_external(DataType::float64(num_vals),ptr);
n_vals.to_float64_array(n_tmp);
const unsigned long long *input = n_vals.value();
vtkm::cont::ArrayHandle<unsigned long long> input_arr = vtkm::cont::make_ArrayHandle(input, num_vals, vtkm::CopyFlag::Off);

vtkm::cont::Invoker invoker;
vtkh::VTKmTypeCast worklet;

invoker(worklet,input_arr,vtkm_arr);

// add field to dataset
if(assoc_str == "vertex")
Expand All @@ -1967,12 +1998,42 @@ VTKHDataAdapter::AddField(const std::string &field_name,
vtkm::cont::Field::Association::Cells,
vtkm_arr));
}
}
}
else
{
// st d::cout << "WE ARE IN UNSUPPORTED DATA TYPE: "
// << n_vals.dtype().name() << std::endl;

// convert to float64, we use this as a comprise to cover the widest range
vtkm::cont::ArrayHandle<vtkm::Float64> vtkm_arr;
vtkm_arr.Allocate(num_vals);

// TODO -- FUTURE: Do this conversion w/ device if on device
void *ptr = (void*) vtkh::GetVTKMPointer(vtkm_arr);
Node n_tmp;
n_tmp.set_external(DataType::float64(num_vals),ptr);
n_vals.to_float64_array(n_tmp);

// add field to dataset
if(assoc_str == "vertex")
{
dset->AddField(vtkm::cont::Field(field_name.c_str(),
vtkm::cont::Field::Association::Points,
vtkm_arr));
}
else if( assoc_str == "element")
{
dset->AddField(vtkm::cont::Field(field_name.c_str(),
vtkm::cont::Field::Association::Cells,
vtkm_arr));
}
}
// else
// {
// std::cout << "SUPPORTED DATA TYPE: "
// << n_vals.dtype().name() << std::endl;
// }
}
}
catch (vtkm::cont::Error error)
{
Expand Down
21 changes: 21 additions & 0 deletions src/libs/vtkh/utils/vtkm_array_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define VTKH_VTKM_ARRAY_UTILS_HPP

#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/worklet/WorkletMapField.h>

namespace vtkh {

Expand All @@ -12,5 +13,25 @@ GetVTKMPointer(vtkm::cont::ArrayHandle<T> &handle)
return handle.WritePortal().GetArray();
}

class VTKmTypeCast : public vtkm::worklet::WorkletMapField
{
public:
VTKM_CONT
VTKmTypeCast() = default;

using ControlSignature = void(FieldIn, FieldOut);
using ExecutionSignature = void( _1, _2);
//using ExecutionSignature = void(InputIndex, _1, _2);

//void operator()(const vtkm::Id idx, const vtkm::cont::ArrayHandle<InType> &input, vtkm::cont::ArrayHandle<OutType> &output) const
template<typename InType, typename OutType>
VTKM_EXEC
void operator()(const InType &input, OutType &output) const
{
//output.Set(idx, static_cast<OutType>(input[idx]));
output = static_cast<OutType>(input);
}
};

}//namespace vtkh
#endif

0 comments on commit af8498b

Please sign in to comment.