imate
C++/CUDA Reference
ConvergenceTools< DataType > Class Template Reference

A static class to compute the trace of implicit matrix functions using stochastic Lanczos quadrature method. This class acts as a templated namespace, where the member methods is public and static. The internal private member functions are also static. More...

#include <convergence_tools.h>

Static Public Member Functions

static FlagType check_convergence (DataType **samples, const IndexType min_num_samples, const IndexType num_inquiries, const IndexType *processed_samples_indices, const IndexType num_processed_samples, const DataType confidence_level, const DataType error_atol, const DataType error_rtol, DataType *error, IndexType *num_samples_used, FlagType *converged)
 Checks if the standard deviation of the set of the cumulative averages of trace estimators converged below the given tolerance. More...
 
static void average_estimates (const DataType confidence_level, const DataType outlier_significance_level, const IndexType num_inquiries, const IndexType max_num_samples, const IndexType *num_samples_used, const IndexType *processed_samples_indices, DataType **samples, IndexType *num_outliers, DataType *trace, DataType *error)
 Averages the estimates of trace. Removes outliers and reevaluates the error to take into account for the removal of the outliers. More...
 

Detailed Description

template<typename DataType>
class ConvergenceTools< DataType >

A static class to compute the trace of implicit matrix functions using stochastic Lanczos quadrature method. This class acts as a templated namespace, where the member methods is public and static. The internal private member functions are also static.

See also
Diagonalization

Definition at line 36 of file convergence_tools.h.

Member Function Documentation

◆ average_estimates()

template<typename DataType >
void ConvergenceTools< DataType >::average_estimates ( const DataType  confidence_level,
const DataType  outlier_significance_level,
const IndexType  num_inquiries,
const IndexType  max_num_samples,
const IndexType num_samples_used,
const IndexType processed_samples_indices,
DataType **  samples,
IndexType num_outliers,
DataType *  trace,
DataType *  error 
)
static

Averages the estimates of trace. Removes outliers and reevaluates the error to take into account for the removal of the outliers.

Note
The elimination of outliers does not affect the elements of samples array, rather it only affects the reevaluation of trac and error arrays.
Parameters
[in]confidence_levelThe confidence level of the error, which is a number between 0 and 1. This affects the scale of error.
[in]outlier_significance_levelOne minus the confidence level of the uncertainty band of the outlier. This is a number between 0 and 1. Confidence level of outleir and significance level of outlier are commlement of each other.
[in]num_inquiriesThe number of batches of parameters. This function computes num_inquiries values of trace corresponding to different batch of parameters of the linear operator A. Hence, the number of output trace is num_inquiries. Hence, it is the number of columns of the output array samples.
[in]max_num_samplesThe number of times that the trace estimation is repeated. The output trace value is the average of the samples. Hence, this is the number of rows of the output array samples. Larger number of samples leads to a better trace estimation. The computational const linearly increases with number of samples.
[in]num_samples_used1D array of the size of the number of columns of samples. Each element indicates how many iterations were used till convergence is reached for each column of the samples. The number of iterations should be a number between min_num_samples and max_num_samples.
[in]processed_samples_indicesA 1D array indicating the processing order of rows of the samples. In paralleli processing, this order of processing the rows of samples is not necessarly sequential.
[out]samples2D array of all estimated trace samples. The shape of this array is (max_num_samples*num_inquiries). The average of the rows is also given in trace array.
[out]num_outliers1D array with the size of number of columns of samples. Each element indicates how many rows of the samples array were outliers and were removed during averaging rows of samples.
[out]traceThe output trace of size num_inquiries. These values are the average of the rows of samples array.
[out]errorThe error of estimation of trace, which is the standard deviation of the rows of samples array. The size of this array is num_inquiries.

Definition at line 256 of file convergence_tools.cpp.

