Skip to content

Commit

Permalink
Finish basic vector add.
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Sep 5, 2012
1 parent 37253a5 commit 99a2240
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
a.out a.out
.*.swp .*.swp
*~ *~
*-demo.c
File renamed without changes.
46 changes: 46 additions & 0 deletions 01-sequential-soln.c
@@ -0,0 +1,46 @@
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2)
{
// STRIP
fprintf(stderr, "need two arguments!\n");
abort();
}

const long n = atol(argv[1]);
double *x = (double *) malloc(sizeof(double) * n);
if (!x) { perror("alloc x"); abort(); }
double *y = (double *) malloc(sizeof(double) * n);
if (!y) { perror("alloc y"); abort(); }
double *z = (double *) malloc(sizeof(double) * n);
if (!z) { perror("alloc z"); abort(); }

for (int i = 0; i < n; ++i)
{
x[i] = i;
y[i] = 2*i;
}

struct timespec time1, time2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

const int ntrips = 2;
for (int trip = 0; trip < ntrips; ++trip)
{
for (int i = 0; i < n; ++i)
{
z[i] = x[i] + y[i];
}
}

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
double elapsed = timespec_diff_in_seconds(time1,time2)/ntrips;
printf("%f GB/s\n",
3*n*sizeof(double)/1e9/elapsed);
printf("%f GFlops/s\n",
n/1e9/elapsed);
}
33 changes: 33 additions & 0 deletions 01-sequential-start.c
@@ -0,0 +1,33 @@
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2)
{
// FIXME
}

const long n = atol(argv[1]);
double *x = (double *) malloc(sizeof(double) * n);
double *y = (double *) malloc(sizeof(double) * n);
double *z = (double *) malloc(sizeof(double) * n);

for (int i = 0; i < n; ++i)
{
x[i] = i;
y[i] = 2*i;
}

struct timespec time1, time2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

// FIXME

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
printf("%f seconds for %d entries\n",
timespec_diff_in_seconds(time1,time2),
n);
}

46 changes: 46 additions & 0 deletions 02-flexible-soln.c
@@ -0,0 +1,46 @@
#include "timing.h"
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 2)
{
// STRIP
fprintf(stderr, "need two arguments!\n");
abort();
}

const long n = atol(argv[1]);
double *x = (double *) malloc(sizeof(double) * n);
if (!x) { perror("alloc x"); abort(); }
double *y = (double *) malloc(sizeof(double) * n);
if (!y) { perror("alloc y"); abort(); }
double *z = (double *) malloc(sizeof(double) * n);
if (!z) { perror("alloc z"); abort(); }

for (int i = 0; i < n; ++i)
{
x[i] = i;
y[i] = 2*i;
}

struct timespec time1, time2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);

const int ntrips = 2;
for (int trip = 0; trip < ntrips; ++trip)
{
for (int i = 0; i < n; ++i)
{
z[i] = x[i] + y[i];
}
}

clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
double elapsed = timespec_diff_in_seconds(time1,time2)/ntrips;
printf("%f GB/s\n",
3*n*sizeof(double)/1e9/elapsed);
printf("%f GFlops/s\n",
n/1e9/elapsed);
}
19 changes: 17 additions & 2 deletions notes.txt
@@ -1,8 +1,23 @@
000-timing-example.c: colorscheme evening

00-timing-example.c:
- basic timing - basic timing
- run into all errors - run into all errors
- -O3 - -O3
- enable result printing - enable result printing
- time again - time again


001-simple.c: 01-simple.c:
- simple timing
- run into segfault
- debug it
- what do the numbers mean?
- timing repeatability
- O3
- set-governor performance/ondemand
- run through a few decades
- why is it doing what it's doing?!
- Control flow overhead
- Cache

02-flexible.c:
5 changes: 5 additions & 0 deletions reset.sh
@@ -0,0 +1,5 @@
#! /bin/sh

for i in *-start.c; do
echo cp $i ${i%%-start.c}-demo.c
done
5 changes: 5 additions & 0 deletions set-governor
@@ -0,0 +1,5 @@
#! /bin/sh

for i in 0 1 2 3; do
cpufreq-set -g $1 -c 1
done

0 comments on commit 99a2240

Please sign in to comment.