Last active
July 29, 2020 12:42
-
-
Save Robadob/02a43160cee11a6708aefde03c7ce928 to your computer and use it in GitHub Desktop.
Simple CUDA version of std::array
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
#include <cassert> | |
template<typename T, size_t N> | |
class GenericArray { | |
T buffer[N]; | |
public: | |
__host__ __device__ GenericArray() { | |
memset(buffer, 0, N * sizeof(N)); | |
} | |
__host__ __device__ GenericArray(const T *in) { | |
memcpy(buffer, in, N * sizeof(N)); | |
} | |
__host__ __device__ GenericArray(const GenericArray<T, N> &in) { | |
memcpy(buffer, in.data(), N * sizeof(N)); | |
} | |
__host__ __device__ GenericArray<T, N> &operator=(const GenericArray<T, N> &rhs) { | |
if(this == &rhs) | |
return *this; | |
memcpy(buffer, rhs.data(), N * sizeof(N)); | |
return *this; | |
} | |
__host__ __device__ T &operator[](const size_t &index) { | |
if (index > N) { | |
assert(false); | |
return buffer[0]; | |
} | |
return buffer[index]; | |
} | |
__host__ __device__ T *data() const { | |
return (T *)&buffer[0]; | |
} | |
__host__ __device__ constexpr size_t length() { | |
return N; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment