-
Notifications
You must be signed in to change notification settings - Fork 105
Distributed Computing (proposal)
Raivat Shah edited this page Mar 7, 2021
·
4 revisions
Idea: Introduce Promises in Source 2 to facilitate distributed computing.
- Student A executes:
function my_shared_function(x) {
display(x);
}
share(my_shared_function); // returns access-token
-
REPL displays the access token
"2e7d5196-bc90-4482-9b9f-749b6c23b089"
-
Student A sends the access token to Student B (e.g. through Telegram)
-
Student B executes:
const promise = connect("2e7d5196-bc90-4482-9b9f-749b6c23b089");
then(promise, shared_function_from_a =>
shared_function_from_a("hi there");
}
- Student A sees "hi there" in their REPL
- Student A executes (whats_your_name could be from a library):
function whats_your_name() {
return prompt("what's your name?");
}
share(whats_your_name);
-
A's REPL shows string
"2e7d5196-bc90-4482-9b9f-749b6c23b089"
-
Student A sends string to Student B
-
Student B executes
const promise = connect("2e7d5196-bc90-4482-9b9f-749b6c23b089");
then(promise, shared_function_from_a => {
const my_promise = shared_function_from_a();
then(my_promise, name => display(name));
}
-
Student A gets a prompt:
"what's your name?"
, and enters"Raivat"
-
Student B sees
"Raivat"
in the REPL
- Both Student A and Student B have their individual copies of the following:
function merge_sort(data) {
if (length(data) <= 1) {
return data;
} else {
const result = split(data);
const left = merge_sort(head(result));
const right = merge_sort(head(tail(result)));
return merge(left, right);
}
}
function split(data) {
const median = math_floor(length(data) / 2);
function split_helper(left, right, n) {
if (n === 0) {
return pair(reverse(left), right);
} else {
return split_helper(pair(head(right), left), tail(right), n-1);
}
}
return split_helper(list(), data, median);
}
function merge(left, right) {
if (length(left) === 0) {
return right;
} else if (length(right) === 0) {
return left;
} else if (head(left) < head(right)) {
return pair(head(left), merge(tail(left), right));
} else {
return pair(head(right), merge(left, tail(right)));
}
}
- Student A executes the following
share(merge_sort);
-
A's REPL shows string
"2e7d5196-bc90-4482-9b9f-749b6c23b089"
-
Student B executes the following
const data = list(10, 12, 5, 6, 7, 13, 19);
function start_distribution() {
const result = split(data);
const promise = connect("2e7d5196-bc90-4482-9b9f-749b6c23b089");
const sorted_data = then(promise, shared_merge_sort => {
const left = shared_merge_sort(head(result));
const right = merge_sort(head(tail(result)));
return then(my_promise, left_result => merge(left, right));
}
return sorted_data;
}
const sorted_data = start_distribution();
- After computation, the sorted data is stored in
stored_data
. The computation on the initial left and right happens simultaneously and on their respective systems. B as the controller performs the finalmerge
to merge the results.