Last active
November 20, 2018 12:25
-
-
Save glzjin/c034973fb36a20c020e3909a07ea72f5 to your computer and use it in GitHub Desktop.
Test OpenMp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "omp.h" | |
#include <sys/time.h> | |
#include <chrono> | |
using namespace std; | |
double random(double start, double end) | |
{ | |
return start + (end - start) * rand() / (RAND_MAX + 1.0); | |
} | |
int main(int argc, char* argv[]) { | |
srand(unsigned(time(0))); | |
long cpuNums = omp_get_num_procs(); | |
cout << "You have " << cpuNums << " CPU nums." << endl; | |
long baseNumber; | |
if(argc > 1) { | |
baseNumber = atoi(argv[1]); | |
} else { | |
cout << "Now, please enter your base number for elements you want:" << endl; | |
cin >> baseNumber; | |
} | |
auto startTime = chrono::steady_clock::now(); | |
long elementNumber = cpuNums * baseNumber; | |
cout << "Element number:" << elementNumber << endl; | |
long shift = elementNumber / cpuNums; | |
long a[elementNumber], b[elementNumber], c[elementNumber]; | |
for(long i = 0; i < elementNumber; i++) { | |
a[i] = random(0, 100); | |
b[i] = random(0, 100); | |
} | |
// cout << "===========" << endl; | |
// cout << "A:" << endl; | |
// for(long i = 0; i < elementNumber; i++) { | |
// cout << i << ":" << a[i] << endl; | |
// } | |
// | |
// cout << "===========" << endl; | |
// cout << "B:" << endl; | |
// for(long i = 0; i < elementNumber; i++) { | |
// cout << i << ":" << b[i] << endl; | |
// } | |
cout << "===========" << endl; | |
auto Time1 = chrono::steady_clock::now(); | |
cout << "init:" << chrono::duration <long long, nano>(Time1 - startTime).count() << endl; | |
//Algorithm 0 | |
for(long i = 0; i < elementNumber; i++) { | |
c[i] = a[i] * b[i]; | |
} | |
// cout << "===========" << endl; | |
// cout << "C:" << endl; | |
// for(long i = 0; i < elementNumber; i++) { | |
// cout << i << ":" << c[i] << endl; | |
// } | |
cout << "===========" << endl; | |
auto Time2 = chrono::steady_clock::now(); | |
cout << "A0:" << chrono::duration <long long, nano>(Time2 - Time1).count() << endl; | |
//Algorithm 1 | |
#pragma omp parallel | |
{ | |
long tid = omp_get_thread_num(); | |
for(long i = 0 ; i < shift; i++) { | |
c[tid + i * cpuNums] = a[tid + i * cpuNums] * b[tid + i * cpuNums]; | |
} | |
}; | |
// cout << "===========" << endl; | |
// cout << "C:" << endl; | |
// for(long i = 0; i < elementNumber; i++) { | |
// cout << i << ":" << c[i] << endl; | |
// } | |
cout << "===========" << endl; | |
auto Time3 = chrono::steady_clock::now(); | |
cout << "A1:" << chrono::duration <long long, nano>(Time3 - Time2).count() << endl; | |
//Algorithm 2 | |
#pragma omp for schedule(static, 8) | |
for(long i = 0; i < elementNumber; i++) { | |
c[i] = a[i] * b[i]; | |
} | |
// cout << "===========" << endl; | |
// cout << "C:" << endl; | |
// for(long i = 0; i < elementNumber; i++) { | |
// cout << i << ":" << c[i] << endl; | |
// } | |
cout << "===========" << endl; | |
auto Time4 = chrono::steady_clock::now(); | |
cout << "A2:" << chrono::duration <long long, nano>(Time4 - Time3).count() << endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import subprocess | |
import xlsxwriter | |
times = 51 | |
def test(element_number): | |
base_number = element_number / 8 | |
s = subprocess.Popen("openmp " + str(base_number), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
element_final_number = 0 | |
a0 = 0 | |
a1 = 0 | |
a2 = 0 | |
while True: | |
next_line = s.stdout.readline().strip().decode() | |
if next_line.find("Element number") != -1: | |
r = re.findall('Element number:(\d*)', next_line) | |
element_final_number = int(r[0]) | |
if next_line.find("A0") != -1: | |
r = re.findall('A0:(\d*)', next_line) | |
a0 = int(r[0]) | |
if next_line.find("A1") != -1: | |
r = re.findall('A1:(\d*)', next_line) | |
a1 = int(r[0]) | |
if next_line.find("A2") != -1: | |
r = re.findall('A2:(\d*)', next_line) | |
a2 = int(r[0]) | |
if next_line == "": | |
break | |
return element_final_number, a0, a1, a2 | |
result_set = {} | |
for i in range(0, 1): | |
for j in range(0, 10): | |
for k in range(0, 100): | |
result_list = [] | |
results = test(100000 * i + 10000 * j + 100 * k) | |
element_number = results[0] | |
print("Testing:" + str(element_number)) | |
for m in range(1, times): | |
results = test(100000 * i + 10000 * j + 100 * k) | |
result_list.append(results[1]) | |
result_list.append(results[2]) | |
result_list.append(results[3]) | |
result_set[element_number] = result_list | |
workbook = xlsxwriter.Workbook('openmp.xlsx') | |
worksheet = workbook.add_worksheet() | |
print(result_set) | |
row = 1 | |
col = 0 | |
for number, result in result_set.items(): | |
worksheet.write(row, col, number) | |
for i in range(1, times): | |
worksheet.write(row, i, result[(i - 1) * 3]) | |
for i in range(1, times): | |
worksheet.write(row, times + i - 1, result[1 + (i - 1) * 3]) | |
for i in range(1, times): | |
worksheet.write(row, times * 2 + i - 2, result[2 + (i - 1) * 3]) | |
row += 1 | |
worksheet.write(0, 0, 'Number') | |
for i in range(1, times): | |
worksheet.write(0, i, 'Algorithm 0 - %i(ns)' % i) | |
for i in range(1, times): | |
worksheet.write(0, times + i - 1, 'Algorithm 1 - %i(ns)' % i) | |
for i in range(1, times): | |
worksheet.write(0, times * 2 + i - 2, 'Algorithm 2 - %i(ns)' % i) | |
workbook.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment