def wait_nobarrier(activation_list, replica_list):

    print("wq_wait_nobarrier")
    #Stat collection variables
    global num_task_resubmissions
    global replicas_running

    #Wait for a pywren_task to finish execution.
    print("wait_nobarrier {}".format(len(activation_list)))
    for task in activation_list:
        #pywren_task = res.get_result()
        print("wait_nobarrier. got task {}".format(task.task_str))
        if (task):
            #Get replica id from finished pywren_task.
            replica_id = int(task.tag)
            print("wait_nobarrier. task id {}".format(replica_id))

            #Check if pywren_task (replica) failed. If so, resubmit.
            if task.result != 0:
                num_task_resubmissions += 1
                print("Replica failed!")
                #time.sleep(3)

                #Resubmit the pywren_task.
                #wq.submit(pywren_task)
                continue

            #Task was succesful. Update run information.
            #replicas_running -= 1
            #replica_list[replica_id].running = 0

            #Get potential energy value of the completed replica run.
            energies_file = "%s/simfiles/eng/%d/%d.eng" % (
                protomol_utils.output_path, replica_id, replica_id)
            energies_stream = storage_service.get_file(
                protomol_utils.remove_first_dots(energies_file))._raw_stream
            #open(energies_file, "r")
            line = energies_stream.readline()
            print(line)
            slist = (line).split()
            potential_energy = float(slist[1])
            replica_list[replica_id].potential_energy = potential_energy

            #Store temperature and exchanged replica id values from the current run.
            replica_list[replica_id].prev_temp = replica_list[replica_id].temp
            replica_list[replica_id].exchgd_replica_id = replica_id

            #Replica should be currently at this step which is its exchange step.
            if len(replica_list[replica_id].exch_steps) > 0:
                replica_exch_step = replica_list[replica_id].exch_steps.pop(0)
            #Else replica is at the last MC step of this run.
            else:
                replica_exch_step = protomol_utils.monte_carlo_steps - 1

            #Find the exchange partner of this replica.
            if (replica_id == replica_exch_list[replica_exch_step][0]):
                replica_exch_partner = replica_exch_list[replica_exch_step][1]
            elif (replica_id == replica_exch_list[replica_exch_step][1]):
                replica_exch_partner = replica_exch_list[replica_exch_step][0]
            else:
                if (replica_exch_step !=
                    (protomol_utils.monte_carlo_steps - 1)):
                    #If this replica is not part of the exchange pair for this
                    #step and is not at the last MC step of the run, something
                    #is amiss..
                    print("Replica {} should not be here at step {}".format(
                        replica_id, replica_exch_step))
                    sys.exit(1)
                else:
                    #If all replicas have completed last MC step, return.
                    if replicas_running == 0:
                        print("all replicas running 0. return")
                        return
                    #If not, loop back to receive other replicas.
                    else:
                        print("loop to get more replicas")
                        continue

            #If exchange partner is still running, go back to get other tasks.
            if replica_list[replica_exch_partner].running:
                print("Go back to get other tasks")
                continue
            #Otherwise check if partner has finished the current exchange step.
            else:
                if (replica_list[replica_exch_partner].last_seen_step <
                        replica_exch_step):
                    #Exchange partner is currently behind the exchange step of
                    #this replica. So loop back to get other tasks.
                    print("here")
                    continue
                elif (replica_list[replica_exch_partner].last_seen_step >
                      replica_exch_step):
                    #Should never get here. Something went wrong.
                    print(
                        "Partner of replica {} - replica %d is currently at step {} which is beyond step {}"
                        .format(
                            replica_id, replica_exch_partner,
                            replica_list[replica_exch_partner].exch_steps[0],
                            replica_exch_step))
                    sys.exit(1)
                else:
                    #Make sure the replicas are checked in the same order they were chosen at the start.
                    if (replica_exch_partner ==
                            replica_exch_list[replica_exch_step][0]):
                        replica_1 = replica_exch_partner
                        replica_2 = replica_id
                    else:
                        replica_1 = replica_id
                        replica_2 = replica_exch_partner

                    if debug_mode:
                        print(
                            "Replicas {} & {} are attempted for an exchange at step {}"
                            .format(replica_1, replica_2, replica_exch_step))

                    #Attempt exchange between the two.
                    attempt_replica_exch(replica_list, replica_1, replica_2)

                    #Add these two replicas to return list.
                    replicas_to_run = [replica_1, replica_2]
                    print(replicas_to_run)

                    return replicas_to_run
def serverless_task_process(task, time_per_function):
    # get input files
    # execute local script
    # upload result files
    # return data
    time_per_function = time.time()
    temp_dir = task.temp_dir
    if not os.path.exists(temp_dir):
        os.mkdir(temp_dir)
    #exists = os.path.isfile(temp_dir + '/' +task.input_remote_execn_file)
    #if not exists:
    input_local_execn_file = protomol_utils.remove_first_dots(
        task.input_local_execn_file)
    #res = ibm_cos.get_object(Bucket = bucket, Key = input_local_execn_file)
    with open(temp_dir + '/' + task.input_remote_execn_file, 'w') as localfile:
        localfile.write(task.execn_script)
    localfile.close()
    #shutil.copyfileobj(res['Body'], localfile)
    print("local exec file is {}, remote {}".format(
        input_local_execn_file, task.input_remote_execn_file))

    input_local_file_pdb = protomol_utils.remove_first_dots(
        task.input_local_file_pdb)
    with cloud_open(input_local_file_pdb, 'rb') as f:
        res = f.readlines()
    file_utils.write_file_locally(temp_dir + '/' + task.input_remote_file_pdb,
                                  res)

    input_par_file = protomol_utils.remove_first_dots(task.input_par_file)
    with cloud_open(input_par_file, 'rb') as f:
        res = f.readlines()
    file_utils.write_file_locally(temp_dir + '/' + task.input_par_file_name,
                                  res)

    input_psf_file = protomol_utils.remove_first_dots(task.input_psf_file)
    with cloud_open(input_psf_file, 'rb') as f:
        res = f.readlines()
    file_utils.write_file_locally(temp_dir + '/' + task.input_psf_file_name,
                                  res)

    if (task.input_local_file_velocity is not None):
        input_vel_file = protomol_utils.remove_first_dots(
            task.input_local_file_velocity)
        print(input_vel_file)
        print(task.input_remote_file_velocity)
        with cloud_open(input_vel_file, 'rb') as f:
            res = f.readlines()
        file_utils.write_file_locally(
            temp_dir + '/' + task.input_remote_file_velocity, res)

    #bring all config files
    for conf_entry in task.input_conf_file:
        remote_config = protomol_utils.remove_first_dots(conf_entry[1])
        local_config = conf_entry[2]
        with cloud_open(remote_config, 'rb') as f:
            res = f.readlines()
        print("download config local {} remote {}".format(
            local_config, remote_config))
        file_utils.write_file_locally(temp_dir + '/' + local_config, res)

    import stat
    os.chmod(
        temp_dir + '/' + task.input_remote_execn_file, stat.S_IRUSR
        | stat.S_IWUSR | stat.S_IRWXG | stat.S_IRWXO | stat.S_IEXEC)
    os.chdir(temp_dir)
    arr = os.listdir()
    print(arr)
    cmd = "./" + task.input_remote_execn_file

    import subprocess
    subprocess.call(cmd, shell=True)

    #str: ./simfiles/eng/1/1.eng
    output_file_local_energy = task.output_file_local_energy
    #str: 1.eng
    output_file_remote_energy = task.output_file_remote_energy
    #cos.upload_to_cos(ibm_cos, temp_dir + '/' + output_file_remote_energy,
    #              input_config['ibm_cos']['bucket'], pywren_protomol.remove_first_dots(output_file_local_energy))
    #read local energy file and update task
    energies_file = temp_dir + '/' + output_file_remote_energy
    energies_stream = open(energies_file, "r")
    line = energies_stream.readline()
    task.update_energy(line)

    #str: ./simfiles/350.0/ww_exteq_nowater1.1-1.vel
    output_file_local_velocity = task.output_file_local_velocity
    #str: ww_exteq_nowater1.1-1.vel
    output_file_remote_velocity = task.output_file_remote_velocity
    upload_to_remote_storage(
        temp_dir + '/' + output_file_remote_velocity,
        protomol_utils.remove_first_dots(output_file_local_velocity))

    #str: ./simfiles/350.0/ww_exteq_nowater1.1-1.pdb
    output_file_pdb = task.output_file_pdb
    #str: ww_exteq_nowater1.1-1.pdb
    output_file_pdb_name = task.output_file_pdb_name
    flo = protomol_utils.remove_first_dots(output_file_pdb)
    upload_to_remote_storage(temp_dir + '/' + output_file_pdb_name, flo)

    task.result = 0
    time_per_function = time.time() - time_per_function
    task.specify_function_time(time_per_function)
    return task
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 = "/simfiles/%s/%s.pdb" % (
        protomol_utils.output_path,
        protomol_utils.remove_trailing_dots(
            protomol_utils.parse_file_name(pdb_file)))
    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

    target_key = "%s/simfiles/runs/%s" % (
        protomol_utils.output_path,
        protomol_utils.remove_first_dots(
            protomol_utils.parse_file_name(execn_script_name)))
    storage_service.upload_file(str.encode(execn_script), 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)