Created
March 25, 2019 23:27
-
-
Save mszkb/5547ea91da355d638e06bd834462b22f to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/pifetuw
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
let n = 20; // This is the input size | |
let p = 7; // amount of processors at hand | |
// Create 3 arrays and fill them up with some numbers | |
let a = new Array(n) | |
let b = new Array(n) | |
let c = new Array(n) | |
for(let i = 0; i < n; i++) { | |
a[i] = i; | |
b[i] = i; | |
c[i] = i; | |
} | |
const rest = n % p; | |
console.log('we have ' + rest + ' rest') | |
console.log(n/p) | |
// Split the array in n/p pieces | |
// In each piece, sum up each element of b and c | |
// save the sum into a | |
// n / p is the split size | |
// for example, n = 50 and you have 5 processors | |
// the interval is 10 | |
let counter = 1; | |
let prozessor = 1; | |
for(let i = 0; i < n-rest; i += Math.floor(n/p)) { | |
// In each interval go through each element | |
// the position is calculated by interval start i + array counter j | |
for(let j = 0; j < Math.ceil(n/p); j++) { | |
a[i+j] = b[i+j] + c[i+j]; | |
console.log(counter + ', prozessor '+prozessor+': step ' + i + ', ' + j + ': ' + a[i+j]) | |
counter++; | |
} | |
prozessor++; | |
} | |
let last = n-rest | |
prozessor = 1; | |
for(let i = 0; i < rest; i++) { | |
counter++; | |
a[i + last] = b[i + last] + c[i + last]; | |
console.log(counter + ', prozessor ' + prozessor + ': step ' + (last) + ', ' + i + ': ' + a[i + last]) | |
prozessor++; | |
} | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">let n = 20; // This is the input size | |
let p = 7; // amount of processors at hand | |
// Create 3 arrays and fill them up with some numbers | |
let a = new Array(n) | |
let b = new Array(n) | |
let c = new Array(n) | |
for(let i = 0; i < n; i++) { | |
a[i] = i; | |
b[i] = i; | |
c[i] = i; | |
} | |
const rest = n % p; | |
console.log('we have ' + rest + ' rest') | |
console.log(n/p) | |
// Split the array in n/p pieces | |
// In each piece, sum up each element of b and c | |
// save the sum into a | |
// n / p is the split size | |
// for example, n = 50 and you have 5 processors | |
// the interval is 10 | |
let counter = 1; | |
let prozessor = 1; | |
for(let i = 0; i < n-rest; i += Math.floor(n/p)) { | |
// In each interval go through each element | |
// the position is calculated by interval start i + array counter j | |
for(let j = 0; j < Math.ceil(n/p); j++) { | |
a[i+j] = b[i+j] + c[i+j]; | |
console.log(counter + ', prozessor '+prozessor+': step ' + i + ', ' + j + ': ' + a[i+j]) | |
counter++; | |
} | |
prozessor++; | |
} | |
let last = n-rest | |
prozessor = 1; | |
for(let i = 0; i < rest; i++) { | |
counter++; | |
a[i + last] = b[i + last] + c[i + last]; | |
console.log(counter + ', prozessor ' + prozessor + ': step ' + (last) + ', ' + i + ': ' + a[i + last]) | |
prozessor++; | |
} | |
</script></body> | |
</html> |
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
let n = 20; // This is the input size | |
let p = 7; // amount of processors at hand | |
// Create 3 arrays and fill them up with some numbers | |
let a = new Array(n) | |
let b = new Array(n) | |
let c = new Array(n) | |
for(let i = 0; i < n; i++) { | |
a[i] = i; | |
b[i] = i; | |
c[i] = i; | |
} | |
const rest = n % p; | |
console.log('we have ' + rest + ' rest') | |
console.log(n/p) | |
// Split the array in n/p pieces | |
// In each piece, sum up each element of b and c | |
// save the sum into a | |
// n / p is the split size | |
// for example, n = 50 and you have 5 processors | |
// the interval is 10 | |
let counter = 1; | |
let prozessor = 1; | |
for(let i = 0; i < n-rest; i += Math.floor(n/p)) { | |
// In each interval go through each element | |
// the position is calculated by interval start i + array counter j | |
for(let j = 0; j < Math.ceil(n/p); j++) { | |
a[i+j] = b[i+j] + c[i+j]; | |
console.log(counter + ', prozessor '+prozessor+': step ' + i + ', ' + j + ': ' + a[i+j]) | |
counter++; | |
} | |
prozessor++; | |
} | |
let last = n-rest | |
prozessor = 1; | |
for(let i = 0; i < rest; i++) { | |
counter++; | |
a[i + last] = b[i + last] + c[i + last]; | |
console.log(counter + ', prozessor ' + prozessor + ': step ' + (last) + ', ' + i + ': ' + a[i + last]) | |
prozessor++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment