Multi-Threaded Implementation of Summation of 1/i
Keywords: multi-threaded implementation summation 1/i harmonic multi_thread syntax ok sum num_sum num_threads source

| Prev | Next |  |  |  |  |  |  |

Multi-Threaded Implementation of Summation of 1/i
Syntax
`ok = harmonic(sum, num_sum, num_threads)`
Summation
Multi-threaded computation of the summation that defines the harmonic series s = 1 + 1 / 2 + 1 / 3 + . . . + 1 / n
ok
This return value has prototype `
bool ok
` If this return value is false, an error occurred during `harmonic`.
sum
This argument has prototype `
double& sum
` The input value of the argument does not matter. Upon return it is the value of the summation; i.e. s .
num_sum
This argument has prototype `
size_t num_sum
` It specifies the number of terms in the summation; i.e. n .
num_threads
This argument has prototype `
size_t num_threads
` It specifies the number of threads that are available for this test. If it is zero, the test is run without a multi-threading environment.
Contents

harmonic_time.cpp | Timing Test of Multi-Threaded Summation of 1/i
harmonic_work.cpp | Multi-threading Sum of 1/i Utility Routines

Source `

    // general purpose multi-threading interface
    # include "team_thread.hpp"
    // special utilities for the harmonic problem
    # include "harmonic_work.hpp"

    bool harmonic(double& sum, size_t num_sum, size_t num_threads)
    {	// sum = 1/num_sum + 1/(num_sum-1) + ... + 1
    	bool ok = true;

    	// setup the work for num_threads_ threads
    	ok &= harmonic_setup(num_sum, num_threads);

    	// now do the work for each thread
    	if( num_threads > 0 )
    		team_work( harmonic_worker );
    	else	harmonic_worker();

    	// now combine the result for all the threads
    	ok &= harmonic_combine(sum);

    	return ok;
    }

`

Input File: multi_thread/harmonic.cpp