简体   繁体   English

线程矩阵乘法函数

[英]threaded matrix multiplication function

I'm trying to write two matrix multiplication functions, one standard and one that is threaded. 我正在尝试编写两个矩阵乘法函数,一个是标准函数,另一个是线程化的。 However, I can't figure out how to call the threaded function correctly. 但是,我不知道如何正确调用线程函数。

Here is the code: 这是代码:

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

using namespace std;

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



static void matrixMultiply(double** a, double** b, double** product, int size) {

    for (int i = 0; i < size; i++) {
       for (int j = 0; j < size; j++) {
          for (int k = 0; k < size; k++) {
          product[i][j] += a[i][k] * b[k][j];
          }
       }
    }


}

static void matrixMultiplyThreaded(double** a, double** b, double** product, int dimLower, int dimUpper, int dim) {
    for (int i = dimLower; i < dimUpper; i++) {
        for (int j = 0; j < dim; j++) {
            for (int k = 0; k < dim; k++) {
                product[i][j] += a[i][k] * b[k][j];
            }
        }
    }
}

int main(int argc, char *argv[]) {
    if (argc < 3) {
       cout << "Not enough arguments.";
    }
    int numTimes = atoi(argv[1]);
    char *threadOrNo = argv[2];
    int size = atoi(argv[3]);
    double a[size][size], b[size][size] = {};
    double product[size][size] = {};

    for (int i=0; i<size; i++) {
       for (int j=0; j < size; j++) {
          a[i][j] = 2;
          b[i][j] = 3;
       }
    }
    double t1 = get_wallTime();
    if (*threadOrNo == 'y') {
       int dim1 = size / 2;
       for (int n = 0; n < numTimes; n++) {
          std::thread first (matrixMultiplyThreaded, a, b, product, 0, dim1, size);

          std::thread second (matrixMultiplyThreaded, a, b, product, dim1, size, size);
          first.join();
          second.join();
          }
    }
    else { 
       for (int m = 0; m < numTimes; m++) {

          matrixMultiply(a[size][size],b[size][size], product[size][size], size);
    }
    }
    double t2 = get_wallTime();
    double totalTime = t2 - t1;
    cout << "time : " << totalTime;


}

If anyone could give any advice I would be eternally grateful. 如果有人可以提供任何建议,我将永远感激不已。 Specifically, what's the right way to implement threads for the threaded function? 具体来说,为线程函数实现线程的正确方法是什么?

Here is the first error message I receive, which I've tried to fix several times: 这是我收到的第一条错误消息,我已尝试修复多次:

multiplyMatrix.cpp: In function 'int main(int, char**)':
multiplyMatrix.cpp:64:82: error: no matching function for call to 'std::thread::thread(void (&)(double**, double**, double**, int, int, int), double [size][size], double [size][size], double [size][size], int, int&, int&)'
           std::thread first (matrixMultiplyThreaded, a, b, product, 0, dim1, size);

I couldn't compile your code because of other errors, but for the part that launches threads, you should use lambdas: 由于其他错误,我无法编译您的代码,但是对于启动线程的部分,您应该使用lambda:

std::thread first([=]() { matrixMultiplyThreaded(a, b, product, 0, dim1, size); });
std::thread second([=]() { matrixMultiplyThreaded(a, b, product, dim1, size, size); });

Among other errors, you can't statically allocate arrays ( a , b and product ) with a variable (which is a dynamic value). 除其他错误外,您不能为变量(动态值)静态分配数组( abproduct )。 Do as follows: 进行如下操作:

double **a = new double*[size];
double **b = new double*[size];
double **product = new double*[size];
for (int i = 0; i < size; i++)
{
    a[i] = new double[size];
    b[i] = new double[size];
    product[i] = new double[size];
}

And don't forget to free those afterwards, unless you're willing to use shared_ptr or shared_array . 并且不要忘了以后free它们,除非您愿意使用shared_ptrshared_array

I wouldn't publish two versions of matrixMultiply . 我不会发布matrixMultiply两个版本。 Delete matrixMultiply and use only matrixMultiplyThreaded (which should be renamed, then). 删除matrixMultiply并仅使用matrixMultiplyThreaded (然后应重命名)。 If you reaaaaally want to expose a matrixMultiply without the dim parameters, code in in terms of matrixMultiplyThreaded : 如果您reaaaaally要公开一个matrixMultiply中的条款,而不昏暗的参数,代码matrixMultiplyThreaded

static void matrixMultiply(double** a, double** b, double** product, int size) {
    matrixMultiplyThreaded(a, b, product, 0, size, size);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM