Created
July 2, 2012 12:51
-
-
Save timholy/3033117 to your computer and use it in GitHub Desktop.
Julia function for calling threads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
v = zeros(10) # the "output" of the thread computation | |
index1 = [1,2,4,8] # the elements handled by thread 1 | |
index2 = [3,5,6] # the elements handled by thread 2 | |
indexwarmup = [7,10] # elements handled on warmup | |
# The function run by each thread | |
function fillvals(out, index, val) | |
out[index] = val | |
return nothing | |
end | |
# This is a version that tries to avoid any type of allocation | |
# (without success). Is it the return statement? | |
function fillvals_forloop(out, index, val) | |
for i = index | |
arrayset(out, i, val) | |
end | |
return nothing | |
end | |
# Run the function once to make sure it is compiled | |
fillvals_forloop(v, indexwarmup, -1.0) | |
function runthreads(v, index1, index2) | |
# Launch the threads | |
thread1 = thread_create(fillvals_forloop, v, index1, 1.0) | |
thread2 = thread_create(fillvals_forloop, v, index2, 2.0) | |
# Wait until each thread has completed its task | |
thread_join(thread1) | |
thread_join(thread2) | |
return v | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment