Parallel Sampling - dynexcoin/DynexSDK GitHub Wiki
In the previous sections we learned how to define models and how to perform sampling. Because samplings are performed decentralised on the Dynex Platform, it is also possible to sample models in parallel. This section explains how to sample multiple models in parallel on the Dynex Platform. Please note that parallel sampling is only possible on the Dynex mainnet (by setting the sampling parameter to 'mainnet=True', which is the default value), local sampling does not support parallel sampling.
Parallel Sampling on the Dynex Platform
The Dynex sampler class is thread-safe and can therefore be parallelised. This is for example especially useful for federated learning tasks where multiple models / network layers have to be computed at the same time. Here we show an example where we run 8 samplers in parallel, collect the results and print them.
To facilitate parallel sampling, we use Python's multiprocessing libary:
import multiprocessing
Then we define a parallel sampler function:
def parallelsampler(q,x, model):
print('Sampler:' + str(x) + ' started');
sampler = dynex.DynexSampler(model, mainnet=True, logging=False, description='SDK Parallel Job');
sampleset = sampler.sample(num_reads=10000, annealing_time = 1000, debugging=False);
print('Sampler:' + str(x) + ' finished');
q.put(sampleset);
return
Next we start eight parallel jobs as an example, collect the results and print the assignments:
model = dynex.BQM(bqm)
PARALLEL_INSTANCES = 8
jobs = [];
results = [];
# Define n samplers:
for i in range(PARALLEL_INSTANCES):
q = Queue()
results.append(q)
p = multiprocessing.Process(target=parallelsampler, args=(q,i,model,))
jobs.append(p)
p.start()
# wait for samplers to finish:
for job in jobs:
job.join()
# collect samples for each job:
for result in results:
assignments = result.get();
print(assignments);
Note that this example samples the same model on all 8 parallel samplers for demonstration purposes. Usually one would use different models for each job. All parallel jobs are started on the Dynex Platform at the same time. It outputs:
[DYNEX] MODEL CONVERTED TO QUBO
[DYNEX] PRECISION SET TO 0.001
[DYNEX] QUBO: Constant offset of the binary quadratic model: 45.0
Sampler:0 started
Sampler:2 started
Sampler:1 started
Sampler:3 started
Sampler:4 started
Sampler:5 started
Sampler:6 started
Sampler:7 started
progress: 100%
2/2 [00:05<00:00, 2.18s/it]
progress: 100%
2/2 [00:05<00:00, 2.18s/it]
Sampler:0 finished
Sampler:1 finished
progress: 100%
2/2 [00:05<00:00, 2.19s/it]
progress: 100%
2/2 [00:05<00:00, 2.19s/it]
Sampler:2 finished
Sampler:4 finished
progress: 100%
2/2 [00:05<00:00, 2.17s/it]
progress: 100%
2/2 [00:05<00:00, 2.18s/it]
Sampler:3 finished
Sampler:5 finished
progress: 100%
2/2 [00:05<00:00, 2.18s/it]
progress: 100%
2/2 [00:05<00:00, 2.18s/it]
Sampler:6 finished
Sampler:7 finished
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 22.262867 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 641.814067 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 0 7.845689 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 10.685581 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 22.160108 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 11.414923 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 22.160108 1
['BINARY', 1 rows, 1 samples, 15 variables]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 energy num_oc.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 45.0 1
['BINARY', 1 rows, 1 samples, 15 variables]