寻找具有螺纹功能的二维数组最大的记录 [英] finding largest entry in 2d array with threaded function

查看:141
本文介绍了寻找具有螺纹功能的二维数组最大的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎C ++编写的是准确的,比我的非线程版本快线程程序。

I'm struggling to write a threaded program in c++ that is accurate and faster than my non-threaded version.

我发现在随机双打的二维数组里最大的记录。

I'm finding the largest entry in a 2d array of random doubles.

下面是一般code:

void getLargest(double** anArray, double largestEntry, int dimLower, int dimUpper, int dim) {

    for (int i = dimLower; i < dimUpper; i++) {
        for (int  j = 0; j < dim; j++) {
            if (anArray[i][j] > largestEntry) {
                largestEntry = anArray[i][j]; 
            }
        }
    }

}
int main(){

  // Seed the random number generator
  srand( time(NULL));

  // 2D array dimension
  int dim = 30000;

  // Specify max values
  double max = (double) (dim * dim * dim);
  double min = (double) (dim * dim * dim * -1.0);

  double t1 = get_wallTime();
  // Create a 2D array
  double **myArray = new double*[dim];
  for (int i=0; i<dim; i++){
    myArray[i] = new double[dim];
    for (int j=0; j<dim; j++){
      // generate random number
      myArray[i][j] = genRandNum(min, max);
    }
  }

  double largestEntry = 0.0;
  int portion = dim / 5;
  std::future<void> thread1 = std::async (std::launch::async, getLargest, myArray, largestEntry, 0, portion, dim);
  thread1.get();

  std::future<void> thread2 = std::async (std::launch::async, getLargest, myArray, largestEntry, portion, (portion * 2), dim);
  thread2.get();

  std::future<void> thread3 = std::async (std::launch::async, getLargest, myArray, largestEntry, (portion * 2), (portion * 3), dim);
  thread3.get();

  std::future<void> thread4 = std::async (std::launch::async, getLargest, myArray, largestEntry, (portion * 3), (portion * 4), dim);
  thread4.get();

  std::future<void> thread5 = std::async (std::launch::async, getLargest, myArray, largestEntry, (portion *4), dim, dim);
  thread5.get();

  double t2 = get_wallTime();
  double t3 = t2 - t1;

  cout << " The largest entry is " << largestEntry << endl;  

  cout << "runtime : " <<  t3 << "\n";
}

我有适当的#includes。
我明白我的code作为更新双 largestEntry 从每个线程,如果二维数组,该线程处理已经比线程大项的部分在它之前。然后我输出最大的记录,和运行。

I have the appropriate #includes. I understand my code as updating the double largestEntry from each thread if the portion of the 2d array that the thread is processing has a larger entry than the thread prior to it. Then I output the largest entry, and the runtime.

下面是输出:

 The largest entry is 0
runtime : 14.7113

本管理办法的速度比我期待它来,而最大的入境不应该是零。基本上,我无法找到这是为什么。我不是很舒服,使用异步,但当我面前,这种方法效果很不错。我知道我没有更新 largestEntry 正确的,虽然我不确定,我犯了一个错误。

This runs way faster than I'm expecting it to, and the largest entry should not be zero. Basically, I'm having trouble finding why that is. I'm not very comfortable with using async, but when I have before, this method worked very well. I know I'm not updating largestEntry correctly, though I'm unsure of where I've made a mistake.

感谢你们能给任何建议。

Thanks for any advice you guys could give.

推荐答案

您正在传递 largestEntry getLargest 按价值计算,所以当它是在函数里只更新值被更新,而不是在值

You're passing largestEntry into getLargest by value, so when it is updated only the value within the function is updated, not the value in main.

另外两个注意事项:该 thread1.get()等通话都应该在创建线程之后,所以他们都同时运行

Two other notes: The thread1.get() etc. calls should all be after the threads are created, so they all run simultaneously.

二,每个线程应返回 largestEntry它自己的价值(也可以是未来的值),然后比较这些发现最大的。如果他们都引用你要进入的线程,CPU缓存颠簸,可能坏答案之间的竞争条件取决于优化器如何处理更新largestEntry相同的变量(可以避开写出来的值,直到所有的循环已完成)。

Two, each thread should return it's own value for largestEntry (it can be the value of the future), then compare those to find the largest. If they all reference the same variable you're going to get into race conditions between the threads, CPU cache thrashing, and possibly bad answers depending on how the optimizer handles the updates to largestEntry (it could avoid writing the value out until all the looping was done).

这篇关于寻找具有螺纹功能的二维数组最大的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