Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9208133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:33:04+00:00 2026-06-18T00:33:04+00:00

My C++ code evaluates very large integrals on timeseries data (t2 >> t1). The

  • 0

My C++ code evaluates very large integrals on timeseries data (t2 >> t1). The integrals are fixed length and currently stored in [m x 2] column array of doubles. Column 1 is time. Column 2 is the signal that’s being integrated. The code is running on a quadcore or 8 core machine.

For a machine with k cores, I want to:

  • Spin off k-1 worker processes (one for each of the remaining cores) to evaluate portions of the integral (trapezoidal integrations) and return their results to the waiting master thread.
  • Achieve the above without deep copying portions of the original array.
  • Implement C++11 async template for portability

How can I achieve the above without hardcoding the number of available cores?

I am Currently using VS 2012.

Update for Clarity:

For example, here’s the rough psuedo-code

data is [100000,2] double

result = MyIntegrator(data[1:50000,1:2]) + MyIntegrator(data[50001:100000, 1:2]); 

I need the MyIntegrator() functions to be evaluated in separate threads. The master thread waits for the two results.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T00:33:05+00:00Added an answer on June 18, 2026 at 12:33 am

    Here is source that does a multi-threaded integration of the problem.

    #include <vector>
    #include <memory>
    #include <future>
    #include <iterator>
    #include <iostream>
    
    struct sample {
      double duration;
      double value;
    };
    typedef std::pair<sample*, sample*> data_range;
    sample* begin( data_range const& r ) { return r.first; }
    sample* end( data_range const& r ) { return r.second; }
    
    typedef std::unique_ptr< std::future< double > > todo_item;
    
    double integrate( data_range r ) {
      double total = 0.;
      for( auto&& s:r ) {
        total += s.duration * s.value;
      }
      return total;
    }
    
    todo_item threaded_integration( data_range r ) {
      return todo_item( new std::future<double>( std::async( integrate, r )) );
    }
    double integrate_over_threads( data_range r, std::size_t threads ) {
      if (threads > std::size_t(r.second-r.first))
        threads = r.second-r.first;
      if (threads == 0)
        threads = 1;
      sample* begin = r.first;
      sample* end = r.second;
    
      std::vector< std::unique_ptr< std::future< double > > > todo_list;
    
      sample* highwater = begin;
    
      while (highwater != end) {
        sample* new_highwater = (end-highwater)/threads+highwater;
        --threads;
        todo_item item = threaded_integration( data_range(highwater, new_highwater) );
        todo_list.push_back( std::move(item) );
        highwater = new_highwater;
      }
      double total = 0.;
      for (auto&& item: todo_list) {
        total += item->get();
      }
      return total;
    }
    
    sample data[5] = {
      {1., 1.},
      {1., 2.},
      {1., 3.},
      {1., 4.},
      {1., 5.},
    };
    int main() {
      using std::begin; using std::end;
      double result = integrate_over_threads( data_range( begin(data), end(data) ), 2 );
      std::cout << result << "\n";
    }
    

    it requires some modification to read data in exactly the format you specified.

    But you can call it with std::thread::hardware_concurrency() as the number of threads, and it should work.

    (In particular, to keep it simple, I have pairs of (duration, value) rather than (time, value), but that is just a minor detail).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I am writing a very simple C code of dynamic 2D array declaration
I've the following code.if i give control_word as 6 why if condition evaluates to
The following code: var constant = Expression.Constant(find me, typeof(string)); // memberExpression evaluates to a
Here is code of very simple expression evaluator using IronRuby public class BasicRubyExpressionEvaluator {
I am currently using a code construction like this: string line; using (System.IO.StreamReader file
I need to have very high-performance loop going over large datasets. I need to
i am very new in c programming. i am using a c code from
I am very new to C programming. I'm using C code from the book
I'm trying to create some code that is very user friendly to someone not
I have a problem with a very simple piece of code written in Javascript,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.