1

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);
slippeel
  • 103
  • 2
  • 10
  • [Are you sure](http://lwn.net/Articles/255364/) you need threads to [improve performance](http://lwn.net/Articles/258188/) here, or is this just an exercise? (See section 6.2.1) – tweej Oct 27 '15 at 23:51

1 Answers1

0

I couldn't compile your code because of other errors, but for the part that launches threads, you should use lambdas:

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). 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.

I wouldn't publish two versions of matrixMultiply. Delete matrixMultiply and use only matrixMultiplyThreaded (which should be renamed, then). If you reaaaaally want to expose a matrixMultiply without the dim parameters, code in in terms of matrixMultiplyThreaded:

static void matrixMultiply(double** a, double** b, double** product, int size) {
    matrixMultiplyThreaded(a, b, product, 0, size, size);
}
Community
  • 1
  • 1
Gabriel
  • 2,841
  • 4
  • 33
  • 43