Skip to content
Snippets Groups Projects
Verified Commit c39241f9 authored by Martin Sjölund's avatar Martin Sjölund
Browse files

Added debugging/profiling lab

parent c15bd2e0
Branches
Tags
No related merge requests found
*.mem
dividearrays
fileutil
workitems
a.out
copy.txt
all: workitems dividearrays fileutil
workitems: workitems.c
$(CC) -g -O0 -o $@ $< -lpthread
dividearrays: dividearrays.cpp
$(CXX) -g -O0 -o $@ $<
fileutil: fileutil.cpp
$(CXX) -g -O0 -o $@ $<
run: fileutil
./fileutil
diff -u bible.txt copy.txt
This diff is collapsed.
#include <random>
int main(int argc, char **argv)
{
std::random_device generator;
std::uniform_int_distribution<int> intdist(0,50);
double res[1024];
for (int i=0; i<1024; i++) {
int nom = intdist(generator);
int denom = intdist(generator);
res[i] = nom / denom;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
namespace std {
static void getline(char *line, FILE *fin)
{
int c = '\0', cur = 0;
while (c != '\n' && (c = fgetc(fin)) != EOF) {
line[cur++] = c;
}
line[cur] = '\0';
}
}
using namespace std;
int main(int argc, char **argv)
{
int cur = 0;
char *lines[16536];
char line[80];
// Fix num lines, num columns, strdup-1
FILE *fin = fopen("bible.txt", "r");
while (!feof(fin)) {
getline(line, fin);
lines[cur] = (char*) malloc(strlen(line));
strcpy(lines[cur], line);
cur++;
}
fclose(fin);
FILE *fout = fopen("copy.txt", "w");
for (int i=0; i<cur; i++) {
fputs(lines[i], fout);
}
fclose(fout);
return 0;
}
#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct workitem {
void (*fn)(double *);
int current;
int len;
double *data;
int started;
} workitem;
static void* launchThread(void *in)
{
int n;
workitem *data = (workitem*) in;
volatile int started = 0;
while (!(started = data->started));
while (1) {
n = data->current++;
if (n >= data->len) break;
data->fn(&data->data[n]);
}
return NULL;
}
void launchParallel(int numThreads, double *values, int len, void (*fn)(double *))
{
workitem *data = calloc(1, sizeof(workitem));
pthread_t th[numThreads];
memset(th, 0, numThreads*sizeof(pthread_t));
data->fn = fn;
data->current = 0;
data->len = len;
data->data = values;
data->started = 0;
for (int i=0; i<numThreads; i++) {
assert(0 == pthread_create(&th[i], NULL, launchThread, data));
}
data->started = 1;
for (int i=0; i<numThreads; i++) {
assert(th[i] && 0==pthread_join(th[i], NULL));
}
assert(data->current >= len);
free(data);
}
void mul2(double *d)
{
*d = *d * 2;
}
int main(int argc, char **argv)
{
double vals[100];
for (int i=0; i<100; i++) {
vals[i] = i*1.5;
}
launchParallel(4 /* Increase this if no error occurs */, vals, 100, mul2);
for (int i=0; i<100; i++) {
if (vals[i] != 1.5*2*i) {
fprintf(stderr, "Expected vals[%d] to have value %.1f, got value %.1f\n", i, (double)1.5*2*i, (double)vals[i]);
}
}
return 0;
}
......@@ -5,6 +5,7 @@ run: julia-1.0.4/bin/julia
julia-1.0.4/bin/julia: julia-1.0.4-linux-x86_64.tar.gz
tar xzf $<
$@ -e 'using Pkg; Pkg.add("BenchmarkTools"); Pkg.add("StatProfilerHTML")'
touch $@
julia-1.0.4-linux-x86_64.tar.gz:
wget https://julialang-s3.julialang.org/bin/linux/x64/1.0/julia-1.0.4-linux-x86_64.tar.gz
.PHONY: run
run: ../julia/julia-1.0.4/bin/julia
$< test.jl
../julia/julia-1.0.4/bin/julia:
$(MAKE) -C ../julia julia-1.0.4/bin/julia
using BenchmarkTools
using Random
function f()
lst = rand(50000)
for i in lst
s = "List entry $(i)\r" # The \r makes it so the terminal isn't filled
if i > 0.99
print(s)
end
end
end
f()
@time f()
x = @benchmark f()
println()
println(x)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment