-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkload.R
112 lines (93 loc) · 2.45 KB
/
workload.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# ______ _
# | ___ \ | |
# | |_/ / ___ ___ ___ __ _ _ __ ___ | |__
# | / / _ \/ __| / _ \ / _` || '__| / __|| '_ \
# | |\ \ | __/\__ \| __/| (_| || | | (__ | | | |
# \_| \_| \___||___/ \___| \__,_||_| \___||_| |_|
#
# _____ _
# / __ \ | |
# | / \/ ___ _ __ ___ _ __ _ _ | |_ ___
# | | / _ \ | '_ ` _ \ | '_ \ | | | || __| / _ \
# | \__/\| (_) || | | | | || |_) || |_| || |_ | __/
# \____/ \___/ |_| |_| |_|| .__/ \__,_| \__| \___|
# | |
# |_|
#
# 2019_10_Parallelism
#
# R Users Group
# 2019_10_Parallelism
# Macquarie Univeristy
#
# Let's create some articifal work using R's Random Uniform Distribution function "runif(x)"
distribution <- runif(10)
average <- mean(distribution)
print(average)
barplot(distribution)
# now let's set the SEED to get predictable numbers
{
set.seed(12345678)
distribution <- runif(10)
average <- mean(distribution)
print(average)
barplot(distribution)
}
# And lets create more work (scaling up)
{
set.seed(87654321)
distribution <- runif(100)
average <- mean(distribution)
print(average)
barplot(distribution)
}
# and how long is that exactly ?
system.time({
set.seed(12345678)
distribution <- runif(100e6)
average <- mean(distribution)
print(average)
})
# Let's wrap this up in a function for our later tests
workload <- function (seed=0) {
set.seed(seed)
distribution <- runif(100e6)
average <- mean(distribution)
return(average)
}
# It should give the same result - so let's check
workload(12345678)
# and take almost the same time to run
system.time({
print(workload(12345678))
})
# Most scientific compute task involve doing similar things over and over.
# Let's simulate this by computing averages for 10 different SEEDs
workload(1)
workload(2)
workload(3)
workload(4)
workload(5)
workload(6)
workload(7)
workload(8)
workload(9)
workload(10)
# or of course we could use a FOR loop ...
for (seed in 1:10) {
average <- workload(seed)
print(average)
}
# and we could APPLY the workload function to an array of SEEDs
lapply(1:10, workload)
# but which one is faster, loops ...
system.time({
for (seed in 1:10) {
average <- workload(seed)
print(average)
}
})
# or applying ?
system.time({
print(lapply(1:10, workload))
})