Skip to content
Open
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
4 changes: 3 additions & 1 deletion include/ambit/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Tensor
*
* Example successful use case:
* Tensor A = Tensor::build(CoreTensor, "A3", {4,5,6});
* A.at({0,0,0}) = 1.0; // Fill element
* A.at({0,0,0}) = 1.0; // Fill element
*
* Results:
* @return reference to the element, if tensor object supports it
Expand Down Expand Up @@ -517,6 +517,8 @@ class Tensor
public:
void reshape(const Dimension &dims);

void resize(const Dimension &dims);

// => Operator Overloading API <= //

LabeledTensor operator()(const string &indices) const;
Expand Down
108 changes: 108 additions & 0 deletions include/ambit/tensor_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* @BEGIN LICENSE
*
* ambit: C++ library for the implementation of tensor product calculations
* through a clean, concise user interface.
*
* Copyright (c) 2014-2017 Ambit developers.
*
* The copyrights for code used from other parties are included in
* the corresponding files.
*
* This file is part of ambit.
*
* Ambit is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3.
*
* Ambit is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with ambit; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @END LICENSE
*/

#if !defined(TENSOR_POOL_H)
#define TENSOR_POOL_H

#include <vector>

#include <ambit/tensor.h>

#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif

namespace ambit
{

class TempTensor;

class TensorPool
{
public:
// => Constructors <= //

/**
* Returns a temporary Core tensor. This function is thread safe.
*
* Results:
* @return a TempTensor object that will automatically make the
* tensor available to the pool upon destruction.
**/
TempTensor get_tensor();

/**
* Release a temporary Tensor
*
* Parameters:
* @param id the id of the Tensor
*
* Results:
* release a temporary tensor and make it available for a new call from
* get_tensor
**/
void release_tensor(size_t id);

/**
* Frees the TensorPool's internal memory allocation.
*/
void reset();

private:
std::vector<std::pair<bool, Tensor>> tensor_pool_;
};

class TempTensor
{
public:
// => Constructor <= //
TempTensor(size_t id, Tensor t, TensorPool *tp) : id_(id), t_(t), tp_(tp) {}
~TempTensor() { tp_->release_tensor(id_); }
Tensor tensor() { return t_; }

private:
size_t id_;
Tensor t_;
TensorPool *tp_;
};

namespace tensor_pool
{
void initialize();
void finalize();
TempTensor get_tensor();
} // namespace tensor_pool
} // namespace ambit

#if defined(__clang__)
#pragma clang diagnostic pop
#endif

#endif
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ set(TENSOR_SOURCES
tensor/slice.cc
tensor/sliced_tensor.cc
tensor/tensor.cc
tensor/tensor_pool.cc
tensor/tensorimpl.cc
tensor/timer.cc

Expand Down
15 changes: 15 additions & 0 deletions src/tensor/core/core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ void CoreTensorImpl::reshape(const Dimension &dims)
TensorImpl::reshape(dims);
}

void CoreTensorImpl::resize(const Dimension &dims, bool trim)
{
TensorImpl::reshape(dims);
// Requests that the vector capacity be at least enough to contain numel
// elements. If numel is greater than the current vector capacity, the
// function causes the container to reallocate its storage increasing its
// capacity to numel (or greater).

if (numel() > data_.size())
{
// TODO: implement trimming
data_.reserve(numel());
}
}

double CoreTensorImpl::norm(int type) const
{
double val = 0.0;
Expand Down
2 changes: 2 additions & 0 deletions src/tensor/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class CoreTensorImpl : public TensorImpl
// the strides in the slice codes.
void reshape(const Dimension &dims);

void resize(const Dimension &dims, bool trim = true);

vector<double> &data() { return data_; }
const vector<double> &data() const { return data_; }

Expand Down
Loading