5
5
import sys
6
6
import traceback
7
7
from MCintegration import MonteCarlo , MarkovChainMonteCarlo , Vegas
8
+
8
9
os .environ ["NCCL_DEBUG" ] = "OFF"
9
10
os .environ ["TORCH_DISTRIBUTED_DEBUG" ] = "OFF"
10
11
os .environ ["GLOG_minloglevel" ] = "2"
11
12
os .environ ["MASTER_ADDR" ] = os .getenv ("MASTER_ADDR" , "localhost" )
12
13
os .environ ["MASTER_PORT" ] = os .getenv ("MASTER_PORT" , "12355" )
13
14
14
15
backend = "nccl"
16
+ # backend = "gloo"
17
+
15
18
16
19
def init_process (rank , world_size , fn , backend = backend ):
17
20
try :
@@ -23,6 +26,7 @@ def init_process(rank, world_size, fn, backend=backend):
23
26
dist .destroy_process_group ()
24
27
raise e
25
28
29
+
26
30
def run_mcmc (rank , world_size ):
27
31
try :
28
32
if rank != 0 :
@@ -42,7 +46,12 @@ def func(x, f):
42
46
ninc = 1000
43
47
n_therm = 20
44
48
45
- device = torch .device (f"cuda:{ rank } " )
49
+ if backend == "gloo" :
50
+ device = torch .device ("cpu" )
51
+ elif backend == "nccl" :
52
+ device = torch .device (f"cuda:{ rank } " )
53
+ else :
54
+ raise ValueError (f"Invalid backend: { backend } " )
46
55
47
56
print (f"Process { rank } using device: { device } " )
48
57
@@ -54,26 +63,32 @@ def func(x, f):
54
63
55
64
print ("Integration Results for log(x)/sqrt(x):" )
56
65
57
-
58
66
# Plain MC Integration
59
- mc_integrator = MonteCarlo (bounds , func , batch_size = batch_size ,device = device )
67
+ mc_integrator = MonteCarlo (bounds , func , batch_size = batch_size , device = device )
60
68
print ("Plain MC Integral Result:" , mc_integrator (n_eval ))
61
69
62
70
# MCMC Integration
63
71
mcmc_integrator = MarkovChainMonteCarlo (
64
- bounds , func , batch_size = batch_size , nburnin = n_therm ,device = device
72
+ bounds , func , batch_size = batch_size , nburnin = n_therm , device = device
65
73
)
66
74
print ("MCMC Integral Result:" , mcmc_integrator (n_eval , mix_rate = 0.5 ))
67
75
68
76
# Perform VEGAS integration
69
- vegas_integrator = MonteCarlo (bounds , func , maps = vegas_map , batch_size = batch_size ,device = device )
77
+ vegas_integrator = MonteCarlo (
78
+ bounds , func , maps = vegas_map , batch_size = batch_size , device = device
79
+ )
70
80
res = vegas_integrator (n_eval )
71
81
72
82
print ("VEGAS Integral Result:" , res )
73
83
74
84
# VEGAS-MCMC Integration
75
85
vegasmcmc_integrator = MarkovChainMonteCarlo (
76
- bounds , func , maps = vegas_map , batch_size = batch_size , nburnin = n_therm ,device = device
86
+ bounds ,
87
+ func ,
88
+ maps = vegas_map ,
89
+ batch_size = batch_size ,
90
+ nburnin = n_therm ,
91
+ device = device ,
77
92
)
78
93
res_vegasmcmc = vegasmcmc_integrator (n_eval , mix_rate = 0.5 )
79
94
print ("VEGAS-MCMC Integral Result:" , res_vegasmcmc )
@@ -100,6 +115,7 @@ def test_mcmc(world_size):
100
115
except Exception as e :
101
116
print (f"Error in test_mcmc: { e } " )
102
117
118
+
103
119
if __name__ == "__main__" :
104
120
mp .set_start_method ("spawn" , force = True )
105
- test_mcmc (4 )
121
+ test_mcmc (4 )
0 commit comments