import os
from heapq import merge
import subprocess
file_number = 8
ip_other = input('please input other machine ipv4:')
def over_write_config():
mpi_config = '/mpi_config/mpi_config'
with open(mpi_config,'r',encoding='utf-8') as f:
ips = f.read().split('\n')
del ips[len(ips)-1]
for i in range(len(ips)):
ips[i] = ips[i][:-2]
new_ips = []
for i,ip in enumerate(ips):
try:
p = subprocess.call('ping -c 1 '+ip,shell=True,timeout=1)
except subprocess.TimeoutExpired:
continue
if p == 0:
new_ips.append(ip)
with open(mpi_config,'w',encoding='utf-8') as f:
for ip in new_ips:
f.write(ip+':'+str(1)+'\n')
print('update mpi_config')
cmd4 = 'tar czf - /mpi_config/mpi_config | ssh root@'+ip_other+' tar xzf - -C /'
try:
p = subprocess.call('ping -c 1 ' + ip_other, shell=True, timeout=1)
except subprocess.TimeoutExpired:
print('the '+ip_other+' loss connect')
return
os.system(cmd4)
divid_finish = 1
for i in range(file_number):
file_small = '/data/'+'data'+str(i)+'.txt'
if os.path.exists(file_small):
print('File already exists')
continue
else:
divid_finish = 0
break
if divid_finish == 0:
file = '/data/data.txt'
print('Now,we are reading data.txt......')
f = open(file, 'r')
data_list = list(f.read().split(' '))
small_number = len(data_list)//file_number
for i in range(file_number):
data = data_list[i*small_number:(i+1)*small_number]
print('The file_' + str(i) + " devide finished")
file_small = '/data/'+'data'+str(i)+'.txt'
if os.path.exists(file_small):
print('File already exists')
continue
with open(file_small, 'a', encoding='utf-8') as small_file:
for number in data:
small_file.write(number+' ')
print('The file_' + str(i) + " write finished")
f.close()
mpi_config_file = '/mpi_config/mpi_config'
mpi_config = open(mpi_config_file, 'r')
ips = list(mpi_config.read().split('\n'))
del ips[len(ips)-1]
for i in range(len(ips)):
ips[i] = ips[i][:-2]
flag_file = '/data/flag.txt'
if os.path.exists(flag_file):
print('The flag file already exists')
else:
with open(flag_file,'a',encoding='utf-8') as f:
for i in range(file_number):
f.write('0 ')
flag = open(flag_file,'r',encoding='utf-8').read().split(' ')
if len(flag) > file_number:
for i in range(file_number,len(flag)):
del flag[i]
flag_i = 0
while flag_i < file_number:
if flag[flag_i] == '1':
flag_i += 1
elif flag[flag_i] == '0':
cmd = 'mpiexec -n 10 -f /mpi_config/mpi_config /mpi_config/sort ' + str(flag_i)
p = subprocess.Popen(cmd,shell=True)
try:
p.wait(40)
except subprocess.TimeoutExpired:
print('some containers may not running,please check it')
p.kill()
over_write_config()
continue
print('aaaaaaaa')
cmd2 = 'tar czf - /data/sorted_data'+str(flag_i)+'.txt'+' | ssh root@'+ip_other+' tar xzf - -C /'
try:
p = subprocess.call('ping -c 1 ' + ip_other, shell=True, timeout=1)
os.system(cmd2)
except subprocess.TimeoutExpired:
print('the '+ip_other+' loss connect')
print('The '+str(flag_i)+' batch of data sort fininshed and has been written to file')
if os.path.exists('/data/sorted_data' + str(flag_i) + '.txt'):
flag[flag_i] = '1'
flag_i += 1
with open(flag_file, 'w', encoding='utf-8') as f:
for i in flag:
f.write(i+' ')
cmd3 = 'tar czf - /data/flag.txt' + '| ssh root@'+ip_other+' tar xzf - -C /'
try:
p = subprocess.call('ping -c 1 ' + ip_other, shell=True, timeout=1)
os.system(cmd3)
except subprocess.TimeoutExpired:
print('the ' + ip_other + ' loss connect')
print('The flag file of ' + str(flag_i - 1) + ' batch of data has been changed')
sorted_result = []
original_result = []
print('Now,we are ready for sorting every sorted_result')
for i in range(file_number):
with open('/data/sorted_data' + str(i) +'.txt','r',encoding='utf-8') as f:
original_result = f.read().split(' ')
del original_result[len(original_result)-1]
temp_result = []
for j in range(len(original_result)):
temp_result.append(0)
temp_result[j] = int(original_result[j])
sorted_result.append(temp_result)
print('the '+str(i)+ ' file has been read')
num=len(sorted_result)
times=0
j=num
while j!=1:
j=j//2
times+=1
t=1
while t<=times:
num = len(sorted_result)
i=0
while i<=num-2:
left=sorted_result[i]
right=sorted_result[i+1]
sorted_list=list(merge(left,right))
sorted_result.append(sorted_list)
i+=2
sorted_result=sorted_result[num:]
t+=1
print('The All Sorting completed and ready to write to file')
with open('/data/sorted_data.txt', 'w', encoding='utf-8') as f:
for i in sorted_result[0]:
f.write(str(i)+' ')
print('Write to file completed')
python调用mpi进行排序
最新推荐文章于 2024-07-12 16:16:27 发布