Skip to content
Snippets Groups Projects
Commit c65f4d14 authored by Tong Zhang's avatar Tong Zhang
Browse files

code for part2

parents
Branches part2
No related tags found
No related merge requests found
.DS_Store 0 → 100644
File added
*.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;
}
*.tar.gz
julia-*
.PHONY: run
VERSION=1.2.0
run: julia-$(VERSION)/bin/julia
$< run.jl
shell: $(VERSION)/bin/julia
$<
julia-%/bin/julia: julia-%-linux-x86_64.tar.gz
tar xzf $<
$@ -e 'using Pkg; Pkg.add("BenchmarkTools"); Pkg.add("StatProfilerHTML")'
touch $@
julia-%-linux-x86_64.tar.gz:
wget https://julialang-s3.julialang.org/bin/linux/x64/`echo $@ | grep -E -o [0-9]+[.][0-9]+`/$@
macro doWhile(block, cond)
println("__source__ ", __source__)
println("cond ", cond)
println("block ", block)
res = quote
error("TODO: Your code here")
end
println(res) # Should not print any lines referencing dowhile.jl
res
end
"""Template programming is a sort of metaprogramming where you fill
in the blanks, but most parts of the code are very similar."""
# Problem1
for (name,op) in [(:add, :+), (:sub, :-), (:mul, :*)]
# Create a function that is named for example add and returns the
# sum of its two arguments
end
@assert 3 == add(1, 2)
@assert -1.0 == sub(1.0, 2.0)
@assert "abc" == mul("ab", "c")
"""Reflective programming often has to do with accessing or creating functions
at runtime, but it is so similar to template programming that you could
try to delete a function instead."""
module Problem2
f(a::Int) = 1
f(a::Any) = 2
end
# TODO: Delete the method f(a::Int) so that f(1) returns the value 2 instead of 1
@assert 2 == Problem2.f(1)
# Problem3
# TODO: Make an until block for Julia, similar to do {} while (cond); in C
#
# Make sure that any assertions and error-messages refer to lines in the original code.
# Hint: You can access a hidden input __source__ in a macro and use that to replace source information.
# By having the macro in a separate file, you can check source locations that match dowhile.jl and replace them with __source__.
include("dowhile.jl") # Modify the macro in dowhile.jl
function testDoWhile()
local a = 5
@doWhile begin
@assert a < 18
a += 1
println(a)
end a==100
end
testDoWhile()
.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
if i > 0.99
print("List entry $(i)\r")
end
end
end
f()
@time f()
x = @benchmark f()
println()
println(x)
\ No newline at end of file
using BenchmarkTools
using Random
function f()
lst = rand(50000)
for i in lst
if i > 0.99
io = IOBuffer()
print(io,"List entry $(i)\r")
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