-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddArrayofIntegers_C.pl
executable file
·154 lines (127 loc) · 3.8 KB
/
addArrayofIntegers_C.pl
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/home/chrisarg/perl5/perlbrew/perls/current/bin/perl
use v5.36;
use List::Util qw(sum);
use Benchmark::CSV;
use PDL::Lite;
use PDL::NiceSlice;
use PDL::IO::Misc;
use PDL::IO::CSV ':all';
use PDL::Stats::Basic;
use Time::HiRes qw(time);
use OpenMP::Environment;
use Inline (
C => 'DATA',
ccflagsex => q{-fopenmp},
lddlflags => join( q{ }, $Config::Config{lddlflags}, q{-fopenmp} ),
myextlib => ''
);
my $openmp_env = OpenMP::Environment->new;
$openmp_env->omp_num_threads(16);
my $benchmark = Benchmark::CSV->new(
output => './addArrayofIntegers.csv',
sample_size => 1,
);
my $num_elements = 1_000_003;
## Create an array of $num_elements random integers, between -100, 100
my @array = map { int( rand(200) ) - 100 } 1 .. $num_elements;
my $array_double_ASM = pack "d*", @array;
say "Starting benchmark";
$benchmark->add_instance(
'C_SIMD_wo_alloc' => sub {
sum_array_SIMD_C( $array_double_ASM, scalar @array );
},
);
$benchmark->add_instance(
'C_SIMD_w_alloc' => sub {
my $array_double_ASM = pack "d*", @array;
sum_array_SIMD_C( $array_double_ASM, scalar @array );
},
);
$benchmark->add_instance(
'C_SIMD_OMP_wo_alloc' => sub {
sum_array_SIMD_OMP_C( $array_double_ASM, scalar @array );
},
);
$benchmark->add_instance(
'C_SIMD_OMP_w_alloc' => sub {
my $array_double_ASM = pack "d*", @array;
sum_array_SIMD_OMP_C( $array_double_ASM, scalar @array );
},
);
$benchmark->add_instance(
'C_OMP_wo_alloc' => sub {
sum_array_OMP_C( $array_double_ASM, scalar @array );
},
);
$benchmark->add_instance(
'C_OMP_w_alloc' => sub {
my $array_double_ASM = pack "d*", @array;
sum_array_OMP_C( $array_double_ASM, scalar @array );
},
);
$benchmark->run_iterations(1000);
# Load the CSV file
my @data = rcsv1D( 'addArrayofIntegers.csv', { text2bad => 1, header => 1 } );
my %summary_stats = ();
foreach my $col ( 0 .. $#data ) {
my $pdl = pdl( $data[$col] );
my $mean = $pdl->average;
my $stddev = $pdl->stdv_unbiased;
my $median = $pdl->median;
$summary_stats{ $data[$col]->hdr->{col_name} } =
{ mean => $mean, stddev => $stddev, median => $median };
}
# Get the column names from the first row
my @column_names = sort keys %{ $summary_stats{ ( keys %summary_stats )[0] } };
# Define the width for each column
my $width_name = 24;
my $width_col = 10;
# Print the column names
printf "%-${width_name}s", '';
printf "%${width_col}s", $_ for @column_names;
print "\n";
# Print each row
foreach my $row_name ( sort keys %summary_stats ) {
printf "%-${width_name}s", $row_name;
printf "%${width_col}.1e", $summary_stats{$row_name}{$_} for @column_names;
print "\n";
}
unlink 'addArrayofIntegers.csv';
## load the CSV file and print a summary of the results using PDL
__DATA__
__C__
#include <omp.h>
void _ENV_set_num_threads() {
char *num;
num = getenv("OMP_NUM_THREADS");
omp_set_num_threads(atoi(num));
}
double sum_array_SIMD_C(char *array_in, size_t length) {
double sum = 0.0;
double * array = (double *) array_in;
#pragma omp simd reduction(+:sum)
for (size_t i = 0; i < length; i++) {
sum += array[i];
}
return sum;
}
double sum_array_SIMD_OMP_C(char *array_in, size_t length) {
double sum = 0.0;
double * array = (double *) array_in;
_ENV_set_num_threads();
#pragma omp parallel for simd reduction(+:sum) schedule(static,8)
for (size_t i = 0; i < length; i++) {
sum += array[i];
}
return sum;
}
double sum_array_OMP_C(char *array_in, size_t length) {
double sum = 0.0;
double * array = (double *) array_in;
_ENV_set_num_threads();
#pragma omp parallel for reduction(+:sum) schedule(static,8)
for (size_t i = 0; i < length; i++) {
sum += array[i];
}
return sum;
}