J
J
Julia2016-09-13 10:27:54
C++ / C#
Julia, 2016-09-13 10:27:54

How to calculate sum using thread?

Hi all.
I have a sum
S = 1 / n*n
n from 1 to 100001
Please look at my pathetic attempt to calculate this using streams. The number of threads is passed in the header bool sum(unsigned int number_threads)
I don't know where to set the approximation limits from 1 to 1000001.
Task Calculate
the sum from n0 till n1 in parallel, fast and thread-safe . (Without main-Thread)

#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <cstdlib>
#include <algorithm>
 
#include <mutex>
using namespace std;
mutex m_sum;
 
void Work( int &sum , int n0, int n1 ){  // передавать границы в Work ????? 
    unique_lock<mutex> lck {m_sum}; 
    double tmp ; 
    for ( int j = n0 ; j>n1 ; j++) {
         tmp = 1/(n*n);
            sum=sum+tmp; } ///// _-------> ???? 
    lck.unlock();}
    
bool sum(unsigned int number_threads)
 
{
    const double pi = 3.14159265358979323846264338327;
    const double exact = pi*pi/6.0; //// ___ проверка решения 
    
    unsigned int n0 = 1;
    unsigned   int n1 = 1000001;  // --- > собственно границы
    double s=0;
 
    thread* t = new thread[number_threads];
    
   for(  unsigned int i = 0; i < number_threads; ++i){
      
        t[i] = thread(Work, std::ref(s), n0, n1);
    }
    
      for(  unsigned int i = 0; i < number_threads; ++i){
        t[i].join();    }
    
    
    cout << "sum=" << s << " " << fabs(s-exact) << endl;
    return fabs(s-exact)<1e-4;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2016-09-13
@res2001

You count the total amount in each thread, but you need to transfer your own range of n0-n1 to each thread, which is different from the others. And get rid of mutexes - allocate for each thread its own place where it will add the sum. This will greatly increase performance. After all the threads have worked in the main one, it remains only to add up the amounts.
By the way, you can look in the direction of OpenMP - this library is well suited for such tasks.

J
Julia, 2016-09-14
@Julila

#include <iostream>
#include <thread>
#include <vector>
#include <cstdlib>
#include <algorithm>

#include <mutex>
using namespace std;
mutex m_sum;
void Work( double &sum ){ 	
    unsigned int n0 = 1;
    unsigned   int n1 = 1000001;
    double tmp; 
unique_lock<mutex> lck {m_sum}; 
 
  for (unsigned int j = n0 ; j<n1 ; j++) {
  tmp = 1.0/(j*j);
      double sum=sum+tmp; }
    cout << "b" ;	
  
  lck.unlock();}
  
bool sum(unsigned int number_threads)

{
    const double pi = 3.14159265358979323846264338327;
    const double exact = pi*pi/6.0;
    
       double s=0;

    thread* t = new thread[number_threads];
    
   for( long unsigned int i = 0; i < number_threads; ++i){
    
 	t[i] = thread(Work, std::ref(s) );
  }
  
    for( long unsigned int i = 0; i < number_threads; ++i){
 	t[i].join();
  }
 	delete []t;
 	cout << "sum=" << s;
    cout << "sum=" << s << " " << fabs(s-exact) << endl;
    
    return fabs(s-exact)<1e-4;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question