4

我是线程编程的新手,我有一个概念问题。我正在为我的班级做矩阵乘法。但是,我在不使用线程的情况下执行此操作,然后使用线程计算答案矩阵的每个单元格的标量积,然后再次将第一个矩阵拆分为比例,以便每个线程都有相等的部分要计算。我的问题是标量产品实现很快就完成了,这是我所期望的,但是第三个实现并没有比非线程实现更快地计算出答案。例如,如果它使用 2 个线程,它会在大约一半的时间内计算它,因为它可以同时在矩阵的两半上工作,但事实并非如此。我觉得第三次实施中存在问题,我没有 不认为它是并行运行的,代码如下。谁能让我直截了当?并非所有代码都与问题相关,但我将其包括在内,以防问题不是本地问题。谢谢,

主程序:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include<fstream>
#include<string>
#include<sstream>

#include <matrix.h>
#include <timer.h>
#include <random_generator2.h>

const float averager=2.0; //used to find the average of the time taken to multiply the matrices.

//Precondition: The matrix has been manipulated in some way and is ready to output the statistics
//Outputs the size of the matrix along with the user elapsed time.
//Postconidition: The stats are outputted to the file that is specified with the number of threads used
//file name example: "Nonparrallel2.dat"
void output(string file, int numThreads , long double time, int n);

//argv[1] = the size of the matrix
//argv[2] = the number of threads to be used.
//argv[3] = 
int main(int argc, char* argv[])
{ 
  random_generator rg;
  timer t, nonparallel, scalar, variant;
  int n, total = 0, numThreads = 0;
  long double totalNonP = 0, totalScalar = 0, totalVar = 0;

  n = 100;

/*
 * check arguments
 */
      n = atoi(argv[1]);
      n = (n < 1) ? 1 : n;
      numThreads = atoi(argv[2]);
/*
 * allocated and generate random strings
 */
  int** C;
  int** A;
  int** B;

  cout << "**NOW STARTING ANALYSIS FOR " << n << " X " << n << " MATRICES WITH " << numThreads << "!**"<< endl;

  for (int timesThrough = 0; timesThrough < averager; timesThrough++)
  {

      cout << "Creating the matrices." << endl;
      t.start();
      C = create_matrix(n);
      A = create_random_matrix(n, rg);
      B = create_random_matrix(n, rg);
      t.stop();

      cout << "Timer (generate): " << t << endl;

        //---------------------------------------------------------Ends non parallel-----------------------------
        /*
         * run algorithms
         */
          cout << "Running non-parallel matrix multiplication: " << endl;
          nonparallel.start();
          multiply(C, A, B, n);
          nonparallel.stop();
        //-----------------------------------------Ends non parallel----------------------------------------------


        //cout << "The correct matrix" <<endl;
        //output_matrix(C, n);

          cout << "Timer (multiplication): " << nonparallel << endl;
          totalNonP += nonparallel.user();


          //D is the transpose of B so that the p_scalarproduct function does not have to be rewritten
          int** D = create_matrix(n); 
          for (int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                D[i][j] = B[j][i];
        //---------------------------------------------------Start Threaded Scalar Poduct--------------------------
          cout << "Running scalar product in parallel" << endl;
          scalar.start();
          //Does the scalar product in parallel to multiply the two matrices.
          for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++){
            C[i][j] = 0;
            C[i][j] = p_scalarproduct(A[i],D[j],n,numThreads);
            }//ends the for loop with j
          scalar.stop();

          cout << "Timer (scalar product in parallel): " << scalar << endl;
          totalScalar += scalar.user();
        //---------------------------------------------------Ends Threaded Scalar Poduct------------------------


        //---------------------------------------------------Starts Threaded Variant For Loop---------------
           cout << "Running the variation on the for loop." << endl;
            boost :: thread** thrds;


            //create threads and bind to p_variantforloop_t
            thrds = new boost::thread*[numThreads];

            variant.start();
            for (int i = 1; i <= numThreads; i++)
                thrds[i-1] = new boost::thread(boost::bind(&p_variantforloop_t, 
                        C, A, B, ((i)*n - n)/numThreads ,(i * n)/numThreads, numThreads, n));   
cout << "before join" <<endl;
            // join threads 
              for (int i = 0; i < numThreads; i++)
            thrds[i]->join();
             variant.stop();

            // cleanup 
              for (int i = 0; i < numThreads; i++)
            delete thrds[i];
              delete[] thrds;

        cout << "Timer (variation of for loop): " << variant <<endl;
        totalVar += variant.user();
        //---------------------------------------------------Ends Threaded Variant For Loop------------------------

         // output_matrix(A, n);
         // output_matrix(B, n);
         //   output_matrix(E,n);

        /*
         * free allocated storage
         */

        cout << "Deleting Storage" <<endl;

          delete_matrix(A, n);
          delete_matrix(B, n);
          delete_matrix(C, n);
          delete_matrix(D, n);  

        //avoids dangling pointers
          A = NULL;
          B = NULL;
          C = NULL;
          D = NULL;
  }//ends the timesThrough for loop   

  //output the results to .dat files
  output("Nonparallel", numThreads, (totalNonP / averager) , n);
  output("Scalar", numThreads, (totalScalar / averager), n);
  output("Variant", numThreads, (totalVar / averager), n);

  cout << "Nonparallel = " << (totalNonP / averager) << endl;
  cout << "Scalar = " << (totalScalar / averager) << endl;
  cout << "Variant = " << (totalVar / averager) << endl;

  return 0;
}

