I'm writing code that must perform a dot product between a vector b and a matrix C, where the dot is performed between b and each line of C.
I wrote two implementations, one in C++ and one in Python. The latter performs better - I'd like to obtain the same speed in C++.
There is one limit though, any improvement can't be done on the way data is stored, only in the way the computation is performed.
This is the C++ code:
#include
#include
#include
#include
#include
using namespace std;
using namespace std::chrono;
int main() {
size_t jmax = 700;
size_t kmax = 100000;
vector> C(jmax + 1, vector(kmax));
vector b(jmax + 1);
random_device rd;
mt19937 gen(rd());
uniform_real_distribution dist_b(5.0, 10.0);
uniform_real_distribution dist_C(-1.0, 1.0);
// fake b and C
for (size_t j = 0; j <= jmax; ++j) {
b[j] = dist_b(gen);
for (size_t k = 0; k < kmax; ++k) {
C[j][k] = dist_C(gen);
}
}
double* eps_ptr = C[jmax].data();
auto start = high_resolution_clock::now();
for (size_t j = 0; j < jmax; ++j) {
double* c_ptr = C[j].data();
double bj = b[jmax - j];
#pragma loop(ivdep)
for (size_t k = 0; k < kmax; ++k) {
eps_ptr[k] += bj * c_ptr[k];
}
}
auto end = high_resolution_clock::now();
auto ms = duration_cast(end - start);
cout << ms.count() << endl;
return 0;
}
And here's the Python code:
import numpy as np
import time
jmax = 700
kmax = 100000
C = np.random.uniform(-1.0, 1.0, size=(jmax, kmax))
b = np.random.uniform(5.0, 10.0, size=jmax)
start = time.time()
bb= b[::-1]
eps = np.dot(bb, C)
end = time.time()
print(int((end - start) * 1000))
Do you have any suggestions? (C++ is compiled using -O2). To be clear, I'm only interested in improving performance for second part of the code, where dot is performed.