def get_caused_main_task_records(record_old):
    # assure_record_in_main_task(record_old)
    new_records = []
    if (record_old.get_taskid() == 0):

        if (record_old.is_dependency()):
            # this is a wait-on
            dependent_on = record_old.dependency_from_whom()
            if (not translation_state.is_already_waited_on(dependent_on)):
                translation_state.wait_on(dependent_on)

                dependency_source = dependent_on
                dependency_destination = 0  # main task
                MPI_process = record_old.get_MPI_process()

                dependency_record = utils.TraceRecord.create_in_dependency(MPI_process, dependency_source,
                                                                           dependency_destination)

                add_records_for_blocking_main_task(new_records)
                new_records.append(dependency_record)
                add_records_for_unblocking_main_task(new_records)

        elif (record_old.is_barrier() or record_old.is_css_finish()):
            # this is css barrier or pragma css finish
            last_task_waited_with_barrier = translation_state.get_tasks_waited_so_far_with_barriers()
            last_task_executed = translation_state.get_tasks_entered_so_far()

            add_records_for_blocking_main_task(new_records)
            new_records.append(record_old)
            for task in range(last_task_waited_with_barrier + 1, last_task_executed + 1):
                if (not translation_state.is_already_waited_on(task)):
                    translation_state.wait_on(task)
                    dependency_source = task
                    dependency_destination = 0  # main task
                    MPI_process = record_old.get_MPI_process()
                    dependency_record = utils.TraceRecord.create_in_dependency(MPI_process, dependency_source,
                                                                               dependency_destination)
                    new_records.append(dependency_record)
            add_records_for_unblocking_main_task(new_records)

        elif (record_old.is_blocking_MPI()):
            # some MPI record that may block the task -> do this for pp of paraver trace
            add_records_for_blocking_main_task(new_records)
            new_records.append(record_old)
            add_records_for_unblocking_main_task(new_records)

        elif record_old.is_enter_critical():
            # print(record_old)
            # print("entering critical id: {0}".format(record_old.get_critical_id()))
            utils.add_records_for_pre_starting_critical(new_records, record_old.get_MPI_process(),
                                                        0, record_old.get_critical_id())
            enter_critical_record = utils.TraceRecord.create_start_critical(record_old.get_MPI_process(),
                                                                            0, record_old.get_critical_id())
            new_records.append(enter_critical_record)
            utils.add_records_for_post_starting_critical(new_records, record_old.get_MPI_process(),
                                                        0, record_old.get_critical_id())

        elif record_old.is_exit_critical():
            # print(record_old)
            # print("exiting critical: {0}".format(record_old.get_critical_id()))
            exit_critical_record = utils.TraceRecord.create_end_critical(record_old.get_MPI_process(),
                                                                         0, record_old.get_critical_id())
            new_records.append(exit_critical_record)
            utils.add_records_for_ending_critical(new_records, record_old.get_MPI_process(), 0)

        else:
            #any other event of the main task
            new_records.append(record_old)

    else:
        #this is for processing worker task records, but obtaining new main task records
        assert (record_old.get_taskid() > 0)

        new_task = record_old.get_entering_task()
        if (new_task != None):
            # detected entering a task
            translation_state.set_tasks_entered_so_far(new_task)
            dependency_source = 0  # main task is sending the dependency to spawn the worker
            dependency_destination = new_task
            MPI_process = record_old.get_MPI_process()

            dependency_record = utils.TraceRecord.create_out_dependency(MPI_process, dependency_source,
                                                                        dependency_destination)
            new_records.append(dependency_record)

    return (new_records)