void output(string file, int numThreads , long double time, int n)
{
    ofstream dataFile;
    stringstream ss;

    ss << numThreads;
    file += ss.str();
    file += ".dat";

    dataFile.open(file.c_str(), ios::app);
    if(dataFile.fail())
    {
        cout << "The output file didn't open." << endl;
        exit(1);
    }//ends the if statement.
    dataFile << n << "     " << time << endl;
    dataFile.close();
}//ends optimalOutput function

矩阵文件:

#include <matrix.h>
#include <stdlib.h>

using namespace std;

int** create_matrix(int n)
{
  int** matrix;

  if (n < 1) 
    return 0;

  matrix = new int*[n];
  for (int i = 0; i < n; i++)
    matrix[i] = new int[n];

  return matrix;
}

int** create_random_matrix(int n, random_generator& rg)
{
  int** matrix;

  if (n < 1) 
    return 0;

  matrix = new int*[n];
  for (int i = 0; i < n; i++)
    {
      matrix[i] = new int[n];
      for (int j = 0; j < n; j++)
    //rg >> matrix[i][j];
    matrix[i][j] = rand() % 100;
    }

  return matrix;
}

void delete_matrix(int** matrix, int n)
{ 
    for (int i = 0; i < n; i++) 
      delete[] matrix[i];

    delete[] matrix;

    //avoids dangling pointers.
    matrix = NULL;
}

/*
 * non-parallel matrix multiplication
 */
void multiply(int** C, int** A, int** B, int n)
{ 
  if ((C == A) || (C == B))
    { 
      cout << "ERROR: C equals A or B!" << endl;
      return;
    }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      {
    C[i][j] = 0;
    for (int k = 0; k < n; k++)
      C[i][j] += A[i][k] * B[k][j];
     }
} 

void p_scalarproduct_t(int* c, int* a, int* b, 
                   int s, int e, boost::mutex* lock)
{ 
  int tmp;

  tmp = 0;
  for (int k = s; k < e; k++){
    tmp += a[k] * b[k];
//cout << "a[k]= "<<a[k]<<"b[k]= "<< b[k] <<"    "<<k<<endl;
}
  lock->lock();
  *c = *c + tmp;
  lock->unlock();
} 

int p_scalarproduct(int* a, int* b, int n, int m)
{ 
  int c;
  boost::mutex lock;
  boost::thread** thrds;

  c = 0;

/* create threads and bind to p_merge_sort_t */
  thrds = new boost::thread*[m];
  for (int i = 0; i < m; i++)
    thrds[i] = new boost::thread(boost::bind(&p_scalarproduct_t, 
                             &c, a, b, i*n/m, (i+1)*n/m, &lock));
/* join threads */
  for (int i = 0; i < m; i++)
    thrds[i]->join();

/* cleanup */
  for (int i = 0; i < m; i++)
    delete thrds[i];
  delete[] thrds;

  return c;
} 

void output_matrix(int** matrix, int n)
{ 
  cout << "[";
  for (int i = 0; i < n; i++)
    {
      cout << "[ ";
      for (int j = 0; j < n; j++)
    cout << matrix[i][j] << " ";
      cout << "]" << endl;
    }
  cout << "]" << endl;
}





void p_variantforloop_t(int** C, int** A, int** B, int s, int e, int numThreads, int n)
{
//cout << "s= " <<s<<endl<< "e= " << e << endl;
    for(int i = s; i < e; i++)
        for(int j = 0; j < n; j++){
          C[i][j] = 0;
//cout << "i " << i << "  j " << j << endl;
          for (int k = 0; k < n; k++){
            C[i][j] += A[i][k] * B[k][j];}
        }
}//ends the function
4

