oscarwang2 commited on
Commit
494e7cf
1 Parent(s): 1926364

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -2
app.py CHANGED
@@ -56,7 +56,7 @@ def handle_upload():
56
  script_file.write(script_content)
57
 
58
  # Run the script using MPI
59
- log_output = run_script(script_path, folder_path)
60
 
61
  # Create a zip file of the entire folder
62
  zip_path = os.path.join(temp_dir, 'output_folder.zip')
@@ -154,6 +154,31 @@ def update_cpu_usage():
154
  def index():
155
  return render_template('index.html')
156
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
  def run_script(script_path, folder_path):
158
  comm = MPI.COMM_WORLD
159
  rank = comm.Get_rank()
@@ -165,11 +190,29 @@ def run_script(script_path, folder_path):
165
  for i in range(1, size):
166
  log_output = comm.recv(source=i, tag=11)
167
  log_outputs.append(log_output)
168
- return '\n'.join(log_outputs)
 
169
  else:
170
  # Worker process
171
  log_output = target_function(script_path, folder_path)
172
  comm.send(log_output, dest=0, tag=11)
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  if __name__ == "__main__":
175
  app.run(host='0.0.0.0', port=7860, threaded=True)
 
56
  script_file.write(script_content)
57
 
58
  # Run the script using MPI
59
+ log_output = run_script_with_mpi(script_path, folder_path)
60
 
61
  # Create a zip file of the entire folder
62
  zip_path = os.path.join(temp_dir, 'output_folder.zip')
 
154
  def index():
155
  return render_template('index.html')
156
 
157
+ def run_script_with_mpi(script_path, folder_path):
158
+ # Create a temporary directory for MPI processes
159
+ mpi_temp_dir = tempfile.mkdtemp()
160
+ mpi_script_path = os.path.join(mpi_temp_dir, 'mpi_script.py')
161
+
162
+ # Write the MPI script to the temporary directory
163
+ with open(mpi_script_path, 'w') as mpi_script_file:
164
+ mpi_script_file.write(f"""
165
+ import os
166
+ import tempfile
167
+ import subprocess
168
+ from mpi4py import MPI
169
+
170
+ def target_function(script_path, folder_path):
171
+ output_log = tempfile.TemporaryFile(mode='w+t')
172
+ try:
173
+ result = subprocess.run(['python', script_path], cwd=folder_path, stdout=output_log, stderr=subprocess.STDOUT)
174
+ output_log.seek(0)
175
+ log_output = output_log.read()
176
+ except Exception as e:
177
+ log_output = str(e)
178
+ finally:
179
+ output_log.close()
180
+ return log_output
181
+
182
  def run_script(script_path, folder_path):
183
  comm = MPI.COMM_WORLD
184
  rank = comm.Get_rank()
 
190
  for i in range(1, size):
191
  log_output = comm.recv(source=i, tag=11)
192
  log_outputs.append(log_output)
193
+ with open(os.path.join(folder_path, 'mpi_log_output.txt'), 'w') as log_file:
194
+ log_file.write('\\n'.join(log_outputs))
195
  else:
196
  # Worker process
197
  log_output = target_function(script_path, folder_path)
198
  comm.send(log_output, dest=0, tag=11)
199
 
200
+ if __name__ == "__main__":
201
+ run_script('{script_path}', '{folder_path}')
202
+ """)
203
+
204
+ # Run the MPI script using subprocess
205
+ result = subprocess.run(['mpiexec', '-n', str(psutil.cpu_count(logical=False)), 'python', mpi_script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
206
+
207
+ # Read the log output from the file
208
+ log_output_path = os.path.join(folder_path, 'mpi_log_output.txt')
209
+ with open(log_output_path, 'r') as log_file:
210
+ log_output = log_file.read()
211
+
212
+ # Clean up the temporary directory
213
+ shutil.rmtree(mpi_temp_dir)
214
+
215
+ return log_output
216
+
217
  if __name__ == "__main__":
218
  app.run(host='0.0.0.0', port=7860, threaded=True)