Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft] Move memory transpose logic to the Fortran client #473

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/redisserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ class RedisServer {
/*!
* \brief Default socket timeout (milliseconds)
*/
static constexpr int _DEFAULT_SOCKET_TIMEOUT = 250;
static constexpr int _DEFAULT_SOCKET_TIMEOUT = 1000;

/*!
* \brief Default value of connection timeout (seconds)
Expand Down
2 changes: 0 additions & 2 deletions include/sr_enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ typedef enum {
SRMemLayoutInvalid = 0, // Invalid or uninitialized memory layout
SRMemLayoutNested = 1, // Multidimensional row-major array layout with nested arrays of pointers (contiguous at innermost layer)
SRMemLayoutContiguous = 2, // Multidimensional row-major array layout in contiguous memory
SRMemLayoutFortranNested = 3, // Multidimensional column-major array layout with nested arrays of pointers (contiguous at innermost layer)
SRMemLayoutFortranContiguous = 4 // Multidimensional column-major array layout in contiguous memory
} SRMemoryLayout;

/*!
Expand Down
85 changes: 1 addition & 84 deletions include/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,85 +217,6 @@ class Tensor : public TensorBase
const std::vector<size_t>& dims,
const SRMemoryLayout mem_layout);

/*!
* \brief This function will copy a fortran array
* memory space (column major) to a c-style
* memory space layout (row major)
* \param c_data A pointer to the row major memory space
* \param f_data A pointer to the col major memory space
* \param dims The dimensions of the tensor
*/
void _f_to_c_memcpy(T* c_data,
const T* f_data,
const std::vector<size_t>& dims);

/*!
* \brief This function will copy a c-style array
* memory space (row major) to a fortran
* memory space layout (col major)
* \param f_data A pointer to the col major memory space
* \param c_data A pointer to the row major memory space
* \param dims The dimensions of the tensor
*/
void _c_to_f_memcpy(T* f_data,
const T* c_data,
const std::vector<size_t>& dims);

/*!
* \brief This is a recursive function used to copy
* fortran column major memory to c-style row
* major memory
* \param c_data A pointer to the row major memory space
* \param f_data A pointer to the col major memory space
* \param dims The dimensions of the tensor
* \param dim_positions The current position in each
* dimension
* \param current_dim The index of the current dimension
*/
void _f_to_c(T* c_data,
const T* f_data,
const std::vector<size_t>& dims,
std::vector<size_t> dim_positions,
size_t current_dim);

/*!
* \brief This is a recursive function used to
* copy c-style row major memory to fortran
* column major memory
* \param f_data A pointer to the col major memory space
* \param c_data A pointer to the row major memory space
* \param dims The dimensions of the tensor
* \param dim_positions The current position in each
* dimension
* \param current_dim The index of the current dimension
*/
void _c_to_f(T* f_data,
const T* c_data,
const std::vector<size_t>& dims,
std::vector<size_t> dim_positions,
size_t current_dim);

/*!
* \brief Calculate the contiguous array position
* for a column major position
* \param dims The tensor dimensions
* \param dim_positions The current position for each
* dimension
* \returns The contiguous memory index position
*/
inline size_t _f_index(const std::vector<size_t>& dims,
const std::vector<size_t>& dim_positions);

/*!
* \brief Calculate the contiguous array position
* for a row major position
* \param dims The tensor dimensions
* \param dim_positions The current position for each dimension
* \returns The contiguous memory index position
*/
inline size_t _c_index(const std::vector<size_t>& dims,
const std::vector<size_t>& dim_positions);

/*!
* \brief Get the total number of bytes of the data
* \returns Total number of bytes of the data
Expand All @@ -305,12 +226,8 @@ class Tensor : public TensorBase
/*!
* \brief Memory allocated for c nested tensor memory views
*/
SharedMemoryList<T*> _c_mem_views;
SharedMemoryList<T*> _mem_views;

/*!
* \brief Memory allocated for f nested tensor memory views
*/
SharedMemoryList<T> _f_mem_views;
};

#include "tensor.tcc"
Expand Down
147 changes: 5 additions & 142 deletions include/tensor.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ Tensor<T>::Tensor(const Tensor<T>& tensor) : TensorBase(tensor)
return;

_set_tensor_data(tensor._data, tensor._dims, SRMemLayoutContiguous);
_c_mem_views = tensor._c_mem_views;
_f_mem_views = tensor._f_mem_views;
_mem_views = tensor._mem_views;
}

