Skip to content

Commit

Permalink
Reformat code, grandfather in branch-predict.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadley Luker committed Aug 17, 2018
1 parent ce00184 commit 2357e00
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
40 changes: 26 additions & 14 deletions examples/branch-predict.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,68 @@
// https://stackoverflow.com/questions/11227809/why-is-it-faster-to-process-a-sorted-array-than-an-unsorted-array

#include <algorithm>
#include <cstring>
#include <ctime>
#include <iostream>
#include <pthread.h>
#include <string.h>

#define ARRAY_SIZE 32786
#define RAND_LIMIT 256

void *sum_unsorted(void *data) {
int *real_data = (int *)malloc(sizeof(int) * ARRAY_SIZE);
void *sum_unsorted(void *data)
{
auto *real_data = static_cast<int *>(malloc(sizeof(int) * ARRAY_SIZE));
memcpy(real_data, data, ARRAY_SIZE * sizeof(int));
int *sum = (int *)malloc(sizeof(int));
*sum = 0;
for (unsigned i = 0; i < 100000; ++i) {
for (unsigned i = 0; i < 100000; ++i)
{
// Primary loop
for (unsigned c = 0; c < ARRAY_SIZE; ++c) {
for (unsigned c = 0; c < ARRAY_SIZE; ++c)
{
if (real_data[c] >= 128)
*sum += real_data[c];
}
}
return sum;
}

void *sum_sorted(void *data) {
void *sum_sorted(void *data)
{
int *sum = (int *)malloc(sizeof(int));
*sum = 0;
int *real_data = (int *)malloc(sizeof(int) * ARRAY_SIZE);
auto *real_data = static_cast<int *>(malloc(sizeof(int) * ARRAY_SIZE));
// real_data = (int *) data;
memcpy(real_data, data, ARRAY_SIZE * sizeof(int));

// !!! With this, the next loop runs faster
std::sort(real_data, real_data + ARRAY_SIZE);
for (unsigned i = 0; i < 100000; ++i) {
for (unsigned i = 0; i < 100000; ++i)
{
// Primary loop
for (unsigned c = 0; c < ARRAY_SIZE; ++c) {
for (unsigned c = 0; c < ARRAY_SIZE; ++c)
{
if (real_data[c] >= 128)
*sum += real_data[c];
}
}
return sum;
}

float fib_float(float n) {
if (n <= 1.1111111111) {
float fib_float(float n)
{
if (n <= 1.1111111111)
{
return n;
} else {
}
else
{
return fib_float(n - 1.0000000000) + fib_float(n - 2.0000000000);
}
}

int main() {
int main()
{
// Generate data
const unsigned arraySize = ARRAY_SIZE;
int data[arraySize];
Expand All @@ -76,7 +87,8 @@ int main() {
fprintf(stderr, "branch-predict: join sort thread\n");
pthread_join(sorted_thread, (void **)&sum2);

for (int i = 0; i < 10000; i++) {
for (int i = 0; i < 10000; i++)
{
float n = 20.1111111111;
fib_float(n);
}
Expand Down
10 changes: 5 additions & 5 deletions examples/cache.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include <stdio.h> // for perror()
#include <stdlib.h> // for srand(), rand()
#include <sys/random.h> // for getrandom()
#include <time.h> // for time()
#include <stdio.h> // for perror()
#include <stdlib.h> // for srand(), rand()
#include <sys/random.h> // for getrandom()
#include <time.h> // for time()

#define TINY_BUFLEN 8192 // the control
#define TINY_BUFLEN 8192 // the control
#define SMALL_BUFLEN 73728
#define MEDIUM_BUFLEN 1646592
#define LARGE_BUFLEN 26222592
Expand Down
11 changes: 5 additions & 6 deletions examples/fork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int dimensional_array[N][M];
class math_func {
int cube(int n) { return n * n * n; }

public:
public:
void calculate_sum(void) {
long *loops = (long *)malloc(sizeof(long));
*loops = 0;
Expand All @@ -24,8 +24,7 @@ class math_func {
int sum = 0;
for (int k = i - 1; k <= i + 1; k++) {
for (int h = j - 1; h <= j + 1; h++) {
if (k < 0 || h < 0)
continue;
if (k < 0 || h < 0) continue;
sum += dimensional_array[h][j];
}
}
Expand All @@ -41,15 +40,15 @@ int main(void) {
printf("$ ");
char *args[] = {line, (char *)0};
for (int i = 0; i < 5; i++) {
int pid = fork(); // fork child
if (pid == 0) { // Child
int pid = fork(); // fork child
if (pid == 0) { // Child
math_func new_mathfunc;
new_mathfunc.calculate_sum();
printf("this is the child process %d\n", getpid());
execl("/bin/bash", "-c", "echo \"hello world!\"", NULL);
perror("exec");
exit(1);
} else { // Parent
} else { // Parent
printf("this is parent process %d", getpid());
wait(NULL);
}
Expand Down
6 changes: 2 additions & 4 deletions examples/threadarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ void *calculate_sum(void *args) {
long *loops = (long *)malloc(sizeof(long));
*loops = 0;
for (int i = 0; i <= N - 1; i++) {
if (i % NTHREADS != threadindex)
continue;
if (i % NTHREADS != threadindex) continue;
for (int j = 0; j <= M - 1; j++) {
int sum = 0;
for (int k = i - 1; k <= i + 1; k++) {
for (int h = j - 1; h <= j + 1; h++) {
if (k < 0 || h < 0)
continue;
if (k < 0 || h < 0) continue;
sum += dimensional_array[h][j];
}
}
Expand Down
6 changes: 4 additions & 2 deletions protos/header.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ message Header {
// extra events
repeated string events = 3;
// map of presets to abstract events and low level event names
// unfortunately, protobuf doesn't support a map of a map, so we need to do some finagling
// unfortunately, protobuf doesn't support a map of a map, so we need to do
// some finagling
map<string, PresetEvents> presets = 4;

string program_input = 5;
repeated string program_args = 6;
}

// a map of a preset's event name (ie. misses) to the low level event names (ie. "MEM_LOAD_RETIRED.L3_MISS")
// a map of a preset's event name (ie. misses) to the low level event names (ie.
// "MEM_LOAD_RETIRED.L3_MISS")
message PresetEvents {
map<string, EventList> events = 1;
}
Expand Down

0 comments on commit 2357e00

Please sign in to comment.