Skip to content

Commit

Permalink
correlator: Switched compression function to std::vector and value se…
Browse files Browse the repository at this point in the history
…mantics.
  • Loading branch information
fweik committed Nov 27, 2017
1 parent 83e6bbf commit 7a16323
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/core/correlators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ std::vector<std::shared_ptr<Correlators::Correlator>> auto_update_correlators;

void auto_update() {
for (auto& c : auto_update_correlators) {
if (sim_time - c->last_update >c->dt *0.9999) {
if (sim_time - c->last_update() >c->dt *0.9999) {
c->get_data();
}
}
Expand Down
119 changes: 61 additions & 58 deletions src/core/correlators/Correlator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,51 @@

namespace Correlators {

int identity(double *input, unsigned int n_input, double *A,
unsigned int dim_A) {
if (n_input != dim_A) {
return 5;
}
for (unsigned i = 0; i < dim_A; i++) {
A[i] = input[i];
}
return 0;
}

/** The minimal version of compression function */
std::vector<double> compress_do_nothing(std::vector<double> const &A1,
std::vector<double> const &A2) {
return {};
}

/** Compress computing arithmetic mean: A_compressed=(A1+A2)/2 */
std::vector<double> compress_linear(std::vector<double> const &A1,
std::vector<double> const &A2) {
assert(A1.size() == A2.size());
std::vector<double> A_compressed(A1.size());

std::transform(A1.begin(), A1.end(), A2.begin(), A_compressed.begin(),
[](double a, double b) -> double { return 0.5 * (a + b); });

return A_compressed;
}

/** Compress discarding the 1st argument and return the 2nd */
std::vector<double> compress_discard1(std::vector<double> const &A1,
std::vector<double> const &A2) {
assert(A1.size() == A2.size());
std::vector<double> A_compressed(A2);
return A_compressed;
}

/** Compress discarding the 2nd argument and return the 1st */
std::vector<double> compress_discard2(std::vector<double> const &A1,
std::vector<double> const &A2) {
assert(A1.size() == A2.size());
std::vector<double> A_compressed(A1);
return A_compressed;
}

/* global variables */

/* Error codes */
Expand Down Expand Up @@ -108,8 +153,7 @@ int Correlator::get_correlation_time(double *correlation_time) {
}

Correlator::Correlator()
: t(0), finalized(0), autoupdate(0), initialized(0),
correlation_args{} {}
: t(0), finalized(0), autoupdate(0), initialized(0), correlation_args{} {}

void Correlator::initialize() {
unsigned int i, j, k;
Expand Down Expand Up @@ -326,12 +370,12 @@ int Correlator::get_data() {
// folding)
newest[i + 1] = (newest[i + 1] + 1) % (tau_lin + 1);
n_vals[i + 1] += 1;
(*compressA)(A[i][(newest[i] + 1) % (tau_lin + 1)].data(),
A[i][(newest[i] + 2) % (tau_lin + 1)].data(),
A[i + 1][newest[i + 1]].data(), dim_A);
(*compressB)(B[i][(newest[i] + 1) % (tau_lin + 1)].data(),
B[i][(newest[i] + 2) % (tau_lin + 1)].data(),
B[i + 1][newest[i + 1]].data(), dim_B);
A[i + 1][newest[i + 1]] =
(*compressA)(A[i][(newest[i] + 1) % (tau_lin + 1)],
A[i][(newest[i] + 2) % (tau_lin + 1)]);
B[i + 1][newest[i + 1]] =
(*compressB)(B[i][(newest[i] + 1) % (tau_lin + 1)],
B[i][(newest[i] + 2) % (tau_lin + 1)]);
}

newest[0] = (newest[0] + 1) % (tau_lin + 1);
Expand Down Expand Up @@ -361,8 +405,9 @@ int Correlator::get_data() {
for (j = 0; j < int(MIN(tau_lin + 1, n_vals[0])); j++) {
index_new = newest[0];
index_old = (newest[0] - j + tau_lin + 1) % (tau_lin + 1);
error = (corr_operation)(A[0][index_old].data(), dim_A, B[0][index_new].data(), dim_B,
temp, dim_corr, correlation_args);
error =
(corr_operation)(A[0][index_old].data(), dim_A, B[0][index_new].data(),
dim_B, temp, dim_corr, correlation_args);
if (error != 0)
return error;
n_sweeps[j]++;
Expand All @@ -389,7 +434,7 @@ int Correlator::get_data() {
}
}
free(temp);
last_update = sim_time;
m_last_update = sim_time;
return 0;
}

Expand Down Expand Up @@ -464,12 +509,10 @@ int Correlator::finalize() {
newest[i + 1] = (newest[i + 1] + 1) % (tau_lin + 1);
n_vals[i + 1] += 1;

(*compressA)(A[i][(newest[i] + 1) % (tau_lin + 1)].data(),
A[i][(newest[i] + 2) % (tau_lin + 1)].data(),
A[i + 1][newest[i + 1]].data(), dim_A);
(*compressB)(B[i][(newest[i] + 1) % (tau_lin + 1)].data(),
B[i][(newest[i] + 2) % (tau_lin + 1)].data(),
B[i + 1][newest[i + 1]].data(), dim_B);
(*compressA)(A[i][(newest[i] + 1) % (tau_lin + 1)],
A[i][(newest[i] + 2) % (tau_lin + 1)]);
(*compressB)(B[i][(newest[i] + 1) % (tau_lin + 1)],
B[i][(newest[i] + 2) % (tau_lin + 1)]);
}
newest[ll] = (newest[ll] + 1) % (tau_lin + 1);

Expand Down Expand Up @@ -502,7 +545,7 @@ void Correlator::start_auto_update() {
if (update_frequency > 0) {
correlations_autoupdate = 1;
autoupdate = 1;
last_update = sim_time;
m_last_update = sim_time;
} else {
throw std::runtime_error(
"Could not start autoupdate: update frequency not set");
Expand Down Expand Up @@ -537,46 +580,6 @@ std::vector<double> Correlator::get_correlation() {
return res;
}

int identity(double *input, unsigned int n_input, double *A,
unsigned int dim_A) {
if (n_input != dim_A) {
return 5;
}
for (unsigned i = 0; i < dim_A; i++) {
A[i] = input[i];
}
return 0;
}

int compress_do_nothing(double *A1, double *A2, double *A_compressed,
unsigned int dim_A) {
return 0;
}

int compress_linear(double *A1, double *A2, double *A_compressed,
unsigned int dim_A) {
unsigned int i;
for (i = 0; i < dim_A; i++)
A_compressed[i] = 0.5 * (A1[i] + A2[i]);
return 0;
}

int compress_discard1(double *A1, double *A2, double *A_compressed,
unsigned int dim_A) {
unsigned int i;
for (i = 0; i < dim_A; i++)
A_compressed[i] = A2[i];
return 0;
}

int compress_discard2(double *A1, double *A2, double *A_compressed,
unsigned int dim_A) {
unsigned int i;
for (i = 0; i < dim_A; i++)
A_compressed[i] = A1[i];
return 0;
}

int obs_nothing(void *params, double *A, unsigned int n_A) { return 0; }

int scalar_product(double *A, unsigned int dim_A, double *B, unsigned int dim_B,
Expand Down
64 changes: 22 additions & 42 deletions src/core/correlators/Correlator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,29 @@ class Correlator {
void start_auto_update();
void stop_auto_update();

double last_update() const {
return m_last_update;
}

std::string compressA_name;
std::string compressB_name;
int autoupdate;
std::string corr_operation_name;

int initialized;
int n_result; // the total number of result values

std::shared_ptr<Observables::Observable> A_obs;
std::shared_ptr<Observables::Observable> B_obs;

private:
// Convenience pointers to our stored data
// indices: A[level][tau_i][component]
std::vector<int> tau; // time differences
boost::multi_array<std::vector<double>, 2> A;
boost::multi_array<std::vector<double>, 2> B;

boost::multi_array<double, 2> result; // output quantity
int n_result; // the total number of result values

// The actual allocated storage space
std::vector<unsigned int>
Expand All @@ -266,35 +281,21 @@ class Correlator {
std::vector<double> B_accumulated_average; // all B values are added up here
unsigned int n_data; // a counter to calculated averages and variances

int is_from_file;
int autoupdate;
double last_update;

std::string compressA_name;
std::string compressB_name;
std::string corr_operation_name;

int initialized;
double m_last_update;

std::shared_ptr<Observables::Observable> A_obs;
std::shared_ptr<Observables::Observable> B_obs;

private:
unsigned int dim_A; // dimensionality of A
unsigned int dim_B;
// Functions producing observables A and B from the input data
int A_obs_id;
int B_obs_id;

int (*corr_operation)(double *A, unsigned int dim_A, double *B,
unsigned int dim_B, double *C, unsigned int dim_corr,
Vector3d);

using compression_function = std::vector<double> (*)(
std::vector<double> const &A1, std::vector<double> const &A2);

// compressing functions
int (*compressA)(double *A1, double *A2, double *A_compressed,
unsigned int dim_A);
int (*compressB)(double *B1, double *B2, double *A_compressed,
unsigned int dim_B);
compression_function compressA;
compression_function compressB;
};

extern int correlations_autoupdate;
Expand All @@ -308,27 +309,6 @@ extern const char double_correlation_get_data_errors[][64];
int identity(double *input, unsigned int n_input, double *A,
unsigned int dim_A);

/* *************************
*
* Functions for compressing data
*
**************************/
/** The minimal version of compression function */
int compress_do_nothing(double *A1, double *A2, double *A_compressed,
unsigned int dim_A);

/** Compress computing arithmetic mean: A_compressed=(A1+A2)/2 */
int compress_linear(double *A1, double *A2, double *A_compressed,
unsigned int dim_A);

/** Compress discarding the 1st argument and return the 2nd */
int compress_discard1(double *A1, double *A2, double *A_compressed,
unsigned int dim_A);

/** Compress discarding the 2nd argument and return the 1st */
int compress_discard2(double *A1, double *A2, double *A_compressed,
unsigned int dim_A);

/* *************************
*
* Functions for correlation operations
Expand Down
5 changes: 3 additions & 2 deletions src/script_interface/correlators/Correlator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Correlator : public ScriptInterfaceBase {
throw std::runtime_error(
"Correlator cannot be changed after initial setup");
}

if ((name == "obs1") || (name == "obs2")) {
auto obs_ptr = get_value<std::shared_ptr<Observables::Observable>>(value);

Expand Down Expand Up @@ -102,10 +103,12 @@ class Correlator : public ScriptInterfaceBase {
std::shared_ptr<::Correlators::Correlator> correlator() {
return m_correlator;
}

void check_if_initialized() {
if (!m_correlator->initialized)
throw std::runtime_error("The correlator has not yet been initialied.");
}

virtual Variant call_method(std::string const &method,
VariantMap const &parameters) override {
check_if_initialized();
Expand All @@ -130,8 +133,6 @@ class Correlator : public ScriptInterfaceBase {
return m_correlator->n_result;
if (method == "dim_corr")
return m_correlator->dim_corr;
if (method == "hierarchy_depth")
return m_correlator->hierarchy_depth;

return {};
}
Expand Down

0 comments on commit 7a16323

Please sign in to comment.