-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcase1.py
65 lines (48 loc) · 2.3 KB
/
case1.py
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
import os
import sys
import numpy as np
from sharedDefs import tsprint, stimestamp, stimediff
from case1Defs import drawSample, sequential, parallel
ECO_SEED = 23
#-----------------------------------------------------------------------------------------------------------
# CASE 1 - computing a distance matrix
#-----------------------------------------------------------------------------------------------------------
def main(nc, ss, nd):
# draws a sample of size <ss>, as specified in the command line
tsprint('Drawing a sample with {0} {1}D-vectors.'.format(ss, nd))
sample = drawSample(ss, nd, ECO_SEED)
if(nc == 1):
# obtains the distance matrix using a sequential execution scheme
tsprint('Sequential execution started (process {0}).'.format(os.getpid()))
startTs = stimestamp()
mm = sequential(sample, ss)
finishTs = stimestamp()
tsprint('Sequential execution completed.')
# presents the obtained results
tsprint('-- the distance matrix has {0} elements.'.format(len(mm)))
tsprint('-- the average distance between vectors is {0:5.3f}.'.format(sum(mm.values())/len(mm)))
tsprint('-- the process took about {0} seconds to complete.'.format(stimediff(finishTs, startTs)))
elif(nc > 1):
# obtains the distance matrix using a parallel execution scheme
tsprint('Parallel execution with {0} processes spawned from {1}.'.format(nc, os.getpid()))
startTs = stimestamp()
mm = parallel(sample, ss, nc)
finishTs = stimestamp()
tsprint('Parallel execution completed.')
# presents the obtained results
tsprint('-- the distance matrix has {0} elements.'.format(len(mm)))
tsprint('-- the average distance between vectors is {0:5.3f}.'.format(sum(mm.values())/len(mm)))
tsprint('-- the process took about {0} seconds to complete.'.format(stimediff(finishTs, startTs)))
else:
raise ValueError('Number of cores was wrongly specified.')
if __name__ == "__main__":
# command line:
#
# python case1.py <number of cores> <sample size> <dimensionality>
# -- <number of cores>: how many cores can be assigned to tasks;
# -- <sample size>: number of vectors that will comprise the sample
# -- <dimensionality>: the number of dimensions of the sampling space
nc = int(sys.argv[1])
ss = int(sys.argv[2])
nd = int(sys.argv[3])
main(nc, ss, nd)