簡體   English   中英

c ++中的線程,在2d數組中找到最大的條目

[英]threading in c++, finding largest entry in 2d array

我在查找具有線程函數的2d數組中的最大條目時遇到了一些麻煩。 我已經被粘在屏幕上了幾個小時,我可以使用一些幫助。

這是代碼:

#include <iostream>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <random>
#include <ctime>
#include <future>
#include <thread>

using namespace std;

// A vanilla random number generator
double genRandNum(double min, double max){

  return min + (rand() / (RAND_MAX / (max - min)));

}

double get_wallTime() {
    struct timeval tp;
    gettimeofday(&tp, NULL);
    return (double) (tp.tv_sec + tp.tv_usec/1000000.0);
} 

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]; 
            }
        }
    }

}

// Main routine
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;
  double largestEntry2 = 0.0;
  double largestEntry3 = 0.0;
  double largestEntry4 = 0.0;
  double largestEntry5 = 0.0;
  int portion = dim / 5;
  std::future<void> thread1 = std::async (std::launch::async, getLargest, myArray, largestEntry, 0, portion, dim);

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

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

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

  std::future<void> thread5 = std::async (std::launch::async, getLargest, myArray, largestEntry5, (portion *4), dim, dim);
  thread1.get();
  thread2.get();
  thread3.get();
  thread4.get();

  thread5.get();


  if (largestEntry2 > largestEntry) {
     largestEntry = largestEntry2;
  } 
  if (largestEntry3 > largestEntry) {
    largestEntry = largestEntry3;
  }
  if (largestEntry4 > largestEntry) {
    largestEntry = largestEntry4;
  }
  if (largestEntry5 > largestEntry) {
    largestEntry = largestEntry5;
  }

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

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

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

我相信我已經正確設置了所有內容,但是我剛剛學習了如何使用異步功能,因此可以肯定有些錯誤。 運行時,我得到的最大條目的輸出為零,這是錯誤的,並且我得到的運行時間比我期望的要長得多。

這是輸出:

 The largest entry is 0
runtime : 13.8261

我想我沒有正確地將largestEntry傳遞給線程函數,但是我不知道如何解決它。 線程函數getLargest應該找到它提供的數組的最大條目,並且當我創建線程時,我將每個線程分配給數組的一部分。

任何人都可以提供的任何建議將不勝感激。

當前,您只是在更改本地largestEntry參數的值。 這不會更改調用程序中的任何內容。

一種解決方案是傳入largestEntry作為參考(將函數更改為在其參數列表中說double &largestEntry

請注意,這與線程無關,您可以正常調用函數進行測試。

編輯:我在嘗試使用引用時遇到了麻煩(可能只是一個舊的編譯器)-如果引用不起作用,則始終可以將largestEntry作為指針傳遞-函數參數列表中的double *largestEntry和一些*&放在其余的代碼中。

The Dark已在最后一個答案中描述了該問題。 但是,我發現解決方案並不理想。 對我來說,邏輯上說,您的函數返回這樣找到的值,而不是將其寫入參數中,這將更加合乎邏輯。

這樣,您可以使用future的get()函數在線程完成時獲得答案,並且在真正不需要它的地方不使用引用或指針。

注意:在這里僅使用引用將不起作用,因為復制了std::async的參數,與std::thread方式相同。 如果希望它與參考一起使用,則在這種情況下,需要使用std::reflink )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM