def assign_task_output_files(task, replica_list, replica_id,
                             replica_next_starting_step,
                             replica_next_ending_step):
    #Find pdb file for current replica
    replica_pdb = "%s.%d" % (protomol_utils.remove_trailing_dots(
        protomol_utils.parse_file_name(pdb_file)), replica_id)

    #Assign local and remote xyz output files.
    if generate_xyz:
        local_xyz_output_file = "%s/simfiles/%s/%s.%d-%d.xyz" % (
            protomol_utils.output_path, replica_list[replica_id].temp,
            protomol_utils.xyz_file_name, replica_id, replica_next_ending_step)
        remote_xyz_output_file = "%d.xyz" % (replica_id)
        task.specify_output_file_xyz(remote_xyz_output_file,
                                     local_xyz_output_file)

    #Assign local and remote dcd output files.
    if generate_dcd:
        local_dcd_output_file = "%s/simfiles/%s/%s.%d-%d.dcd" % (
            protomol_utils.output_path, replica_list[replica_id].temp,
            protomol_utils.dcd_file_name, replica_id, replica_next_ending_step)
        remote_dcd_output_file = "%d.dcd" % (replica_id)
        task.specify_output_file_dcd(remote_dcd_output_file,
                                     local_dcd_output_file)

    #Assign local and remote (output) energies files.
    local_energies_file = "%s/simfiles/eng/%d/%d.eng" % (
        protomol_utils.output_path, replica_id, replica_id)
    remote_energies_file = "%d.eng" % replica_id
    task.specify_output_file_energy(local_energies_file,
                                    remote_energies_file,
                                    cache=False)

    #Assign local and remote velocity output files.
    local_velocity_output_file = "%s/simfiles/%s/%s-%d.vel" % (
        protomol_utils.output_path, replica_list[replica_id].temp, replica_pdb,
        replica_next_ending_step + 1)
    remote_velocity_output_file = "%s-%d.vel" % (replica_pdb,
                                                 replica_next_ending_step + 1)
    task.specify_output_file_velocity(local_velocity_output_file,
                                      remote_velocity_output_file,
                                      cache=False)

    pdb_output_file = "%s/simfiles/%s/%s-%d.pdb" % (
        protomol_utils.output_path, replica_list[replica_id].temp, replica_pdb,
        replica_next_ending_step + 1)
    task.specify_output_file_pdb(
        pdb_output_file,
        protomol_utils.parse_file_name(pdb_output_file),
        cache=False)
def make_directories(output_path, temp_list, num_replicas):
    count = 0
    for i in temp_list:
        target_key = "%s/simfiles/%s/%s.%d-%d.pdb" % (
            output_path, i,
            protomol_utils.remove_trailing_dots(
                protomol_utils.parse_file_name(pdb_file)), count, 0)
        file_utils.upload_to_remote_storage(pdb_file, target_key)

        count += 1
def make_directories(output_path, temp_list, shared_map):
    count = 0
    for i in temp_list:
        target_key = "%s/simfiles/%s/%s.%d-%d.pdb" % (
            output_path, i,
            protomol_utils.remove_trailing_dots(
                protomol_utils.parse_file_name(pdb_file)), count, 0)
        with open(pdb_file, 'rb') as sourceFile:
            shared_map[target_key] = sourceFile.readlines()
        count += 1
def cf_main(pool_client, replica_list, replicas_to_run):

    #Stat collection variables
    global replicas_running
    global step_time
    global total_functions_executed

    #Variable that tracks replicas which completed simulations over all MC steps
    num_replicas_completed = 0

    #-------Perform computation for each replica at current monte carlo step--------
    '''Each computation is a pywren_task in work queue.
       Each pywren_task will be run on one of the connected workers.'''
    #Assign local and remote psf and par inputs
    target_psf_file = "%s/simfiles/input_data/ww_exteq_nowater1.psf" % (
        protomol_utils.output_path)
    if upload_data:
        file_utils.upload_to_remote_storage(psf_file, target_psf_file)

    target_par_file = "%s/simfiles/input_data/par_all27_prot_lipid.inp" % (
        protomol_utils.output_path)
    if upload_data:
        file_utils.upload_to_remote_storage(par_file, target_par_file)

    while num_replicas_completed < len(replica_list):

        print("num_replicas_completed: {}".format(num_replicas_completed))
        print("len(replica_list): {}".format(len(replica_list)))

        #Iterate through the given set of replicas and start their
        #         computation for the current monte carlo step.
        activation_list = []
        task_list_iterdata = []
        for j in replicas_to_run:
            if not replica_list[j].running:
                #Initialize step time.
                step_time = time.time()

                replica_id = replica_list[j].id
                #Each replica does computation at its current temperature
                replica_temperature = replica_list[j].temp
                '''Get the last seen step of replica. The last_seen_step
                is the step at which this replica was brought back and
                attempted for an exchange.'''
                replica_last_seen_step = replica_list[j].last_seen_step

                #record the starting, ending steps for current iteration of
                #this replica.
                replica_next_starting_step = replica_last_seen_step + 1
                if replica_next_starting_step >= protomol_utils.monte_carlo_steps:
                    break

                if use_barrier:
                    #Barrier version, so run one step at a time.
                    replica_next_ending_step = replica_next_starting_step
                else:
                    #Run all steps until the step where the replica will be
                    #chosen to attempt an exchange.
                    if len(replica_list[j].exch_steps) > 0:
                        replica_next_ending_step = replica_list[j].exch_steps[
                            0]
                    #If there are no more exchange steps for this replica, run the
                    #remainder of monte carlo steps.
                    else:
                        replica_next_ending_step = protomol_utils.monte_carlo_steps - 1

                #Set the last_seen_step to the next exchange step at which the
                #replica (its output) will be brought back.
                replica_list[j].last_seen_step = replica_next_ending_step

                task = create_task(replica_id, local_temp_dir)
                task.specify_input_psf_file(
                    target_psf_file, protomol_utils.parse_file_name(psf_file))
                task.specify_input_par_file(
                    target_par_file, protomol_utils.parse_file_name(par_file))

                assign_task_input_files(task, replica_list, replica_id,
                                        replica_next_starting_step,
                                        replica_next_ending_step)
                assign_task_output_files(task, replica_list, replica_id,
                                         replica_next_starting_step,
                                         replica_next_ending_step)

                #Keep count of replicas that iterated through all MC steps.
                if (replica_next_ending_step ==
                        protomol_utils.monte_carlo_steps - 1):
                    num_replicas_completed += 1

                task_dictionary = {}
                task_dictionary['task'] = task
                task_dictionary['time_per_function'] = 0
                task_list_iterdata.append(task_dictionary)

                #Submitted for execution. So mark this replica as running.
                replica_list[j].running = 1
                replicas_running += 1

        #Wait for tasks to complete.
        total_functions_executed += len(task_list_iterdata)
        #serverless_task_process(task_list_iterdata[0]['task'],task_list_iterdata[0]['time_per_function'])
        activation_list = pool_client.map(serverless_task_process,
                                          task_list_iterdata)
        for j in range(len(activation_list)):
            replica_list[j].running = 0
            replicas_running -= 1
            execution_time_per_function.append(
                activation_list[j].function_time)
        if use_barrier:
            replicas_to_run = wait_barrier(activation_list, replica_list,
                                           replica_next_starting_step, 5)
        else:
            replicas_to_run = wait_nobarrier(activation_list, replica_list, 5)
