定义两个矩阵乘法方法时遇到问题

时间:2015-10-27 00:30:49

标签: c++ matrix-multiplication

我正在努力定义标准的乘法方法和线程化的方法。

首先,我似乎无法使标准方法正确。我希望它只用两个矩阵的乘积填充另一个空矩阵,我相信我已经正确地写了它,但是我在错误时不断出错。

以下是所有代码:

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

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


}

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 (argv < 3) {
       cout << "Not enough arguments.";
    }
    int numTimes = atoi(argv[1]);

    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 (argv[2] = "yes") {
       int dim1 = size / 2;
       for (int n = 0; n < numTimes; n++) {
          auto thread1 = std::async(matrixMultiplyThreaded, a, b, product, 0, dim1, size);

          auto thread2 = std::async(matrixMultiplyThreaded, a, b, product, dim1, size, size);

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


}

我测量了线程版本和标准版本之间运行时的差异。但是,我是来自C / Java的C ++新手,而且我对SYNac的差异感到懊恼。如果有人能对此有所了解,我将非常感激。

以下是我收到的一些错误消息:

multiplyMatrix.cpp: In function 'void matrixMultiplyThreaded(double, double, double, int, int, int)':
multiplyMatrix.cpp:37:26: error: invalid types 'double[int]' for array subscript
                 product[i][j] += a[i][k] * b[k][j];
                          ^
multiplyMatrix.cpp:37:37: error: invalid types 'double[int]' for array subscript
                 product[i][j] += a[i][k] * b[k][j];
                                     ^
multiplyMatrix.cpp:37:47: error: invalid types 'double[int]' for array subscript
                 product[i][j] += a[i][k] * b[k][j];
                                               ^
multiplyMatrix.cpp: In function 'int main(int, char**)':

0 个答案:

没有答案