forked from joshuaybang/python_example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_test.cpp
More file actions
50 lines (38 loc) · 1.59 KB
/
simple_test.cpp
File metadata and controls
50 lines (38 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <Eigen/Sparse>
#include <Eigen/Dense>
#include <chrono>
#include <omp.h>
int main() {
int p = 5000;
int num_threads = omp_get_max_threads(); // Set the number of threads
omp_set_num_threads(num_threads);
// Define a sparse matrix
Eigen::SparseMatrix<double> sparseMatrix(p, p);
sparseMatrix.insert(0, 0) = 1;
sparseMatrix.insert(1, 1) = 2;
// Define a dense matrix
Eigen::MatrixXd denseMatrix = Eigen::MatrixXd::Random(p, p);
// Start measuring total time
auto total_start = std::chrono::steady_clock::now();
#pragma omp parallel
{
for (int i = 0; i < 5; ++i) {
// Start measuring time for each iteration
auto start = std::chrono::steady_clock::now();
// Perform the multiplication
Eigen::MatrixXd result = sparseMatrix * denseMatrix;
// End measuring time for each iteration
auto end = std::chrono::steady_clock::now();
// Calculate the elapsed time for each iteration
std::chrono::duration<double> elapsed_seconds = end - start;
std::cout << "Thread " << omp_get_thread_num() << " - Iteration " << i + 1 << " - Elapsed time: " << elapsed_seconds.count() << "s\n";
}
}
// End measuring total time
auto total_end = std::chrono::steady_clock::now();
// Calculate total wall clock time
std::chrono::duration<double> total_elapsed_seconds = total_end - total_start;
std::cout << "Total time: " << total_elapsed_seconds.count() << "s\n"; // Print total elapsed time
return 0;
}