// Tensor move constructor
template <class T>
Tensor<T>::Tensor(Tensor<T>&& tensor) : TensorBase(std::move(tensor))
{
_c_mem_views = std::move(tensor._c_mem_views);
_f_mem_views = std::move(tensor._f_mem_views);
_mem_views = std::move(tensor._mem_views);
}

// Tensor copy assignment operator
Expand All @@ -75,8 +73,7 @@ Tensor<T>& Tensor<T>::operator=(const Tensor<T>& tensor)
// Deep copy tensor data
TensorBase::operator=(tensor);
_set_tensor_data(tensor._data, tensor._dims, SRMemLayoutContiguous);
_c_mem_views = tensor._c_mem_views;
_f_mem_views = tensor._f_mem_views;
_mem_views = tensor._mem_views;

// Done
return *this;
Expand All @@ -92,8 +89,7 @@ Tensor<T>& Tensor<T>::operator=(Tensor<T>&& tensor)

// Move data
TensorBase::operator=(std::move(tensor));
_c_mem_views = std::move(tensor._c_mem_views);
_f_mem_views = std::move(tensor._f_mem_views);
_mem_views = std::move(tensor._mem_views);

// Done
return *this;
Expand Down Expand Up @@ -132,11 +128,6 @@ void* Tensor<T>::data_view(const SRMemoryLayout mem_layout)
pointers so that the caller can cast
to a nested pointer structure and index
with multiple [] operators.
3) MemoryLayout::fortran_contiguous :
The internal row major format will
be copied into a new allocated memory
space that is the transpose (column major)
of the row major layout.
*/

