I tried a cpp program which uses threads on 12th Gen Intel(R) Core(TM) i7-1265U and got no errors, when trying the same program on a container running on Intel(R) Xeon(R) Silver 4216 CPU @ 2.10GHz I get the following error error image.
#include <iostream>
#include <cstdlib>
#include <thread>
#include <chrono>
#include<vector>
using namespace std;
using namespace std::chrono;
void fillRandom(float *arr, int size) {
for (int i = 0; i < size; ++i) {
arr[i] = static_cast<float>(rand()) / RAND_MAX * 10.0;
}
}
void helper(float *c,float*a,float*b,int n,int o,int i,int j)
{
int t=0,l;
for (l = 0; l < o; l++) {
t += a[i * o + l] * b[l * n + j];
}
c[i * n + j] = t;
}
void matmul(float *a, float *b, float *c, int m, int n, int o) {
vector <thread> threads;
int i, j, l, t;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
threads.emplace_back(&helper,c,a,b,n,o,i,j);
}
}
for(auto& th : threads){
th.join();
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
cout << "Usage: " << argv[0] << " <matrix_size> <repetitions>" << endl;
return 1;
}
srand(static_cast<unsigned int>(time(nullptr)));
int m = atoi(argv[1]);
int n = m;
int repetitions = atoi(argv[2]);
float *a = new float[m * n];
float *b = new float[m * n];
float *c = new float[m * n];
fillRandom(a, m * n);
fillRandom(b, m * n);
for (int rep = 0; rep < repetitions; ++rep) {
auto start = high_resolution_clock::now();
matmul(a, b, c, m, n, m);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(stop - start);
cout << "Time taken for " << rep<< "th repetition is " << duration.count() << " milliseconds" << endl;
}
delete[] a;
delete[] b;
delete[] c;
return 0;
}
This is a mulit-threaded matrix multiply code, I am new to this, some help would be great, thanks.
New contributor
newb2k4p is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.