This repository has been archived by the owner on Aug 3, 2021. It is now read-only.
forked from torch/cutorch
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request torch#558 from gchanan/genericDeviceTensorUtils
Add generic type support for toDeviceTensor.
- Loading branch information
Showing
4 changed files
with
60 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#ifndef THC_GENERIC_FILE | ||
#define THC_GENERIC_FILE "generic/THCDeviceTensorUtils.cu" | ||
#else | ||
|
||
/// Constructs a THCDeviceTensor initialized from a THCudaTensor. Will | ||
/// error if the dimensionality does not match exactly. | ||
template <typename T, int Dim, | ||
typename IndexT, template <typename U> class PtrTraits> | ||
THCDeviceTensor<T, Dim, IndexT, PtrTraits> | ||
toDeviceTensor(THCState* state, THCTensor* t); | ||
|
||
template <typename T, int Dim, typename IndexT> | ||
THCDeviceTensor<T, Dim, IndexT, DefaultPtrTraits> | ||
toDeviceTensor(THCState* state, THCTensor* t) { | ||
return toDeviceTensor<T, Dim, IndexT, DefaultPtrTraits>(state, t); | ||
} | ||
|
||
template <typename T, int Dim> | ||
THCDeviceTensor<T, Dim, int, DefaultPtrTraits> | ||
toDeviceTensor(THCState* state, THCTensor* t) { | ||
return toDeviceTensor<T, Dim, int, DefaultPtrTraits>(state, t); | ||
} | ||
|
||
template <typename T, int Dim, | ||
typename IndexT, template <typename U> class PtrTraits> | ||
THCDeviceTensor<T, Dim, IndexT, PtrTraits> | ||
toDeviceTensor(THCState* state, THCTensor* t) { | ||
if (Dim != THCTensor_(nDimension)(state, t)) { | ||
THError("THCudaTensor dimension mismatch"); | ||
} | ||
// Determine the maximum offset into the tensor achievable; `IndexT` | ||
// must be smaller than this type in order to use it. | ||
ptrdiff_t maxOffset = 0; | ||
IndexT sizes[Dim]; | ||
IndexT strides[Dim]; | ||
|
||
for (int i = 0; i < Dim; ++i) { | ||
long size = THCTensor_(size)(state, t, i); | ||
long stride = THCTensor_(stride)(state, t, i); | ||
|
||
maxOffset += (size - 1) * stride; | ||
|
||
sizes[i] = (IndexT) size; | ||
strides[i] = (IndexT) stride; | ||
} | ||
|
||
if (maxOffset > std::numeric_limits<IndexT>::max()) { | ||
THError("THCudaTensor sizes too large for THCDeviceTensor conversion"); | ||
} | ||
|
||
return THCDeviceTensor<T, Dim, IndexT, PtrTraits>( | ||
THCTensor_(data)(state, t), sizes, strides); | ||
} | ||
|
||
#endif |