Skip to content

Commit e94cef3

Browse files
committed
Add C++ array arithmetic program
1 parent 177c7c5 commit e94cef3

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.pytest_cache/
22
**.pyc
3+
build/

CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# set the project name
4+
project(arraymath)
5+
6+
# add the executable
7+
add_executable(arraymath array_arithmetic.cpp)

array_arithmetic.cpp

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
#include <iostream>
3+
#include <vector>
4+
#include <time.h>
5+
6+
7+
int main()
8+
{
9+
10+
std::cout << "Configuration" << std::endl;
11+
12+
// Python float is 64 bit / 8 byte
13+
int ARRAY_SIZE = 1000 * 1000 * 5;
14+
// int ARRAY_SIZE = 5;
15+
std::cout << "- Size of data array: " << ARRAY_SIZE * 8 / (1000 * 1000) << "MB" << std::endl;
16+
17+
int N = 10;
18+
std::cout << "- Number of iterations to average: " << N << std::endl;
19+
20+
21+
time_t t_start, t_end;
22+
double elapsed_time = 0.0;
23+
double average;
24+
25+
{
26+
std::vector<double> array1(ARRAY_SIZE), array2(ARRAY_SIZE), buffer_array(ARRAY_SIZE);
27+
std::vector<int> index_map(ARRAY_SIZE);
28+
29+
for(int i=0; i<ARRAY_SIZE; i++) {
30+
index_map[i] = rand()%ARRAY_SIZE;
31+
}
32+
33+
for (int i=0; i<N; i++) {
34+
t_start = time(NULL);
35+
for (int j=0; j<ARRAY_SIZE; j++) {
36+
int index = index_map[j];
37+
buffer_array[j] = array1[index] * array2[index] + array1[index] * array2[index];
38+
}
39+
t_end = time(NULL);
40+
41+
elapsed_time += difftime(t_end, t_start);
42+
}
43+
average = elapsed_time / N;
44+
std::cout << "Non-contiguous access, vectorization: " << elapsed_time << " " << average << std::endl;
45+
}
46+
47+
48+
{
49+
std::vector<double> array1(ARRAY_SIZE), array2(ARRAY_SIZE), buffer_array(ARRAY_SIZE);
50+
std::vector<int> index_map(ARRAY_SIZE);
51+
52+
for(int i=0; i<ARRAY_SIZE; i++) {
53+
index_map[i] = i;
54+
}
55+
56+
for (int i=0; i<N; i++) {
57+
t_start = time(NULL);
58+
for (int j=0; j<ARRAY_SIZE; j++) {
59+
int index = index_map[j];
60+
buffer_array[j] = array1[index] * array2[index] + array1[index] * array2[index];
61+
}
62+
t_end = time(NULL);
63+
64+
elapsed_time += difftime(t_end, t_start);
65+
}
66+
average = elapsed_time / N;
67+
std::cout << "Contiguous access, vectorization: " << elapsed_time << " " << average << std::endl;
68+
}
69+
70+
71+
// {
72+
// std::vector<double> array1(ARRAY_SIZE), array2(ARRAY_SIZE), buffer_array(ARRAY_SIZE);
73+
74+
// for (int i=0; i<N; i++) {
75+
// t_start = time(NULL);
76+
// buffer_array = array1 * array2 + array1 * array2;
77+
// t_end = time(NULL);
78+
79+
// elapsed_time += difftime(t_end, t_start);
80+
// }
81+
// average = elapsed_time / N;
82+
// std::cout << "No indexing: " << elapsed_time << " " << average << std::endl;
83+
// }
84+
85+
return 0;
86+
}

0 commit comments

Comments
 (0)