def get_caused_worker_task_records(record_old):
    # assure_record_in_main_task(record_old)
    new_records = []
    if (record_old.get_taskid() != 0):

        #this is for processing worker task records, and obtaining final worker task records
        assert (record_old.get_taskid() > 0)

        new_task = record_old.get_entering_task()
        if (new_task != None):
            # detected entering a task
            assert (not translation_state.is_currently_in_task())
            translation_state.enter_task(new_task)
            dependency_source = 0  # main task is sending the dependency to spawn the worker
            dependency_destination = new_task
            MPI_process = record_old.get_MPI_process()

            dependency_record = utils.TraceRecord.create_in_dependency(MPI_process, dependency_source,
                                                                       dependency_destination)
            new_records.append(dependency_record)
            new_records.append(record_old)

        elif (record_old.is_exiting_task()):
            # detected exiting a task
            assert (translation_state.is_currently_in_task())
            dependency_source = translation_state.get_tasks_entered_so_far()
            dependency_destination = 0  # to the main task
            MPI_process = record_old.get_MPI_process()

            dependency_record = utils.TraceRecord.create_out_dependency(MPI_process, dependency_source,
                                                                        dependency_destination)
            new_records.append(dependency_record)

            # add block_end for this task
            taskname_coded = translation_state.get_current_task_type()
            block_id = taskname_coded + utils.TASKS_BLOCKS_OFFSET
            current_task = translation_state.get_tasks_entered_so_far()
            block_end_record = utils.TraceRecord.create_block_end(MPI_process, current_task, block_id)
            new_records.append(block_end_record)

            # add the user event signaling the exit from the task
            new_records.append(record_old)
            translation_state.exit_task()

        elif (record_old.is_setting_task_name()):
            # detected setting taskname
            assert (translation_state.is_currently_in_task())
            taskname_coded = record_old.get_task_name()
            translation_state.set_current_task_type(taskname_coded)

            new_records.append(record_old)

            # add block_begin for this task
            block_id = taskname_coded + utils.TASKS_BLOCKS_OFFSET
            MPI_process = record_old.get_MPI_process()
            current_task = translation_state.get_tasks_entered_so_far()
            block_begin_record = utils.TraceRecord.create_block_begin(MPI_process, current_task, block_id)
            new_records.append(block_begin_record)

        elif (record_old.is_dependency()):
            # this is a dependency among tasks
            assert (translation_state.is_currently_in_task())
            dependent_on = record_old.dependency_from_whom()

            dependency_source = dependent_on
            dependency_destination = translation_state.get_tasks_entered_so_far()
            MPI_process = record_old.get_MPI_process()

            dependency_in_record = utils.TraceRecord.create_in_dependency(MPI_process, dependency_source,
                                                                          dependency_destination)
            dependency_out_record = utils.TraceRecord.create_out_dependency(MPI_process, dependency_source,
                                                                            dependency_destination)

            add_records_for_blocking_task(new_records)
            new_records.append(dependency_in_record)
            add_records_for_unblocking_task(new_records)
            new_records.append(dependency_out_record)
            new_records.append(record_old)

        elif (record_old.is_blocking_MPI()):
            # some MPI record that may block the task -> do this for pp of paraver trace
            #print "here is a blocking MPI in a task"
            add_records_for_blocking_task(new_records)
            new_records.append(record_old)
            add_records_for_unblocking_task(new_records)

        elif record_old.is_enter_critical():
            # print(record_old)
            # print("entering critical id: {0}".format(record_old.get_critical_id()))
            utils.add_records_for_pre_starting_critical(new_records, record_old.get_MPI_process(),
                                                        translation_state.get_tasks_entered_so_far(),
                                                        record_old.get_critical_id())
            enter_critical_record = utils.TraceRecord.create_start_critical(record_old.get_MPI_process(),
                                                                translation_state.get_tasks_entered_so_far(),
                                                                record_old.get_critical_id())
            add_records_for_blocking_task(new_records)
            new_records.append(enter_critical_record)
            add_records_for_unblocking_task(new_records)
            utils.add_records_for_post_starting_critical(new_records, record_old.get_MPI_process(),
                                                        translation_state.get_tasks_entered_so_far(),
                                                        record_old.get_critical_id())

        elif record_old.is_exit_critical():
            # print(record_old)
            # print("exiting critical: {0}".format(record_old.get_critical_id()))
            exit_critical_record = utils.TraceRecord.create_end_critical(record_old.get_MPI_process(),
                                                                translation_state.get_tasks_entered_so_far(),
                                                                record_old.get_critical_id())
            new_records.append(exit_critical_record)
            utils.add_records_for_ending_critical(new_records, record_old.get_MPI_process(),
                                                  translation_state.get_tasks_entered_so_far())

        else:
            new_records.append(record_old)

    return (new_records)