Notebook Questions Flashcards
from here: https://www.francescverdugo.com/XM_40017/dev/
What will be the value of x
in the last line ? (Think your answer before executing next cell to find out the result)
x = 1
y = x
y = 2
x
In the first line, we assign a variable to a value. In the second line, we assign another variable to the same value. Thus, we have 2 variables associated with the same value. In line 3, we associate y to a new value (re-assignment). Thus, we have 2 variables associated with 2 different values. Variable x is still associated with its original value. Thus, the value at the final line is x=1.
What will be the value of x
in the last line ?
function q(x)
x = 2
x
end
x = 1
y = q(x)
x
It will be 1 for very similar reasons as in the previous questions: we are reassigning a local variable, not the global variable defined outside the function.
Which will be the value of x
below?
function hofun(x)
y -> x*y
end
f2 = hofun(2)
x = f2(3)
x
It will be 6. In the returned function f2, x is equal to 2. Thus, when calling f2(3) we compute 2*3.
How long will the compute time of next cell be?
n = 140_000_000
@time for i in 1:10
@show compute_π(n)
end
a) 10t
b) t
c) 0.1t
d) O(1), i.e. time independent from n
Evaluating compute_π(100_000_000) takes about 0.25 seconds on the teacher’s laptop. Thus, the loop would take about 2.5 seconds since we are calling the function 10 times.
How long will the compute time of next cell be?
n = 140_000_000
@time for i in 1:10
@async @show compute_π(n)
end
a) 10t
b) t
c) 0.1t
d) O(1)
The time in doing the loop will be O(1) since the loop just schedules 10 tasks, which should be a (small) constant time independent of n.
How long will the compute time of next cell be?
n = 140_000_000
@time @sync for i in 1:10
@async @show compute_π(n)
end
a) 10t
b) t
c) 0.1t
d) O(1)
It will take 2.5 seconds, like in question 1. The @sync macro forces to wait for all tasks we have generated with the @async macro. Since we have created 10 tasks and each of them takes about 0.25 seconds, the total time will be about 2.5 seconds.
How long will the compute time of the 2nd cell be?
buffer_size = 4
chnl = Channel{Int}(buffer_size)
@time begin
put!(chnl,3)
i = take!(chnl)
sleep(i)
end
a) infinity
b) 1 second
c) less than 1 seconds
d) 3 seconds
It will take about 3 seconds. The channel has buffer size 4, thus the call to put!will not block. The call to take! will not block neither since there is a value stored in the channel. The taken value is 3 and therefore we will wait for 3 seconds.
How long will the compute time of the 2nd cell be?
chnl = Channel{Int}()
@time begin
put!(chnl,3)
i = take!(chnl)
sleep(i)
end
a) infinity
b) 1 second
c) less than 1 seconds
d) 3 seconds
The channel is not buffered and therefore the call to put! will block. The cell will run forever, since there is no other task that calls take! on this channel.
How many integers are transferred between master and worker? Including both directions.
a = rand(Int,4,4)
proc = 4
@fetchfrom proc sum(a^2)
a) 17
b) 32
c) 16^2
d) 65
We send the matrix (16 entries) and then we receive back the result (1 extra integer). Thus, the total number of transferred integers in 17.
How many integers are transferred between master and worker? Including both directions.
a = rand(Int,4,4)
proc = 4
@fetchfrom proc sum(a[2,2]^2)
a) 2
b) 17
c) 5
d) 32
Even though we only use a single entry of the matrix in the remote worker, the entire matrix is captured and sent to the worker. Thus, we will transfer 17 integers like in Question 1.
Which value will be the value of x
?
a = zeros(Int,3)
proc = 3
@sync @spawnat proc a[2] = 2
x = a[2]
x
The value of x will still be zero since the worker receives a copy of the matrix and it modifies this copy, not the original one.
Which value will be the value of x
?
a = zeros(Int,3)
proc = myid()
@sync @spawnat proc a[2] = 2
x = a[2]
x
In this case, the code a[2]=2 is executed in the main process. Since the matrix is already in the main process, it is not needed to create and send a copy of it. Thus, the code modifies the original matrix and the value of x will be 2.
Which is the complexity (number of operations) of the serial algorithm? Assume that all matrices are
N-by- N matrices.
for j in 1:N
for i in 1:N
Cij = z
for k in 1:N
@inbounds Cij += A[i,k]*B[k,j]
end
C[i,j] = Cij
end
end
a) O(1)
b) O(N)
c) O(N²)
d) O(N³)
d) O(N³)
Which are the data dependencies of the computations done by the worker in charge of computing entry C[i,j] ?
a) column A[:,i] and row B[j,:]
b) row A[i,:] and column B[:,j]
c) the whole matrices A and B
d) row A[i,:] and the whole matrix B
b) row A[i,:] and column B[:,j]
How many scalars are communicated from and to a worker? Assume that matrices A, B, and C are N by N matrices.
a) O(1)
b) O(N)
c) O(N²)
d) O(N³)
b) O(N)
How many operations are done in a worker?
a) O(1)
b) O(N)
c) O(N²)
d) O(N³)
b) O(N)
Which are the data dependencies of the computations done by the worker in charge of computing row C[i,:] ?
a) column A[:,i] and row B[j,:]
b) row A[i,:] and column B[:,j]
c) the whole matrices A and B
d) row A[i,:] and the whole matrix B
b) row A[i,:] and column B[:,j]
Which is the complexity of the communication and computations done by a worker in algorithm 2?
a) O(N) communication and O(N^2) computation
b) O(N^2) communication and O(N^2) computation
c) O(N^3) communication and O(N^3) computation
d) O(N) communication and O(N) computation
d) O(N) communication and O(N) computation