This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatmat_loop_switch.cpp
More file actions
56 lines (52 loc) · 1.41 KB
/
matmat_loop_switch.cpp
File metadata and controls
56 lines (52 loc) · 1.41 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
51
52
53
54
55
56
#include <cstdlib>
#include <iostream>
#include <ctime>
#include <vector>
#include <chrono>
#include <omp.h>
int main(int argc, char **argv) {
typedef float real;
std::srand(std::time(0));
//std::cout << "Please give N: ";
int N;
//std::cin >> N;
if (argc > 1) {
N = atoi(argv[1]);
}
else {
N = 512;
}
//Create 3 matrices
std::vector<real> a(N*N);
std::vector<real> b(N*N);
std::vector<real> c(N*N);
//Fill matrices with random values
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
a[i*N+j] = (real)std::rand()/(real)RAND_MAX;
b[i*N+j] = (real)std::rand()/(real)RAND_MAX;
}
}
auto t1 = std::chrono::high_resolution_clock::now();
#pragma omp parallel for
for (int i = 0; i < N; i++) {
for (int k = 0; k < N; k++) {
for (int j = 0; j < N; j++) {
c[i*N+j] += b[i*N+k] * a[k*N+j];
}
}
}
//implement matrix-matrix multiplication here
auto t2 = std::chrono::high_resolution_clock::now();
//checksum
real sum = 0;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
sum += c[i*N+j];
std::cout << sum << std::endl;
std::cout << "took "
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
<< " milliseconds\n";
std::cout << 2*N*N*N/std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()/1000.0/1000.0 << " GFLOPS/s" << std::endl;
return 0;
}