2 回答 2

3

我的猜测是您遇到了False Sharing。尝试在中使用局部变量p_variantforloop_t

void p_variantforloop_t(int** C, int** A, int** B, int s, int e, int numThreads, int n)
{
    for(int i = s; i < e; i++)
        for(int j = 0; j < n; j++){
          int accu = 0;
          for (int k = 0; k < n; k++)
            accu += A[i][k] * B[k][j];
          C[i][j] = accu;
        }
}
于 2011-05-01T19:43:19.290 回答
0

根据您在评论中的回复,理论上,由于您只有一个线程(即 CPU)可用,所有线程版本应该与单线程版本相同或更长,因为线程管理开销。您根本不应该看到任何加速,因为解决矩阵的一部分所花费的时间片是从另一个并行任务中窃取的时间片。使用单个 CPU,您只是分时共享 CPU 的资源——在给定的单个时间片内没有真正的并行工作。我怀疑你的第二个实现运行得更快的原因是因为你在你的内部循环中做更少的指针取消引用和内存访问。例如,在C[i][j] += A[i][k] * B[k][j];从两者的主要操作multiplyp_variantforloop_t,您正在查看汇编级别的许多操作,其中许多与内存相关。在“汇编伪代码”中,它看起来类似于以下内容:

1) 将指针值从A堆栈上引用的地址移动到寄存器R1
2) 将寄存器中的地址增加R1变量引用的堆栈外的值ijk
3) 将指针地址值从指向的地址移动R1R1
4)将 in 的 地址从变量、或 5R1所引用的堆栈中的值递增堆栈到寄存器中(所以现在保存的值)ijk
R1R1R1A[i][k]
BR2R2B[k][j]
7) 对堆栈上引用的地址执行步骤 1-4C到寄存器R3
8) 将值从指向的地址移动R3R4(即,R4将实际值保存在C[i][j])
9) 将寄存器相乘R1R2存储在寄存器中R5
10) 加寄存器R4R5和存储在R4
11) 将最终值从R4后面移到所指向的内存地址中R3(现在C[i][j]有最终结果)

这是假设我们有 5 个通用寄存器可供使用,并且编译器正确优化了您的 C 代码以利用它们。我留下了循环索引变量i, j, 和k在堆栈上,因此访问它们将比在寄存器中花费更多的时间……这实际上取决于您的编译器必须在您的平台上使用多少个寄存器。此外,如果您在没有任何优化的情况下进行编译,您可能会在堆栈外进行更多的内存访问,其中一些临时值存储在堆栈中而不是寄存器中,然后从堆栈中重新访问,这需要更长的时间而不是在寄存器之间移动值。无论哪种方式,上面的代码都很难优化。它可以工作,但是如果您使用的是 32 位 x86 平台,那么您将不会使用那么多通用寄存器(尽管您应该至少有 6 个)。x86_64 有更多的寄存器可供使用,但仍然有所有的内存访问需要处理。

另一方面,像tmp += a[k] * b[k]fromp_scalarproduct_t在紧密的内部循环中这样的操作会移动得更快……这是汇编伪代码中的上述操作:

循环会有一个小的初始化步骤

1)tmp创建一个寄存器R1而不是堆栈变量,并将其值初始化为 0
2) 将堆栈上引用的地址值移动aR2
3) 将堆栈s外的值添加到R2并将结果地址保存到R2
4) 移动引用的地址值通过b在堆栈上进入R3
5) 将堆栈s外的值添加到R3并将结果地址保存在6) 设置一个初始化为R3
的计数器R6e - s

在一次性初始化之后,我们将开始实际的内部循环

7) 将 指向的地址中的值R2移入R4
8) 将 指向的地址中的值R3移入R5
9) 相乘R4并将R5结果存储在R5
10) 将结果加R5R1存储在R1
11) 递增R2R3
12) 递减计数器直到R6它达到零,我们终止循环

我不能保证这正是你的编译器设置这个循环的方式,但是你可以从你的标量示例中看到一般来说,内部循环中所需的步骤更少,更重要的是内存访问更少。因此,仅使用寄存器的操作可以完成更多操作,而不是包含内存位置并需要内存提取的操作,这比仅使用寄存器的操作慢得多。所以总的来说它会移动得更快,这与线程无关。

最后,我注意到标量积只有两个嵌套循环,因此它的复杂度为 O(N^2),而对于其他两种方法,您有三个嵌套循环,复杂度为 O(N^3)。这也会有所作为。

于 2011-05-01T23:07:09.103 回答