LvArray
numpyHelpers.hpp
Go to the documentation of this file.
1 /*
2  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3  * Copyright (c) 2019, Lawrence Livermore National Security, LLC.
4  *
5  * Produced at the Lawrence Livermore National Laboratory
6  *
7  * LLNL-CODE-746361
8  *
9  * All rights reserved. See COPYRIGHT for details.
10  *
11  * This file is part of the GEOSX Simulation Framework.
12  *
13  * GEOSX is a free software; you can redistribute it and/or modify it under
14  * the terms of the GNU Lesser General Public License (as published by the
15  * Free Software Foundation) version 2.1 dated February 1999.
16  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17  */
18 
24 #pragma once
25 
26 // Source include
28 #include "pythonHelpers.hpp"
29 #include "../limits.hpp"
30 
31 // system includes
32 #include <vector>
33 #include <typeindex>
34 #include <type_traits>
35 
36 namespace LvArray
37 {
38 namespace python
39 {
40 namespace internal
41 {
42 
47 template< typename T >
48 constexpr bool canExportToNumpy = std::is_arithmetic< T >::value;
49 
61 PyObject * createNumpyArrayImpl( void * const data,
62  std::type_index const type,
63  bool const dataIsConst,
64  int const ndim,
65  long long const * const dims,
66  long long const * const strides );
67 
79 PyObject * createCupyArrayImpl( void * const data,
80  std::type_index const type,
81  bool const dataIsConst,
82  int const ndim,
83  long long const * const dims,
84  long long const * const strides );
85 
86 } // namespace internal
87 
93 
105 template< typename T, typename INDEX_TYPE >
106 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
107 createNumPyArray( T * const data,
108  bool const modify,
109  int const ndim,
110  INDEX_TYPE const * const dimsPtr,
111  INDEX_TYPE const * const stridesPtr )
112 {
113  std::vector< long long > dims( ndim );
114  std::vector< long long > strides( ndim );
115 
116  for( int i = 0; i < ndim; ++i )
117  {
118  dims[ i ] = integerConversion< long long >( dimsPtr[ i ] );
119  strides[ i ] = integerConversion< long long >( stridesPtr[ i ] );
120  }
121 
122  if( false )
123  {
124  return internal::createCupyArrayImpl( const_cast< void * >( static_cast< void const * >( data ) ),
125  std::type_index( typeid( T ) ),
126  std::is_const< T >::value || !modify,
127  ndim,
128  dims.data(),
129  strides.data() );
130  }
131  return internal::createNumpyArrayImpl( const_cast< void * >( static_cast< void const * >( data ) ),
132  std::type_index( typeid( T ) ),
133  std::is_const< T >::value || !modify,
134  ndim,
135  dims.data(),
136  strides.data() );
137 }
138 
145 template< typename T >
146 std::enable_if_t< internal::canExportToNumpy< T >, PyObject * >
147 create( T & value )
148 {
149  long long dims = 1;
150  long long strides = 1;
151 
152  return internal::createNumpyArrayImpl( const_cast< void * >( static_cast< void const * >( &value ) ),
153  std::type_index( typeid( T ) ),
154  std::is_const< T >::value,
155  1,
156  &dims,
157  &strides );
158 }
159 
167 std::tuple< PyObjectRef< PyObject >, void const *, long long >
168 parseNumPyArray( PyObject * const obj, std::type_index const expectedType );
169 
175 std::type_index getTypeIndexFromNumPy( int const numpyType );
176 
182 std::string getNumPyTypeName( int const numpyType );
183 
190 PyObject * getNumPyTypeObject( std::type_index const typeIndex );
191 
192 } // namespace python
193 } // namespace LvArray
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> create(T &value)
Create a NumPy 1D array of length 1 containing the scalar value.
Definition: numpyHelpers.hpp:147
std::type_index getTypeIndexFromNumPy(int const numpyType)
Return the std::type_index corresponding to the NumPy type numpyType.
Definition: numpyHelpers.cpp:276
Forward declarations of Python Objects.
The top level namespace.
Definition: Array.hpp:24
constexpr bool canExportToNumpy
True if T can be represented by a NumPy native data type.
Definition: numpyHelpers.hpp:48
PyObject * getNumPyTypeObject(std::type_index const typeIndex)
Return the NumPy type object corresponding to typeIndex.
Definition: numpyHelpers.cpp:398
bool import_array_wrapper()
Attempt to import the NumPy API if it was not already imported, return true iff successful.
Definition: numpyHelpers.cpp:221
std::tuple< PyObjectRef< PyObject >, void const *, long long > parseNumPyArray(PyObject *const obj, std::type_index const expectedType)
Attempt to parse obj into a NumPy ndarray of type expectedType.
Definition: numpyHelpers.cpp:233
std::string getNumPyTypeName(int const numpyType)
Return the name corresponding to the NumPy type numpyType.
Definition: numpyHelpers.cpp:335
std::enable_if_t< internal::canExportToNumpy< T >, PyObject *> createNumPyArray(T *const data, bool const modify, int const ndim, INDEX_TYPE const *const dimsPtr, INDEX_TYPE const *const stridesPtr)
Return a NumPy ndarray view of data.
Definition: numpyHelpers.hpp:107