harmonic_common

View page source

Common Variables Used by Multi-threading Sum of 1/i

Purpose

This source code defines the common include files, defines, and variables that are used by the summation that defines the harmonic series

\[1 + 1/2 + 1/3 + ... + 1/n\]

Source

# include <cppad/cppad.hpp>
# include "harmonic.hpp"
# include "team_thread.hpp"
# define MAX_NUMBER_THREADS 48

namespace {
    using CppAD::thread_alloc; // fast multi-threadeding memory allocator

    // Number of threads, set by previous call to harmonic_time
    // (zero means one thread with no multi-threading setup)
    size_t num_threads_ = 0;

    // value of mega_sum, set by previous call to harmonic_time.
    size_t mega_sum_;

    // structure with information for one thread
    typedef struct {
        // index to start summation at (worker input)
        // set by previous call to harmonic_setup
        size_t start;
        // index to end summation at (worker input)
        // set by previous call to harmonic_setup
        size_t stop;
        // summation for this thread
        // set by worker
        double sum;
        // false if an error occurs, true otherwise
        // set by worker
        bool   ok;
    } work_one_t;

    // vector with information for all threads
    // (use pointers instead of values to avoid false sharing)
    work_one_t* work_all_[MAX_NUMBER_THREADS];
}