267 {
268  IndexType i;
269  IndexType j;
270  DataType summand;
271  DataType mean;
272  DataType std;
273  DataType mean_discrepancy;
274  DataType outlier_half_interval;
275 
276  // Flag which samples are outliers
277  FlagType* outlier_indices = new FlagType[max_num_samples];
278 
279  // Quantile of normal distribution (usually known as the "z" coefficient)
280  DataType error_z_score = sqrt(2) * erf_inv(confidence_level);
281 
282  // Confidence level of outlier is the complement of significance level
283  DataType outlier_confidence_level = 1.0 - outlier_significance_level;
284 
285  // Quantile of normal distribution area where is not considered as outlier
286  DataType outlier_z_score = sqrt(2.0) * erf_inv(outlier_confidence_level);
287 
288  for (j=0; j < num_inquiries; ++j)
289  {
290  // Initialize outlier indices for each column of samples
291  for (i=0; i < max_num_samples; ++i)
292  {
293  outlier_indices[i] = 0;
294  }
295  num_outliers[j] = 0;
296 
297  // Compute mean of the j-th column
298  summand = 0.0;
299  for (i=0; i < num_samples_used[j]; ++i)
300  {
301  summand += samples[processed_samples_indices[i]][j];
302  }
303  mean = summand / num_samples_used[j];
304 
305  // Compute std of the j-th column
306 
307  if (num_samples_used[j] > 1)
308  {
309  summand = 0.0;
310  for (i=0; i < num_samples_used[j]; ++i)
311  {
312  mean_discrepancy = \
313  samples[processed_samples_indices[i]][j] - mean;
314  summand += mean_discrepancy * mean_discrepancy;
315  }
316  std = sqrt(summand / (num_samples_used[j] - 1.0));
317  }
318  else
319  {
320  std = INFINITY;
321  }
322 
323  // Outlier half interval
324  outlier_half_interval = outlier_z_score * std;
325 
326  // Difference of each element from
327  for (i=0; i < num_samples_used[j]; ++i)
328  {
329  mean_discrepancy = samples[processed_samples_indices[i]][j] - mean;
330  if (std::abs(mean_discrepancy) > outlier_half_interval)
331  {
332  // Outlier detected
333  outlier_indices[i] = 1;
334  num_outliers[j] += 1;
335  }
336  }
337 
338  // Reevaluate mean but leave out outliers
339  summand = 0.0;
340  for (i=0; i < num_samples_used[j]; ++i)
341  {
342  if (outlier_indices[i] == 0)
343  {
344  summand += samples[processed_samples_indices[i]][j];
345  }
346  }
347  mean = summand / (num_samples_used[j] - num_outliers[j]);
348 
349  // Reevaluate std but leave out outliers
350  if (num_samples_used[j] > 1 + num_outliers[j])
351  {
352  summand = 0.0;
353  for (i=0; i < num_samples_used[j]; ++i)
354  {
355  if (outlier_indices[i] == 0)
356  {
357  mean_discrepancy = \
358  samples[processed_samples_indices[i]][j] - mean;
359  summand += mean_discrepancy * mean_discrepancy;
360  }
361  }
362  std = sqrt(summand/(num_samples_used[j] - num_outliers[j] - 1.0));
363  }
364  else
365  {
366  std = INFINITY;
367  }
368 
369  // trace and its error
370  trace[j] = mean;
371  error[j] = error_z_score * std / \
372  sqrt(num_samples_used[j] - num_outliers[j]);
373  }
374 
375  delete[] outlier_indices;
376 }
double erf_inv(const double x)
Inverse error function.
int FlagType
Definition: types.h:68
int IndexType
Definition: types.h:65

References erf_inv().

Referenced by cTraceEstimator< DataType >::c_trace_estimator(), and cuTraceEstimator< DataType >::cu_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_convergence()

template<typename DataType >
FlagType ConvergenceTools< DataType >::check_convergence ( DataType **  samples,
const IndexType  min_num_samples,
const IndexType  num_inquiries,
const IndexType processed_samples_indices,
const IndexType  num_processed_samples,
const DataType  confidence_level,
const DataType  error_atol,
const DataType  error_rtol,
DataType *  error,
IndexType num_samples_used,
FlagType converged 
)
static

Checks if the standard deviation of the set of the cumulative averages of trace estimators converged below the given tolerance.

The convergence criterion for each trace inquiry is if:

            standard_deviation < max(rtol * average[i], atol)

        where \c rtol and \c atol are relative and absolute tolerances,
        respectively. If this criterion is satisfied for *all* trace
        inquiries, this function returns \c 1, otherwise \c 0.