void* ptr = NULL;
Expand All @@ -145,10 +136,6 @@ void* Tensor<T>::data_view(const SRMemoryLayout mem_layout)
case SRMemLayoutContiguous:
ptr = _data;
break;
case SRMemLayoutFortranContiguous:
ptr = _f_mem_views.allocate_bytes(_n_data_bytes());
_c_to_f_memcpy((T*)ptr, (T*)_data, _dims);
break;
case SRMemLayoutNested:
_build_nested_memory(&ptr,
_dims.data(),
Expand Down Expand Up @@ -196,9 +183,6 @@ void Tensor<T>::fill_mem_space(void* data,

// Copy over the data
switch (mem_layout) {
case SRMemLayoutFortranContiguous:
_c_to_f_memcpy((T*)data, (T*)_data, _dims);
break;
case SRMemLayoutContiguous:
std::memcpy(data, _data, _n_data_bytes());
break;
Expand Down Expand Up @@ -275,7 +259,7 @@ T* Tensor<T>::_build_nested_memory(void** data,
"_build_nested_memory");
}
if (n_dims > 1) {
T** new_data = _c_mem_views.allocate(dims[0]);
T** new_data = _mem_views.allocate(dims[0]);
if (new_data == NULL)
throw SRBadAllocException("nested memory for tensor");
(*data) = reinterpret_cast<void*>(new_data);
Expand Down Expand Up @@ -310,9 +294,6 @@ void Tensor<T>::_set_tensor_data(const void* src_data,
case SRMemLayoutContiguous:
std::memcpy(_data, src_data, n_bytes);
break;
case SRMemLayoutFortranContiguous:
_f_to_c_memcpy((T*)_data, (const T*)src_data, dims);
break;
case SRMemLayoutNested:
_copy_nested_to_contiguous(
src_data, dims.data(), dims.size(), _data);
Expand All @@ -329,123 +310,5 @@ size_t Tensor<T>::_n_data_bytes()
{
return num_values() * sizeof(T);
}
// Copy a fortran memory space layout (col major) to a
// c-style array memory space (row major)
template <class T>
void Tensor<T>::_f_to_c_memcpy(T* c_data,
const T* f_data,
const std::vector<size_t>& dims)
{
if (c_data == NULL || f_data == NULL) {
throw SRRuntimeException("Invalid buffer suppplied to _f_to_c_memcpy");
}
std::vector<size_t> dim_positions(dims.size(), 0);
_f_to_c(c_data, f_data, dims, dim_positions, 0);
}

// Copy a c-style array memory space (row major) to a
// fortran memory space layout (col major)
template <class T>
void Tensor<T>::_c_to_f_memcpy(T* f_data,
const T* c_data,
const std::vector<size_t>& dims)
{
if (c_data == NULL || f_data == NULL) {
throw SRRuntimeException("Invalid buffer suppplied to _c_to_f_memcpy");
}
std::vector<size_t> dim_positions(dims.size(), 0);
_c_to_f(f_data, c_data, dims, dim_positions, 0);
}

// Copy fortran column major memory to c-style row major memory recursively
template <class T>
void Tensor<T>::_f_to_c(T* c_data,
const T* f_data,
const std::vector<size_t>& dims,
std::vector<size_t> dim_positions,
size_t current_dim)
{
if (c_data == NULL || f_data == NULL) {
throw SRRuntimeException("Invalid buffer suppplied to _f_to_c");
}
size_t start = dim_positions[current_dim];
size_t end = dims[current_dim];
bool more_dims = (current_dim + 1 != dims.size());

for (size_t i = start; i < end; i++) {
if (more_dims)
_f_to_c(c_data, f_data, dims, dim_positions,
current_dim + 1);
else {
size_t f_index = _f_index(dims, dim_positions);
size_t c_index = _c_index(dims, dim_positions);
c_data[c_index] = f_data[f_index];
}
dim_positions[current_dim]++;
}
}

// Copy c-style row major memory to fortran column major memory recursively
template <class T>
void Tensor<T>::_c_to_f(T* f_data,
const T* c_data,
const std::vector<size_t>& dims,
std::vector<size_t> dim_positions,
size_t current_dim)
{
if (c_data == NULL || f_data == NULL) {
throw SRRuntimeException("Invalid buffer suppplied to _f_to_c");
}
size_t start = dim_positions[current_dim];
size_t end = dims[current_dim];
bool more_dims = (current_dim + 1 != dims.size());

for (size_t i = start; i < end; i++) {
if (more_dims) {
_c_to_f(f_data, c_data, dims, dim_positions,
current_dim + 1);
}
else {
size_t f_index = _f_index(dims, dim_positions);
size_t c_index = _c_index(dims, dim_positions);
f_data[f_index] = c_data[c_index];
}
dim_positions[current_dim]++;
}
}

// Calculate the contiguous array position for a column major position
template <class T>
inline size_t Tensor<T>::_f_index(const std::vector<size_t>& dims,
const std::vector<size_t>& dim_positions)
{
size_t position = 0;

for (size_t k = 0; k < dims.size(); k++) {
size_t sum_product = dim_positions[k];
for (size_t m = 0; m < k; m++) {
sum_product *= dims[m];
}
position += sum_product;
}
return position;
}

// Calculate the contiguous array position for a row major position
template <class T>
inline size_t Tensor<T>::_c_index(const std::vector<size_t>& dims,
const std::vector<size_t>& dim_positions)
{
size_t position = 0;

for(size_t k = 0; k < dims.size(); k++) {
size_t sum_product = dim_positions[k];
for(size_t m = k + 1; m < dims.size(); m++) {
sum_product *= dims[m];
}
position += sum_product;
}
return position;
}

#endif // SMARTREDIS_TENSOR_TCC
3 changes: 1 addition & 2 deletions src/cpp/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,8 +425,7 @@ void Client::unpack_tensor(const std::string& name,
std::vector<size_t> reply_dims = GetTensorCommand::get_dims(reply);

// Make sure we have the right dims to unpack into (Contiguous case)
if (mem_layout == SRMemLayoutContiguous ||
mem_layout == SRMemLayoutFortranContiguous) {
if (mem_layout == SRMemLayoutContiguous) {
size_t total_dims = 1;
for (size_t i = 0; i < reply_dims.size(); i++) {
total_dims *= reply_dims[i];
Expand Down
1 change: 1 addition & 0 deletions src/cpp/redis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,7 @@ void Redis::set_model_chunk_size(int chunk_size)
inline CommandReply Redis::_run(const Command& cmd)
{
for (int i = 1; i <= _command_attempts; i++) {
std::cout<<"Attempt # "<<i<<std::endl;
try {
// Run the command
CommandReply reply = _redis->command(cmd.cbegin(), cmd.cend());
Expand Down
3 changes: 1 addition & 2 deletions tests/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ list(APPEND EXECUTABLES
client_test_put_get_3D
client_test_put_get_3D_static_values
client_test_put_get_contiguous_3D
client_test_put_get_transpose_3D
client_test_put_get_2D
client_test_put_get_1D
client_test_mnist
Expand All @@ -103,7 +102,7 @@ list(APPEND EXECUTABLES
foreach(EXECUTABLE ${EXECUTABLES})
add_executable(${EXECUTABLE}_cpp_test
${EXECUTABLE}.cpp
)
)make t
set_target_properties(${EXECUTABLE}_cpp_test PROPERTIES
OUTPUT_NAME ${EXECUTABLE}
)
Expand Down
Loading
Loading