Fixed print statements and xrange to be compatibile with Python 2 and 3. (#55)

This commit is contained in:
Dennis O'Brien 2018-02-26 12:19:04 -08:00 committed by Tianqi Chen
parent 0759d5ed2b
commit 440e81db0b
3 changed files with 14 additions and 9 deletions

View File

@ -2,6 +2,8 @@
""" """
demo python script of rabit demo python script of rabit
""" """
from __future__ import print_function
from builtins import range
import os import os
import sys import sys
import numpy as np import numpy as np
@ -14,12 +16,12 @@ rabit.init()
n = 3 n = 3
rank = rabit.get_rank() rank = rabit.get_rank()
a = np.zeros(n) a = np.zeros(n)
for i in xrange(n): for i in range(n):
a[i] = rank + i a[i] = rank + i
print '@node[%d] before-allreduce: a=%s' % (rank, str(a)) print('@node[%d] before-allreduce: a=%s' % (rank, str(a)))
a = rabit.allreduce(a, rabit.MAX) a = rabit.allreduce(a, rabit.MAX)
print '@node[%d] after-allreduce-max: a=%s' % (rank, str(a)) print('@node[%d] after-allreduce-max: a=%s' % (rank, str(a)))
a = rabit.allreduce(a, rabit.SUM) a = rabit.allreduce(a, rabit.SUM)
print '@node[%d] after-allreduce-sum: a=%s' % (rank, str(a)) print('@node[%d] after-allreduce-sum: a=%s' % (rank, str(a)))
rabit.finalize() rabit.finalize()

View File

@ -2,6 +2,7 @@
""" """
demo python script of rabit demo python script of rabit
""" """
from __future__ import print_function
import os import os
import sys import sys
# add path to wrapper # add path to wrapper
@ -15,8 +16,8 @@ rank = rabit.get_rank()
s = None s = None
if rank == 0: if rank == 0:
s = {'hello world':100, 2:3} s = {'hello world':100, 2:3}
print '@node[%d] before-broadcast: s=\"%s\"' % (rank, str(s)) print('@node[%d] before-broadcast: s=\"%s\"' % (rank, str(s)))
s = rabit.broadcast(s, 0) s = rabit.broadcast(s, 0)
print '@node[%d] after-broadcast: s=\"%s\"' % (rank, str(s)) print('@node[%d] after-broadcast: s=\"%s\"' % (rank, str(s)))
rabit.finalize() rabit.finalize()

View File

@ -1,4 +1,6 @@
#!/usr/bin/python #!/usr/bin/python
from __future__ import print_function
from builtins import range
import rabit import rabit
import numpy as np import numpy as np
@ -13,11 +15,11 @@ if version == 0:
model = np.zeros(n) model = np.zeros(n)
local = np.ones(n) local = np.ones(n)
else: else:
print '[%d] restart from version %d' % (rank, version) print('[%d] restart from version %d' % (rank, version))
for i in xrange(version, nround): for i in range(version, nround):
res = rabit.allreduce(data + model+local, rabit.SUM) res = rabit.allreduce(data + model+local, rabit.SUM)
print '[%d] iter=%d: %s' % (rank, i, str(res)) print('[%d] iter=%d: %s' % (rank, i, str(res)))
model = res model = res
local[:] = i local[:] = i
rabit.checkpoint(model, local) rabit.checkpoint(model, local)