def assign_task_input_files(task, replica_list, replica_id,
                            replica_next_starting_step,
                            replica_next_ending_step):
    #Find pdb file for current replica
    replica_pdb = "%s.%d" % (protomol_utils.remove_trailing_dots(
        protomol_utils.parse_file_name(pdb_file)), replica_id)

    #Find pdb file for replica that exchanged with current replica in last step
    if (replica_list[replica_id].exchgd_replica_id > -1):
        exchgd_replica_pdb = "%s.%d" % (protomol_utils.remove_trailing_dots(
            protomol_utils.parse_file_name(
                pdb_file)), replica_list[replica_id].exchgd_replica_id)
    else:
        exchgd_replica_pdb = "%s.%d" % (protomol_utils.remove_trailing_dots(
            protomol_utils.parse_file_name(pdb_file)), replica_id)
    '''Local_file: name for file brought back and stored on local site where this is run.
       Remote_file: name for file sent to remote worker and used in execution there.'''
    #Assign local and remote execution scripts
    local_execn_file = "%s/simfiles/%s/exec-%d.sh" % (
        protomol_utils.output_path, "runs", replica_id)
    remote_execn_file = "exec-%d.sh" % (replica_id)
    task.specify_input_local_execn_file(local_execn_file,
                                        remote_execn_file,
                                        cache=False)

    #Assign local and remote pdb inputs
    local_pdb_input_file = "%s/simfiles/%s/%s-%d.pdb" % (
        protomol_utils.output_path, replica_list[replica_id].temp,
        exchgd_replica_pdb, replica_next_starting_step)
    remote_pdb_input_file = "%s-%d.pdb" % (replica_pdb,
                                           replica_next_starting_step)
    task.specify_input_pdb_file(local_pdb_input_file,
                                remote_pdb_input_file,
                                cache=False)

    #Velocity input only required after first step since it is output
    #of first step.
    if (replica_next_starting_step > 0):
        #Assign local and remote velocity input files.
        local_velocity_input_file = "%s/simfiles/%s/%s-%d.vel" % (
            protomol_utils.output_path, replica_list[replica_id].temp,
            exchgd_replica_pdb, replica_next_starting_step)
        remote_velocity_input_file = "%s-%d.vel" % (replica_pdb,
                                                    replica_next_starting_step)
        task.specify_input_file_velocity(local_velocity_input_file,
                                         remote_velocity_input_file,
                                         cache=False)

    for i in range(replica_next_starting_step, replica_next_ending_step + 1):
        #Assign local and remote config files.
        local_config_file = "%s/simfiles/config/%d/%d-%d.cfg" % (
            protomol_utils.output_path, replica_id, replica_id, i)
        remote_config_file = "%d-%d.cfg" % (replica_id, i)
        task.specify_input_file(i,
                                local_config_file,
                                remote_config_file,
                                cache=False)

    #Call function to generate execution script.
    [execn_script_name,
     execn_script] = generate_execn_script(replica_list[replica_id],
                                           replica_next_starting_step,
                                           replica_next_ending_step)
    print(execn_script)
    task.execn_script = execn_script
    '''
    if upload_data:
        target_key = "%s/simfiles/runs/%s" % (pywren_protomol.output_path, pywren_protomol.remove_first_dots(pywren_protomol.parse_file_name(execn_script_name)))
        cos.upload_bytes_to_cos(ibm_cos, str.encode(execn_script), input_config['ibm_cos']['bucket'], target_key)
    '''

    #Assign executable that will be run on remote worker to pywren_task string.
    if not protomol_local_install:
        local_executable = "%s" % (protomol_utils.EXECUTABLE)
        task.specify_executable(local_executable)
    print("Number of failures:              {}".format(num_task_resubmissions))
    print("Replica Exchanges:               {}".format(num_replica_exchanges))
    print("Acceptance Rate:                 {}".format(
        (num_replica_exchanges * 100) / protomol_utils.monte_carlo_steps))

    averageValue = 0
    for itr in montecarlo_return_time_list:
        averageValue += itr
    averageValue = averageValue / protomol_utils.monte_carlo_steps
    print("Average Response Time            {}".format(averageValue))

    #Write stats to a stats file
    stat_file_name = "%s/%s.stat" % (
        local_temp_dir,
        protomol_utils.remove_trailing_dots(
            protomol_utils.parse_file_name(pdb_file)))
    stat_file_stream = open(stat_file_name, "w")

    stat_file_stream.write("%s\n" %
                           "Printing replica temperature execution matrix:")
    #Sort and format the replica exchange matrix.
    for itr in range(num_replicas):
        replica_temp_execution_list[itr].sort()
        unique(replica_temp_execution_list[itr])
        stat_file_stream.write("Replica %d: %s\n" %
                               (itr, replica_temp_execution_list[itr]))

    #If barrier version was used, write the MC step times to stats file.
    if use_barrier:
        #Write run time for each step to stats file
        stat_file_stream.write("\n\n%s\n" %
def save_protomol_template(output_path, pdb_file, psf_file, par_file, monte_carlo_step, md_steps, output_freq,
                           replica_obj, generate_xyz = False, generate_dcd = False):

    protomol_config_map = OrderedDict()

    # Parse supplied files so only actual file name is passed, not full path of the file name
    input_pdb = "%s.%d-%d.pdb" % (protomol_utils.remove_trailing_dots(protomol_utils.parse_file_name(pdb_file)), replica_obj.id, monte_carlo_step)
    parsed_psf_file = protomol_utils.parse_file_name(psf_file)
    parsed_par_file = protomol_utils.parse_file_name(par_file)
    str_output_freq = str(output_freq)
    str_md_steps = str(md_steps)
    protomol_config_map["randomtype"] = "1"
    protomol_config_map["numsteps"] = str_md_steps
    protomol_config_map["outputfreq"] = str_output_freq
    protomol_config_map["posfile"] = input_pdb
    protomol_config_map["psffile"] = parsed_psf_file
    protomol_config_map["parfile"] = parsed_par_file

    if monte_carlo_step > 0:
        protomol_config_map["velfile"] = "%s.%d-%d.vel" % (
            protomol_utils.remove_trailing_dots(protomol_utils.parse_file_name(pdb_file)), replica_obj.id, monte_carlo_step)

    protomol_config_map["dofinPDBPosFile"] = "true"
    protomol_config_map["finPDBPosFile"] = "%s.%d-%d.pdb" % (
        protomol_utils.remove_trailing_dots(protomol_utils.parse_file_name(pdb_file)), replica_obj.id, monte_carlo_step + 1)
    protomol_config_map["finXYZVelFile"] = "%s.%d-%d.vel" % (
        protomol_utils.remove_trailing_dots(protomol_utils.parse_file_name(pdb_file)), replica_obj.id, monte_carlo_step + 1)

    protomol_config_map["temperature"] = "%f" % replica_obj.temp
    protomol_config_map["boundaryConditions"] = "%s" % DEFAULT_BOUNDARY_CONDITIONS

    protomol_config_map["cellManager"] = "Cubic"
    protomol_config_map["cellsize"] = "69"

    if generate_xyz:
        protomol_config_map["XYZPosFile"] = "%d.xyz" % replica_obj.id
        protomol_config_map["XYZPosFileOutputFreq"] = "%d" % str_md_steps
    if generate_dcd:
        protomol_config_map["DCDFile"] = "%d.dcd" % replica_obj.id
        protomol_config_map["DCDFileOutputFreq"] = str_output_freq

    protomol_config_map["allEnergiesFile"] = "%d.eng" % replica_obj.id
    protomol_config_map["allEnergiesFileOutputFreq"] = str_output_freq

    protomol_config_map["seed"] = str(random.randint(1, 1000000))
    protomol_config_map["shake"] = "on"


    langevin_impulse_dictionary = OrderedDict()
    langevin_impulse_dictionary["temperature"] = str(replica_obj.temp)
    langevin_impulse_dictionary["gamma"] = "5"
    langevin_impulse_dictionary["timestep"] = "2"
    langevin_impulse_dictionary["force"] = ["bond","angle","dihedral","improper","LennardJones Coulomb"]
    langevin_impulse_dictionary["-switchingFunction"] = ["C2","C1","-algorithm NonbondedCutoff"]
    langevin_impulse_dictionary["-switchon"] = "10"
    langevin_impulse_dictionary["-cutoff"] = ["12","12","12"]

    integrator_dictionary = OrderedDict()
    integrator_dictionary["level"] = "0"
    integrator_dictionary["langevinImpulse"] = langevin_impulse_dictionary

    protomol_config_map["integrator"] = integrator_dictionary


    cfg_file_name = "%s/%s/%s/%d/%d-%d.cfg" % (
    output_path, "simfiles", "config", replica_obj.id, replica_obj.id, monte_carlo_step)
    serialized_object = json.dumps(protomol_config_map)
    redis_connector.save_file_to_redis(cfg_file_name, serialized_object)

    return cfg_file_name