Hey there, can you help in fixing an issue I have with my code? It keeps running on a…
- User
Hey there, can you help in fixing an issue I have with my code? It keeps running on a single thread when it should run on 1,2,4,6,8,10,12,14,16 threads.
Code: #include #include #include #include #include <sys/time.h>
class Matrix { public: Matrix(int rows, int cols) : data(rows*cols), rows(rows), cols(cols) {}
std::vector<std::complex<double>>::iterator begin() { return data.begin(); } std::vector<std::complex<double>>::iterator end() { return data.end(); } int rows; int cols; std::vector<std::complex<double>> data;};
Matrix multiply(const Matrix& a, const Matrix& b) { Matrix c(a.rows, b.cols); for (int i = 0; i < a.rows; i++) { for (int j = 0; j < b.cols; j++) { for (int k = 0; k < a.cols; k++) { c.data[ic.cols + j] += a.data[ia.cols + k] * b.data[k*b.cols + j]; } } } return c; }
Matrix pow(const Matrix& a, int n, int num_threads) { if (n == 1) { return a; }
Matrix b = pow(a, n/2, num_threads); b = multiply(b, b); if (n % 2 == 1) { b = multiply(b, a); } // Measure execution time before creating threads struct timeval start, end; gettimeofday(&start, NULL); // Create threads std::vector<std::thread> threads; for (int i = 0; i < num_threads; i++) { threads.push_back(std::thread([&]() { b = multiply(b, a); })); } // Wait for all threads to finish for (auto& thread : threads) { thread.join(); } // Measure execution time after joining threads gettimeofday(&end, NULL); double elapsed_time = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0; std::cout << "Execution time with " << num_threads << " threads: " << elapsed_time << " seconds" << std::endl; return b;}
int main() { Matrix A(2048, 2048); Matrix B(2048, 2048);
// initialize matrices A and B with values from the set {-1, 0, 1} Matrix X(2048, 2048); X.data = std::vector<std::complex<double>>(A.data.begin(), A.data.end()); std::vector<int> num_threads_list = {1, 2, 4, 6, 8, 12, 16}; for (int num_threads : num_threads_list) { Matrix X_pow_15 = pow(X, 15, num_threads); } return 0;}
- Assistant
- User
mat.cpp: In function ‘Matrix parallel_pow(const Matrix&, int, int)’: mat.cpp:78:13: error: ‘multiply’ was not declared in this scope 78 | b = multiply(b, a); | ^~~~~~~~ mat.cpp: In function ‘int main()’: mat.cpp:101:31: error: cannot convert ‘Matrix’ to ‘double’ 101 | Matrix X_pow_15 = pow(X, 15, num_threads); | ^ | | | Matrix In file included from /usr/include/c++/11/cmath:45, from /usr/include/c++/11/complex:44, from mat.cpp:1: /usr/include/x86_64-linux-gnu/bits/mathcalls.h:140:1: note: initializing argument 1 of ‘double pow(double, double)’ 140 | __MATHCALL_VEC (pow,, (Mdouble __x, Mdouble __y)); | ^
- Assistant
There are a couple of issues here. First, I mistakenly suggested you to use
multiplyinstead ofparallel_powin the code. Second, there's a name clash with the standard C++powfunction fromcmath, which is causing the error. - User
Can you give the full corrected code?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.