Parameters
[in]samplesA 2D array of the estimated trace. This array has the shape (max_num_samples,num_inquiries). Each column of this array is the estimated trace values for a different trace inquiry. The rows are Monte-Carlo samples of the estimation of trace. The cumulative average of the rows are expected to converge. Note that because of parallel processing, the rows of this array are not filled sequentially. The list of filled rows are stored in processed_samples_indices.
[in]min_num_samplesMinimum number of sample iterations.
[in]num_inquiriesNumber of columns of samples array.
[in]processed_samples_indicesA 1D array indicating the processing order of rows of the samples. In parallel processing, this order of processing the rows of samples is not necessarly sequential.
[in]num_processed_samplesA counter that keeps track of how many samples were processed so far in the iterations.
[in]confidence_levelThe confidence level of the error, which is a number between 0 and 1. This affects the scale of error.
[in]error_atolAbsolute tolerance criterion to be compared with the standard deviation of the cumulative averages. If error_atol is zero, it is ignored, and only rtol is used.
[in]error_rtolRelative tolerance criterion to be compared with the standard deviation of the cumulative averages. If error_rtol is zero, it is ignored, and only error_atol is used.
[out]errorThe error of estimation of trace, which is the standard deviation of the rows of samples array. The size of this array is num_inquiries.
[out]num_samples_used1D array of the size of the number of columns of samples. Each element indicates how many iterations were used till convergence is reached for each column of the samples. The number of iterations should be a number between min_num_samples and max_num_samples.
[out]converged1D array of the size of the number of columns of samples. Each element indicates which column of samples has converged to the tolerance criteria. Normally, if the num_samples_used is less than max_num_samples, it indicates that the convergence has reached. the rows of samples array. The size of this array is num_inquiries.
Returns
A signal to indicate the status of computation:
  • 1 indicates successful convergence within the given tolerances was met. Convergence is achieved when all elements of convergence array are below convergence_atol or convergence_rtol times trace.
  • 0 indicates the convergence criterion was not met for at least one of the trace inquiries.

Definition at line 95 of file convergence_tools.cpp.

107 {
108  FlagType all_converged;
109  IndexType j;
110 
111  // If number of processed samples are not enough, set to not converged yet.
112  // This is essential since in the first few iterations, the standard
113  // deviation of the cumulative averages are still too small.
114  if (num_processed_samples < min_num_samples)
115  {
116  // Skip computing error. Fill outputs with trivial initial values
117  for (j=0; j < num_inquiries; j++)
118  {
119  error[j] = INFINITY;
120  converged[j] = 0;
121  num_samples_used[j] = num_processed_samples;
122  }
123  all_converged = 0;
124  return all_converged;
125  }
126 
127  IndexType i;
128  DataType summand;
129  DataType mean;
130  DataType std;
131  DataType data;
132 
133  // Quantile of normal distribution (usually known as the "z" coefficient)
134  DataType standard_z_score = sqrt(2) * \
135  static_cast<DataType>(erf_inv(static_cast<double>(confidence_level)));
136 
137  // For each column of samples, compute error of all processed rows
138  for (j=0; j < num_inquiries; ++j)
139  {
140  // Do not check convergence if j-th column already converged
141  if (converged[j] == 0)
142  {
143  // mean of j-th column using all processed rows of j-th column
144  summand = 0.0;
145  for (i=0; i < num_processed_samples; ++i)
146  {
147  summand += samples[processed_samples_indices[i]][j];
148  }
149  mean = summand / num_processed_samples;
150 
151  // std of j-th column using all processed rows of j-th column
152  if (num_processed_samples > 1)
153  {
154  summand = 0.0;
155  for (i=0; i < num_processed_samples; ++i)
156  {
157  data = samples[processed_samples_indices[i]][j];
158  summand += (data - mean) * (data - mean);
159  }
160  std = sqrt(summand / (num_processed_samples - 1.0));
161  }
162  else
163  {
164  std = INFINITY;
165  }
166 
167  // Compute error based of std and confidence level
168  error[j] = standard_z_score * std / sqrt(num_processed_samples);
169 
170  // Check error with atol and rtol to find if j-th column converged
171  if (error[j] < std::max(error_atol, error_rtol*mean))
172  {
173  converged[j] = 1;
174  }
175 
176  // Update how many samples used so far to average j-th column
177  num_samples_used[j] = num_processed_samples;
178  }
179  }
180 
181  // Check convergence is reached for all columns (all inquiries)
182  all_converged = 1;
183  for (j=0; j < num_inquiries; ++j)
184  {
185  if (converged[j] == 0)
186  {
187  // The j-th column not converged.
188  all_converged = 0;
189  break;
190  }
191  }
192 
193  return all_converged;
194 }

References erf_inv().

Referenced by cTraceEstimator< DataType >::c_trace_estimator(), and cuTraceEstimator< DataType >::cu_trace_estimator().

Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following files: